20 enableGold ? stdenv.targetPlatform.isElf,
21 enableShared ? !stdenv.hostPlatform.isStatic,
22 # WARN: Enabling all targets increases output size to a multiple.
23 withAllTargets ? false,
26 # WARN: configure silently disables ld.gold if it's unsupported, so we need to
27 # make sure that intent matches result ourselves.
28 assert enableGold -> stdenv.targetPlatform.isElf;
31 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
37 url = "mirror://gnu/binutils/binutils-${version}.tar.bz2";
38 sha256 = "sha256-Bw7HHPB3pqWOC5WfBaCaNQFTeMLYpR6Q866r/jBZDvg=";
40 vc4-none = fetchFromGitHub {
42 repo = "binutils-vc4";
43 rev = "708acc851880dbeda1dd18aca4fd0a95b2573b36";
44 sha256 = "1kdrz6fki55lm15rwwamn74fnqpy0zlafsida2zymk76n3656c63";
48 #INFO: The targetPrefix prepended to binary names to allow multiple binuntils
49 # on the PATH to both be usable.
50 targetPrefix = lib.optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-";
54 pname = targetPrefix + "binutils";
57 # HACK: Ensure that we preserve source from bootstrap binutils to not rebuild LLVM
59 stdenv.__bootPackages.binutils-unwrapped_2_38.src or srcs.${targetPlatform.system} or srcs.normal;
61 # WARN: this package is used for bootstrapping fetchurl, and thus cannot use
62 # fetchpatch! All mutable patches (generated by GitHub or cgit) that are
63 # needed here should be included directly in Nixpkgs as files.
66 # Make binutils output deterministic by default.
69 # Breaks nm BSD flag detection
70 ./0001-Revert-libtool.m4-fix-nm-BSD-flag-detection.patch
72 # Required for newer macos versions
73 ./0001-libtool.m4-update-macos-version-detection-block.patch
75 # For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
76 # not clear why this behavior was decided upon but it has the unfortunate
77 # consequence that the linker will fail to find transitive dependencies of
78 # shared objects when cross-compiling. Consequently, we are forced to
79 # override this behavior, forcing ld to search DT_RPATH even when
81 ./always-search-rpath.patch
84 # https://sourceware.org/bugzilla/show_bug.cgi?id=28885
85 # https://sourceware.org/git/?p=binutils-gdb.git;a=patch;h=99852365513266afdd793289813e8e565186c9e6
86 # https://github.com/NixOS/nixpkgs/issues/170946
87 ./deterministic-temp-prefixes.patch
89 ++ lib.optional targetPlatform.isiOS ./support-ios.patch
90 ++ lib.optional stdenv.targetPlatform.isWindows ./windres-locate-gcc.patch
92 lib.optional stdenv.targetPlatform.isMips64n64
93 # this patch is from debian:
94 # https://sources.debian.org/data/main/b/binutils/2.38-3/debian/patches/mips64-default-n64.diff
96 if stdenv.targetPlatform.isMusl then
98 src = ./mips64-default-n64.patch;
106 ./mips64-default-n64.patch
108 # On PowerPC, when generating assembly code, GCC generates a `.machine`
109 # custom instruction which instructs the assembler to generate code for this
110 # machine. However, some GCC versions generate the wrong one, or make it
111 # too strict, which leads to some confusing "unrecognized opcode: wrtee"
112 # or "unrecognized opcode: eieio" errors.
114 # To remove when binutils 2.39 is released.
117 # https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=cebc89b9328eab994f6b0314c263f94e7949a553
118 ++ lib.optional stdenv.targetPlatform.isPower ./ppc-make-machine-less-strict.patch;
127 depsBuildBuild = [ buildPackages.stdenv.cc ];
134 ++ lib.optionals targetPlatform.isiOS [ autoreconfHook ]
135 ++ lib.optionals buildPlatform.isDarwin [
141 ++ lib.optionals targetPlatform.isVc4 [ flex ];
151 (lib.optionalString buildPlatform.isDarwin ''
152 for i in */configure.ac; do
153 pushd "$(dirname "$i")"
154 echo "Running autoreconf in $PWD"
155 # autoreconf doesn't work, don't know why
156 # autoreconf ''${autoreconfFlags:---install --force --verbose}
162 # Clear the default library search path.
163 if test "$noSysDirs" = "1"; then
164 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
167 # Use symlinks instead of hard links to save space ("strip" in the
168 # fixup phase strips each hard link separately).
169 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
170 sed -i "$i" -e 's|ln |ln -s |'
174 # As binutils takes part in the stdenv building, we don't want references
175 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
176 env.NIX_CFLAGS_COMPILE =
177 if hostPlatform.isDarwin then
178 "-Wno-string-plus-int -Wno-deprecated-declarations"
187 configurePlatforms = [
195 "--enable-64-bit-bfd"
198 "--enable-deterministic-archives"
200 "--enable-fix-loongson2f-nop"
202 # Turn on --enable-new-dtags by default to make the linker set
203 # RUNPATH instead of RPATH on binaries. This is important because
204 # RUNPATH can be overridden using LD_LIBRARY_PATH at runtime.
207 # force target prefix. Some versions of binutils will make it empty if
208 # `--host` and `--target` are too close, even if Nixpkgs thinks the
209 # platforms are different (e.g. because not all the info makes the
210 # `config`). Other versions of binutils will always prefix if `--target` is
211 # passed, even if `--host` and `--target` are the same. The easiest thing
212 # for us to do is not leave it to chance, and force the program prefix to be
213 # what we want it to be.
214 "--program-prefix=${targetPrefix}"
216 ++ lib.optionals withAllTargets [ "--enable-targets=all" ]
217 ++ lib.optionals enableGold [
237 # Remove on next bump. It's a vestige of past conditional. Stays here to avoid
241 # Break dependency on pkgsBuildBuild.gcc when building a cross-binutils
243 if stdenv.hostPlatform != stdenv.targetPlatform then
244 "bin lib ${stdenv.hostPlatform.config}"
248 # INFO: Otherwise it fails with:
249 # `./sanity.sh: line 36: $out/bin/size: not found`
250 doInstallCheck = (buildPlatform == hostPlatform) && (hostPlatform == targetPlatform);
252 enableParallelBuilding = true;
255 inherit targetPrefix;
256 hasGold = enableGold;
261 description = "Tools for manipulating binaries (linker, assembler, etc.)";
263 The GNU Binutils are a collection of binary tools. The main
264 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
265 They also include the BFD (Binary File Descriptor) library,
266 `gprof', `nm', `strip', etc.
268 homepage = "https://www.gnu.org/software/binutils/";
269 license = licenses.gpl3Plus;
270 maintainers = with maintainers; [
274 platforms = platforms.unix;
276 # INFO: Give binutils a lower priority than gcc-wrapper to prevent a
277 # collision due to the ld/as wrappers/symlinks in the latter.