[PATCH] D87537: [lld][WebAssembly] Add mutable-globals to feature section if needed

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 11 11:36:15 PDT 2020


sbc100 created this revision.
Herald added subscribers: llvm-commits, ecnelises, sunfish, jgravelle-google, dschuff.
Herald added a project: LLVM.
sbc100 requested review of this revision.
Herald added a subscriber: aheejin.

Automatically add `mutable_globals` to feature section when
writing binaryies that import or export mutable globals.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D87537

Files:
  lld/test/wasm/gc-imports.s
  lld/wasm/Writer.cpp


Index: lld/wasm/Writer.cpp
===================================================================
--- lld/wasm/Writer.cpp
+++ lld/wasm/Writer.cpp
@@ -445,6 +445,27 @@
               std::any_of(file->segments.begin(), file->segments.end(), isTLS);
   }
 
+  // If no object files explictly used mutable globals we still might need
+  // to include it due to linker-generated globals such as those use in the
+  // PIC ABI for importing and exported symbol offsets.
+  if (!config->relocatable && used.count("mutable-globals") == 0) {
+    for (Symbol *sym : symtab->getSymbols()) {
+      if (auto *global = dyn_cast<GlobalSymbol>(sym)) {
+        if (global->getGlobalType()->Mutable) {
+          if (!sym->isLive())
+            continue;
+          if (!sym->isUsedInRegularObj)
+            continue;
+          if (sym->isUndefined() && sym->isWeak() && !config->relocatable)
+            continue;
+          if (sym->isUndefined() || sym->isExported()) {
+            used.insert({"mutable-globals", "<internal>"});
+          }
+        }
+      }
+    }
+  }
+
   if (inferFeatures)
     for (const auto &key : used.keys())
       allowed.insert(std::string(key));
Index: lld/test/wasm/gc-imports.s
===================================================================
--- lld/test/wasm/gc-imports.s
+++ lld/test/wasm/gc-imports.s
@@ -42,6 +42,11 @@
 # CHECK-NEXT:         Name:            _start
 # CHECK-NEXT:       - Index:           2
 # CHECK-NEXT:         Name:            use_undef_global
+# CHECK-NEXT:   - Type:            CUSTOM
+# CHECK-NEXT:     Name:            target_features
+# CHECK-NEXT:     Features:
+# CHECK-NEXT:       - Prefix:          USED
+# CHECK-NEXT:         Name:            mutable-globals
 # CHECK-NEXT: ...
 
 # RUN: wasm-ld --no-gc-sections --allow-undefined \
@@ -84,4 +89,9 @@
 # NO-GC-NEXT:         Name:            _start
 # NO-GC-NEXT:       - Index:           5
 # NO-GC-NEXT:         Name:            use_undef_global
+# NO-GC-NEXT:   - Type:            CUSTOM
+# NO-GC-NEXT:     Name:            target_features
+# NO-GC-NEXT:     Features:
+# NO-GC-NEXT:       - Prefix:          USED
+# NO-GC-NEXT:         Name:            mutable-globals
 # NO-GC-NEXT: ...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87537.291294.patch
Type: text/x-patch
Size: 2208 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200911/1d82445d/attachment.bin>


More information about the llvm-commits mailing list