Include .bin Binary Files in a GNU Linker File

Sometimes it is needed or desired just to add or link a piece of data or BLOB (Binary Large OBject) to the application. For example I have created a .bin file of my code and constant data, and I need to add it to an application using the linker file. How to do this?

added BLOB to application

First, I have to create the .bin file, for example have a look at MCUXpresso IDE: S-Record, Intel Hex and Binary Files.

Next, edit the linker .ld file and add the following, usually at the beginning of the file:

TARGET(binary) /* specify the file format of binary file */
INPUT ("<your file name here>") /* include the file */
OUTPUT_FORMAT(default) /* restore the out file format */

The TARGET() directive changes the file format for the linker to the binary format, and with OUTPUT_FORMAT() it switches back to the default format. With the INPUT() the file gets loaded.

Below is an example:

TARGET(binary) /* specify the file format of binary file */
INPUT ("../RomLib.bin") /* include the file */
OUTPUT_FORMAT(default) /* restore the out file format */

Lastly, the data needs to be placed somewhere. I prefer the following notation which places it at an absolute address:

.text <address> : {
  "<your file name here>"
}

For example:

    .text 0xF000 : {
      "../RomLib.bin"
    }    

Below is an example I’m using:

Example Linker File

The result then looks like below, with an entry in map file telling that the data has been loaded to the correct address:

Merged BLOB with Application

That’s it!

Happy Blobing 🙂

Links

2 thoughts on “Include .bin Binary Files in a GNU Linker File

  1. Thanks Erich for your useful and helpful article. I am wondering if we can have a parametrized path of the binary file in the linker script. Is it possible to set the path of the binary file in a CMakelist.txt such that it becomes visible to the linker script (e.g. add link options)?

    Like

What do you think?

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