{"id":7,"date":"2005-08-26T22:53:11","date_gmt":"2005-08-26T22:27:09","guid":{"rendered":""},"modified":"2017-03-07T20:17:44","modified_gmt":"2017-03-07T20:17:44","slug":"very_late_assignments_with_ld","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/rphillips\/2005\/08\/26\/very_late_assignments_with_ld\/","title":{"rendered":"Very Late Assignments with LD"},"content":{"rendered":"<p>The other day I found a really slick way of adding arbitrary data to an .o (object) file with &#8216;ld&#8217;.  It is probably easiest to show the setup, then explain it.<\/p>\n<p><strong>test.data<\/strong><br \/>\n<code><br \/>\nHello, World<br \/>\n<\/code><\/p>\n<p><strong>extra.h<\/strong><br \/>\n<code><br \/>\nextern const char _binary_test_data_start[];<br \/>\nextern const char _binary_test_data_end[];<br \/>\n<\/code><\/p>\n<p><strong>extra.c<\/strong><br \/>\n<code><br \/>\n#include \"extra.h\"<br \/>\n\/* this C file when compiled to an object will include the data found in test.data *\/<br \/>\n<\/code><\/p>\n<p><strong>main.c<\/strong><br \/>\n<code><br \/>\n#include \"extra.h\"<\/p>\n<p>void print_str(const char *ptr, int length);<\/p>\n<p>int main()<br \/>\n{<br \/>\n    int size = _binary_test_data_end - _binary_test_data_start;<br \/>\n    print_str(_binary_test_data_start, size);<br \/>\n    return 0;<br \/>\n}<br \/>\n<\/code><\/p>\n<p><strong>Compilation Steps<\/strong><br \/>\n<code><br \/>\n# gcc extra.c -c<br \/>\n# ld -b binary -r -o extra.o test.data<br \/>\n# gcc extra.o main.c -o out<br \/>\n<\/code><\/p>\n<p>note: print_str prints character by character to a specified length<br \/>\nnote2: _binary_test_data_size is exported, but I haven&#8217;t been able to figure out how to use it yet.<\/p>\n<p>LD creates a new ELF segment within the extra.o object file.  readelf and objdump will give quite a bit of useful information regarding the segments  that will eventually get linked into the executable <\/p>\n<p><strong># objdump -t extra.o<\/strong><\/p>\n<pre>\r\nextra.o:     file format elf32-i386\r\n\r\nSYMBOL TABLE:\r\n00000000 l    d  .data\t00000000 \r\n00000000 l    d  *ABS*\t00000000 \r\n00000000 l    d  *ABS*\t00000000 \r\n00000000 l    d  *ABS*\t00000000 \r\n0000000c g       .data\t00000000 _binary_test_data_end\r\n0000000c g       *ABS*\t00000000 _binary_test_data_size\r\n00000000 g       .data\t00000000 _binary_test_data_start\r\n<\/pre>\n<p>The start of the test.data file will be located in the variable &#8216;_binary_test_data_start&#8217; and similary the end will be &#8216;_binary_test_data_end&#8217;.<\/p>\n<p>This might be useful for embedding cryptographic signatures, or maybe game data files.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day I found a really slick way of adding arbitrary data to an .o (object) file with &#8216;ld&#8217;. It is probably easiest to show the setup, then explain it. test.data Hello, World extra.h extern const char _binary_test_data_start[]; extern const char _binary_test_data_end[]; extra.c #include &#8220;extra.h&#8221; \/* this C file when compiled to an object &hellip; <a href=\"https:\/\/blogs.gentoo.org\/rphillips\/2005\/08\/26\/very_late_assignments_with_ld\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Very Late Assignments with LD<\/span><\/a><\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":1,"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/posts\/7\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/rphillips\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}