Index: wasm/Writer.cpp =================================================================== --- wasm/Writer.cpp +++ wasm/Writer.cpp @@ -124,7 +124,7 @@ std::vector ImportedSymbols; unsigned NumImportedFunctions = 0; unsigned NumImportedGlobals = 0; - std::vector ExportedSymbols; + std::vector Exports; std::vector DefinedFakeGlobals; std::vector InputGlobals; std::vector InputFunctions; @@ -268,37 +268,15 @@ } void Writer::createExportSection() { - bool ExportMemory = !Config->Relocatable && !Config->ImportMemory; - - uint32_t NumExports = (ExportMemory ? 1 : 0) + ExportedSymbols.size(); - if (!NumExports) + if (!Exports.size()) return; SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT); raw_ostream &OS = Section->getStream(); - writeUleb128(OS, NumExports, "export count"); - - if (ExportMemory) - writeExport(OS, {"memory", WASM_EXTERNAL_MEMORY, 0}); - - unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size(); - - for (const Symbol *Sym : ExportedSymbols) { - StringRef Name = Sym->getName(); - WasmExport Export; - DEBUG(dbgs() << "Export: " << Name << "\n"); - - if (isa(Sym)) - Export = {Name, WASM_EXTERNAL_FUNCTION, Sym->getOutputIndex()}; - else if (isa(Sym)) - Export = {Name, WASM_EXTERNAL_GLOBAL, Sym->getOutputIndex()}; - else if (isa(Sym)) - Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++}; - else - llvm_unreachable("unexpected symbol type"); + writeUleb128(OS, Exports.size(), "export count"); + for (const WasmExport &Export : Exports) writeExport(OS, Export); - } } void Writer::createElemSection() { @@ -669,6 +647,11 @@ if (Config->Relocatable) return; + if (!Config->Relocatable && !Config->ImportMemory) + Exports.push_back(WasmExport{"memory", WASM_EXTERNAL_MEMORY, 0}); + + unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size(); + for (Symbol *Sym : Symtab->getSymbols()) { if (!Sym->isDefined()) continue; @@ -677,17 +660,23 @@ if (!Sym->isLive()) continue; - DEBUG(dbgs() << "exporting sym: " << Sym->getName() << "\n"); - - if (auto *D = dyn_cast(Sym)) { - // TODO Remove this check here; for non-relocatable output we actually - // used only to create fake-global exports for the synthetic symbols. Fix - // this in a future commit + StringRef Name = Sym->getName(); + WasmExport Export; + if (isa(Sym)) { + Export = {Name, WASM_EXTERNAL_FUNCTION, Sym->getOutputIndex()}; + } else if (isa(Sym)) { + Export = {Name, WASM_EXTERNAL_GLOBAL, Sym->getOutputIndex()}; + } else if (auto *D = dyn_cast(Sym)) { if (Sym != WasmSym::DataEnd && Sym != WasmSym::HeapBase) continue; DefinedFakeGlobals.emplace_back(D); + Export = {Name, WASM_EXTERNAL_GLOBAL, FakeGlobalIndex++}; + } else { + llvm_unreachable("unexpected symbol type"); } - ExportedSymbols.emplace_back(Sym); + + DEBUG(dbgs() << "Export: " << Name << "\n"); + Exports.push_back(Export); } }