diff options
author | Yuri Iozzelli <y.iozzelli@gmail.com> | 2022-02-25 16:36:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-25 15:36:35 +0000 |
commit | 1f59b65d8befc8512ff9045bb371ea5ec379a78c (patch) | |
tree | 349621c048c7716a746d3361c8ec6219d32f0250 /src/binary-reader-objdump.cc | |
parent | 08cf71aa180674432750a02581f1e214c310042d (diff) | |
download | wabt-1f59b65d8befc8512ff9045bb371ea5ec379a78c.tar.gz wabt-1f59b65d8befc8512ff9045bb371ea5ec379a78c.tar.bz2 wabt-1f59b65d8befc8512ff9045bb371ea5ec379a78c.zip |
Add initial support for code metadata (#1840)
See https://github.com/WebAssembly/tool-conventions/blob/main/CodeMetadata.md for the specification.
In particular this pr implements the following:
- Parsing code metadata sections in BinaryReader, providing appropriate callbacks that a BinaryReaderDelegate can implement:
- BinaryReaderObjdump: show the sections in a human-readable form
- BinaryReaderIr: add code metadata in the IR as expressions
- Parsing code metadata annotations in text format, adding them in the IR like the BinaryReaderIR does
- Writing the code metadata present in the IR in the proper sections when converting IR to binary
- Support in wasm-decompiler for showing code metadata as comments in the pseudo-code
All the features have corresponding tests.
Support for code metadata is gated through the --enable-code-metadata feature. For reading/writing in the text format, --enable-annotations is also required.
Missing features:
Support for function-level code metadata (offset 0)
Extensive validation in validator.cc (like making sure that all metadata instances are at the same code offset of an instruction)
Diffstat (limited to 'src/binary-reader-objdump.cc')
-rw-r--r-- | src/binary-reader-objdump.cc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc index deb1167f..cb682698 100644 --- a/src/binary-reader-objdump.cc +++ b/src/binary-reader-objdump.cc @@ -1139,6 +1139,10 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase { Result OnF32ConstExpr(uint32_t value) override; Result OnF64ConstExpr(uint64_t value) override; Result OnGlobalGetExpr(Index global_index) override; + Result OnCodeMetadataCount(Index function_index, Index count) override; + Result OnCodeMetadata(Offset code_offset, + const void* data, + Address size) override; private: Result InitExprToConstOffset(const InitExpr& expr, uint64_t* out_offset); @@ -2192,6 +2196,31 @@ Result BinaryReaderObjdump::OnTagType(Index index, Index sig_index) { return Result::Ok; } +Result BinaryReaderObjdump::OnCodeMetadataCount(Index function_index, + Index count) { + if (!ShouldPrintDetails()) { + return Result::Ok; + } + printf(" - func[%" PRIindex "]", function_index); + auto name = GetFunctionName(function_index); + if (!name.empty()) { + printf(" <" PRIstringview ">", WABT_PRINTF_STRING_VIEW_ARG(name)); + } + printf(":\n"); + return Result::Ok; +} +Result BinaryReaderObjdump::OnCodeMetadata(Offset code_offset, + const void* data, + Address size) { + if (!ShouldPrintDetails()) { + return Result::Ok; + } + printf(" - meta[%" PRIzx "]:\n", code_offset); + + out_stream_->WriteMemoryDump(data, size, 0, PrintChars::Yes, " - "); + return Result::Ok; +} + } // end anonymous namespace std::string_view ObjdumpNames::Get(Index index) const { |