[clang] [lld] [llvm] [LTO][LLD] Prevent invalid LTO libfunc transforms (PR #164916)

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 03:22:41 PST 2025


================
@@ -2701,15 +2701,30 @@ static void markBuffersAsDontNeed(Ctx &ctx, bool skipLinkedOutput) {
 template <class ELFT>
 void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
   llvm::TimeTraceScope timeScope("LTO");
+  // Capture the triple before moving the bitcode into the bitcode compiler.
+  std::optional<llvm::Triple> tt;
+  if (!ctx.bitcodeFiles.empty())
+    tt = llvm::Triple(ctx.bitcodeFiles.front()->obj->getTargetTriple());
   // Compile bitcode files and replace bitcode symbols.
   lto.reset(new BitcodeCompiler(ctx));
   for (BitcodeFile *file : ctx.bitcodeFiles)
     lto->add(*file);
 
-  if (!ctx.bitcodeFiles.empty())
+  llvm::BumpPtrAllocator alloc;
+  llvm::StringSaver saver(alloc);
+  SmallVector<StringRef> bitcodeLibFuncs;
+  if (!ctx.bitcodeFiles.empty()) {
     markBuffersAsDontNeed(ctx, skipLinkedOutput);
+    for (StringRef libFunc : lto::LTO::getLibFuncSymbols(*tt, saver)) {
+      Symbol *sym = ctx.symtab->find(libFunc);
+      if (!sym)
+        continue;
+      if (isa<BitcodeFile>(sym->file))
+        bitcodeLibFuncs.push_back(libFunc);
+    }
+  }
 
-  ltoObjectFiles = lto->compile();
+  ltoObjectFiles = lto->compile(bitcodeLibFuncs);
----------------
smithp35 wrote:

>From the callsite alone this could be interpreted as just compiling the bitcodeLibFuncs. 

Suggestions:
* Does the ltoObj->setBitcodeLibFuncs(bitcodeLibFuncs); need to be done in compile? Perhaps add a lto->setBitcodeLibFuncs(bitcodeLibFuncs) as it looks like ltoObj is private.
* Rename to compileObjectsAndLibFuncs(bitcodeLibFuncs).

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


More information about the llvm-commits mailing list