Index: nixpkgs/pkgs/development/tools/misc/binutils/default.nix
===================================================================
--- nixpkgs/pkgs/development/tools/misc/binutils/default.nix	(revision 24799)
+++ nixpkgs/pkgs/development/tools/misc/binutils/default.nix	(working copy)
@@ -1,13 +1,22 @@
-{stdenv, fetchurl, noSysDirs, cross ? null}:
+{stdenv, fetchurl, noSysDirs, cross ? null, goldEnabled ? false, bison ? null}:
 
+assert goldEnabled -> bison != null;
+
 let
-    basename = "binutils-2.20";
+    basename = "binutils";
+    version  = if goldEnabled then "2.20.90"
+               else "2.20";
 in
 stdenv.mkDerivation rec {
-  name = basename + stdenv.lib.optionalString (cross != null) "-${cross.config}";
+  name = basename + "-" + version
+                  + stdenv.lib.optionalString goldEnabled "-gold"
+                  + stdenv.lib.optionalString (cross != null) "-${cross.config}";
 
-  src = fetchurl {
-    url = "mirror://gnu/binutils/${basename}.tar.bz2";
+  src = if goldEnabled then fetchurl {
+    url =  "ftp://sourceware.org/pub/binutils/snapshots/${basename}-${version}.tar.bz2";
+    sha256 = "0wadr0x9xq635l5dln45cd2jlxbrfryiwrz678blx83ywvrbfwbv";
+  } else fetchurl {
+    url = "mirror://gnu/binutils/${basename}-${version}.tar.bz2";
     sha256 = "1c3m789p5rwmmnck5ms4zcnc40axss3gxzivz571al1vmbq0kpz1";
   };
 
@@ -16,16 +25,25 @@
     # RUNPATH instead of RPATH on binaries.  This is important because
     # RUNPATH can be overriden using LD_LIBRARY_PATH at runtime.
     ./new-dtags.patch
+  ]
 
-    ./as-pr10856.patch
-  ];
+  # Fix a regression in GNU as in stable binutils
+  ++ stdenv.lib.optional (!goldEnabled)
+       ./as-pr10856.patch
 
+  # patch gold to allow paths INPUT to be relative to the current working directory
+  ++ stdenv.lib.optional goldEnabled
+       ./gold-input-path.patch
+  ;
+
   inherit noSysDirs;
 
+  buildInputs = stdenv.lib.optional goldEnabled bison;
+
   preConfigure = ''
     # Clear the default library search path.
     if test "$noSysDirs" = "1"; then
-	echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
+        echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
     fi
 
     # Use symlinks instead of hard links to save space ("strip" in the
@@ -36,7 +54,8 @@
   '';
 
   configureFlags = "--disable-werror" # needed for dietlibc build
-      + stdenv.lib.optionalString (cross != null) " --target=${cross.config}";
+      + stdenv.lib.optionalString (cross != null) " --target=${cross.config}"
+      + stdenv.lib.optionalString goldEnabled " --enable-gold";
 
   meta = {
     description = "GNU Binutils, tools for manipulating binaries (linker, assembler, etc.)";
Index: nixpkgs/pkgs/development/tools/misc/binutils/gold-input-path.patch
===================================================================
--- nixpkgs/pkgs/development/tools/misc/binutils/gold-input-path.patch	(revision 0)
+++ nixpkgs/pkgs/development/tools/misc/binutils/gold-input-path.patch	(revision 0)
@@ -0,0 +1,21 @@
+Fix a bug in Gold ld:
+http://sourceware.org/bugzilla/show_bug.cgi?id=11182
+
+Gold can't find INPUT files with fully specified paths
+
+--- a/gold/script.cc	2010/01/20 22:36:09 +1300
++++ b/gold/script.cc	2010/01/20 22:36:09 +1300
+@@ -2182,6 +2182,13 @@
+ 	}
+     }
+ 
++  // remove any path in file name
++  size_t path_part = name_string.find_last_of('/');
++  if (path_part != length)
++    {
++      name_string = name_string.substr(path_part + 1);
++    }
++
+   Input_file_argument file(name_string.c_str(),
+ 			   Input_file_argument::INPUT_FILE_TYPE_FILE,
+ 			   extra_search_path, false,
Index: nixpkgs/pkgs/development/libraries/libgcrypt/default.nix
===================================================================
--- nixpkgs/pkgs/development/libraries/libgcrypt/default.nix	(revision 24799)
+++ nixpkgs/pkgs/development/libraries/libgcrypt/default.nix	(working copy)
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, libgpgerror }:
+{ fetchurl, stdenv, libgpgerror, goldEnabled ? false }:
 
 stdenv.mkDerivation rec {
   name = "libgcrypt-1.4.5";
@@ -10,6 +10,9 @@
 
   propagatedBuildInputs = [ libgpgerror ];
 
+  configureFlags = stdenv.lib.optionalString goldEnabled
+                     "LDFLAGS=-lgpg-error";
+
   doCheck = true;
 
   # For some reason the tests don't find `libgpg-error.so'.
Index: nixpkgs/pkgs/development/libraries/imlib2/default.nix
===================================================================
--- nixpkgs/pkgs/development/libraries/imlib2/default.nix	(revision 24799)
+++ nixpkgs/pkgs/development/libraries/imlib2/default.nix	(working copy)
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, x11, libjpeg, libtiff, libungif, libpng, bzip2 }:
+{ stdenv, fetchurl, x11, libjpeg, libtiff, libungif, libpng, bzip2, goldEnabled ? false }:
 
 stdenv.mkDerivation {
   name = "imlib2-1.3.0";
@@ -9,4 +9,8 @@
   };
   
   buildInputs = [ x11 libjpeg libtiff libungif libpng bzip2 ];
+
+  preConfigure = stdenv.lib.optionalString goldEnabled ''
+    configureFlagsArray=( LDFLAGS="-lX11 -lm" )
+  '';
 }
Index: nixpkgs/pkgs/development/compilers/ghc/6.12.3.nix
===================================================================
--- nixpkgs/pkgs/development/compilers/ghc/6.12.3.nix	(revision 24799)
+++ nixpkgs/pkgs/development/compilers/ghc/6.12.3.nix	(working copy)
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, ghc, perl, gmp, ncurses}:
+{stdenv, fetchurl, ghc, perl, gmp, ncurses, gcc }:
 
 stdenv.mkDerivation rec {
   version = "6.12.3";
@@ -25,7 +25,7 @@
   '';
 
   configureFlags=[
-    "--with-gcc=${stdenv.gcc}/bin/gcc"
+    "--with-gcc=${gcc}/bin/gcc"
   ];
 
   # required, because otherwise all symbols from HSffi.o are stripped, and
Index: nixpkgs/pkgs/tools/networking/ssmtp/default.nix
===================================================================
--- nixpkgs/pkgs/tools/networking/ssmtp/default.nix	(revision 24799)
+++ nixpkgs/pkgs/tools/networking/ssmtp/default.nix	(working copy)
@@ -1,4 +1,4 @@
-{stdenv, fetchurl, tlsSupport ? false, openssl ? null}:
+{stdenv, fetchurl, tlsSupport ? false, openssl ? null, goldEnabled ? false }:
 
 assert tlsSupport -> openssl != null;
 
@@ -14,7 +14,9 @@
       sha256 = "2eb5b2af76d220f14e5133ec4078bab531209fb2f9f8f4e780a0ab8de4818d39";
      })
   ];
-  configureFlags = "${if tlsSupport then "--enable-ssl" else ""}";
+  configureFlags = if tlsSupport then "--enable-ssl"
+                        + stdenv.lib.optionalString goldEnabled "-lcrypto"
+                   else "";
   postConfigure = "
     sed -e '/INSTALLED_CONFIGURATION_FILE/d' \\
         -e 's|\\(DSSMTPCONFDIR.*\\).(.*)\\(.*$\\)|\\1/etc/ssmtp\\2|' \\
Index: nixpkgs/pkgs/tools/networking/curl/default.nix
===================================================================
--- nixpkgs/pkgs/tools/networking/curl/default.nix	(revision 24799)
+++ nixpkgs/pkgs/tools/networking/curl/default.nix	(working copy)
@@ -3,6 +3,7 @@
 , sslSupport ? false, openssl
 , scpSupport ? false, libssh2
 , linkStatic ? false
+, goldEnabled ? false
 }:
 
 assert zlibSupport -> zlib != null;
@@ -33,7 +34,8 @@
   dontDisableStatic = if linkStatic then true else false;
 
   CFLAGS = if stdenv ? isDietLibC then "-DHAVE_INET_NTOA_R_2_ARGS=1" else "";
-  LDFLAGS = if linkStatic then "-static" else "";
+  LDFLAGS = stdenv.lib.optionalString linkStatic "-static"
+          + stdenv.lib.optionalString goldEnabled " -lrt";
   CXX = "g++";
   CXXCPP = "g++ -E";
 
