[PATCH] D42003: [WebAssembly] Add --export flag to force a symbol to be exported

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 12 11:08:38 PST 2018


sbc100 created this revision.
Herald added subscribers: llvm-commits, sunfish, aheejin, jgravelle-google, dschuff, jfb.

This is useful for emscripten or other tools that want to
selectively exports symbols without necessarily changing the
source code.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D42003

Files:
  test/wasm/export.ll
  wasm/Driver.cpp
  wasm/Options.td
  wasm/Symbols.cpp
  wasm/Symbols.h


Index: wasm/Symbols.h
===================================================================
--- wasm/Symbols.h
+++ wasm/Symbols.h
@@ -67,6 +67,7 @@
   bool hasFunctionType() const { return FunctionType != nullptr; }
   const WasmSignature &getFunctionType() const;
   void setFunctionType(const WasmSignature *Type);
+  void setHidden(bool IsHidden);
 
   uint32_t getOutputIndex() const;
 
Index: wasm/Symbols.cpp
===================================================================
--- wasm/Symbols.cpp
+++ wasm/Symbols.cpp
@@ -97,6 +97,14 @@
   return (Flags & WASM_SYMBOL_VISIBILITY_MASK) == WASM_SYMBOL_VISIBILITY_HIDDEN;
 }
 
+void Symbol::setHidden(bool IsHidden) {
+  Flags &= ~WASM_SYMBOL_VISIBILITY_MASK;
+  if (IsHidden)
+    Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
+  else
+    Flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
+}
+
 std::string lld::toString(const wasm::Symbol &Sym) {
   if (Config->Demangle)
     if (Optional<std::string> S = demangleItanium(Sym.getName()))
Index: wasm/Options.td
===================================================================
--- wasm/Options.td
+++ wasm/Options.td
@@ -74,6 +74,9 @@
 
 // The follow flags are unique to wasm
 
+defm export: Eq<"export">,
+  HelpText<"Force a symbol to be exported">;
+
 def global_base: J<"global-base=">,
   HelpText<"Where to start to place global data">;
 
Index: wasm/Driver.cpp
===================================================================
--- wasm/Driver.cpp
+++ wasm/Driver.cpp
@@ -330,6 +330,14 @@
   if (errorCount())
     return;
 
+  for (StringRef S : args::getStrings(Args, OPT_export)) {
+    Symbol *Sym = Symtab->find(S);
+    if (!Sym || !Sym->isDefined())
+      error("symbol exported via --export not found: " + S);
+    else
+      Sym->setHidden(false);
+  }
+
   if (!Config->Entry.empty() && !Symtab->find(Config->Entry)->isDefined())
     error("entry point not found: " + Config->Entry);
   if (errorCount())
Index: test/wasm/export.ll
===================================================================
--- /dev/null
+++ test/wasm/export.ll
@@ -0,0 +1,29 @@
+; RUN: llc -filetype=obj -mtriple=wasm32-unknown-unknown-wasm %s -o %t.o
+; RUN: not lld -flavor wasm --export=missing -o %t.wasm %t.o 2>&1 | FileCheck -check-prefix=CHECK-ERROR %s
+; RUN: lld -flavor wasm --export=hidden_function -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+define hidden i32 @hidden_function() local_unnamed_addr {
+entry:
+  ret i32 0
+}
+
+define i32 @_start() local_unnamed_addr {
+entry:
+  ret i32 0
+}
+
+; CHECK-ERROR: error: symbol exported via --export not found: missing
+
+; CHECK:        - Type:            EXPORT
+; CHECK-NEXT:     Exports:         
+; CHECK-NEXT:       - Name:            memory
+; CHECK-NEXT:         Kind:            MEMORY
+; CHECK-NEXT:         Index:           0
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           1
+; CHECK-NEXT:       - Name:            hidden_function
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           0
+; CHECK-NEXT:   - Type:            CODE


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42003.129667.patch
Type: text/x-patch
Size: 3129 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180112/36000a47/attachment.bin>


More information about the llvm-commits mailing list