diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-11-10 16:45:27 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-10 16:45:27 -0800 |
commit | a3afc150c3768b351434c734448b7ab456d17f57 (patch) | |
tree | 8532f1d284164ee94e8606e2daaeebb387bbcbf1 | |
parent | 3dac399ba503e05ee014e96a7ce9c82f29f6981c (diff) | |
parent | f0c32b28cb01fe17cf86c0f48b56bb221407ad76 (diff) | |
download | binaryen-a3afc150c3768b351434c734448b7ab456d17f57.tar.gz binaryen-a3afc150c3768b351434c734448b7ab456d17f57.tar.bz2 binaryen-a3afc150c3768b351434c734448b7ab456d17f57.zip |
Merge pull request #831 from WebAssembly/symbol-map
Symbols fixes
-rw-r--r-- | src/tools/wasm-as.cpp | 5 | ||||
-rw-r--r-- | src/wasm-binary.h | 3 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 51 |
3 files changed, 56 insertions, 3 deletions
diff --git a/src/tools/wasm-as.cpp b/src/tools/wasm-as.cpp index 95e6c9371..067c0f28d 100644 --- a/src/tools/wasm-as.cpp +++ b/src/tools/wasm-as.cpp @@ -29,6 +29,7 @@ using namespace wasm; int main(int argc, const char *argv[]) { bool debugInfo = false; + std::string symbolMap; Options options("wasm-as", "Assemble a .wast (WebAssembly text format) into a .wasm (WebAssembly binary format)"); options.extra["validate"] = "wasm"; options @@ -50,6 +51,9 @@ int main(int argc, const char *argv[]) { .add("--debuginfo", "-g", "Emit names section and debug info", Options::Arguments::Zero, [&](Options *o, const std::string &arguments) { debugInfo = true; }) + .add("--symbolmap", "-s", "Emit a symbol map (indexes => names)", + Options::Arguments::One, + [&](Options *o, const std::string &argument) { symbolMap = argument; }) .add_positional("INFILE", Options::Arguments::One, [](Options *o, const std::string &argument) { o->extra["infile"] = argument; @@ -83,6 +87,7 @@ int main(int argc, const char *argv[]) { BufferWithRandomAccess buffer(options.debug); WasmBinaryWriter writer(&wasm, buffer, options.debug); writer.setDebugInfo(debugInfo); + if (symbolMap.size() > 0) writer.setSymbolMap(symbolMap); writer.write(); if (options.debug) std::cerr << "writing to output..." << std::endl; diff --git a/src/wasm-binary.h b/src/wasm-binary.h index efff5a77b..6ec55f757 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -527,6 +527,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> { BufferWithRandomAccess& o; bool debug; bool debugInfo = true; + std::string symbolMap; MixedArena allocator; @@ -537,6 +538,7 @@ public: } void setDebugInfo(bool set) { debugInfo = set; } + void setSymbolMap(std::string set) { symbolMap = set; } void write(); void writeHeader(); @@ -569,6 +571,7 @@ public: void writeFunctionTableDeclaration(); void writeTableElements(); void writeNames(); + void writeSymbolMap(); // helpers void writeInlineString(const char* name); diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index d2a648294..4a4c878e9 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(); } @@ -368,18 +369,53 @@ 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); } +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); @@ -1387,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(); |