1{ stdenv, lib, buildPackages
2, fetchurl, zlib, autoreconfHook
3# Enabling all targets increases output size to a multiple.
4, withAllTargets ? false, libbfd, libopcodes
5, enableShared ? true
6, noSysDirs, gold ? true, bison ? null
7}:
8
9let
10 reuseLibs = enableShared && withAllTargets;
11
12 # Remove gold-symbol-visibility patch when updating, the proper fix
13 # is now upstream.
14 # https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=commitdiff;h=330b90b5ffbbc20c5de6ae6c7f60c40fab2e7a4f;hp=99181ccac0fc7d82e7dabb05dc7466e91f1645d3
15 version = "2.31.1";
16 basename = "binutils-${version}";
17 # The targetPrefix prepended to binary names to allow multiple binuntils on the
18 # PATH to both be usable.
19 targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
20 "${stdenv.targetPlatform.config}-";
21in
22
23stdenv.mkDerivation rec {
24 name = targetPrefix + basename;
25
26 # HACK to ensure that we preserve source from bootstrap binutils to not rebuild LLVM
27 src = stdenv.__bootPackages.binutils-unwrapped.src or (fetchurl {
28 url = "mirror://gnu/binutils/${basename}.tar.bz2";
29 sha256 = "1l34hn1zkmhr1wcrgf0d4z7r3najxnw3cx2y2fk7v55zjlk3ik7z";
30 });
31
32 patches = [
33 # Make binutils output deterministic by default.
34 ./deterministic.patch
35
36 # Bfd looks in BINDIR/../lib for some plugins that don't
37 # exist. This is pointless (since users can't install plugins
38 # there) and causes a cycle between the lib and bin outputs, so
39 # get rid of it.
40 ./no-plugins.patch
41
42 # Help bfd choose between elf32-littlearm, elf32-littlearm-symbian, and
43 # elf32-littlearm-vxworks in favor of the first.
44 # https://github.com/NixOS/nixpkgs/pull/30484#issuecomment-345472766
45 ./disambiguate-arm-targets.patch
46
47 # For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
48 # not clear why this behavior was decided upon but it has the unfortunate
49 # consequence that the linker will fail to find transitive dependencies of
50 # shared objects when cross-compiling. Consequently, we are forced to
51 # override this behavior, forcing ld to search DT_RPATH even when
52 # cross-compiling.
53 ./always-search-rpath.patch
54
55 # https://sourceware.org/bugzilla/show_bug.cgi?id=22868
56 ./gold-symbol-visibility.patch
57
58 # https://sourceware.org/bugzilla/show_bug.cgi?id=23428
59 # un-break features so linking against musl doesn't produce crash-only binaries
60 ./0001-x86-Add-a-GNU_PROPERTY_X86_ISA_1_USED-note-if-needed.patch
61 ./0001-x86-Properly-merge-GNU_PROPERTY_X86_ISA_1_USED.patch
62 ./0001-x86-Properly-add-X86_ISA_1_NEEDED-property.patch
63 ] ++ lib.optional stdenv.targetPlatform.isiOS ./support-ios.patch;
64
65 outputs = [ "out" "info" "man" ];
66
67 depsBuildBuild = [ buildPackages.stdenv.cc ];
68 nativeBuildInputs = [
69 bison
70 ] ++ lib.optionals stdenv.targetPlatform.isiOS [
71 autoreconfHook
72 ];
73 buildInputs = [ zlib ];
74
75 inherit noSysDirs;
76
77 preConfigure = ''
78 # Clear the default library search path.
79 if test "$noSysDirs" = "1"; then
80 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
81 fi
82
83 # Use symlinks instead of hard links to save space ("strip" in the
84 # fixup phase strips each hard link separately).
85 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
86 sed -i "$i" -e 's|ln |ln -s |'
87 done
88 '';
89
90 # As binutils takes part in the stdenv building, we don't want references
91 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
92 NIX_CFLAGS_COMPILE = if stdenv.hostPlatform.isDarwin
93 then "-Wno-string-plus-int -Wno-deprecated-declarations"
94 else "-static-libgcc";
95
96 hardeningDisable = [ "format" "pie" ];
97
98 # TODO(@Ericson2314): Always pass "--target" and always targetPrefix.
99 configurePlatforms = [ "build" "host" ] ++ lib.optional (stdenv.targetPlatform != stdenv.hostPlatform) "target";
100
101 configureFlags =
102 (if enableShared then [ "--enable-shared" "--disable-static" ]
103 else [ "--disable-shared" "--enable-static" ])
104 ++ lib.optional withAllTargets "--enable-targets=all"
105 ++ [
106 "--enable-64-bit-bfd"
107 "--with-system-zlib"
108
109 "--enable-deterministic-archives"
110 "--disable-werror"
111 "--enable-fix-loongson2f-nop"
112
113 # Turn on --enable-new-dtags by default to make the linker set
114 # RUNPATH instead of RPATH on binaries. This is important because
115 # RUNPATH can be overriden using LD_LIBRARY_PATH at runtime.
116 "--enable-new-dtags"
117 ] ++ lib.optionals gold [ "--enable-gold" "--enable-plugins" ];
118
119 doCheck = false; # fails
120
121 postFixup = lib.optionalString reuseLibs ''
122 rm "$out"/lib/lib{bfd,opcodes}-${version}.so
123 ln -s '${lib.getLib libbfd}/lib/libbfd-${version}.so' "$out/lib/"
124 ln -s '${lib.getLib libopcodes}/lib/libopcodes-${version}.so' "$out/lib/"
125 '';
126
127 # else fails with "./sanity.sh: line 36: $out/bin/size: not found"
128 doInstallCheck = stdenv.buildPlatform == stdenv.hostPlatform && stdenv.hostPlatform == stdenv.targetPlatform;
129
130 enableParallelBuilding = true;
131
132 passthru = {
133 inherit targetPrefix version;
134 };
135
136 meta = with lib; {
137 description = "Tools for manipulating binaries (linker, assembler, etc.)";
138 longDescription = ''
139 The GNU Binutils are a collection of binary tools. The main
140 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
141 They also include the BFD (Binary File Descriptor) library,
142 `gprof', `nm', `strip', etc.
143 '';
144 homepage = https://www.gnu.org/software/binutils/;
145 license = licenses.gpl3Plus;
146 maintainers = with maintainers; [ ericson2314 ];
147 platforms = platforms.unix;
148
149 /* Give binutils a lower priority than gcc-wrapper to prevent a
150 collision due to the ld/as wrappers/symlinks in the latter. */
151 priority = 10;
152 };
153}