Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool for simple modification of elf file?

Tags:

c

embedded

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?

like image 404
Doug Currie Avatar asked Oct 01 '10 20:10

Doug Currie


People also ask

How do I edit ELF files?

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.

How do I decode an ELF file?

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).

What is a .ELF file?

An ELF file is an executable file format for the Linux and Unix platforms. Its known file extensions are . axf, . bin, .


1 Answers

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;
  • stitching the two together (e.g. with a few lines of your favourite scripting language) gives the offset within the file to patch.
like image 120
Matthew Slattery Avatar answered Oct 17 '22 23:10

Matthew Slattery