Edit variable values in ELF file?
Asked Answered
C

2

7

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!!

Condescending answered 10/11, 2013 at 20:51 Comment(3)
Do you have adb (the assembly language debugger, not the android bridge)? Back in the day, we'd do something like echo "0x1000?W 0x11111111" | adb -w MyFile.elfLiving
I haven't tried/heard of adb. I don't think I have access to it for my project but I'll look into it regardless. Thanks!Condescending
Having to adjust a compiled binary is not as common as you think. How would a "universal" tool know where to find the values to change? In this case, if you need to do this on a regular basis(1), writing a custom utility is exactly what I'd do. (1) For one-offs I'd simply use a hex editor.Archiepiscopal
C
4

"gdb --write /your/application/binary" should be able to change value of initialized data and write it back to the executable.

Add "-batch" and "-x command_file" and you should be able to get it to do what you want.

Carmacarmack answered 11/11, 2013 at 6:0 Comment(4)
I haven't tried this but several people has suggested gdb. I'm programming in a WIN environment and the toolchain was not delivered with gdb but I suppose I could find another toolchain for a similar CPU? Thanks a lot for your help :)Condescending
What toolchain do you use and what's the target (x86? ARM? something else)? cygwin and mingw both provide pre-compiled gdb binaries, but that would only work for the host binaries.Carmacarmack
ARM, Freescale Kinetis K61 to be more precise :) I suppose that if I find a pre-compiled gdb for a similar CPU I could possibly use that. Thank you!Condescending
I forgot to mention I (am forced to) do my development on a WIN7-x64 machine :(Condescending
A
2

QNX has a built-in tool called "spatch" that allows you to do exactly this. The other suggestions to use gdb or a hex editor are equally valid.

While patching binary code is totally possible, it sounds like you're doing it wrong :-). Perhaps these values would be better suited to be stored in some data file distributed with the binary and read in during a constructor of some sort? Unless you have some compelling reason to require this to be in the binary, I would seriously look at the design and see if you really need to do this.

If the answer you come up with is "yes, I really need to do this," then great. You've got a couple very valid methods suggested for accomplishing this. Good luck.

Apologist answered 11/11, 2013 at 6:15 Comment(2)
Thanks for your ideas. There's no file system etc. The image I'm editing basically constitutes this data file and is programmed straight to flash (using JTAG).Condescending
@Condescending Ah. Then you are not "doing it wrong", as I incorrectly indicated. Good luck. :-)Apologist

© 2022 - 2024 — McMap. All rights reserved.