title: How to 'link' object file to executable/compiled binary?
body: <p><strong>Problem</strong></p>
  
  <p>I wish to inject an object file into an existing binary. As a concrete example, consider a source <code>Hello.c</code>:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  int main(void)
  {
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>It can be compiled to an executable named <code>Hello</code> through <code>gcc -std=gnu99 -Wall Hello.c -o Hello</code>. Furthermore, now consider <code>Embed.c</code>:</p>
  
  <pre><code>func1(void)
  {
  }
  </code></pre>
  
  <p>An object file <code>Embed.o</code> can be created from this through <code>gcc -c Embed.c</code>. My question is how to generically insert <code>Embed.o</code> into <code>Hello</code> in such a way that the necessary relocations are performed, and the appropriate ELF internal tables (e.g. symbol table, PLT, etc.) are patched properly?</p>
  
  <hr>
  
  <p><strong>Assumptions</strong></p>
  
  <p>It can be assumed that the object file to be embedded has its dependencies statically linked already. Any dynamic dependencies, such as the C runtime can be assumed to be present also in the target executable.</p>
  
  <hr>
  
  <p><strong>Current Attempts/Ideas</strong></p>
  
  <ul>
  <li>Use <code>libbfd</code> to copy sections from the object file into the binary. The progress I have made with this is that I can create a new object with the sections from the original binary and the sections from the object file. The problem is that since the object file is relocatable, its sections can not be copied properly to the output without performing the relocations first.</li>
  <li>Convert the binary back to an object file and relink with <code>ld</code>. So far I tried using <code>objcopy</code> to perform the conversion <code>objcopy --input elf64-x86-64 --output elf64-x86-64 Hello Hello.o</code>. Evidently this does not work as I intend since <code>ld -o Hello2 Embed.o Hello.o</code> will then result in <code>ld: error: Hello.o: unsupported ELF file type 2</code>. I guess this should be expected though since <code>Hello</code> is not an object file.</li>
  <li>Find an existing tool which performs this sort of insertion?</li>
  </ul>
  
  <hr>
  
  <p><strong>Rationale <em>(Optional Read)</em></strong></p>
  
  <p>I am making a static executable editor, where the vision is to allow the instrumentation of arbitrary user-defined routines into an existing binary. This will work in two steps:</p>
  
  <ol>
  <li>The injection of an object file (containing the user-defined routines) into the binary. <strong>This is a mandatory step and can not be worked around by alternatives such as injection of a shared object instead.</strong></li>
  <li>Performing static analysis on the new binary and using this to statically detour routines from the original code to the newly added code.</li>
  </ol>
  
  <p>I have, for the most part, already completed the work necessary for step 2, but I am having trouble with the injection of the object file. The problem is definitely solvable given that other tools use the same method of object injection (e.g. <a href="http://pages.cs.wisc.edu/~larus/eel.html" rel="noreferrer">EEL</a>).</p>