[clang] [clang] force libc linked with --no-as-needed when using compiler-rt (PR #95848)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 17 14:09:04 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Ziyao (ziyao233)

<details>
<summary>Changes</summary>

crtbegin.o in compiler-rt contains weak reference to __cxa_finalize, which is implemented in libc. When building a shared library with --as-needed option, libc may be incorrectly marked as inactive, resulting in unresolved symbols in crtbegin.o

This commit forces libc is linked with --no-as-needed option when using compiler-rt, fixing the problem.

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


1 Files Affected:

- (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+9-1) 


``````````diff
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index dedbfac6cb25d..419c6b6e43664 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -378,6 +378,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   const bool HasCRTBeginEndFiles =
       ToolChain.getTriple().hasEnvironment() ||
       (ToolChain.getTriple().getVendor() != llvm::Triple::MipsTechnologies);
+  const ToolChain::RuntimeLibType RLTType = ToolChain.GetRuntimeLibType(Args);
 
   ArgStringList CmdArgs;
 
@@ -625,8 +626,15 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
       if (Args.hasArg(options::OPT_fsplit_stack))
         CmdArgs.push_back("--wrap=pthread_create");
 
-      if (!Args.hasArg(options::OPT_nolibc))
+      if (!Args.hasArg(options::OPT_nolibc)) {
+        if (RLTType == ToolChain::RLT_CompilerRT) {  
+          CmdArgs.push_back("--push-state");
+          CmdArgs.push_back("--no-as-needed");
+        }
         CmdArgs.push_back("-lc");
+        if (RLTType == ToolChain::RLT_CompilerRT)
+          CmdArgs.push_back("--pop-state");
+      }
 
       // Add IAMCU specific libs, if needed.
       if (IsIAMCU)

``````````

</details>


https://github.com/llvm/llvm-project/pull/95848


More information about the cfe-commits mailing list