<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:admin="http://webns.net/mvcb/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:html="http://www.w3.org/1999/html" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>Ross Burton</title><link>http://www.burtonini.com/blog</link><description>A potted account of Ross' life</description><language>en</language><ttl>60</ttl><dc:creator>Ross Burton</dc:creator><admin:generatorAgent rdf:resource="http://pyblosxom.sourceforge.net/"/><admin:errorReportsTo rdf:resource="mailto:ross@burtonini.com"/><item><title>Embedding Binary Blobs With GCC</title><guid isPermaLink="false">computers/ld-blobs-2007-07-13-15-50</guid><link>http://www.burtonini.com/blog/computers/ld-blobs-2007-07-13-15-50</link><description>For a long time I've wanted to know how to embed binary blobs into executables. This would be most useful ...</description><content:encoded><![CDATA[    <p>
      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.
    </p>
    <p>
      First, a caveat.  This probably requires GNU ld, which may or may not be a
      deal breaker for many people.
    </p>
    <p>
      First, create a data file.  Let's call it foo.txt, and put some text in it.
    </p>
    <pre>Hello, World!</pre>
    <p>
      Using <tt>ld</tt> this can be read in as a plain binary blob, and then
      written as a standard relocatable ELF object.
    </p>
    <pre>ld -r -b binary -o foo.o foo.txt</pre>
    <p>
      Now we have a standard ELF object with the data and some useful symbols
      defined.  <tt>objdump</tt> will show you the contents.
    </p>
    <pre>$ 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</pre>
    <p>
      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.
    </p>
    <pre>#include &lt;stdio.h&gt;
extern char _binary_foo_txt_start[];

int main (void) {
  puts (_binary_foo_txt_start);
  return 0;
}</pre>
    <p>
      Now if we compile this and link it against the generated object, we'll have a binary.
    </p>
    <pre>$ gcc -o test test.c foo.o
$ ./test
Hello, World!</pre>
    <p>
      Hooray!  One small problem which alert people should have noticed: the
      string itself is in the <tt>.data</tt> section, which is read/write.  For
      my use, I want it to be read-only data in the <tt>.rodata</tt> section so
      that it isn't copied for every instance of the application.  As far as I
      know, this isn't possible with <tt>ld</tt> but <tt>objcopy</tt> will let
      us rename sections on the fly.
    </p>
    <pre>$ 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</pre>
    <p>
      Excellent, problem solved.  If you want to download this sample, I have <a
      href="http://burtonini.com/computing/ldblob.tar.gz">a tarball</a>.  Many
      thanks to Daniel Jacobowitz for pointing out how to achieve this.
    </p>
    <p>
      <strong>Update:</strong> 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.
    </p>
    <p>
      <small>NP: <cite>()</cite>, Sigur R&oacute;s</small>
    </p>
]]></content:encoded><dc:date>2007-07-13T14:50:00Z</dc:date></item></channel></rss>