tags:

views:

118

answers:

3

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?

A: 

I'm not sure if this would work, but you might be able to arrange it so that the CRC location within your object file were to be set to the address of an external symbol X. That external symbol might then be satisfied by a last linking step by linking in an elf file that did nothing but specify that X's address was the CRC that you have calculated.

This is still pretty hacky, and I'm not sure if it's easily do-able (since it is such an abuse of the tools).

nategoose
Yeah, I haven't figured out how to make this work since the link won't complete with missing symbols, and I need the link done so I can compute the CRC.
Doug Currie
I was thinking that this might not be an issue, but it seems that even when relinking a built executable (all dependencies met by that one filefile) that `ld` reorganizes and breaks things. There are several programs for cramming serial numbers into firmware images out there. Maybe there is one for elf images that you could repurpose for this.
nategoose
A: 

The elf file format is trivial to parse, you could have a tool in an evening if so enclined...

dwelch
+3  A: 

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.
Matthew Slattery
This is a great suggestion, +1. I'll give it a try.
Doug Currie