[lld] [lld][WebAssembly] Fix check for implicitly exported mutable globals (PR #160966)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 26 16:13:25 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lld

Author: Sam Clegg (sbc100)

<details>
<summary>Changes</summary>

This change is designed to avoid exporting mutable globals in the case when mutable globals are not available.  However, it was being applied in all cases even when mutable globals was enabled.

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


2 Files Affected:

- (modified) lld/test/wasm/mutable-global-exports.s (+3) 
- (modified) lld/wasm/Writer.cpp (+5-1) 


``````````diff
diff --git a/lld/test/wasm/mutable-global-exports.s b/lld/test/wasm/mutable-global-exports.s
index 4ffaf0a6cbaf0..1c10e92083b5c 100644
--- a/lld/test/wasm/mutable-global-exports.s
+++ b/lld/test/wasm/mutable-global-exports.s
@@ -73,6 +73,9 @@ _start:
 # CHECK-ALL-NEXT:      - Name:            __wasm_call_ctors
 # CHECK-ALL-NEXT:        Kind:            FUNCTION
 # CHECK-ALL-NEXT:        Index:           0
+# CHECK-ALL-NEXT:      - Name:            __stack_pointer
+# CHECK-ALL-NEXT:        Kind:            GLOBAL
+# CHECK-ALL-NEXT:        Index:           0
 # CHECK-ALL-NEXT:      - Name:            _start
 # CHECK-ALL-NEXT:        Kind:            FUNCTION
 # CHECK-ALL-NEXT:        Index:           1
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 0d36893653110..9a5b56fc52e2f 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -784,6 +784,9 @@ void Writer::calculateExports() {
   unsigned globalIndex =
       out.importSec->getNumImportedGlobals() + out.globalSec->numGlobals();
 
+  bool hasMutableGlobals =
+      out.targetFeaturesSec->features.count("mutable-globals") > 0;
+
   for (Symbol *sym : symtab->symbols()) {
     if (!sym->isExported())
       continue;
@@ -801,7 +804,8 @@ void Writer::calculateExports() {
       }
       export_ = {name, WASM_EXTERNAL_FUNCTION, f->getExportedFunctionIndex()};
     } else if (auto *g = dyn_cast<DefinedGlobal>(sym)) {
-      if (g->getGlobalType()->Mutable && !g->getFile() && !g->forceExport) {
+      if (!hasMutableGlobals && g->getGlobalType()->Mutable && !g->getFile() &&
+          !g->isExportedExplicit()) {
         // Avoid exporting mutable globals are linker synthesized (e.g.
         // __stack_pointer or __tls_base) unless they are explicitly exported
         // from the command line.

``````````

</details>


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


More information about the llvm-commits mailing list