Embedding Binary Blobs With GCC

For a long time I've wanted to know how to embed binary blobs into executables. This would be most useful for files like Glade and and UI Manager definitions, which are required for a given program to work at all but either cannot be embedded as a string literal (Glade) or can be but is annoying (UI Manager). I finally asked the Interweb, and Daniel Jacobowitz replied with some pointers. It turns out that doing this is remarkable simple.

First, a caveat. This probably requires GNU ld, which may or may not be a deal breaker for many people.

First, create a data file. Let's call it foo.txt, and put some text in it.

Hello, World!

Using ld this can be read in as a plain binary blob, and then written as a standard relocatable ELF object.

ld -r -b binary -o foo.o foo.txt

Now we have a standard ELF object with the data and some useful symbols defined. objdump will show you the contents.

$ objdump -x foo.o 
foo.o:     file format elf32-i386

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .data         0000000d  00000000  00000000  00000034  2**0
                  CONTENTS, ALLOC, LOAD, DATA
SYMBOL TABLE:
00000000 l    d  .data  00000000 .data
0000000d g       .data  00000000 _binary_foo_txt_end
0000000d g       *ABS*  00000000 _binary_foo_txt_size
00000000 g       .data  00000000 _binary_foo_txt_start

Here we see 13 bytes of data, and a symbol which contains the address of the data. This is all we need to access it from a C program.

#include <stdio.h>
extern char _binary_foo_txt_start[];

int main (void) {
  puts (_binary_foo_txt_start);
  return 0;
}

Now if we compile this and link it against the generated object, we'll have a binary.

$ gcc -o test test.c foo.o
$ ./test
Hello, World!

Hooray! One small problem which alert people should have noticed: the string itself is in the .data section, which is read/write. For my use, I want it to be read-only data in the .rodata section so that it isn't copied for every instance of the application. As far as I know, this isn't possible with ld but objcopy will let us rename sections on the fly.

$ objcopy --rename-section .data=.rodata,alloc,load,readonly,data,contents foo.o foo.o
$ objdump  -h foo.o
...
  0 .rodata       0000000d  00000000  00000000  00000034  2**0

Excellent, problem solved. If you want to download this sample, I have a tarball. Many thanks to Daniel Jacobowitz for pointing out how to achieve this.

Update: note that any data embedded in the binary like this won't be terminated with a NULL. This is obvious in hindsight, but due to luck my example still worked. There might be a way of asking objcopy to append a 0 to the end of the data, but if not always remember to use the start and end pointers or size instead of just the start, or append a NULL yourself before converting to an ELF.

NP: (), Sigur Rós

