diff --git a/lld/docs/WebAssembly.rst b/lld/docs/WebAssembly.rst --- a/lld/docs/WebAssembly.rst +++ b/lld/docs/WebAssembly.rst @@ -152,7 +152,8 @@ in turn can be set using ``__attribute__((export_name))`` clang attribute. In addition, symbols can be exported via the linker command line using -``--export``. +``--export``, or ``--export-dynamic-symbol``. The former will generate an +error if the symbol is not defined whereas the later will not. Finally, just like with native ELF linker the ``--export-dynamic`` flag can be used to export symbols in the executable which are marked as diff --git a/lld/test/wasm/export-dynamic-symbol.s b/lld/test/wasm/export-dynamic-symbol.s new file mode 100644 --- /dev/null +++ b/lld/test/wasm/export-dynamic-symbol.s @@ -0,0 +1,37 @@ +# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s +# RUN: wasm-ld --export-dynamic-symbol=foo -o %t1.wasm %t.o +# RUN: obj2yaml %t1.wasm | FileCheck %s + +# RUN: wasm-ld --export-dynamic-symbol=bar -o %t2.wasm %t.o +# RUN: obj2yaml %t2.wasm | FileCheck %s --check-prefixes=MISSING + +.globl foo +foo: + .functype foo () -> () + end_function + +.globl _start +_start: + .functype _start () -> () + end_function + +# CHECK: - Type: EXPORT +# CHECK-NEXT: Exports: +# CHECK-NEXT: - Name: memory +# CHECK-NEXT: Kind: MEMORY +# CHECK-NEXT: Index: 0 +# CHECK-NEXT: - Name: foo +# CHECK-NEXT: Kind: FUNCTION +# CHECK-NEXT: Index: 0 +# CHECK-NEXT: - Name: _start +# CHECK-NEXT: Kind: FUNCTION +# CHECK-NEXT: Index: 1 + +# MISSING: - Type: EXPORT +# MISSING-NEXT: Exports: +# MISSING-NEXT: - Name: memory +# MISSING-NEXT: Kind: MEMORY +# MISSING-NEXT: Index: 0 +# MISSING-NEXT: - Name: _start +# MISSING-NEXT: Kind: FUNCTION +# MISSING-NEXT: Index: 0 diff --git a/lld/wasm/Config.h b/lld/wasm/Config.h --- a/lld/wasm/Config.h +++ b/lld/wasm/Config.h @@ -73,6 +73,7 @@ llvm::StringSet<> allowUndefinedSymbols; llvm::StringSet<> exportedSymbols; + std::vector requiredExports; std::vector searchPaths; llvm::CachePruningPolicy thinLTOCachePolicy; llvm::Optional> features; diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -865,9 +865,14 @@ for (auto *arg : args.filtered(OPT_trace_symbol)) symtab->trace(arg->getValue()); - for (auto *arg : args.filtered(OPT_export)) + for (auto *arg : args.filtered(OPT_export_dynamic_symbol)) config->exportedSymbols.insert(arg->getValue()); + for (auto *arg : args.filtered(OPT_export)) { + config->exportedSymbols.insert(arg->getValue()); + config->requiredExports.push_back(arg->getValue()); + } + createSyntheticSymbols(); // Add all files to the symbol table. This will add almost all @@ -883,8 +888,8 @@ // Handle the `--export ` options // This works like --undefined but also exports the symbol if its found - for (auto *arg : args.filtered(OPT_export)) - handleUndefined(arg->getValue()); + for (auto &iter : config->exportedSymbols) + handleUndefined(iter.first()); Symbol *entrySym = nullptr; if (!config->relocatable && !config->entry.empty()) { @@ -958,8 +963,8 @@ if (!wrapped.empty()) wrapSymbols(wrapped); - for (auto *arg : args.filtered(OPT_export)) { - Symbol *sym = symtab->find(arg->getValue()); + for (auto &iter : config->exportedSymbols) { + Symbol *sym = symtab->find(iter.first()); if (sym && sym->isDefined()) sym->forceExport = true; } diff --git a/lld/wasm/Options.td b/lld/wasm/Options.td --- a/lld/wasm/Options.td +++ b/lld/wasm/Options.td @@ -6,6 +6,12 @@ class J: Joined<["--", "-"], name>; class S: Separate<["--", "-"], name>; +multiclass EEq { + def NAME: Separate<["--"], name>; + def NAME # _eq: Joined<["--"], name # "=">, Alias(NAME)>, + HelpText; +} + multiclass Eq { def NAME: Separate<["--", "-"], name>; def NAME # _eq: Joined<["--", "-"], name # "=">, Alias(NAME)>, @@ -48,6 +54,13 @@ "Put symbols in the dynamic symbol table", "Do not put symbols in the dynamic symbol table (default)">; +defm export_dynamic_symbol : EEq<"export-dynamic-symbol", + "(executable) Put matched symbols in the dynamic symbol table. " + "(shared object) References to matched non-local STV_DEFAULT symbols " + "shouldn't be bound to definitions within the shared object. " + "Does not imply -Bsymbolic.">, + MetaVarName<"glob">; + def entry: S<"entry">, MetaVarName<"">, HelpText<"Name of entry point symbol">; diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -1399,8 +1399,7 @@ // Delay reporting error about explict exports until after addStartStopSymbols // which can create optional symbols. - for (auto &entry : config->exportedSymbols) { - StringRef name = entry.first(); + for (auto &name : config->requiredExports) { Symbol *sym = symtab->find(name); if (sym && sym->isDefined()) sym->forceExport = true;