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 # Make binutils output deterministic by default.
66 ./deterministic.patch
67
68 # Breaks nm BSD flag detection
69 ./0001-Revert-libtool.m4-fix-nm-BSD-flag-detection.patch
70
71 # Required for newer macos versions
72 ./0001-libtool.m4-update-macos-version-detection-block.patch
73
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
79 # cross-compiling.
80 ./always-search-rpath.patch
81
82 # Fixed in 2.39
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
87 ]
88 ++ lib.optional targetPlatform.isiOS ./support-ios.patch
89 ++ lib.optional stdenv.targetPlatform.isWindows ./windres-locate-gcc.patch
90 ++
91 lib.optional stdenv.targetPlatform.isMips64n64
92 # this patch is from debian:
93 # https://sources.debian.org/data/main/b/binutils/2.38-3/debian/patches/mips64-default-n64.diff
94 (
95 if stdenv.targetPlatform.isMusl then
96 substitute {
97 src = ./mips64-default-n64.patch;
98 substitutions = [
99 "--replace"
100 "gnuabi64"
101 "muslabi64"
102 ];
103 }
104 else
105 ./mips64-default-n64.patch
106 )
107 # On PowerPC, when generating assembly code, GCC generates a `.machine`
108 # custom instruction which instructs the assembler to generate code for this
109 # machine. However, some GCC versions generate the wrong one, or make it
110 # too strict, which leads to some confusing "unrecognized opcode: wrtee"
111 # or "unrecognized opcode: eieio" errors.
112 #
113 # To remove when binutils 2.39 is released.
114 #
115 # Upstream commit:
116 # https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=cebc89b9328eab994f6b0314c263f94e7949a553
117 ++ lib.optional stdenv.targetPlatform.isPower ./ppc-make-machine-less-strict.patch;
118
119 outputs = [
120 "out"
121 "info"
122 "man"
123 ];
124
125 strictDeps = true;
126 depsBuildBuild = [ buildPackages.stdenv.cc ];
127 nativeBuildInputs = [
128 bison
129 perl
130 texinfo
131 ]
132 ++ lib.optionals targetPlatform.isiOS [ autoreconfHook ]
133 ++ lib.optionals buildPlatform.isDarwin [
134 autoconf269
135 automake
136 gettext
137 libtool
138 ]
139 ++ lib.optionals targetPlatform.isVc4 [ flex ];
140
141 buildInputs = [
142 zlib
143 gettext
144 ];
145
146 inherit noSysDirs;
147
148 preConfigure =
149 (lib.optionalString buildPlatform.isDarwin ''
150 for i in */configure.ac; do
151 pushd "$(dirname "$i")"
152 echo "Running autoreconf in $PWD"
153 # autoreconf doesn't work, don't know why
154 # autoreconf ''${autoreconfFlags:---install --force --verbose}
155 autoconf
156 popd
157 done
158 '')
159 + ''
160 # Clear the default library search path.
161 if test "$noSysDirs" = "1"; then
162 echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
163 fi
164
165 # Use symlinks instead of hard links to save space ("strip" in the
166 # fixup phase strips each hard link separately).
167 for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
168 sed -i "$i" -e 's|ln |ln -s |'
169 done
170 '';
171
172 # As binutils takes part in the stdenv building, we don't want references
173 # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
174 env.NIX_CFLAGS_COMPILE =
175 if hostPlatform.isDarwin then
176 "-Wno-string-plus-int -Wno-deprecated-declarations"
177 else
178 "-static-libgcc";
179
180 hardeningDisable = [
181 "format"
182 "pie"
183 ];
184
185 configurePlatforms = [
186 "build"
187 "host"
188 "target"
189 ];
190
191 configureFlags = [
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 ++ lib.optionals withAllTargets [ "--enable-targets=all" ]
214 ++ lib.optionals enableGold [
215 "--enable-gold"
216 "--enable-plugins"
217 ]
218 ++ (
219 if enableShared then
220 [
221 "--enable-shared"
222 "--disable-static"
223 ]
224 else
225 [
226 "--disable-shared"
227 "--enable-static"
228 ]
229 );
230
231 # Fails
232 doCheck = false;
233
234 # Remove on next bump. It's a vestige of past conditional. Stays here to avoid
235 # mass rebuild.
236 postFixup = "";
237
238 # Break dependency on pkgsBuildBuild.gcc when building a cross-binutils
239 stripDebugList =
240 if stdenv.hostPlatform != stdenv.targetPlatform then
241 "bin lib ${stdenv.hostPlatform.config}"
242 else
243 null;
244
245 # INFO: Otherwise it fails with:
246 # `./sanity.sh: line 36: $out/bin/size: not found`
247 doInstallCheck = (buildPlatform == hostPlatform) && (hostPlatform == targetPlatform);
248
249 enableParallelBuilding = true;
250
251 passthru = {
252 inherit targetPrefix;
253 hasGold = enableGold;
254 isGNU = true;
255 };
256
257 meta = with lib; {
258 description = "Tools for manipulating binaries (linker, assembler, etc.)";
259 longDescription = ''
260 The GNU Binutils are a collection of binary tools. The main
261 ones are `ld' (the GNU linker) and `as' (the GNU assembler).
262 They also include the BFD (Binary File Descriptor) library,
263 `gprof', `nm', `strip', etc.
264 '';
265 homepage = "https://www.gnu.org/software/binutils/";
266 license = licenses.gpl3Plus;
267 maintainers = with maintainers; [
268 ericson2314
269 lovesegfault
270 ];
271 platforms = platforms.unix;
272
273 # INFO: Give binutils a lower priority than gcc-wrapper to prevent a
274 # collision due to the ld/as wrappers/symlinks in the latter.
275 priority = 10;
276 };
277}