From 66bfbedbdfb98bb3c8fbda9ee839e46b430122d1 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 20 Dec 2019 21:44:24 -0800 Subject: [PATCH] [WebAssembly] Support wasm exports with zero-length names. Zero-length strings are valid export names in WebAssembly, so allow users to specify them. Differential Revision: https://reviews.llvm.org/D71793 --- lld/test/wasm/export-name.ll | 11 ++++++++++- lld/wasm/InputChunks.h | 4 ++-- lld/wasm/Writer.cpp | 5 ++--- llvm/include/llvm/BinaryFormat/Wasm.h | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lld/test/wasm/export-name.ll b/lld/test/wasm/export-name.ll index 83a8bd9..b02f253 100644 --- a/lld/test/wasm/export-name.ll +++ b/lld/test/wasm/export-name.ll @@ -8,6 +8,10 @@ define void @foo() #0 { ret void } +define void @qux() #1 { + ret void +} + define void @_start() { call void @foo() ret void @@ -15,6 +19,8 @@ define void @_start() { attributes #0 = { "wasm-export-name"="bar" } +attributes #1 = { "wasm-export-name"="" } + ; CHECK: - Type: EXPORT ; CHECK-NEXT: Exports: ; CHECK-NEXT: - Name: memory @@ -23,6 +29,9 @@ attributes #0 = { "wasm-export-name"="bar" } ; CHECK-NEXT: - Name: bar ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: Index: 0 -; CHECK-NEXT: - Name: _start +; CHECK-NEXT: - Name: '' ; CHECK-NEXT: Kind: FUNCTION ; CHECK-NEXT: Index: 1 +; CHECK-NEXT: - Name: _start +; CHECK-NEXT: Kind: FUNCTION +; CHECK-NEXT: Index: 2 diff --git a/lld/wasm/InputChunks.h b/lld/wasm/InputChunks.h index 030c9a9..cadff68 100644 --- a/lld/wasm/InputChunks.h +++ b/lld/wasm/InputChunks.h @@ -130,8 +130,8 @@ public: void writeTo(uint8_t *sectionStart) const override; StringRef getName() const override { return function->SymbolName; } StringRef getDebugName() const override { return function->DebugName; } - StringRef getExportName() const { - return function ? function->ExportName : ""; + llvm::Optional getExportName() const { + return function ? function->ExportName : llvm::Optional(); } uint32_t getComdat() const override { return function->Comdat; } uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); } diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp index ca0bfef..9f4b6ad 100644 --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -520,9 +520,8 @@ void Writer::calculateExports() { StringRef name = sym->getName(); WasmExport export_; if (auto *f = dyn_cast(sym)) { - StringRef exportName = f->function->getExportName(); - if (!exportName.empty()) { - name = exportName; + if (Optional exportName = f->function->getExportName()) { + name = *exportName; } export_ = {name, WASM_EXTERNAL_FUNCTION, f->getFunctionIndex()}; } else if (auto *g = dyn_cast(sym)) { diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h index e39760e..dd743db 100644 --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -132,7 +132,7 @@ struct WasmFunction { uint32_t CodeSectionOffset; uint32_t Size; uint32_t CodeOffset; // start of Locals and Body - StringRef ExportName; // from the "export" section + Optional ExportName; // from the "export" section StringRef SymbolName; // from the "linking" section StringRef DebugName; // from the "name" section uint32_t Comdat; // from the "comdat info" section -- 2.7.4