title: How does gcc's linktime optimisation (-flto flag) work
body: <p>I understand more or less the idea: When compiling separate modules and producing assembly code, functions calling each other have to respect strictly the <em>calling convention</em>, which kills the opportunity for many optimisations when compiling separate modules.</p>
  
  <p>For instance if I have function A which calls function B which calls function C, all 3 in their own separate source files, it becomes possible to allocate registers evenly within the functions so that no register saving on the stack is necessary at all during those calls. With traditional compile-assembly-linking this is not possible, as the caller-saved and callee-saved registers are imposed by the calling convention.</p>
  
  <p>Another optimisation is to inline functions which are called only once. This previously was possible only if a function is local, but thanks to linktime optimisation it's now possible even if the function is in another source file.</p>
  
  <p>Now, if I compile with both <code>-flto</code> and <code>-S</code> flags, I see that instead of normal assembly instructions, gcc generates an encoded representation of the program, such as this:</p>
  
  <pre><code>    .section        .gnu.lto_.inline.c3c5e6ef8ec983c,"dr0"
      .ascii "x\234mQ;N\303@\20}\273\353\17\370C\234\20\242`\"!Q\20\11Ah\322&amp;\25\242\314\231|\4\32\220\220(,$.@\205D\343\3P Z.\341Tn\231\35\274\31L\342\342\355\314\274\371&lt;\317\30\354\376\356\365\357\333\7\262"
      .ascii "1\240G\325\273\202\7\216\232\204\36\205"
      .ascii "8\242\370\240|\222"
      .ascii "8\374\21\205ty\352\"*r\340!:!n\357n%]\224\345\10|\304\23\342\274z\346"
      .ascii "8\35\23\370\7\4\1\366s\362\203j\271]\27bb{\316\353\27\343\310\4\371\374\237*n#\220\342rA\31"
      .ascii "7\365\263\327\231\26\364\10"
      .ascii "2\\-\311\277\255^w\220}|\340\233\306\352\263\362Qo+e+\314\354\277\246\354\252\277\20\364\224%T\233'eR\301{\32\340\372\313\362\263\242\331\314\340\24\6\21s\210\243!\371\347\325\333&amp;m\210\305\203\355\277*\326\236\34\300-\213\327\306\2Td\317\27\231\26tl,\301\26\21cd\27\335#\262L\223"
      .ascii "8\353\30\351\264{I\26\316\11\14"
      .ascii "9\326h\254\220B}6a\247\13\353\27M\274\231"
      .ascii "0\23M\332\272\272%d[\274\36Q\200\37\321\1&amp;\35"
  </code></pre>
  
  <p>Since the data is in its own particular section, the linker sees this, and does the code generation. If the module was written in either assembly or with no <code>-flto</code> flag, then the linker would see data in the <code>.text</code> section instead, so there is no confusion possible for the linker.</p>
  
  <p>The problem is: How can the linker generate code? Normally only gcc can generate code, the linker's role is just here to change a few offsets and adapt the binary format. In order to generate code, the linker would need to contain a second copy of the entire gcc backend (half of the compiler which generates assembly code from intermediate representation), as well as the entire assembler (since no assembly code was produced). How is such a thing possible, especially considering that binutils is a completely separate entity from gcc, developed by different teams?</p>