From 7d0bb485aa40492c85181f477ae39e6bca56234b Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 9 Nov 2016 11:58:14 -0800 Subject: add a --symbolmap option to wasm-as, which emits a side file with the name mapping (similar to Names section, but external) --- src/wasm/wasm-binary.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index d2a648294..2a81bdc46 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -43,6 +43,7 @@ void WasmBinaryWriter::write() { writeFunctions(); writeDataSegments(); if (debugInfo) writeNames(); + if (symbolMap.size() > 0) writeSymbolMap(); finishUp(); } @@ -380,6 +381,18 @@ void WasmBinaryWriter::writeNames() { finishSection(start); } +void WasmBinaryWriter::writeSymbolMap() { + std::ofstream file(symbolMap); + for (auto& import : wasm->imports) { + if (import->kind == ExternalKind::Function) { + file << getFunctionIndex(import->name) << ":" << import->name.str << std::endl; + } + } + for (auto& func : wasm->functions) { + file << getFunctionIndex(func->name) << ":" << func->name.str << std::endl; + } + file.close(); +} void WasmBinaryWriter::writeInlineString(const char* name) { int32_t size = strlen(name); -- cgit v1.2.3 From f0c32b28cb01fe17cf86c0f48b56bb221407ad76 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 9 Nov 2016 14:23:32 -0800 Subject: emit entries in the Names section for imports as well, as was recently changed in the spec --- src/wasm/wasm-binary.cpp | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'src/wasm/wasm-binary.cpp') diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 2a81bdc46..4a4c878e9 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -369,15 +369,38 @@ void WasmBinaryWriter::writeTableElements() { } void WasmBinaryWriter::writeNames() { - if (wasm->functions.size() == 0) return; + bool hasContents = false; + if (wasm->functions.size() > 0) { + hasContents = true; + getFunctionIndex(wasm->functions[0]->name); // generate mappedFunctions + } else { + for (auto& import : wasm->imports) { + if (import->kind == ExternalKind::Function) { + hasContents = true; + getFunctionIndex(import->name); // generate mappedFunctions + break; + } + } + } + if (!hasContents) return; if (debug) std::cerr << "== writeNames" << std::endl; auto start = startSection(BinaryConsts::Section::User); writeInlineString(BinaryConsts::UserSections::Name); - o << U32LEB(wasm->functions.size()); + o << U32LEB(mappedFunctions.size()); + Index emitted = 0; + for (auto& import : wasm->imports) { + if (import->kind == ExternalKind::Function) { + writeInlineString(import->name.str); + o << U32LEB(0); // TODO: locals + emitted++; + } + } for (auto& curr : wasm->functions) { writeInlineString(curr->name.str); o << U32LEB(0); // TODO: locals + emitted++; } + assert(emitted == mappedFunctions.size()); finishSection(start); } @@ -1400,7 +1423,16 @@ void WasmBinaryBuilder::readTableElements() { void WasmBinaryBuilder::readNames() { if (debug) std::cerr << "== readNames" << std::endl; auto num = getU32LEB(); - assert(num == functions.size()); + if (num == 0) return; + for (auto& import : wasm.imports) { + if (import->kind == ExternalKind::Function) { + getInlineString(); // TODO: use this + auto numLocals = getU32LEB(); + WASM_UNUSED(numLocals); + assert(numLocals == 0); // TODO + if (--num == 0) return; + } + } for (size_t i = 0; i < num; i++) { functions[i]->name = getInlineString(); auto numLocals = getU32LEB(); -- cgit v1.2.3