[lld] r334157 - [WebAssembly] Add --export-all flag

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 6 18:27:07 PDT 2018


Author: sbc
Date: Wed Jun  6 18:27:07 2018
New Revision: 334157

URL: http://llvm.org/viewvc/llvm-project?rev=334157&view=rev
Log:
[WebAssembly] Add --export-all flag

This causes all symbols to be exported in the final wasm binary
even if they were not compiled with default visibility.

This feature is useful for the emscripten toolchain that has a
corresponding EXPORT_ALL feature which allows the JS code to
interact with all C function.

Differential Revision: https://reviews.llvm.org/D47806

Added:
    lld/trunk/test/wasm/export-all.ll
Modified:
    lld/trunk/wasm/Config.h
    lld/trunk/wasm/Driver.cpp
    lld/trunk/wasm/Options.td
    lld/trunk/wasm/Writer.cpp

Added: lld/trunk/test/wasm/export-all.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/export-all.ll?rev=334157&view=auto
==============================================================================
--- lld/trunk/test/wasm/export-all.ll (added)
+++ lld/trunk/test/wasm/export-all.ll Wed Jun  6 18:27:07 2018
@@ -0,0 +1,45 @@
+; RUN: llc -O0 -filetype=obj %s -o %t.o
+
+; RUN: wasm-ld -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+; RUN: wasm-ld --export-all -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=EXPORT
+
+; RUN: wasm-ld --export-all --no-gc-sections -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=NOGC
+
+; Verify the --export-all flag exports hidden symbols
+
+target triple = "wasm32-unknown-unknown"
+
+define hidden void @bar() local_unnamed_addr {
+entry:
+  ret void
+}
+
+define hidden void @foo() local_unnamed_addr {
+entry:
+  ret void
+}
+
+define hidden void @_start() local_unnamed_addr {
+entry:
+  call void @foo()
+  ret void
+}
+
+; CHECK:      - Type:            EXPORT
+; CHECK:         - Name:            _start
+; CHECK-NOT:     - Name:            bar
+; CHECK-NOT:     - Name:            foo
+
+; EXPORT:     - Type:            EXPORT
+; EXPORT:        - Name:            _start
+; EXPORT-NOT:    - Name:            bar
+; EXPORT:        - Name:            foo
+
+; NOGC:       - Type:            EXPORT
+; NOGC:        - Name:            _start
+; NOGC:        - Name:            bar
+; NOGC:        - Name:            foo

Modified: lld/trunk/wasm/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Config.h?rev=334157&r1=334156&r2=334157&view=diff
==============================================================================
--- lld/trunk/wasm/Config.h (original)
+++ lld/trunk/wasm/Config.h Wed Jun  6 18:27:07 2018
@@ -23,6 +23,7 @@ struct Configuration {
   bool CompressRelocTargets;
   bool Demangle;
   bool DisableVerify;
+  bool ExportAll;
   bool ExportTable;
   bool GcSections;
   bool ImportMemory;

Modified: lld/trunk/wasm/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Driver.cpp?rev=334157&r1=334156&r2=334157&view=diff
==============================================================================
--- lld/trunk/wasm/Driver.cpp (original)
+++ lld/trunk/wasm/Driver.cpp Wed Jun  6 18:27:07 2018
@@ -319,6 +319,7 @@ void LinkerDriver::link(ArrayRef<const c
   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
   Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start");
+  Config->ExportAll = Args.hasArg(OPT_export_all);
   Config->ExportTable = Args.hasArg(OPT_export_table);
   errorHandler().FatalWarnings =
       Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
@@ -389,10 +390,6 @@ void LinkerDriver::link(ArrayRef<const c
 
   Symbol *EntrySym = nullptr;
   if (!Config->Relocatable) {
-    // Can't export the SP right now because it's mutable, and mutable
-    // globals aren't yet supported in the official binary format.
-    // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN if/when the
-    // "mutable global" proposal is accepted.
     llvm::wasm::WasmGlobal Global;
     Global.Type = {WASM_TYPE_I32, true};
     Global.InitExpr.Value.Int32 = 0;
@@ -407,6 +404,9 @@ void LinkerDriver::link(ArrayRef<const c
     WasmSym::CallCtors = Symtab->addSyntheticFunction(
         "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
         make<SyntheticFunction>(NullSignature, "__wasm_call_ctors"));
+    // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN when the mutable global
+    // spec proposal is implemented in all major browsers.
+    // See: https://github.com/WebAssembly/mutable-global
     WasmSym::StackPointer = Symtab->addSyntheticGlobal(
         "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer);
     WasmSym::HeapBase = Symtab->addSyntheticDataSymbol("__heap_base", 0);

Modified: lld/trunk/wasm/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Options.td?rev=334157&r1=334156&r2=334157&view=diff
==============================================================================
--- lld/trunk/wasm/Options.td (original)
+++ lld/trunk/wasm/Options.td Wed Jun  6 18:27:07 2018
@@ -105,6 +105,9 @@ def allow_undefined_file_s: Separate<["-
 defm export: Eq<"export">,
   HelpText<"Force a symbol to be exported">;
 
+def export_all: F<"export-all">,
+  HelpText<"Export all symbols (normally combined with --no-gc-sections)">;
+
 def export_table: F<"export-table">,
   HelpText<"Export function table to the environment">;
 

Modified: lld/trunk/wasm/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/Writer.cpp?rev=334157&r1=334156&r2=334157&view=diff
==============================================================================
--- lld/trunk/wasm/Writer.cpp (original)
+++ lld/trunk/wasm/Writer.cpp Wed Jun  6 18:27:07 2018
@@ -742,7 +742,9 @@ void Writer::calculateExports() {
   for (Symbol *Sym : Symtab->getSymbols()) {
     if (!Sym->isDefined())
       continue;
-    if (Sym->isHidden() || Sym->isLocal())
+    if (Sym->isHidden() && !Config->ExportAll)
+      continue;
+    if (Sym->isLocal())
       continue;
     if (!Sym->isLive())
       continue;
@@ -752,6 +754,14 @@ void Writer::calculateExports() {
     if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
       Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
     } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
+      // TODO(sbc): Remove this check once to mutable global proposal is
+      // implement in all major browsers.
+      // See: https://github.com/WebAssembly/mutable-global
+      if (G->getGlobalType()->Mutable) {
+        // Only the __stack_pointer should ever be create as mutable.
+        assert(G == WasmSym::StackPointer);
+        continue;
+      }
       Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
     } else {
       auto *D = cast<DefinedData>(Sym);




More information about the llvm-commits mailing list