A tool to easily modify an elf file?

In my embedded projects, there is a step after the process that replaces the value in the flash CRC executable (some sections). This step can only be done after the link, as this is the first opportunity for CRC images. In the past, the file format was COFF, and I created a special fix tool.

The development tool has switched to ELF, so I need to re-implement the CRC patcher. Before I did this, it seemed to me that I was looking for an existing tool for this. The compiler is gcc-based, but I don't see any combination of ld and nm and readelf that can do the job. A Google search was not fruitful.

My current tool uses nm to find the address to fix, and calls the patcher with the address, the expected value (to prevent overwriting the wrong data) and the new CRC value. CRC is calculated in the "hexadecimal" format of the executable file (which I also fix), so, fortunately, I do not need to repeat this part.

I can implement this with libelf and custom code again, but before I do this, does it already exist?

Is there a better way to accomplish my task of putting the CRC of an executable in an executable so that it is accessible to the application?

+6
c embedded
source share
2 answers

If I understand what you are trying to do correctly, I think the following will work:

  • nm provides the virtual address of the runtime at which you want to install the patch;
  • readelf -S provides you with both a virtual run-time address and a file offset for the start of each section;
  • stitching the two together (for example, several lines of your favorite scripting language) gives a fix to the file.
+4
source share

I’m not sure if this will work, but you can arrange it so that the CRC location in your object file should be set to the address of the external symbol X. Then this external symbol could be satisfied in the last step of the binding by linking in the elf file, which did nothing but indicated that the X address was a calculated CRC.

This is still pretty hacky, and I'm not sure what it does easily (since it is such an abuse of tools).

0
source share

All Articles