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

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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