title: Clang link-time optimization doesn't work properly on Fedora 18
body: <p>I'm a newcomer to clang, so it's likely I'm doing something silly.  But I've spent several hours looking for solutions, including searching here, where I haven't found questions addressing -flto with distro-provided packages.  The detail of this description are specific to Fedora 18, but I'm having similar problems on Ubuntu 13.04, so the problem isn't specific to Fedora.  It's either me or clang.</p>
  
  <p>Problem:  I'm trying to compile a simple hello-world program using <code>clang++ -flto</code> to get the benefits of link-time-optimization.  Without -flto it works fine.  With -flto it fails to link.  Invoking as <code>clang -flto -o hello hello.o -v</code> to see the full linker command line, I get:</p>
  
  <pre><code>$ clang++ -flto -o hello hello.o -v
  clang version 3.2 (tags/RELEASE_32/final)
  Target: x86_64-redhat-linux-gnu
  Thread model: posix
   "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../.. -L/lib -L/usr/lib -plugin /usr/bin/../lib/LLVMgold.so hello.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o
  /usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error loading plugin
  /usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error in plugin cleanup (ignored)
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  
  <p>There seem to be two problems:</p>
  
  <ol>
  <li><p>clang++ invokes the linker as <code>/usr/bin/ld</code>, and that's not the gold linker.  Fedora18 installs gold as <code>/usr/bin/ld.gold</code>.  I've tried creating a symlink from <code>/usr/local/bin/ld</code> to <code>/usr/bin/ld.gold</code>, verified that <code>which ld</code> says <code>/usr/local/bin/ld</code>, but clang++ doesn't use that.  It seems to be hardwired to /usr/bin/ld.</p></li>
  <li><p>clang++ invoked the linker with <code>-plugin /usr/bin/../lib/LLVMgold.so</code>.  That's wrong, as the Fedora distribution of clang places it at <code>/usr/lib64/llvm/LLVMgold.so</code>.</p></li>
  </ol>
  
  <p>I have tried manually invoking that linker line above with the following tweaks:</p>
  
  <ul>
  <li><p>Replace <code>-plugin /usr/bin/../lib/LLVMgold.so</code> with <code>-plugin /usr/lib64/llvm/LLVMgold.so</code>.  This yields the error message <code>hello.o: file not recognized: File format not recognized</code>.  So the non-gold linker seems to know about plugins but wont take the .o's which contain LLVM bitcode.</p></li>
  <li><p>Replace <code>/usr/bin/ld</code> with <code>/usr/bin/ld.gold</code>.  This works, generates an executable that runs as expected.</p></li>
  <li><p>Both of the above with <code>--plugin</code> instead of <code>-plugin</code>.  This change makes no difference.</p></li>
  </ul>
  
  <p>So what's the best way for somebody who prefers to stick to the system-provided packages to use clang -flto?  I'm hoping there is a config file, or undocumented options or environment variables that will let me override these.  Or better, that I'm missing a package and a "yum install ..." will fix it.</p>
  
  <p>I would prefer not to invoke the linker directly, as then my makefiles need to know system objects and libraries that they should be ignorant of (e.g. crt1.o, crtbegin.o, crtend.o).  I could also build clang myself, but I'm not seeing anything in its configure script that lets me configure the path of the linker and plugin.</p>
  
  <p>I'm running Fedora 18.  The only non-distro packages on the computer are google chrome and VMware Tools (it's a guest inside VMWare Fusion).  Versions of relevant Fedora packages (the whole computer is "yum updated" as of today, 29-Apr-2013):</p>
  
  <pre><code>$ yum list --noplugins installed binutils* clang* llvm* gcc*
  Installed Packages
  binutils.x86_64                      2.23.51.0.1-6.fc18                 @updates
  binutils-devel.x86_64                2.23.51.0.1-6.fc18                 @updates
  clang.x86_64                         3.2-2.fc18                         @updates
  clang-devel.x86_64                   3.2-2.fc18                         @updates
  clang-doc.noarch                     3.2-2.fc18                         @updates
  gcc.x86_64                           4.7.2-8.fc18                       @fedora 
  gcc-c++.x86_64                       4.7.2-8.fc18                       @fedora 
  llvm.x86_64                          3.2-2.fc18                         @updates
  llvm-libs.x86_64                     3.2-2.fc18                         @updates
  </code></pre>