3 , autoconf269, automake, libtool
17 , enableGold ? stdenv.targetPlatform.isElf
18 , enableShared ? !stdenv.hostPlatform.isStatic
19 # WARN: Enabling all targets increases output size to a multiple.
20 , withAllTargets ? false
23 # WARN: configure silently disables ld.gold if it's unsupported, so we need to
24 # make sure that intent matches result ourselves.
25 assert enableGold -> stdenv.targetPlatform.isElf;
29 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
35 url = "mirror://gnu/binutils/binutils-${version}.tar.bz2";
36 sha256 = "sha256-Bw7HHPB3pqWOC5WfBaCaNQFTeMLYpR6Q866r/jBZDvg=";
38 vc4-none = fetchFromGitHub {
40 repo = "binutils-vc4";
41 rev = "708acc851880dbeda1dd18aca4fd0a95b2573b36";
42 sha256 = "1kdrz6fki55lm15rwwamn74fnqpy0zlafsida2zymk76n3656c63";
46 #INFO: The targetPrefix prepended to binary names to allow multiple binuntils
47 # on the PATH to both be usable.
48 targetPrefix = lib.optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-";
52 pname = targetPrefix + "binutils";
55 # HACK: Ensure that we preserve source from bootstrap binutils to not rebuild LLVM
56 src = stdenv.__bootPackages.binutils-unwrapped_2_38.src
57 or srcs.${targetPlatform.system}
60 # WARN: this package is used for bootstrapping fetchurl, and thus cannot use
61 # fetchpatch! All mutable patches (generated by GitHub or cgit) that are
62 # needed here should be included directly in Nixpkgs as files.
64 # Make binutils output deterministic by default.
68 # Breaks nm BSD flag detection
69 ./0001-Revert-libtool.m4-fix-nm-BSD-flag-detection.patch
71 # Required for newer macos versions
72 ./0001-libtool.m4-update-macos-version-detection-block.patch
74 # For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
75 # not clear why this behavior was decided upon but it has the unfortunate
76 # consequence that the linker will fail to find transitive dependencies of
77 # shared objects when cross-compiling. Consequently, we are forced to
78 # override this behavior, forcing ld to search DT_RPATH even when
80 ./always-search-rpath.patch
83 # https://sourceware.org/bugzilla/show_bug.cgi?id=28885
84 # https://sourceware.org/git/?p=binutils-gdb.git;a=patch;h=99852365513266afdd793289813e8e565186c9e6
85 # https://github.com/NixOS/nixpkgs/issues/170946
86 ./deterministic-temp-prefixes.patch
88 ++ lib.optional targetPlatform.isiOS ./support-ios.patch
89 ++ lib.optional stdenv.targetPlatform.isWindows ./windres-locate-gcc.patch
90 ++ lib.optional stdenv.targetPlatform.isMips64n64
91 # this patch is from debian:
92 # https://sources.debian.org/data/main/b/binutils/2.38-3/debian/patches/mips64-default-n64.diff
93 (if stdenv.targetPlatform.isMusl
94 then substitute { src = ./mips64-default-n64.patch; substitutions = [ "--replace" "gnuabi64" "muslabi64" ]; }
95 else ./mips64-default-n64.patch)
96 # On PowerPC, when generating assembly code, GCC generates a `.machine`
97 # custom instruction which instructs the assembler to generate code for this
98 # machine. However, some GCC versions generate the wrong one, or make it
99 # too strict, which leads to some confusing "unrecognized opcode: wrtee"
100 # or "unrecognized opcode: eieio" errors.
102 # To remove when binutils 2.39 is released.
105 # https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=cebc89b9328eab994f6b0314c263f94e7949a553
106 ++ lib.optional stdenv.targetPlatform.isPower ./ppc-make-machine-less-strict.patch
109 outputs = [ "out" "info" "man" ];
112 depsBuildBuild = [ buildPackages.stdenv.cc ];
113 nativeBuildInputs = [
118 ++ lib.optionals targetPlatform.isiOS [ autoreconfHook ]
119 ++ lib.optionals buildPlatform.isDarwin [ autoconf269 automake gettext libtool ]
120 ++ lib.optionals targetPlatform.isVc4 [ flex ]
123 buildInputs = [ zlib gettext ];
127 preConfigure = (lib.optionalString buildPlatform.isDarwin ''
128 for i in */configure.ac; do
129 pushd "$(dirname "$i")"
130 echo "Running autoreconf in $PWD"
131 # autoreconf doesn't work, don't know why
132 # autoreconf ''${autoreconfFlags:---install --force --verbose}
137 # Clear the default library search path.
138 if test "$noSysDirs" = "1"; then
139 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
142 # Use symlinks instead of hard links to save space ("strip" in the
143 # fixup phase strips each hard link separately).
144 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
145 sed -i "$i" -e 's|ln |ln -s |'
149 # As binutils takes part in the stdenv building, we don't want references
150 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
151 env.NIX_CFLAGS_COMPILE =
152 if hostPlatform.isDarwin
153 then "-Wno-string-plus-int -Wno-deprecated-declarations"
154 else "-static-libgcc";
156 hardeningDisable = [ "format" "pie" ];
158 configurePlatforms = [ "build" "host" "target" ];
161 "--enable-64-bit-bfd"
164 "--enable-deterministic-archives"
166 "--enable-fix-loongson2f-nop"
168 # Turn on --enable-new-dtags by default to make the linker set
169 # RUNPATH instead of RPATH on binaries. This is important because
170 # RUNPATH can be overridden using LD_LIBRARY_PATH at runtime.
173 # force target prefix. Some versions of binutils will make it empty if
174 # `--host` and `--target` are too close, even if Nixpkgs thinks the
175 # platforms are different (e.g. because not all the info makes the
176 # `config`). Other versions of binutils will always prefix if `--target` is
177 # passed, even if `--host` and `--target` are the same. The easiest thing
178 # for us to do is not leave it to chance, and force the program prefix to be
179 # what we want it to be.
180 "--program-prefix=${targetPrefix}"
182 ++ lib.optionals withAllTargets [ "--enable-targets=all" ]
183 ++ lib.optionals enableGold [ "--enable-gold" "--enable-plugins" ]
185 then [ "--enable-shared" "--disable-static" ]
186 else [ "--disable-shared" "--enable-static" ])
192 # Remove on next bump. It's a vestige of past conditional. Stays here to avoid
196 # Break dependency on pkgsBuildBuild.gcc when building a cross-binutils
197 stripDebugList = if stdenv.hostPlatform != stdenv.targetPlatform then "bin lib ${stdenv.hostPlatform.config}" else null;
199 # INFO: Otherwise it fails with:
200 # `./sanity.sh: line 36: $out/bin/size: not found`
201 doInstallCheck = (buildPlatform == hostPlatform) && (hostPlatform == targetPlatform);
203 enableParallelBuilding = true;
206 inherit targetPrefix;
207 hasGold = enableGold;
212 description = "Tools for manipulating binaries (linker, assembler, etc.)";
214 The GNU Binutils are a collection of binary tools. The main
215 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
216 They also include the BFD (Binary File Descriptor) library,
217 `gprof', `nm', `strip', etc.
219 homepage = "https://www.gnu.org/software/binutils/";
220 license = licenses.gpl3Plus;
221 maintainers = with maintainers; [ ericson2314 lovesegfault ];
222 platforms = platforms.unix;
224 # INFO: Give binutils a lower priority than gcc-wrapper to prevent a
225 # collision due to the ld/as wrappers/symlinks in the latter.