[PATCH] D133884: [lld][WebAssembly] Keep data in `foo` segments alive if `__start_foo` is referenced

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 14 11:54:15 PDT 2022


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

In emscripten we use named segments and export `__start/__stop` symbols
to extract data after the linker has run.  In some cases that data is
not otherwise alive.  For example:

https://github.com/emscripten-core/emscripten/pull/17854

The ELF linker already does this but is currently behind the `-z
nostart-stop-gc` flag.  We could do that here to, but I'm not sure its
necessary.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133884

Files:
  lld/wasm/MarkLive.cpp


Index: lld/wasm/MarkLive.cpp
===================================================================
--- lld/wasm/MarkLive.cpp
+++ lld/wasm/MarkLive.cpp
@@ -25,6 +25,9 @@
 #include "SymbolTable.h"
 #include "Symbols.h"
 
+#include "lld/Common/CommonLinkerContext.h"
+#include "lld/Common/Strings.h"
+
 #define DEBUG_TYPE "lld"
 
 using namespace llvm;
@@ -98,10 +101,23 @@
     enqueue(WasmSym::callDtors);
 
   // Enqueue constructors in objects explicitly live from the command-line.
-  for (const ObjFile *obj : symtab->objectFiles)
-    if (obj->isLive())
+  for (const ObjFile *obj : symtab->objectFiles) {
+    if (obj->isLive()) {
       enqueueInitFunctions(obj);
 
+      // If the `__start_` symbol for a given segment is referenced then keep
+      // all of its contents alive.
+      for (InputChunk *seg : obj->segments) {
+        if (isValidCIdentifier(seg->name)) {
+          StringRef name = saver().save("__start_" + seg->name);
+          if (symtab->find(name) || config->exportedSymbols.count(name) != 0) {
+            seg->live = true;
+          }
+        }
+      }
+    }
+  }
+
   mark();
 
   // If we have any non-discarded init functions, mark `__wasm_call_ctors` as


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133884.460171.patch
Type: text/x-patch
Size: 1196 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220914/8612363a/attachment.bin>


More information about the llvm-commits mailing list