diff options
-rw-r--r-- | src/passes/Print.cpp | 5 | ||||
-rw-r--r-- | src/wasm-binary.h | 2 | ||||
-rw-r--r-- | src/wasm.h | 10 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 16 | ||||
-rw-r--r-- | test/dylib.wasm | bin | 0 -> 306 bytes | |||
-rw-r--r-- | test/dylib.wasm.fromBinary | 50 |
6 files changed, 79 insertions, 4 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp index 9ead2aee7..c46a0621d 100644 --- a/src/passes/Print.cpp +++ b/src/passes/Print.cpp @@ -735,6 +735,11 @@ struct PrintSExpression : public Visitor<PrintSExpression> { visitFunction(child.get()); o << maybeNewLine; } + for (auto& section : curr->userSections) { + doIndent(o, indent); + o << ";; custom section \"" << section.name << "\", size " << section.data.size(); + o << maybeNewLine; + } decIndent(); o << maybeNewLine; currModule = nullptr; diff --git a/src/wasm-binary.h b/src/wasm-binary.h index ecdde571e..f57d351c3 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -638,7 +638,7 @@ public: WasmBinaryBuilder(Module& wasm, std::vector<char>& input, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug) {} void read(); - void readUserSection(); + void readUserSection(size_t payloadLen); bool more() { return pos < input.size();} uint8_t getInt8(); diff --git a/src/wasm.h b/src/wasm.h index 9807ee63c..4ad0870cc 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1528,6 +1528,14 @@ public: bool mutable_; }; +// "Opaque" data, not part of the core wasm spec, that is held in binaries. +// May be parsed/handled by utility code elsewhere, but not in wasm.h +class UserSection { +public: + std::string name; + std::vector<char> data; +}; + class Module { public: // wasm contents (generally you shouldn't access these from outside, except maybe for iterating; use add*() and the get() functions) @@ -1541,6 +1549,8 @@ public: Memory memory; Name start; + std::vector<UserSection> userSections; + MixedArena allocator; private: diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index f7fa821ea..095a0942f 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -951,7 +951,8 @@ void WasmBinaryBuilder::read() { case BinaryConsts::Section::Data: readDataSegments(); break; case BinaryConsts::Section::Table: readFunctionTableDeclaration(); break; default: { - readUserSection(); + readUserSection(payloadLen); + assert(pos <= oldPos + payloadLen); pos = oldPos + payloadLen; } } @@ -963,12 +964,21 @@ void WasmBinaryBuilder::read() { processFunctions(); } -void WasmBinaryBuilder::readUserSection() { +void WasmBinaryBuilder::readUserSection(size_t payloadLen) { + auto oldPos = pos; Name sectionName = getInlineString(); if (sectionName.equals(BinaryConsts::UserSections::Name)) { readNames(); } else { - std::cerr << "unfamiliar section: " << sectionName << std::endl; + // an unfamiliar custom section + wasm.userSections.resize(wasm.userSections.size() + 1); + auto& section = wasm.userSections.back(); + section.name = sectionName.str; + auto sectionSize = payloadLen - (pos - oldPos); + section.data.resize(sectionSize); + for (size_t i = 0; i < sectionSize; i++) { + section.data[i] = getInt8(); + } } } diff --git a/test/dylib.wasm b/test/dylib.wasm Binary files differnew file mode 100644 index 000000000..06ac55e86 --- /dev/null +++ b/test/dylib.wasm diff --git a/test/dylib.wasm.fromBinary b/test/dylib.wasm.fromBinary new file mode 100644 index 000000000..ac2712bee --- /dev/null +++ b/test/dylib.wasm.fromBinary @@ -0,0 +1,50 @@ +(module + (type $0 (func (param i32) (result i32))) + (type $1 (func (result i32))) + (type $2 (func)) + (import "env" "memoryBase" (global $import$0 i32)) + (import "env" "_puts" (func $import$1 (param i32) (result i32))) + (import "env" "memory" (memory $0 256)) + (import "env" "table" (table 0 anyfunc)) + (import "env" "tableBase" (global $import$4 i32)) + (global $global$0 (mut i32) (i32.const 0)) + (global $global$1 (mut i32) (i32.const 0)) + (global $global$2 i32 (i32.const 0)) + (data (get_global $import$0) "hello, world!") + (export "__post_instantiate" (func $2)) + (export "_main" (func $0)) + (export "runPostSets" (func $1)) + (export "_str" (global $global$2)) + (func $0 (type $1) (result i32) + (block $label$0 i32 + (drop + (call $import$1 + (get_global $import$0) + ) + ) + (i32.const 0) + ) + ) + (func $1 (type $2) + (nop) + ) + (func $2 (type $2) + (block $label$0 + (set_global $global$0 + (i32.add + (get_global $import$0) + (i32.const 16) + ) + ) + (set_global $global$1 + (i32.add + (get_global $global$0) + (i32.const 5242880) + ) + ) + (call $1) + ) + ) + ;; custom section "dylink", size 5 +) + |