Wednesday, March 12, 2008

Embedding binary BLOBs into an ELF file

I needed this yesterday, found a link describing it - and forgot it by today :-(

For a project I'm working on, I need to embed a file into an ELF executable. The executable then needs to do things with the embedded file, i.e. it has to know where in memory the file resides and how large it is.

So here it goes, largely copied from the link mentioned above.
  • Create an object file from the binary blob:
    $ ld -r -b binary -o example.o example.bin
  • In the sources, declare the symbols:
    extern char _binary_example_bin_start[];
    extern char _binary_example_bin_end[];
  • Make sure the object file is linked with the other sources:
    $ gcc -o example example.c example.o

No comments: