1{ stdenv, buildPackages
2, fetchurl, zlib
3, buildPlatform, hostPlatform, targetPlatform
4, noSysDirs, gold ? true, bison ? null
5}:
6
7let
8 # Note to whoever is upgrading this: 2.29 is broken.
9 # ('nix-build pkgs/stdenv/linux/make-bootstrap-tools.nix -A test' segfaults on aarch64)
10 # Also glibc might need patching, see commit 733e20fee4a6700510f71fbe1a58ac23ea202f6a.
11 version = "2.28.1";
12 basename = "binutils-${version}";
13 inherit (stdenv.lib) optional optionals optionalString;
14 # The targetPrefix prepended to binary names to allow multiple binuntils on the
15 # PATH to both be usable.
16 targetPrefix = optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-";
17in
18
19stdenv.mkDerivation rec {
20 name = targetPrefix + basename;
21
22 src = fetchurl {
23 url = "mirror://gnu/binutils/${basename}.tar.bz2";
24 sha256 = "1sj234nd05cdgga1r36zalvvdkvpfbr12g5mir2n8i1dwsdrj939";
25 };
26
27 patches = [
28 # Turn on --enable-new-dtags by default to make the linker set
29 # RUNPATH instead of RPATH on binaries. This is important because
30 # RUNPATH can be overriden using LD_LIBRARY_PATH at runtime.
31 ./new-dtags.patch
32
33 # Since binutils 2.22, DT_NEEDED flags aren't copied for dynamic outputs.
34 # That requires upstream changes for things to work. So we can patch it to
35 # get the old behaviour by now.
36 ./dtneeded.patch
37
38 # Make binutils output deterministic by default.
39 ./deterministic.patch
40
41 # Always add PaX flags section to ELF files.
42 # This is needed, for instance, so that running "ldd" on a binary that is
43 # PaX-marked to disable mprotect doesn't fail with permission denied.
44 ./pt-pax-flags.patch
45
46 # Bfd looks in BINDIR/../lib for some plugins that don't
47 # exist. This is pointless (since users can't install plugins
48 # there) and causes a cycle between the lib and bin outputs, so
49 # get rid of it.
50 ./no-plugins.patch
51
52 # Help bfd choose between elf32-littlearm, elf32-littlearm-symbian, and
53 # elf32-littlearm-vxworks in favor of the first.
54 # https://github.com/NixOS/nixpkgs/pull/30484#issuecomment-345472766
55 ./disambiguate-arm-targets.patch
56
57 # For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
58 # not clear why this behavior was decided upon but it has the unfortunate
59 # consequence that the linker will fail to find transitive dependencies of
60 # shared objects when cross-compiling. Consequently, we are forced to
61 # override this behavior, forcing ld to search DT_RPATH even when
62 # cross-compiling.
63 ./always-search-rpath.patch
64 ];
65
66 outputs = [ "out" "info" "man" ];
67
68 depsBuildBuild = [ buildPackages.stdenv.cc ];
69 nativeBuildInputs = [ bison ];
70 buildInputs = [ zlib ];
71
72 inherit noSysDirs;
73
74 preConfigure = ''
75 # Clear the default library search path.
76 if test "$noSysDirs" = "1"; then
77 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
78 fi
79
80 # Use symlinks instead of hard links to save space ("strip" in the
81 # fixup phase strips each hard link separately).
82 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
83 sed -i "$i" -e 's|ln |ln -s |'
84 done
85 '';
86
87 # As binutils takes part in the stdenv building, we don't want references
88 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
89 NIX_CFLAGS_COMPILE = if hostPlatform.isDarwin
90 then "-Wno-string-plus-int -Wno-deprecated-declarations"
91 else "-static-libgcc";
92
93 # TODO(@Ericson2314): Always pass "--target" and always targetPrefix.
94 configurePlatforms = [ "build" "host" ] ++ stdenv.lib.optional (targetPlatform != hostPlatform) "target";
95
96 configureFlags = [
97 "--enable-targets=all" "--enable-64-bit-bfd"
98 "--disable-install-libbfd"
99 "--disable-shared" "--enable-static"
100 "--with-system-zlib"
101
102 "--enable-deterministic-archives"
103 "--disable-werror"
104 "--enable-fix-loongson2f-nop"
105 ] ++ optionals gold [ "--enable-gold" "--enable-plugins" ];
106
107 enableParallelBuilding = true;
108
109 passthru = {
110 inherit targetPrefix version;
111 };
112
113 meta = with stdenv.lib; {
114 description = "Tools for manipulating binaries (linker, assembler, etc.)";
115 longDescription = ''
116 The GNU Binutils are a collection of binary tools. The main
117 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
118 They also include the BFD (Binary File Descriptor) library,
119 `gprof', `nm', `strip', etc.
120 '';
121 homepage = http://www.gnu.org/software/binutils/;
122 license = licenses.gpl3Plus;
123 maintainers = with maintainers; [ ericson2314 ];
124 platforms = platforms.unix;
125
126 /* Give binutils a lower priority than gcc-wrapper to prevent a
127 collision due to the ld/as wrappers/symlinks in the latter. */
128 priority = 10;
129 };
130}