5

I need to change a couple of variables in a compiled ELF file. Trying to explain this clearly I'll use a simple C struct as an example.

The single source file is compiled and linked (@ 0x1000) into MyFile.elf from MyFile.c:

typedef struct {
    uint32_t SerialNumber;      /* Increments for every time it's programmed */
    uint32_t PartNumber;        /* Always the same */
    char     ProdDateTime[32];  /* "YYYY-MM-DD HH:MM:SS" date/time when programmed */
    uint32_t CalcCrc32;         /* Checksum of the above data */
} MyData_T;

const MyData_T MyData = {
    /* SerialNumber      */ 0x11111111,
    /* PartNumber        */ 0x12345678,
    /* ProdDateTime[32]  */ "2013-11-10 12:49:30",
    /* CalcCrc32         */ 0xC0CAC01A
                        };

Now I need a "console-tool" that (without compiling):

  1. Writes a new serial number to 0x1000
  2. Writes a new string to 0x1008
  3. Updates the checksum at 0x1028.

I have not been able to find a tool (objcopy etc?) that even does the first (1) task. Seems this should be a rather common scenario? I've written my own tool for now but would prefer a open source tool or similar.

Any suggestions / ideas / comments / criticisms are highly appreciated :D Thanks you!!

4

2 回答 2

3

“gdb --write /your/application/binary”应该能够更改初始化数据的值并将其写回可执行文件。

添加“-batch”和“-x command_file”,你应该能够让它做你想做的事。

于 2013-11-11T06:00:06.803 回答
2

QNX 有一个名为“spatch”的内置工具,可以让您完全做到这一点。其他使用建议gdb或十六进制编辑器同样有效。

虽然修补二进制代码是完全可能的,但听起来你做错了:-)。也许这些值更适合存储在与二进制文件一起分发的一些数据文件中,并在某种构造函数期间读入?除非你有一些令人信服的理由要求它在二进制文件中,否则我会认真研究设计,看看你是否真的需要这样做。

如果你的回答是“是的,我真的需要这样做”,那就太好了。你有几个非常有效的方法来完成这个建议。祝你好运。

于 2013-11-11T06:15:21.863 回答