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 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: 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/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 S = demangleItanium(Sym.getName()))