[lld] [lld][WebAssembly] Fix stub library deps causing LTO archive members to be required post-LTO (PR #101894)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 4 08:28:56 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld-wasm

Author: Sam Clegg (sbc100)

<details>
<summary>Changes</summary>

Fixes: https://github.com/emscripten-core/emscripten/issues/16836

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


4 Files Affected:

- (modified) lld/test/wasm/lto/stub-library.s (+9-6) 
- (modified) lld/test/wasm/stub-library-archive.s (+1) 
- (modified) lld/wasm/Driver.cpp (+13) 
- (modified) lld/wasm/InputFiles.cpp (+1-1) 


``````````diff
diff --git a/lld/test/wasm/lto/stub-library.s b/lld/test/wasm/lto/stub-library.s
index 20e2a62211413..a5c724012faec 100644
--- a/lld/test/wasm/lto/stub-library.s
+++ b/lld/test/wasm/lto/stub-library.s
@@ -1,12 +1,15 @@
 # RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
-# RUN: llvm-as %S/Inputs/foo.ll -o %t1.o
-# RUN: wasm-ld %t.o %t1.o %p/Inputs/stub.so -o %t.wasm
+# RUN: mkdir -p %t
+# RUN: llvm-as %S/Inputs/foo.ll -o %t/foo.o
+# RUN: rm -f %t/libfoo.a
+# RUN: llvm-ar rcs %t/libfoo.a %t/foo.o
+# RUN: wasm-ld %t.o %t/libfoo.a %p/Inputs/stub.so -o %t.wasm
 # RUN: obj2yaml %t.wasm | FileCheck %s
 
-# The function `bar` is declared in stub.so and depends on `foo`, which happens
-# be in an LTO object.
-# This verifies that stub library dependencies (required exports) can be defined
-# in LTO objects.
+## The function `bar` is declared in stub.so and depends on `foo`, which happens
+## be in an LTO object, in an archive file.
+## This verifies that stub library dependencies (required exports) can be defined
+## in LTO objects.
 .functype bar () -> ()
 
 .globl _start
diff --git a/lld/test/wasm/stub-library-archive.s b/lld/test/wasm/stub-library-archive.s
index 76483d1463d64..131f21bea4a13 100644
--- a/lld/test/wasm/stub-library-archive.s
+++ b/lld/test/wasm/stub-library-archive.s
@@ -1,6 +1,7 @@
 # RUN: split-file %s %t
 # RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %t/main.s
 # RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t/foodeps.o %t/foodeps.s
+# RUN: mkdir -p %t
 # RUN: rm -f %t/libfoodeps.a
 # RUN: llvm-ar rcs %t/libfoodeps.a %t/foodeps.o
 # RUN: wasm-ld %t.o %p/Inputs/libstub.so %t/libfoodeps.a -o %t.wasm
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 8c83d17db02f5..f8a163ee39eb7 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -945,10 +945,23 @@ static void processStubLibrariesPreLTO() {
       // undefined, then mark the dependent symbols as used by a regular
       // object so they will be preserved and exported by the LTO process.
       if (!sym || sym->isUndefined()) {
+        LLVM_DEBUG(llvm::dbgs()
+                   << "marking symbol deps: " << name << "\n");
         for (const auto dep : deps) {
           auto* needed = symtab->find(dep);
           if (needed ) {
             needed->isUsedInRegularObj = true;
+            // Like with handleLibcall we have extract any archive members
+            // that might need to be exported due to stub library symbol being
+            // used.  Without this the LTO object could be extracted during
+            // processStubLibraries, which is too late since LTO has already
+            // beeing performed at that point.
+            if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
+              if (!config->whyExtract.empty())
+                ctx.whyExtractRecords.emplace_back("<stubdep>", needed->getFile(),
+                                                   *needed);
+              cast<LazySymbol>(needed)->extract();
+            }
           }
         }
       }
diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp
index f3f0ef9a99497..532c2e619e1bb 100644
--- a/lld/wasm/InputFiles.cpp
+++ b/lld/wasm/InputFiles.cpp
@@ -761,7 +761,7 @@ void StubFile::parse() {
     }
 
     // Lines starting with # are considered comments
-    if (line.starts_with("#"))
+    if (line.starts_with("#") || !line.size())
       continue;
 
     StringRef sym;

``````````

</details>


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


More information about the llvm-commits mailing list