1let
2 withGold = platform: platform.isElf && !platform.isRiscV && !platform.isLoongArch64;
3in
4
5{
6 stdenv,
7 autoconf269,
8 automake,
9 libtool,
10 bison,
11 buildPackages,
12 fetchurl,
13 gettext,
14 lib,
15 noSysDirs,
16 perl,
17 runCommand,
18 zlib,
19
20 enableGold ? withGold stdenv.targetPlatform,
21 enableGoldDefault ? false,
22 enableShared ? !stdenv.hostPlatform.isStatic,
23 # WARN: Enabling all targets increases output size to a multiple.
24 withAllTargets ? false,
25}:
26
27# WARN: configure silently disables ld.gold if it's unsupported, so we need to
28# make sure that intent matches result ourselves.
29assert enableGold -> withGold stdenv.targetPlatform;
30assert enableGoldDefault -> enableGold;
31
32let
33 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
34
35 version = "2.44";
36
37 #INFO: The targetPrefix prepended to binary names to allow multiple binuntils
38 # on the PATH to both be usable.
39 targetPrefix = lib.optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-";
40in
41
42stdenv.mkDerivation (finalAttrs: {
43 pname = targetPrefix + "binutils";
44 inherit version;
45
46 src = fetchurl {
47 url = "mirror://gnu/binutils/binutils-with-gold-${version}.tar.bz2";
48 hash = "sha256-NHM+pJXMDlDnDbTliQ3sKKxB8OFMShZeac8n+5moxMg=";
49 };
50
51 # WARN: this package is used for bootstrapping fetchurl, and thus cannot use
52 # fetchpatch! All mutable patches (generated by GitHub or cgit) that are
53 # needed here should be included directly in Nixpkgs as files.
54 patches = [
55 # Make binutils output deterministic by default.
56 ./deterministic.patch
57
58 # Breaks nm BSD flag detection, heeds an upstream fix:
59 # https://sourceware.org/PR29547
60 ./0001-Revert-libtool.m4-fix-the-NM-nm-over-here-B-option-w.patch
61 ./0001-Revert-libtool.m4-fix-nm-BSD-flag-detection.patch
62
63 # For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
64 # not clear why this behavior was decided upon but it has the unfortunate
65 # consequence that the linker will fail to find transitive dependencies of
66 # shared objects when cross-compiling. Consequently, we are forced to
67 # override this behavior, forcing ld to search DT_RPATH even when
68 # cross-compiling.
69 ./always-search-rpath.patch
70
71 # Avoid `lib -> out -> lib` reference. Normally `bfd-plugins` does
72 # not need to know binutils' BINDIR at all. It's an absolute path
73 # where libraries are stored.
74 ./plugins-no-BINDIR.patch
75
76 # ld64 needs `-undefined dynamic_lookup` to link `libctf-nobfd.dylib`, but the Darwin
77 # version detection in `libtool.m4` fails to detect the Darwin version correctly.
78 ./0001-libtool.m4-update-macos-version-detection-block.patch
79
80 # Adds AVR-specific options to "size" for compatibility with Atmel's downstream distribution
81 # Patch from arch-community
82 # https://github.com/archlinux/svntogit-community/blob/c8d53dd1734df7ab15931f7fad0c9acb8386904c/trunk/avr-size.patch
83 ./avr-size.patch
84
85 ./windres-locate-gcc.patch
86 ];
87
88 outputs =
89 [
90 "out"
91 "info"
92 "man"
93 "dev"
94 ]
95 # Ideally we would like to always install 'lib' into a separate
96 # target. Unfortunately cross-compiled binutils installs libraries
97 # across both `$lib/lib/` and `$out/$target/lib` with a reference
98 # from $out to $lib. Probably a binutils bug: all libraries should go
99 # to $lib as binutils does not build target libraries. Let's make our
100 # life slightly simpler by installing everything into $out for
101 # cross-binutils.
102 ++ lib.optionals (targetPlatform == hostPlatform) [ "lib" ];
103
104 strictDeps = true;
105 depsBuildBuild = [ buildPackages.stdenv.cc ];
106 # texinfo was removed here in https://github.com/NixOS/nixpkgs/pull/210132
107 # to reduce rebuilds during stdenv bootstrap. Please don't add it back without
108 # checking the impact there first.
109 nativeBuildInputs =
110 [
111 bison
112 perl
113 ]
114 ++ lib.optionals buildPlatform.isDarwin [
115 autoconf269
116 automake
117 gettext
118 libtool
119 ];
120
121 buildInputs = [
122 zlib
123 gettext
124 ];
125
126 inherit noSysDirs;
127
128 preConfigure =
129 (lib.optionalString buildPlatform.isDarwin ''
130 for i in */configure.ac; do
131 pushd "$(dirname "$i")"
132 echo "Running autoreconf in $PWD"
133 # autoreconf doesn't work, don't know why
134 # autoreconf ''${autoreconfFlags:---install --force --verbose}
135 autoconf
136 popd
137 done
138 '')
139 + ''
140 # Clear the default library search path.
141 if test "$noSysDirs" = "1"; then
142 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
143 fi
144
145 # Use symlinks instead of hard links to save space ("strip" in the
146 # fixup phase strips each hard link separately).
147 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
148 sed -i "$i" -e 's|ln |ln -s |'
149 done
150
151 # autoreconfHook is not included for all targets.
152 # Call it here explicitly as well.
153 ${finalAttrs.postAutoreconf}
154 '';
155
156 postAutoreconf = ''
157 # As we regenerated configure build system tries hard to use
158 # texinfo to regenerate manuals. Let's avoid the dependency
159 # on texinfo in bootstrap path and keep manuals unmodified.
160 touch gas/doc/.dirstamp
161 touch gas/doc/asconfig.texi
162 touch gas/doc/as.1
163 touch gas/doc/as.info
164 '';
165
166 # As binutils takes part in the stdenv building, we don't want references
167 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
168 #
169 # for FreeBSD it's more complicated. With -static-libgcc, configure
170 # thinks that limits.h does not exist and the build fails for not finding
171 # LONG_MIN. The configure test itself succeeds but the compiler issues a
172 # warning about -static-libgcc being unused.
173 env.NIX_CFLAGS_COMPILE =
174 if (hostPlatform.isDarwin || hostPlatform.isFreeBSD) then
175 "-Wno-string-plus-int -Wno-deprecated-declarations"
176 else
177 "-static-libgcc";
178
179 hardeningDisable = [
180 "format"
181 "pie"
182 ];
183
184 configurePlatforms = [
185 "build"
186 "host"
187 "target"
188 ];
189
190 configureFlags =
191 [
192 "--enable-64-bit-bfd"
193 "--with-system-zlib"
194
195 "--enable-deterministic-archives"
196 "--disable-werror"
197 "--enable-fix-loongson2f-nop"
198
199 # Turn on --enable-new-dtags by default to make the linker set
200 # RUNPATH instead of RPATH on binaries. This is important because
201 # RUNPATH can be overridden using LD_LIBRARY_PATH at runtime.
202 "--enable-new-dtags"
203
204 # force target prefix. Some versions of binutils will make it empty if
205 # `--host` and `--target` are too close, even if Nixpkgs thinks the
206 # platforms are different (e.g. because not all the info makes the
207 # `config`). Other versions of binutils will always prefix if `--target` is
208 # passed, even if `--host` and `--target` are the same. The easiest thing
209 # for us to do is not leave it to chance, and force the program prefix to be
210 # what we want it to be.
211 "--program-prefix=${targetPrefix}"
212
213 # Unconditionally disable:
214 # - musl target needs porting: https://sourceware.org/PR29477
215 "--disable-gprofng"
216
217 # By default binutils searches $libdir for libraries. This brings in
218 # libbfd and libopcodes into a default visibility. Drop default lib
219 # path to force users to declare their use of these libraries.
220 "--with-lib-path=:"
221 ]
222 ++ lib.optionals withAllTargets [ "--enable-targets=all" ]
223 ++ lib.optionals enableGold [
224 "--enable-gold${lib.optionalString enableGoldDefault "=default"}"
225 "--enable-plugins"
226 ]
227 ++ (
228 if enableShared then
229 [
230 "--enable-shared"
231 "--disable-static"
232 ]
233 else
234 [
235 "--disable-shared"
236 "--enable-static"
237 ]
238 )
239 ++ (lib.optionals (stdenv.cc.bintools.isLLVM && lib.versionAtLeast stdenv.cc.bintools.version "17")
240 [
241 # lld17+ passes `--no-undefined-version` by default and makes this a hard
242 # error; libctf.ver version script references symbols that aren't present.
243 #
244 # This is fixed upstream and can be removed with the future release of 2.43.
245 # For now we allow this with `--undefined-version`:
246 "LDFLAGS=-Wl,--undefined-version"
247 ]
248 );
249
250 # Fails
251 doCheck = false;
252
253 # Break dependency on pkgsBuildBuild.gcc when building a cross-binutils
254 stripDebugList =
255 if stdenv.hostPlatform != stdenv.targetPlatform then
256 "bin lib ${stdenv.hostPlatform.config}"
257 else
258 null;
259
260 # INFO: Otherwise it fails with:
261 # `./sanity.sh: line 36: $out/bin/size: not found`
262 doInstallCheck = (buildPlatform == hostPlatform) && (hostPlatform == targetPlatform);
263
264 enableParallelBuilding = true;
265
266 # For the same reason we don't split "lib" output we undo the $target/
267 # prefix for installed headers and libraries we link:
268 # $out/$host/$target/lib/* to $out/lib/
269 # $out/$host/$target/include/* to $dev/include/*
270 # TODO(trofi): fix installation paths upstream so we could remove this
271 # code and have "lib" output unconditionally.
272 postInstall = lib.optionalString (hostPlatform.config != targetPlatform.config) ''
273 ln -s $out/${hostPlatform.config}/${targetPlatform.config}/lib/* $out/lib/
274 ln -s $out/${hostPlatform.config}/${targetPlatform.config}/include/* $dev/include/
275 '';
276
277 passthru = {
278 inherit targetPrefix;
279 hasGold = enableGold;
280 isGNU = true;
281
282 # The plugin API is not a function of any targets. Expose it separately,
283 # currently only used by LLVM for enabling BFD to do LTO with LLVM bitcode.
284 # (Tar will exit with an error if there are no matches).
285 plugin-api-header = runCommand "libbfd-plugin-api-header" { } ''
286 mkdir -p $out
287 tar --directory=$out \
288 --extract \
289 --file=${finalAttrs.src} \
290 --strip-components=1 \
291 --wildcards '*'/include/plugin-api.h
292 '';
293 };
294
295 meta = with lib; {
296 description = "Tools for manipulating binaries (linker, assembler, etc.)";
297 longDescription = ''
298 The GNU Binutils are a collection of binary tools. The main
299 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
300 They also include the BFD (Binary File Descriptor) library,
301 `gprof', `nm', `strip', etc.
302 '';
303 homepage = "https://www.gnu.org/software/binutils/";
304 license = licenses.gpl3Plus;
305 maintainers = with maintainers; [
306 ericson2314
307 lovesegfault
308 ];
309 platforms = platforms.unix;
310
311 # INFO: Give binutils a lower priority than gcc-wrapper to prevent a
312 # collision due to the ld/as wrappers/symlinks in the latter.
313 priority = 10;
314 };
315})