Storing CRC into an AXF/ELF file

后端 未结 1 1274
一生所求
一生所求 2021-01-02 22:04

I\'m currently working on a C program in the LPCXpresso (eclipse-based) tool-chain on Windows 7, an IDE with gcc targeting the an NXP Cortex M3 microprocessor. It provides

相关标签:
1条回答
  • 2021-01-02 22:28

    There is no industry standard that I am aware of. There are various techniques to do this. I would suggest that you use the crc32_build as an extern in 'C' and define it via a linker script. For instance,

      $ cat ld.script
      .text : {
        _start_crc_region = .;
        *(.text);
        crc32_build = .;
        LONG(CALC_CRC);
        _end_crc_region = .;
      }
    

    You pass the value CALC_CRC as zero for a first invocation and then relink with the value set. For instance,

     $ ld --defsym=CALC_CRC=0 -T ld.script *.o -o phony.elf
     $ objcopy -j sections phony.elf -o phony.bin # sections means checksum 'areas'
     $ ld --defsym=CALC_CRC=`crc32 phony.bin` -T ld.script *.o -o target.elf
    

    I use this technique to add digital signing to images; it should apply equally well to crc values. The linker script allows you to position the variable, which is often important for integrity checks like a CRC, but wouldn't matter for a simple checksum. A linker script also allows you to define symbols for both the start and end of the region. Without a script, you need some elf introspection.

    You can of course extend the idea to include init data and other allocated sections. At some point you need to use objcopy to extract the sections and do the integrity check at build time. The sections may have various alignment constraints and you need to mimic this (in phony.bin above) on the host when doing the build time crc calculation.

    As a bonus, everything is already done when you generate an srec file.

    If you have trouble with --defsym, you can just pre-process the ld.script with sed, awk, perl, python, etc and substitute text with a hex value where CALC_CRC is.

    0 讨论(0)
提交回复
热议问题