Skip to content

[Clang][Driver] Override Generic_ELF::buildLinker() to avoid calling gcc to link #149681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mintsuki
Copy 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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jul 19, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 19, 2025

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: None (mintsuki)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/149681.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+2)
  • (modified) clang/lib/Driver/ToolChains/Gnu.h (+3)
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

@brad0
Copy link
Contributor

brad0 commented Jul 19, 2025

cc @MaskRay

@MaskRay
Copy link
Member

MaskRay commented Jul 20, 2025

Can you add a test to clang/test/Driver to show the behavior difference ?

@mintsuki mintsuki force-pushed the generic_elf-use-gnutools-linker branch from 7401452 to 468635d Compare July 20, 2025 01:28
@mintsuki
Copy link
Author

Can you add a test to clang/test/Driver to show the behavior difference ?

@MaskRay done. Does this work?

…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.
@mintsuki mintsuki force-pushed the generic_elf-use-gnutools-linker branch from 468635d to 491a834 Compare July 20, 2025 01:48
@mintsuki
Copy link
Author

A bunch of tests fail now... I feel like this change is too invasive for what I can possibly do on my own, but I hope that the original commit message is helpful in describing the problem at hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants