1{
2 stdenv,
3 autoreconfHook,
4 autoconf269,
5 automake,
6 libtool,
7 bison,
8 buildPackages,
9 fetchFromGitHub,
10 fetchurl,
11 flex,
12 gettext,
13 lib,
14 noSysDirs,
15 perl,
16 substitute,
17 texinfo,
18 zlib,
19
20 enableGold ? stdenv.targetPlatform.isElf,
21 enableShared ? !stdenv.hostPlatform.isStatic,
22 # WARN: Enabling all targets increases output size to a multiple.
23 withAllTargets ? false,
24}:
25
26# WARN: configure silently disables ld.gold if it's unsupported, so we need to
27# make sure that intent matches result ourselves.
28assert enableGold -> stdenv.targetPlatform.isElf;
29
30let
31 inherit (stdenv) buildPlatform hostPlatform targetPlatform;
32
33 version = "2.38";
34
35 srcs = {
36 normal = fetchurl {
37 url = "mirror://gnu/binutils/binutils-${version}.tar.bz2";
38 sha256 = "sha256-Bw7HHPB3pqWOC5WfBaCaNQFTeMLYpR6Q866r/jBZDvg=";
39 };
40 vc4-none = fetchFromGitHub {
41 owner = "itszor";
42 repo = "binutils-vc4";
43 rev = "708acc851880dbeda1dd18aca4fd0a95b2573b36";
44 sha256 = "1kdrz6fki55lm15rwwamn74fnqpy0zlafsida2zymk76n3656c63";
45 };
46 };
47
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}-";
51in
52
53stdenv.mkDerivation {
54 pname = targetPrefix + "binutils";
55 inherit version;
56
57 # HACK: Ensure that we preserve source from bootstrap binutils to not rebuild LLVM
58 src =
59 stdenv.__bootPackages.binutils-unwrapped_2_38.src or srcs.${targetPlatform.system} or srcs.normal;
60
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.
64 patches =
65 [
66 # Make binutils output deterministic by default.
67 ./deterministic.patch
68
69 # Breaks nm BSD flag detection
70 ./0001-Revert-libtool.m4-fix-nm-BSD-flag-detection.patch
71
72 # Required for newer macos versions
73 ./0001-libtool.m4-update-macos-version-detection-block.patch
74
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
80 # cross-compiling.
81 ./always-search-rpath.patch
82
83 # Fixed in 2.39
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
88 ]
89 ++ lib.optional targetPlatform.isiOS ./support-ios.patch
90 ++ lib.optional stdenv.targetPlatform.isWindows ./windres-locate-gcc.patch
91 ++
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
95 (
96 if stdenv.targetPlatform.isMusl then
97 substitute {
98 src = ./mips64-default-n64.patch;
99 substitutions = [
100 "--replace"
101 "gnuabi64"
102 "muslabi64"
103 ];
104 }
105 else
106 ./mips64-default-n64.patch
107 )
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.
113 #
114 # To remove when binutils 2.39 is released.
115 #
116 # Upstream commit:
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;
119
120 outputs = [
121 "out"
122 "info"
123 "man"
124 ];
125
126 strictDeps = true;
127 depsBuildBuild = [ buildPackages.stdenv.cc ];
128 nativeBuildInputs =
129 [
130 bison
131 perl
132 texinfo
133 ]
134 ++ lib.optionals targetPlatform.isiOS [ autoreconfHook ]
135 ++ lib.optionals buildPlatform.isDarwin [
136 autoconf269
137 automake
138 gettext
139 libtool
140 ]
141 ++ lib.optionals targetPlatform.isVc4 [ flex ];
142
143 buildInputs = [
144 zlib
145 gettext
146 ];
147
148 inherit noSysDirs;
149
150 preConfigure =
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}
157 autoconf
158 popd
159 done
160 '')
161 + ''
162 # Clear the default library search path.
163 if test "$noSysDirs" = "1"; then
164 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
165 fi
166
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 |'
171 done
172 '';
173
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"
179 else
180 "-static-libgcc";
181
182 hardeningDisable = [
183 "format"
184 "pie"
185 ];
186
187 configurePlatforms = [
188 "build"
189 "host"
190 "target"
191 ];
192
193 configureFlags =
194 [
195 "--enable-64-bit-bfd"
196 "--with-system-zlib"
197
198 "--enable-deterministic-archives"
199 "--disable-werror"
200 "--enable-fix-loongson2f-nop"
201
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.
205 "--enable-new-dtags"
206
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}"
215 ]
216 ++ lib.optionals withAllTargets [ "--enable-targets=all" ]
217 ++ lib.optionals enableGold [
218 "--enable-gold"
219 "--enable-plugins"
220 ]
221 ++ (
222 if enableShared then
223 [
224 "--enable-shared"
225 "--disable-static"
226 ]
227 else
228 [
229 "--disable-shared"
230 "--enable-static"
231 ]
232 );
233
234 # Fails
235 doCheck = false;
236
237 # Remove on next bump. It's a vestige of past conditional. Stays here to avoid
238 # mass rebuild.
239 postFixup = "";
240
241 # Break dependency on pkgsBuildBuild.gcc when building a cross-binutils
242 stripDebugList =
243 if stdenv.hostPlatform != stdenv.targetPlatform then
244 "bin lib ${stdenv.hostPlatform.config}"
245 else
246 null;
247
248 # INFO: Otherwise it fails with:
249 # `./sanity.sh: line 36: $out/bin/size: not found`
250 doInstallCheck = (buildPlatform == hostPlatform) && (hostPlatform == targetPlatform);
251
252 enableParallelBuilding = true;
253
254 passthru = {
255 inherit targetPrefix;
256 hasGold = enableGold;
257 isGNU = true;
258 };
259
260 meta = with lib; {
261 description = "Tools for manipulating binaries (linker, assembler, etc.)";
262 longDescription = ''
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.
267 '';
268 homepage = "https://www.gnu.org/software/binutils/";
269 license = licenses.gpl3Plus;
270 maintainers = with maintainers; [
271 ericson2314
272 lovesegfault
273 ];
274 platforms = platforms.unix;
275
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.
278 priority = 10;
279 };
280}