Index: nixpkgs/pkgs/stdenv/linux/default.nix
===================================================================
--- nixpkgs/pkgs/stdenv/linux/default.nix	(revision 24799)
+++ nixpkgs/pkgs/stdenv/linux/default.nix	(working copy)
@@ -191,7 +191,7 @@
   #    bootstrap tools.
   stdenvLinuxBoot3 = stdenvBootFun {
     gcc = wrapGCC rec {
-      inherit (stdenvLinuxBoot2Pkgs) binutils;
+      binutils = stdenvLinuxBoot2Pkgs.binutilsLd;
       coreutils = bootstrapTools;
       libc = stdenvLinuxGlibc;
       gcc = stdenvLinuxBoot2Pkgs.gcc.gcc;
Index: nixpkgs/pkgs/top-level/all-packages.nix
===================================================================
--- nixpkgs/pkgs/top-level/all-packages.nix	(revision 24799)
+++ nixpkgs/pkgs/top-level/all-packages.nix	(working copy)
@@ -508,7 +508,7 @@
 
   curl = makeOverridable (import ../tools/networking/curl) rec {
     fetchurl = fetchurlBoot;
-    inherit stdenv zlib openssl libssh2;
+    inherit stdenv zlib openssl libssh2 goldEnabled;
     zlibSupport = ! ((stdenv ? isDietLibC) || (stdenv ? isStatic));
     sslSupport = zlibSupport;
     scpSupport = zlibSupport && !stdenv.isSunOS && !stdenv.isCygwin;
@@ -710,7 +710,7 @@
 
   grub = import ../tools/misc/grub {
     inherit fetchurl autoconf automake;
-    stdenv = stdenv_32bit;
+    stdenv = overrideGCC stdenv_32bit gccLd;
     buggyBiosCDSupport = (getConfig ["grub" "buggyBiosCDSupport"] true);
   };
 
@@ -1596,8 +1596,8 @@
     cross = assert crossSystem != null; crossSystem;
   };
 
-  gcc43_multi = lowPrio (wrapGCCWith (import ../build-support/gcc-wrapper) glibc_multi (gcc43.gcc.override {
-    stdenv = overrideGCC stdenv (wrapGCCWith (import ../build-support/gcc-wrapper) glibc_multi gcc);
+  gcc43_multi = lowPrio (wrapGCCWith (import ../build-support/gcc-wrapper) glibc_multi binutils (gcc43.gcc.override {
+    stdenv = overrideGCC stdenv (wrapGCCWith (import ../build-support/gcc-wrapper) glibc_multi binutils gcc);
     profiledCompiler = false;
     enableMultilib = true;
   }));
@@ -2004,23 +2004,24 @@
 
   win32hello = callPackage ../development/compilers/visual-c++/test { };
 
-  wrapGCCWith = gccWrapper: glibc: baseGCC: gccWrapper {
+  wrapGCCWith = gccWrapper: glibc: binutils: baseGCC: gccWrapper {
     nativeTools = stdenv ? gcc && stdenv.gcc.nativeTools;
     nativeLibc = stdenv ? gcc && stdenv.gcc.nativeLibc;
     nativePrefix = if stdenv ? gcc then stdenv.gcc.nativePrefix else "";
     gcc = baseGCC;
     libc = glibc;
-    inherit stdenv binutils coreutils zlib;
+    binutils = binutils;
+    inherit stdenv coreutils zlib;
   };
 
-  wrapGCC = wrapGCCWith (import ../build-support/gcc-wrapper) glibc;
+  wrapGCC = wrapGCCWith (import ../build-support/gcc-wrapper) glibc binutils;
 
   # To be removed on stdenv-updates
   # By now this has at least the fix of setting the proper rpath when a file "libbla.so"
   # is passed directly to the linker.
   # This is of interest to programs built by cmake, because this is a common practice
   # in cmake builds.
-  wrapGCC2 = wrapGCCWith (import ../build-support/gcc-wrapper/default2.nix) glibc;
+  wrapGCC2 = wrapGCCWith (import ../build-support/gcc-wrapper/default2.nix) glibc binutils;
   stdenv2 = if (gcc.nativeTools) then stdenv else (overrideGCC stdenv (wrapGCC2 gcc.gcc));
 
   wrapGCCCross =
@@ -2305,6 +2308,14 @@
     inherit noSysDirs;
   };
 
+  goldEnabled = getConfig [ "binutils" "gold" ] false;
+
+  # a non-gold using binutils and gcc
+  binutilsLd = lowPrio (binutils.override {
+    goldEnabled = false;
+  }); 
+  gccLd = lowPrio (wrapGCCWith (import ../build-support/gcc-wrapper) glibc binutilsLd gcc.gcc);
+
   binutilsCross = forceBuildDrv (import ../development/tools/misc/binutils {
       inherit stdenv fetchurl;
       noSysDirs = true;
@@ -2905,7 +2916,9 @@
     installLocales = getPkgConfig "glibc" "locales" false;
   };
 
-  glibcLocales = callPackage ../development/libraries/glibc-2.11/locales.nix { };
+  glibcLocales = callPackage ../development/libraries/glibc-2.11/locales.nix {
+    stdenv = overrideGCC stdenv gccLd;
+  };
 
   glibcInfo = callPackage ../development/libraries/glibc-2.11/info.nix { };
 
