2009년 9월 25일 금요일

Selectively disabling Gold linker

Gold is a new, fast linker written by Ian Lance Taylor and his colleagues at Google. It has been a year since its first public release, and it is now packaged as binutils-gold in Debian, so you can easilly try it. Google claims Gold is five times faster than classic GNU linker for linking large C++ applications (of which Google has plenty), and my experience confirms. Overall, this is a welcome development.

Alas, not everything is as rosy as it seems. GNU linker, in its multi-decades history, has accumulated a lot of features, and Gold, being a from-the-scratch re-implementation, is yet to catch up. There are bugs, incompatibilities, and missing functionalities. Above Debian package installs Gold as /usr/bin/ld, and after installation, some softwares may fail to compile from source. Often it is clear this is Gold's fault; but sometimes it is not. And you probably want to know a way to selectively disable Gold linker so you can check whether it's Gold's fault.

Above Debian package leaves classic GNU linker as /usr/bin/ld.single, and it is likely that other systems will also retain classic GNU linker under different names. So one way to disable Gold linker is to let environment variable LD to point to classic GNU linker before configuring softwares.

That is, if the build system of the software in question honors LD. But quite often, linker is implicitly called by compiler driver gcc, so you want to tell gcc to use another linker, and telling build system to use another linker is useless.

This is where gcc's -B option comes to rescue. You can pass "-B prefix" option to gcc, and gcc will first search under prefix to find the linker it uses, before falling back to default paths. So I arrived at the following solution:

$ mkdir /opt/no_gold
$ ln -s /usr/bin/ld.single /opt/no_gold/ld
$ export CC='gcc -B /opt/no_gold'


By the way, Gold problem happened to me while I compiled Midori from source. Midori is a lightweight web browser based on WebKit, with Adblock, user scripts, user styles, and other interesting features. I can do a little advertisement on my blog, can't I? Also, thanks to people on #midori IRC channel at Freenode for all the help and patience.