Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1let
2 execFormatIsELF = platform: platform.parsed.kernel.execFormat.name == "elf";
3in
4
5{ stdenv, lib, buildPackages
6, fetchFromGitHub, fetchurl, zlib, autoreconfHook, gettext
7# Enabling all targets increases output size to a multiple.
8, withAllTargets ? false, libbfd, libopcodes
9, enableShared ? !stdenv.hostPlatform.isStatic
10, noSysDirs
11, gold ? execFormatIsELF stdenv.targetPlatform
12, bison ? null
13, flex
14, texinfo
15, perl
16}:
17
18# configure silently disables ld.gold if it's unsupported,
19# so we need to make sure that intent matches result ourselves.
20assert gold -> execFormatIsELF stdenv.targetPlatform;
21
22# Note: this package is used for bootstrapping fetchurl, and thus
23# cannot use fetchpatch! All mutable patches (generated by GitHub or
24# cgit) that are needed here should be included directly in Nixpkgs as
25# files.
26
27let
28 reuseLibs = enableShared && withAllTargets;
29
30 version = "2.35.2";
31 basename = "binutils";
32 # The targetPrefix prepended to binary names to allow multiple binuntils on the
33 # PATH to both be usable.
34 targetPrefix = lib.optionalString (stdenv.targetPlatform != stdenv.hostPlatform)
35 "${stdenv.targetPlatform.config}-";
36 vc4-binutils-src = fetchFromGitHub {
37 owner = "itszor";
38 repo = "binutils-vc4";
39 rev = "708acc851880dbeda1dd18aca4fd0a95b2573b36";
40 sha256 = "1kdrz6fki55lm15rwwamn74fnqpy0zlafsida2zymk76n3656c63";
41 };
42 # HACK to ensure that we preserve source from bootstrap binutils to not rebuild LLVM
43 normal-src = stdenv.__bootPackages.binutils-unwrapped.src or (fetchurl {
44 url = "mirror://gnu/binutils/${basename}-${version}.tar.bz2";
45 sha256 = "sha256-z6dkTb7PRZHhNutAfBwdoWV4vSsD8MLorNzroZS7nWE=";
46 });
47in
48
49stdenv.mkDerivation {
50 pname = targetPrefix + basename;
51 inherit version;
52
53 src = if stdenv.targetPlatform.isVc4 then vc4-binutils-src else normal-src;
54
55 patches = [
56 # Make binutils output deterministic by default.
57 ./deterministic.patch
58
59 # Help bfd choose between elf32-littlearm, elf32-littlearm-symbian, and
60 # elf32-littlearm-vxworks in favor of the first.
61 # https://github.com/NixOS/nixpkgs/pull/30484#issuecomment-345472766
62 ./disambiguate-arm-targets.patch
63
64 # For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
65 # not clear why this behavior was decided upon but it has the unfortunate
66 # consequence that the linker will fail to find transitive dependencies of
67 # shared objects when cross-compiling. Consequently, we are forced to
68 # override this behavior, forcing ld to search DT_RPATH even when
69 # cross-compiling.
70 ./always-search-rpath.patch
71
72 # Fix quadratic slowdown in `strip` performance.
73 # See #129467 and https://sourceware.org/bugzilla/show_bug.cgi?id=28058
74 # Remove when we're on binutils > 2.36.1.
75 # The patch is downloaded from
76 # https://sourceware.org/git/?p=binutils-gdb.git;a=blobdiff_plain;f=bfd/elf.c;h=af62aadc3d446cd5b1f0201b207c90c22e7809b1;hp=36733e080dd9d9be28b576b246aaf5bd8c8569c7;hb=84fd26d8209e99fc3a432dd0b09b6c053de1ce65;hpb=abe2a28aaa7a2bfd0f3061c72a98eb898976b721
77 # which is the 2.36 backport (using `TRUE` instead of `true` of binutils master commit:
78 # https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=956ea65cd707707c0f725930214cbc781367a831
79 ./bfd-elf-Dont-read-non-existing-secondary-relocs.patch
80
81 # Fix building plv8’s v8.
82 # https://github.com/NixOS/nixpkgs/issues/134190
83 # Obtained from: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=586e30940e640f67bd55bd72e1d1355a4faf8079
84 ./gold-Update-GNU_PROPERTY_X86_XXX-macros.patch
85
86 ./CVE-2020-35448.patch
87 ./CVE-2021-3487.patch
88 ] ++ lib.optional stdenv.targetPlatform.isiOS ./support-ios.patch
89 ++ # This patch was suggested by Nick Clifton to fix
90 # https://sourceware.org/bugzilla/show_bug.cgi?id=16177
91 # It can be removed when that 7-year-old bug is closed.
92 # This binutils bug causes GHC to emit broken binaries on armv7, and
93 # indeed GHC will refuse to compile with a binutils suffering from it. See
94 # this comment for more information:
95 # https://gitlab.haskell.org/ghc/ghc/issues/4210#note_78333
96 lib.optional (stdenv.targetPlatform.isAarch32 && stdenv.hostPlatform.system != stdenv.targetPlatform.system) ./R_ARM_COPY.patch;
97
98 outputs = [ "out" "info" "man" ];
99
100 depsBuildBuild = [ buildPackages.stdenv.cc ];
101 nativeBuildInputs = [
102 bison
103 perl
104 texinfo
105 ] ++ (lib.optionals stdenv.targetPlatform.isiOS [
106 autoreconfHook
107 ]) ++ lib.optionals stdenv.targetPlatform.isVc4 [ flex ];
108 buildInputs = [ zlib gettext ];
109
110 inherit noSysDirs;
111
112 preConfigure = ''
113 # Clear the default library search path.
114 if test "$noSysDirs" = "1"; then
115 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
116 fi
117
118 # Use symlinks instead of hard links to save space ("strip" in the
119 # fixup phase strips each hard link separately).
120 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
121 sed -i "$i" -e 's|ln |ln -s |'
122 done
123 '';
124
125 # As binutils takes part in the stdenv building, we don't want references
126 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
127 NIX_CFLAGS_COMPILE = if stdenv.hostPlatform.isDarwin
128 then "-Wno-string-plus-int -Wno-deprecated-declarations"
129 else "-static-libgcc";
130
131 hardeningDisable = [ "format" "pie" ];
132
133 configurePlatforms = [ "build" "host" "target" ];
134
135 configureFlags =
136 (if enableShared then [ "--enable-shared" "--disable-static" ]
137 else [ "--disable-shared" "--enable-static" ])
138 ++ lib.optional withAllTargets "--enable-targets=all"
139 ++ [
140 "--enable-64-bit-bfd"
141 "--with-system-zlib"
142
143 "--enable-deterministic-archives"
144 "--disable-werror"
145 "--enable-fix-loongson2f-nop"
146
147 # Turn on --enable-new-dtags by default to make the linker set
148 # RUNPATH instead of RPATH on binaries. This is important because
149 # RUNPATH can be overriden using LD_LIBRARY_PATH at runtime.
150 "--enable-new-dtags"
151
152 # force target prefix. Some versions of binutils will make it empty
153 # if `--host` and `--target` are too close, even if Nixpkgs thinks
154 # the platforms are different (e.g. because not all the info makes
155 # the `config`). Other versions of binutils will always prefix if
156 # `--target` is passed, even if `--host` and `--target` are the same.
157 # The easiest thing for us to do is not leave it to chance, and force
158 # the program prefix to be what we want it to be.
159 "--program-prefix=${targetPrefix}"
160 ] ++ lib.optionals gold [
161 "--enable-gold"
162 "--enable-plugins"
163 ];
164
165 doCheck = false; # fails
166
167 postFixup = lib.optionalString reuseLibs ''
168 rm "$out"/lib/lib{bfd,opcodes}-${version}.so
169 ln -s '${lib.getLib libbfd}/lib/libbfd-${version}.so' "$out/lib/"
170 ln -s '${lib.getLib libopcodes}/lib/libopcodes-${version}.so' "$out/lib/"
171 '';
172
173 # else fails with "./sanity.sh: line 36: $out/bin/size: not found"
174 doInstallCheck = stdenv.buildPlatform == stdenv.hostPlatform && stdenv.hostPlatform == stdenv.targetPlatform;
175
176 enableParallelBuilding = true;
177
178 passthru = {
179 inherit targetPrefix;
180 hasGold = gold;
181 isGNU = true;
182 };
183
184 meta = with lib; {
185 description = "Tools for manipulating binaries (linker, assembler, etc.)";
186 longDescription = ''
187 The GNU Binutils are a collection of binary tools. The main
188 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
189 They also include the BFD (Binary File Descriptor) library,
190 `gprof', `nm', `strip', etc.
191 '';
192 homepage = "https://www.gnu.org/software/binutils/";
193 license = licenses.gpl3Plus;
194 maintainers = with maintainers; [ ericson2314 ];
195 platforms = platforms.unix;
196
197 /* Give binutils a lower priority than gcc-wrapper to prevent a
198 collision due to the ld/as wrappers/symlinks in the latter. */
199 priority = 10;
200 };
201}