15:50 Friday, 13 Jul 2007 [#] [computers] (23 comments)

Posted by Jasen at Wed Sep 12 00:29:57 2012:
to print non-null terminated strings:

  printf(
  %.*s",
  _binary_foo_txt_length,
  _binary_foo_txt_start);

or

fwrite(
  _binary_foo_txt_start
  ,_binary_foo_txt_length
  ,1
  ,stdout
  );
Posted by Kevin at Sat Jul 20 21:38:28 2013:
This was excellent. Thanks so much, I've been trying various forms of `cat`ing the blob onto the end and naively hoping a pointer to it would work, linking, and other things. This was a big help.
Posted by Kevin at Sat Jul 20 22:01:40 2013:
This was excellent. Thanks so much, I've been trying various forms of `cat`ing the blob onto the end and naively hoping a pointer to it would work, linking, and other things. This was a big help.
Posted by japanese ex at Tue Nov 5 01:27:13 2013:
I know this if off topic but I'm looking into starting my own weblog and was curious what all is needed to get setup? I'm assuming having a blog like yours would cost a pretty penny? I'm not very web smart so I'm not 100% http://getfootballnewsdaily.com/ positive. Any recommendations or advice would be greatly appreciated. Cheers
Posted by transportation from sfo at Mon Dec 23 04:45:27 2013:
You need to be a part of http://limousin-chambres-bb.com/ a contest for one of the highest quality websites on the net. I most certainly will highly recommend this blog!
Posted by Henry Betts at Mon Jan 20 19:45:23 2014:
It it worth trying then treat the blob as text. You may have to do a bit of work to make sure there's a '\0' at the end of the text if you need it terminated like that, I got the idea from http://www.hvacpartsshop.com. Some experimenting might be in order
Posted by Playground Mats at Mon Feb 10 05:06:04 2014:
We provide you interlocking Interlocking Rubber Grass Mats to keep your children safe. These interlocking rubber grass mats are very convenient to use. You can install them very easily. It is well suited for places like play areas, decks, rock wall climbing. They can easily be maintained as their cleaning is very easy. http://www.playgroundmats.co.uk/
Posted by geo marketing at Wed Feb 12 10:11:48 2014:
This is excellent. Cheers much, I am seeking different forms of `cat`ing your blob geo marketing onto the finish and also naively hoping a pointer with it would work, back linking, along with other factors. This is a big help.
Posted by Adele Fielder at Fri Feb 28 07:18:56 2014:
Hello! Someone in my Myspace group shared this website with us so I came to take a look. I'm definitely enjoying the information. I'm book-marking and will be tweeting this to my followers! Exceptional blog and fantastic style and design. http://www.youtube.com/watch?v=BdydU8sIEzY
Posted by ada at Fri Mar 14 12:37:39 2014:
You done certain good points there. I did a search on the subject and found nearly all persons will agree with your blog
http://www.yachtchartersitaly.com/
Posted by my website at Wed Mar 19 14:50:53 2014:
Thanks for this awesome blogspot website!
Posted by lisa noel at Thu Mar 20 12:57:39 2014:
This was excellent. Thanks so much https://archive.org/details/FatherOfTheGroomSpeech
Posted by Alex at Mon Mar 24 05:40:46 2014:
Thanks for taking the time to put this out there. Great job. www.e-brokerforex.com :)
Posted by ada at Tue Mar 25 11:21:15 2014:
This is like my third time visiting your Blog. You should write more please, this information will help me and others. Try proof reading a couple of times before publishing. Keep writing though
yachtbooker
Posted by becomingalawyer at Thu Mar 27 07:08:43 2014:
Tennessee has a long history of being a pivotal area in United States history. Some of the most influential events of the Revolutionary War and America's expansion west occurred in the area that is now known as Tennessee. The purchase of land from the Cherokee Nation at Sycamore becomingalawyer
Posted by businessfinancecourse at Thu Mar 27 07:08:51 2014:
Interested people who would like to do business finance courses in India actually have some marvelous options. In particular those who are interested in opting for some extra-vocational training, which may or may not be online, there are institutes which encourage international participation. businessfinancecourse
Posted by cheaphomesecurity at Thu Mar 27 07:08:58 2014:
The first thing that we recommend is that you look at your house from the exterior in much the same way that a burglar would. Burglars case neighborhoods looking for targets of opportunity. Those are homes that look, to them, to be easy targets. cheaphomesecurity
Posted by personal statement writing services at Fri Mar 28 07:56:22 2014:
For quite a while I've needed to know how to install double blobs into executables. This might be most advantageous for records like Glade and UI Manager definitions, which are needed for an offered project to work at everything except either can't be implanted as a string strict (Glade) or could be however is irritating (UI Manager). I at long last asked the Interweb, and Daniel Jacobowitz answered with a few pointers. It would seem doing this is amazing straightforward.personal statement writing services
Posted by ada at Tue Apr 1 10:27:25 2014:
I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end
yachtbooker
Posted by הסעות בלונדון at Sun Apr 13 08:15:44 2014:
Glade and UI Manager definitions, which are needed for an offered project to work at everything except either can't be implanted as a string strict (Glade) or could be however is irritating (UI Manager). I at long last asked the Interweb, and Daniel Jacobowitz answered with a few pointers. It would seem doing this is amazing straightforward
Posted by BlackMen at Sun Apr 13 09:43:43 2014:
This would be most useful for files like Glade and and UI Manager definitions, which are required for a given program to work at all but either cannot be embedded as a string literal (Glade) or can be but is annoying (UI Manager). I finally asked the Interweb, and Daniel Jacobowitz replied with some pointers. It turns out that doing this is remarkable simple.  soundcloud downloader
Posted by kijiji toronto at Mon Apr 14 12:11:33 2014:
an output from the audio systemManaging PeopleThe people who work for you are among your major keys to success It is possible louis vuitton to earn a large sum of money by constantly investing and selling louis vuitton store your holdings while using technical indicators to guide your decisions
Posted by BlackMen at Fri Apr 18 09:21:51 2014:
This would be most useful for files like Glade and and UI Manager definitions, which are required for a given program to work at all but either cannot be embedded as a string literal (Glade) or can be but is annoying (UI Manager). I finally asked the Interweb, and Daniel Jacobowitz replied with some pointers. It turns out that doing this is remarkable simple.  flight simulator game 4

Name:


E-mail:


URL:


Add 4 and 7 (required):


Comment: