Tracking Binary Changes: Learn The DIFF-erent Ways Of The ELF

Source control is often the first step when starting a new project (or it should be, we’d hope!). Breaking changes down into smaller chunks and managing the changes between them makes it easier to share work between developers and to catch and revert mistakes after they happen. As project complexity increases it’s often desirable to add other nice to have features on top of it like automatic build, test, and deployment.

These are less common for firmware but automatic builds (“Continuous Integration” or CI) is repetitively easy to setup and instantly gives you an eye on a range of potential problems. Forget to check in that new header? Source won’t build. Tweaked the linker script and broke something? Software won’t build. Renamed a variable but forgot a few references? Software won’t build. But just building the software is only the beginning. [noseglasses] put together a tool called elf_diff to make tracking binary changes easier, and it’s a nifty addition to any build pipeline.

In firmware-land, where flash space can be limited, it’s nice to keep a handle on code size. This can be done a number of ways. Manual inspection of .map files (colloquially “mapfiles”) is the easiest place to start but not conducive to automatic tracking over time. Mapfiles are generated by the linker and track the compiled sizes of object files generated during build, as well as the flash and RAM layouts of the final output files. Here’s an example generated by GCC from a small electronic badge. This is a relatively simple single purpose device, and the file is already about 4000 lines long. Want to figure out how much codespace a function takes up? That’s in there but you’re going to need to dig for it.

elf_diff automates that process by wrapping it up in a handy report which can be generated automatically as part of a CI pipeline. Fundamentally the tool takes as inputs an old and a new ELF file and generates HTML or PDF reports like this one that include readouts like the image shown here. The resulting table highlights a few classes of binary changes. The most prominent is size change for the code and RAM sections, but it also breaks down code size changes in individual symbols (think structures and functions). [noseglasses] has a companion script to make the CI process easier by compiling a pair of firmware files and running elf_diff over them to generate reports. This might be a useful starting point for your own build system integration.

Thanks [obra] for the tip! Have any tips and tricks for applying modern software practices to firmware development? Tell us in the comments!

3 thoughts on “Tracking Binary Changes: Learn The DIFF-erent Ways Of The ELF

    1. I think you’re coming at it from the wrong direction. This seems to be about being able to say anything at all about executables that are checked into the repository, not unlike a visual diff for when pictures are checked in.

  1. A likewise feature can be found in the mbed cli tooling nowadays:

    Building project my-app (BINBEAT_V1_2, GCC_ARM)
    Scan: my-app
    Link: my-app
    Elf2Bin: my-app
    | Module                                          |      .text |    .data |      .bss |
    |-------------------------------------------------|------------|----------|-----------|
    | [fill]                                          |   1230(+0) |   28(+0) |   120(+0) |
    | [lib]\c.a                                       |  53377(+0) | 2572(+0) |   127(+0) |
    | [lib]\gcc.a                                     |   3496(+0) |    0(+0) |     0(+0) |
    | [lib]\misc                                      |    208(+0) |   12(+0) |    28(+0) |
    | [lib]\nosys.a                                   |     32(+0) |    0(+0) |     0(+0) |
    | [lib]\stdc++.a                                  |     41(+0) |    4(+0) |    16(+0) |
    | main.o                                          |   4375(+0) |    8(+0) |  1318(+0) |
    | mbed-cloud-client-platform-common\mbed-os       |   1260(+0) |    0(+0) |    13(+0) |
    | mbed-cloud-client\certificate-enrollment-client |   2521(+0) |    0(+0) |     0(+0) |
    | mbed-cloud-client\factory-configurator-client   |  33304(+0) |    1(+0) |   115(+0) |
    | mbed-cloud-client\mbed-client                   |     30(+0) |    0(+0) |     0(+0) |
    | mbed-cloud-client\mbed-client-pal               |  18956(+0) |   24(+0) |   156(+0) |
    | mbed-cloud-client\source                        |    152(+0) |    4(+0) |    16(+0) |
    | mbed-os\cmsis                                   |   1192(+0) |    0(+0) |    84(+0) |
    | mbed-os\components                              |  10766(+0) |    0(+0) |     0(+0) |
    | mbed-os\drivers                                 |  10694(+0) |    4(+0) |   200(+0) |
    | mbed-os\events                                  |   2386(+0) |    0(+0) |     0(+0) |
    | mbed-os\features                                | 362232(+0) |  207(+0) |  7364(+0) |
    | mbed-os\hal                                     |   4609(+0) |    4(+0) |    84(+0) |
    | mbed-os\platform                                |  10743(+0) |  276(+0) |  1504(+0) |
    | mbed-os\rtos                                    |  21724(+0) |  168(+0) |  9557(+0) |
    | mbed-os\targets                                 |  30748(+0) |    4(+0) |  1397(+0) |
    | mbed_cloud_dev_credentials.o                    |   1543(+0) |    0(+0) |     0(+0) |
    | mbed_cloud_trust_anchor_credentials.o           |    168(+0) |    0(+0) |     0(+0) |
    | my-app-lib\services                             |   5411(+0) |  168(+0) |   647(+0) |
    | my-app-lib\song2                                |  22469(+0) |  140(+0) |  1121(+0) |
    | my-app-lib\third_party                          |  11145(+0) |    0(+0) |     0(+0) |
    | sda\cose-c                                      |   6482(+0) |    0(+0) |     4(+0) |
    | sda\helpers                                     |    730(+0) |    0(+0) |     4(+0) |
    | sda\sda-bundle-parser                           |   4427(+0) |    0(+0) |     0(+0) |
    | sda\sda-cose                                    |    136(+0) |    0(+0) |     0(+0) |
    | sda\secure-device-access                        |   5120(+0) |    0(+0) |   160(+0) |
    | sda\tinycbor                                    |   6811(+0) |    0(+0) |     0(+0) |
    | sda_initialization.o                            |    748(+0) |    0(+0) |     4(+0) |
    | shields\TARGET_CORDIO_BLUENRG                   |   3592(+0) |    0(+0) |  1641(+0) |
    | Subtotals                                       | 642858(+0) | 3624(+0) | 25680(+0) |
    Total Static RAM memory (data + bss): 29304(+0) bytes
    Total Flash memory (text + data): 646482(+0) bytes
    
    Image: .\BUILD\BINBEAT_V1_2\GCC_ARM-DEBUG\my-app.bin
    [mbed] Working path "c:\work-mbed\my-app" (program)
    

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.