2021-08-03 11:25:59 +00:00
|
|
|
let
|
2023-04-27 14:48:13 +00:00
|
|
|
withGold = platform: platform.parsed.kernel.execFormat.name == "elf" && !platform.isRiscV && !platform.isLoongArch64;
|
2021-08-03 11:25:59 +00:00
|
|
|
in
|
|
|
|
|
2022-02-10 19:50:20 +00:00
|
|
|
{ stdenv
|
|
|
|
, autoreconfHook
|
2022-02-22 03:03:19 +00:00
|
|
|
, autoconf269, automake, libtool
|
2022-02-10 19:50:20 +00:00
|
|
|
, bison
|
|
|
|
, buildPackages
|
|
|
|
, fetchFromGitHub
|
|
|
|
, fetchurl
|
2019-11-02 15:47:38 +00:00
|
|
|
, flex
|
2022-02-10 19:50:20 +00:00
|
|
|
, gettext
|
|
|
|
, lib
|
|
|
|
, noSysDirs
|
2020-06-08 21:01:59 +00:00
|
|
|
, perl
|
2022-04-06 03:07:27 +00:00
|
|
|
, substitute
|
2022-02-10 19:50:20 +00:00
|
|
|
, zlib
|
2022-02-10 20:28:28 +00:00
|
|
|
|
2022-12-25 11:50:28 +00:00
|
|
|
, enableGold ? withGold stdenv.targetPlatform
|
2023-06-22 19:53:46 +00:00
|
|
|
, enableGoldDefault ? false
|
2022-02-10 19:50:20 +00:00
|
|
|
, enableShared ? !stdenv.hostPlatform.isStatic
|
2022-02-10 20:28:28 +00:00
|
|
|
# WARN: Enabling all targets increases output size to a multiple.
|
2022-04-23 07:13:09 +00:00
|
|
|
, withAllTargets ? false
|
2014-01-07 12:57:42 +00:00
|
|
|
}:
|
2004-03-09 17:16:02 +00:00
|
|
|
|
2022-02-10 20:12:58 +00:00
|
|
|
# WARN: configure silently disables ld.gold if it's unsupported, so we need to
|
|
|
|
# make sure that intent matches result ourselves.
|
2022-12-25 11:50:28 +00:00
|
|
|
assert enableGold -> withGold stdenv.targetPlatform;
|
2023-06-22 19:53:46 +00:00
|
|
|
assert enableGoldDefault -> enableGold;
|
2021-08-03 11:25:59 +00:00
|
|
|
|
2020-06-26 20:44:45 +00:00
|
|
|
|
2017-06-04 00:28:05 +00:00
|
|
|
let
|
2022-02-10 20:10:03 +00:00
|
|
|
inherit (stdenv) buildPlatform hostPlatform targetPlatform;
|
2018-09-01 20:30:41 +00:00
|
|
|
|
2023-01-15 16:49:04 +00:00
|
|
|
version = "2.40";
|
2021-08-20 01:09:55 +00:00
|
|
|
|
2022-02-10 20:02:30 +00:00
|
|
|
srcs = {
|
2022-02-21 20:19:22 +00:00
|
|
|
normal = fetchurl {
|
2022-02-10 20:02:30 +00:00
|
|
|
url = "mirror://gnu/binutils/binutils-${version}.tar.bz2";
|
2023-01-15 16:49:04 +00:00
|
|
|
hash = "sha256-+CmOsVOks30RLpRapcsoUAQLzyaj6mW1pxXIOv4F5Io=";
|
2022-02-21 20:19:22 +00:00
|
|
|
};
|
2022-02-10 20:02:30 +00:00
|
|
|
vc4-none = fetchFromGitHub {
|
|
|
|
owner = "itszor";
|
|
|
|
repo = "binutils-vc4";
|
|
|
|
rev = "708acc851880dbeda1dd18aca4fd0a95b2573b36";
|
|
|
|
sha256 = "1kdrz6fki55lm15rwwamn74fnqpy0zlafsida2zymk76n3656c63";
|
|
|
|
};
|
|
|
|
};
|
2022-02-10 20:10:03 +00:00
|
|
|
|
2022-02-10 20:12:58 +00:00
|
|
|
#INFO: The targetPrefix prepended to binary names to allow multiple binuntils
|
2022-02-10 20:28:28 +00:00
|
|
|
# on the PATH to both be usable.
|
2022-02-10 20:10:03 +00:00
|
|
|
targetPrefix = lib.optionalString (targetPlatform != hostPlatform) "${targetPlatform.config}-";
|
2017-06-04 00:28:05 +00:00
|
|
|
in
|
2013-09-02 13:54:12 +00:00
|
|
|
|
2023-01-10 22:59:45 +00:00
|
|
|
stdenv.mkDerivation (finalAttrs: {
|
2022-02-10 20:02:30 +00:00
|
|
|
pname = targetPrefix + "binutils";
|
2020-11-14 16:35:53 +00:00
|
|
|
inherit version;
|
|
|
|
|
2022-02-21 20:19:22 +00:00
|
|
|
# HACK: Ensure that we preserve source from bootstrap binutils to not rebuild LLVM
|
|
|
|
src = stdenv.__bootPackages.binutils-unwrapped.src
|
|
|
|
or srcs.${targetPlatform.system}
|
|
|
|
or srcs.normal;
|
2008-05-29 14:16:35 +00:00
|
|
|
|
2022-02-10 20:06:40 +00:00
|
|
|
# WARN: this package is used for bootstrapping fetchurl, and thus cannot use
|
|
|
|
# fetchpatch! All mutable patches (generated by GitHub or cgit) that are
|
|
|
|
# needed here should be included directly in Nixpkgs as files.
|
2008-05-29 14:16:35 +00:00
|
|
|
patches = [
|
2014-01-07 12:57:42 +00:00
|
|
|
# Make binutils output deterministic by default.
|
2020-11-14 16:35:53 +00:00
|
|
|
./deterministic.patch
|
2014-03-08 20:57:18 +00:00
|
|
|
|
2022-09-05 18:16:31 +00:00
|
|
|
|
|
|
|
# Breaks nm BSD flag detection, heeds an upstream fix:
|
|
|
|
# https://sourceware.org/PR29547
|
|
|
|
./0001-Revert-libtool.m4-fix-the-NM-nm-over-here-B-option-w.patch
|
|
|
|
./0001-Revert-libtool.m4-fix-nm-BSD-flag-detection.patch
|
|
|
|
|
2022-02-22 03:03:19 +00:00
|
|
|
# Required for newer macos versions
|
|
|
|
./0001-libtool.m4-update-macos-version-detection-block.patch
|
|
|
|
|
2017-10-26 20:00:37 +00:00
|
|
|
# For some reason bfd ld doesn't search DT_RPATH when cross-compiling. It's
|
|
|
|
# not clear why this behavior was decided upon but it has the unfortunate
|
|
|
|
# consequence that the linker will fail to find transitive dependencies of
|
|
|
|
# shared objects when cross-compiling. Consequently, we are forced to
|
|
|
|
# override this behavior, forcing ld to search DT_RPATH even when
|
|
|
|
# cross-compiling.
|
2020-11-14 16:35:53 +00:00
|
|
|
./always-search-rpath.patch
|
2022-05-11 22:05:03 +00:00
|
|
|
|
2023-01-15 16:49:04 +00:00
|
|
|
# Avoid `lib -> out -> lib` reference. Normally `bfd-plugins` does
|
|
|
|
# not need to know binutils' BINDIR at all. It's an absolute path
|
|
|
|
# where libraries are stored.
|
|
|
|
./plugins-no-BINDIR.patch
|
2023-05-26 06:26:26 +00:00
|
|
|
|
|
|
|
# CVE-2023-1972 fix to bfd/elf.c from:
|
|
|
|
# https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=c22d38baefc5a7a1e1f5cdc9dbb556b1f0ec5c57
|
|
|
|
./CVE-2023-1972.patch
|
2022-02-10 20:28:28 +00:00
|
|
|
]
|
|
|
|
++ lib.optional targetPlatform.isiOS ./support-ios.patch
|
2022-10-26 07:29:51 +00:00
|
|
|
# Adds AVR-specific options to "size" for compatibility with Atmel's downstream distribution
|
|
|
|
# Patch from arch-community
|
|
|
|
# https://github.com/archlinux/svntogit-community/blob/c8d53dd1734df7ab15931f7fad0c9acb8386904c/trunk/avr-size.patch
|
|
|
|
++ lib.optional targetPlatform.isAvr ./avr-size.patch
|
2022-04-14 07:50:48 +00:00
|
|
|
++ lib.optional stdenv.targetPlatform.isWindows ./windres-locate-gcc.patch
|
|
|
|
++ lib.optional stdenv.targetPlatform.isMips64n64
|
|
|
|
# this patch is from debian:
|
|
|
|
# https://sources.debian.org/data/main/b/binutils/2.38-3/debian/patches/mips64-default-n64.diff
|
|
|
|
(if stdenv.targetPlatform.isMusl
|
|
|
|
then substitute { src = ./mips64-default-n64.patch; replacements = [ "--replace" "gnuabi64" "muslabi64" ]; }
|
|
|
|
else ./mips64-default-n64.patch)
|
2023-03-06 03:23:06 +00:00
|
|
|
# This patch fixes a bug in 2.40 on MinGW, which breaks DXVK when cross-building from Darwin.
|
|
|
|
# See https://sourceware.org/bugzilla/show_bug.cgi?id=30079
|
|
|
|
++ lib.optional stdenv.targetPlatform.isMinGW ./mingw-abort-fix.patch
|
2022-04-14 07:50:48 +00:00
|
|
|
;
|
2009-11-08 00:32:12 +00:00
|
|
|
|
2023-01-15 16:49:04 +00:00
|
|
|
outputs = [ "out" "info" "man" "dev" ]
|
|
|
|
# Ideally we would like to always install 'lib' into a separate
|
|
|
|
# target. Unfortunately cross-compiled binutils installs libraries
|
|
|
|
# across both `$lib/lib/` and `$out/$target/lib` with a reference
|
|
|
|
# from $out to $lib. Probably a binutils bug: all libraries should go
|
|
|
|
# to $lib as binutils does not build target libraries. Let's make our
|
|
|
|
# life slightly simpler by installing everything into $out for
|
|
|
|
# cross-binutils.
|
|
|
|
++ lib.optionals (targetPlatform == hostPlatform) [ "lib" ];
|
2016-02-28 00:10:35 +00:00
|
|
|
|
2022-05-21 16:58:22 +00:00
|
|
|
strictDeps = true;
|
2017-06-26 03:10:03 +00:00
|
|
|
depsBuildBuild = [ buildPackages.stdenv.cc ];
|
2023-01-10 22:59:45 +00:00
|
|
|
# texinfo was removed here in https://github.com/NixOS/nixpkgs/pull/210132
|
|
|
|
# to reduce rebuilds during stdenv bootstrap. Please don't add it back without
|
|
|
|
# checking the impact there first.
|
2018-04-15 23:22:08 +00:00
|
|
|
nativeBuildInputs = [
|
|
|
|
bison
|
2020-06-08 21:01:59 +00:00
|
|
|
perl
|
2022-02-10 20:28:28 +00:00
|
|
|
]
|
|
|
|
++ lib.optionals targetPlatform.isiOS [ autoreconfHook ]
|
2022-04-19 15:03:01 +00:00
|
|
|
++ lib.optionals buildPlatform.isDarwin [ autoconf269 automake gettext libtool ]
|
2022-02-10 20:28:28 +00:00
|
|
|
++ lib.optionals targetPlatform.isVc4 [ flex ]
|
|
|
|
;
|
|
|
|
|
2019-05-01 02:50:21 +00:00
|
|
|
buildInputs = [ zlib gettext ];
|
2010-12-12 23:35:16 +00:00
|
|
|
|
2004-03-29 17:23:01 +00:00
|
|
|
inherit noSysDirs;
|
2008-05-29 14:16:35 +00:00
|
|
|
|
2022-04-19 15:03:01 +00:00
|
|
|
preConfigure = (lib.optionalString buildPlatform.isDarwin ''
|
2022-02-22 03:03:19 +00:00
|
|
|
for i in */configure.ac; do
|
|
|
|
pushd "$(dirname "$i")"
|
|
|
|
echo "Running autoreconf in $PWD"
|
|
|
|
# autoreconf doesn't work, don't know why
|
|
|
|
# autoreconf ''${autoreconfFlags:---install --force --verbose}
|
|
|
|
autoconf
|
|
|
|
popd
|
|
|
|
done
|
2022-03-29 02:11:33 +00:00
|
|
|
'') + ''
|
2008-05-29 14:16:35 +00:00
|
|
|
# Clear the default library search path.
|
|
|
|
if test "$noSysDirs" = "1"; then
|
2013-09-02 13:54:12 +00:00
|
|
|
echo 'NATIVE_LIB_DIRS=' >> ld/configure.tgt
|
2008-05-29 14:16:35 +00:00
|
|
|
fi
|
2010-01-19 17:41:54 +00:00
|
|
|
|
|
|
|
# Use symlinks instead of hard links to save space ("strip" in the
|
|
|
|
# fixup phase strips each hard link separately).
|
2014-01-07 13:55:55 +00:00
|
|
|
for i in binutils/Makefile.in gas/Makefile.in ld/Makefile.in gold/Makefile.in; do
|
2012-01-19 15:02:47 +00:00
|
|
|
sed -i "$i" -e 's|ln |ln -s |'
|
2010-01-19 17:41:54 +00:00
|
|
|
done
|
2023-01-10 22:59:45 +00:00
|
|
|
|
|
|
|
# autoreconfHook is not included for all targets.
|
|
|
|
# Call it here explicitly as well.
|
|
|
|
${finalAttrs.postAutoreconf}
|
|
|
|
'';
|
|
|
|
|
|
|
|
postAutoreconf = ''
|
|
|
|
# As we regenerated configure build system tries hard to use
|
|
|
|
# texinfo to regenerate manuals. Let's avoid the dependency
|
|
|
|
# on texinfo in bootstrap path and keep manuals unmodified.
|
|
|
|
touch gas/doc/.dirstamp
|
|
|
|
touch gas/doc/asconfig.texi
|
|
|
|
touch gas/doc/as.1
|
|
|
|
touch gas/doc/as.info
|
2008-05-29 14:16:35 +00:00
|
|
|
'';
|
2009-11-08 00:32:12 +00:00
|
|
|
|
2011-10-25 18:35:17 +00:00
|
|
|
# As binutils takes part in the stdenv building, we don't want references
|
|
|
|
# to the bootstrap-tools libgcc (as uses to happen on arm/mips)
|
2023-02-19 19:23:32 +00:00
|
|
|
env.NIX_CFLAGS_COMPILE =
|
2022-02-10 20:28:28 +00:00
|
|
|
if hostPlatform.isDarwin
|
2015-06-12 00:58:26 +00:00
|
|
|
then "-Wno-string-plus-int -Wno-deprecated-declarations"
|
|
|
|
else "-static-libgcc";
|
2011-10-25 18:35:17 +00:00
|
|
|
|
2018-11-10 19:49:36 +00:00
|
|
|
hardeningDisable = [ "format" "pie" ];
|
2018-06-13 02:54:10 +00:00
|
|
|
|
2021-04-16 11:55:41 +00:00
|
|
|
configurePlatforms = [ "build" "host" "target" ];
|
2017-08-18 17:00:24 +00:00
|
|
|
|
2022-02-10 20:23:25 +00:00
|
|
|
configureFlags = [
|
2018-09-01 16:13:31 +00:00
|
|
|
"--enable-64-bit-bfd"
|
2017-10-17 20:44:24 +00:00
|
|
|
"--with-system-zlib"
|
|
|
|
|
|
|
|
"--enable-deterministic-archives"
|
|
|
|
"--disable-werror"
|
|
|
|
"--enable-fix-loongson2f-nop"
|
Enable new dtags in a way that works with binutils 2.30.
In 3027bca, binutils was upgraded from 2.28.1 to 2.30. However, in 2.30,
the ldmain.c file within binutils, which the nixpkgs new-dtags.patch
file is meant to modify, was changed in such a way that the patch no
longer works. As a result, the new dtags are not actually enabled, and
binaries are built with RPATH set instead of RUNPATH, thereby preventing
LD_LIBRARY_PATH from overriding this built-in path. This change corrects
this. The patch file is no longer necessary because binutils's ldmain.c
now sets link_info.new_dtags based on the configuration flags.
This was probably not noticed immediately because, when the derivation
is built with nix-build, the fixupPhase runs patchelf --shrink-rpath.
patchelf converts any RPATH in the binary into RUNPATH (unless
--force-rpath is specified). Of course, if the binary is built without
nix-build (such as in a nix-shell), this never occurs, and any RPATH in
the binary is left in place.
2018-07-14 15:32:10 +00:00
|
|
|
|
|
|
|
# Turn on --enable-new-dtags by default to make the linker set
|
|
|
|
# RUNPATH instead of RPATH on binaries. This is important because
|
2022-12-18 00:39:44 +00:00
|
|
|
# RUNPATH can be overridden using LD_LIBRARY_PATH at runtime.
|
Enable new dtags in a way that works with binutils 2.30.
In 3027bca, binutils was upgraded from 2.28.1 to 2.30. However, in 2.30,
the ldmain.c file within binutils, which the nixpkgs new-dtags.patch
file is meant to modify, was changed in such a way that the patch no
longer works. As a result, the new dtags are not actually enabled, and
binaries are built with RPATH set instead of RUNPATH, thereby preventing
LD_LIBRARY_PATH from overriding this built-in path. This change corrects
this. The patch file is no longer necessary because binutils's ldmain.c
now sets link_info.new_dtags based on the configuration flags.
This was probably not noticed immediately because, when the derivation
is built with nix-build, the fixupPhase runs patchelf --shrink-rpath.
patchelf converts any RPATH in the binary into RUNPATH (unless
--force-rpath is specified). Of course, if the binary is built without
nix-build (such as in a nix-shell), this never occurs, and any RPATH in
the binary is left in place.
2018-07-14 15:32:10 +00:00
|
|
|
"--enable-new-dtags"
|
2021-04-16 11:55:41 +00:00
|
|
|
|
2022-02-10 20:12:58 +00:00
|
|
|
# force target prefix. Some versions of binutils will make it empty if
|
|
|
|
# `--host` and `--target` are too close, even if Nixpkgs thinks the
|
|
|
|
# platforms are different (e.g. because not all the info makes the
|
|
|
|
# `config`). Other versions of binutils will always prefix if `--target` is
|
|
|
|
# passed, even if `--host` and `--target` are the same. The easiest thing
|
|
|
|
# for us to do is not leave it to chance, and force the program prefix to be
|
|
|
|
# what we want it to be.
|
2021-04-16 11:55:41 +00:00
|
|
|
"--program-prefix=${targetPrefix}"
|
2022-08-05 20:20:17 +00:00
|
|
|
|
|
|
|
# Unconditionally disable:
|
|
|
|
# - musl target needs porting: https://sourceware.org/PR29477
|
|
|
|
"--disable-gprofng"
|
2023-01-15 16:49:04 +00:00
|
|
|
|
|
|
|
# By default binutils searches $libdir for libraries. This brings in
|
|
|
|
# libbfd and libopcodes into a default visibility. Drop default lib
|
|
|
|
# path to force users to declare their use of these libraries.
|
|
|
|
"--with-lib-path=:"
|
2022-02-10 20:28:28 +00:00
|
|
|
]
|
|
|
|
++ lib.optionals withAllTargets [ "--enable-targets=all" ]
|
2023-06-22 19:53:46 +00:00
|
|
|
++ lib.optionals enableGold [
|
|
|
|
"--enable-gold${lib.optionalString enableGoldDefault "=default"}"
|
|
|
|
"--enable-plugins"
|
|
|
|
] ++ (if enableShared
|
2022-02-10 20:28:28 +00:00
|
|
|
then [ "--enable-shared" "--disable-static" ]
|
|
|
|
else [ "--disable-shared" "--enable-static" ])
|
|
|
|
;
|
2022-02-10 20:23:25 +00:00
|
|
|
|
|
|
|
# Fails
|
|
|
|
doCheck = false;
|
2018-04-25 03:20:18 +00:00
|
|
|
|
2022-06-02 22:32:30 +00:00
|
|
|
# Break dependency on pkgsBuildBuild.gcc when building a cross-binutils
|
|
|
|
stripDebugList = if stdenv.hostPlatform != stdenv.targetPlatform then "bin lib ${stdenv.hostPlatform.config}" else null;
|
|
|
|
|
2022-02-10 20:28:28 +00:00
|
|
|
# INFO: Otherwise it fails with:
|
|
|
|
# `./sanity.sh: line 36: $out/bin/size: not found`
|
|
|
|
doInstallCheck = (buildPlatform == hostPlatform) && (hostPlatform == targetPlatform);
|
2018-08-08 21:09:49 +00:00
|
|
|
|
2011-12-14 14:31:56 +00:00
|
|
|
enableParallelBuilding = true;
|
2013-09-02 13:54:12 +00:00
|
|
|
|
2023-01-30 21:33:26 +00:00
|
|
|
# For the same reason we don't split "lib" output we undo the $target/
|
|
|
|
# prefix for installed headers and libraries we link:
|
|
|
|
# $out/$host/$target/lib/* to $out/lib/
|
|
|
|
# $out/$host/$target/include/* to $dev/include/*
|
|
|
|
# TODO(trofi): fix installation paths upstream so we could remove this
|
|
|
|
# code and have "lib" output unconditionally.
|
2023-02-12 13:40:29 +00:00
|
|
|
postInstall = lib.optionalString (hostPlatform.config != targetPlatform.config) ''
|
2023-01-30 21:33:26 +00:00
|
|
|
ln -s $out/${hostPlatform.config}/${targetPlatform.config}/lib/* $out/lib/
|
|
|
|
ln -s $out/${hostPlatform.config}/${targetPlatform.config}/include/* $dev/include/
|
|
|
|
'';
|
|
|
|
|
2017-06-23 21:45:27 +00:00
|
|
|
passthru = {
|
2020-11-14 16:35:53 +00:00
|
|
|
inherit targetPrefix;
|
2022-02-10 19:50:33 +00:00
|
|
|
hasGold = enableGold;
|
2021-01-31 06:12:28 +00:00
|
|
|
isGNU = true;
|
2023-01-30 22:19:21 +00:00
|
|
|
# Having --enable-plugins is not enough, system has to support
|
|
|
|
# dlopen() or equivalent. See config/plugins.m4 and configure.ac
|
|
|
|
# (around PLUGINS) for cases that support or not support plugins.
|
2023-01-31 18:28:49 +00:00
|
|
|
# No platform specific filters yet here.
|
|
|
|
hasPluginAPI = enableGold;
|
2017-06-23 21:45:27 +00:00
|
|
|
};
|
|
|
|
|
2018-09-01 20:30:41 +00:00
|
|
|
meta = with lib; {
|
2014-08-24 14:21:08 +00:00
|
|
|
description = "Tools for manipulating binaries (linker, assembler, etc.)";
|
2009-02-03 19:36:56 +00:00
|
|
|
longDescription = ''
|
|
|
|
The GNU Binutils are a collection of binary tools. The main
|
|
|
|
ones are `ld' (the GNU linker) and `as' (the GNU assembler).
|
|
|
|
They also include the BFD (Binary File Descriptor) library,
|
|
|
|
`gprof', `nm', `strip', etc.
|
|
|
|
'';
|
2020-04-01 01:11:51 +00:00
|
|
|
homepage = "https://www.gnu.org/software/binutils/";
|
2016-01-25 10:16:23 +00:00
|
|
|
license = licenses.gpl3Plus;
|
2022-02-10 20:28:39 +00:00
|
|
|
maintainers = with maintainers; [ ericson2314 lovesegfault ];
|
2016-01-25 10:16:23 +00:00
|
|
|
platforms = platforms.unix;
|
2007-04-27 23:30:07 +00:00
|
|
|
|
2022-02-10 20:12:58 +00:00
|
|
|
# INFO: Give binutils a lower priority than gcc-wrapper to prevent a
|
|
|
|
# collision due to the ld/as wrappers/symlinks in the latter.
|
2017-04-29 03:02:37 +00:00
|
|
|
priority = 10;
|
2007-04-27 23:30:07 +00:00
|
|
|
};
|
2023-01-10 22:59:45 +00:00
|
|
|
})
|