[clang] [Clang][Driver] Override Generic_ELF::buildLinker() to avoid calling gcc to link (PR #149681)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 19 15:30:12 PDT 2025
https://github.com/mintsuki created https://github.com/llvm/llvm-project/pull/149681
This change primarily makes it so that when targeting freestanding/unknown
OSes, the driver will not use `gcc` to link, but rather will link using
the chosen linker (`-fuse-ld=...`) directly.
If Clang is to be a cross compiler, there is no reason to assume that the
host's gcc is in any way capable of handling the linkage of programs for
targets that do not match the host's triple, especially for freestanding
targets.
>From 7401452c2cf297a03b805c9c5fdd9712d0f2fa4d Mon Sep 17 00:00:00 2001
From: Mintsuki <mintsuki at protonmail.com>
Date: Sun, 20 Jul 2025 00:21:08 +0200
Subject: [PATCH] [Clang][Driver] Override Generic_ELF::buildLinker() to avoid
calling gcc to link
This change primarily makes it so that when targeting freestanding/unknown
OSes, the driver will not use `gcc` to link, but rather will link using
the chosen linker (`-fuse-ld=...`) directly.
If Clang is to be a cross compiler, there is no reason to assume that the
host's gcc is in any way capable of handling the linkage of programs for
targets that do not match the host's triple, especially for freestanding
targets.
---
clang/lib/Driver/ToolChains/Gnu.cpp | 2 ++
clang/lib/Driver/ToolChains/Gnu.h | 3 +++
2 files changed, 5 insertions(+)
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index f5e2655857432..4beb67f888a7c 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -3375,3 +3375,5 @@ void Generic_ELF::addClangTargetOptions(const ArgList &DriverArgs,
options::OPT_fno_use_init_array, true))
CC1Args.push_back("-fno-use-init-array");
}
+
+Tool *Generic_ELF::buildLinker() const { return new tools::gnutools::Linker(*this); }
diff --git a/clang/lib/Driver/ToolChains/Gnu.h b/clang/lib/Driver/ToolChains/Gnu.h
index 3b8df71bbf9d3..152a7ab8d5289 100644
--- a/clang/lib/Driver/ToolChains/Gnu.h
+++ b/clang/lib/Driver/ToolChains/Gnu.h
@@ -382,6 +382,9 @@ class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
}
virtual void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {}
+
+protected:
+ Tool *buildLinker() const override;
};
} // end namespace toolchains
More information about the cfe-commits
mailing list