diff options
-rw-r--r-- | src/tools/wasm-strip.cc | 29 | ||||
-rw-r--r-- | test/strip/keep_section.txt | 34 |
2 files changed, 61 insertions, 2 deletions
diff --git a/src/tools/wasm-strip.cc b/src/tools/wasm-strip.cc index 8d8b33a0..48a83fe5 100644 --- a/src/tools/wasm-strip.cc +++ b/src/tools/wasm-strip.cc @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include <set> #include "wabt/binary-reader-nop.h" #include "wabt/binary-reader.h" @@ -26,6 +27,7 @@ using namespace wabt; static std::string s_filename; static std::string s_outfile; +static std::set<std::string_view> v_sections_to_keep{}; static const char s_description[] = R"( Remove sections of a WebAssembly binary file. @@ -45,12 +47,21 @@ static void ParseOptions(int argc, char** argv) { }); parser.AddOption('o', "output", "FILE", "output wasm binary file", [](const char* argument) { s_outfile = argument; }); + parser.AddOption('k', "keep-section", "SECTION NAME", + "Section name to keep in the final output", + [](const char* value) { + v_sections_to_keep.insert(std::string_view{value}); + }); parser.Parse(argc, argv); } class BinaryReaderStrip : public BinaryReaderNop { public: - explicit BinaryReaderStrip(Errors* errors) : errors_(errors) { + explicit BinaryReaderStrip(std::set<std::string_view> sections_to_keep, + Errors* errors) + : errors_(errors), + sections_to_keep_(sections_to_keep), + section_start_(0) { stream_.WriteU32(WABT_BINARY_MAGIC, "WASM_BINARY_MAGIC"); stream_.WriteU32(WABT_BINARY_VERSION, "WASM_BINARY_VERSION"); } @@ -63,6 +74,7 @@ class BinaryReaderStrip : public BinaryReaderNop { Result BeginSection(Index section_index, BinarySection section_type, Offset size) override { + section_start_ = state->offset; if (section_type == BinarySection::Custom) { return Result::Ok; } @@ -76,9 +88,22 @@ class BinaryReaderStrip : public BinaryReaderNop { return stream_.WriteToFile(filename); } + Result BeginCustomSection(Index section_index, + Offset size, + std::string_view section_name) override { + if (sections_to_keep_.count(section_name) > 0) { + stream_.WriteU8Enum(BinarySection::Custom, "section code"); + WriteU32Leb128(&stream_, size, "section size"); + stream_.WriteData(state->data + section_start_, size, "section data"); + } + return Result::Ok; + } + private: MemoryStream stream_; Errors* errors_; + std::set<std::string_view> sections_to_keep_; + Offset section_start_; }; int ProgramMain(int argc, char** argv) { @@ -102,7 +127,7 @@ int ProgramMain(int argc, char** argv) { ReadBinaryOptions options(features, nullptr, kReadDebugNames, kStopOnFirstError, kFailOnCustomSectionError); - BinaryReaderStrip reader(&errors); + BinaryReaderStrip reader(v_sections_to_keep, &errors); result = ReadBinary(file_data.data(), file_data.size(), &reader, options); FormatErrorsToFile(errors, Location::Type::Binary); if (Failed(result)) { diff --git a/test/strip/keep_section.txt b/test/strip/keep_section.txt new file mode 100644 index 00000000..0b638f36 --- /dev/null +++ b/test/strip/keep_section.txt @@ -0,0 +1,34 @@ +;;; RUN: %(gen_wasm_py)s %(in_file)s -o %(temp_file)s.wasm +;;; RUN: %(wasm-strip)s --keep-section one %(temp_file)s.wasm -o %(temp_file)s_stripped.wasm +;;; RUN: %(wasm-objdump)s -h %(temp_file)s_stripped.wasm +magic +version +section("one") { "Lorem ipsum dolor sit amet," } +section(TYPE) { count[1] function params[0] results[1] i32 } +section("two") { "consectetur adipiscing elit," } +section(FUNCTION) { count[1] type[0] } +section("three") { "sed do eiusmod tempor incididunt" } +section(EXPORT) { count[1] str("main") func_kind func[0] } +section("four") { "ut labore et dolore magna aliqua." } +section(CODE) { + count[1] + func { + locals[0] + i32.const + leb_i32(-420) + return + } +} +section("five") { "Ut enim ad minim veniam," } +(;; STDOUT ;;; + +keep_section_stripped.wasm: file format wasm 0x1 + +Sections: + + Custom start=0x0000000a end=0x00000029 (size=0x0000001f) "one" + Type start=0x0000002b end=0x00000030 (size=0x00000005) count: 1 + Function start=0x00000032 end=0x00000034 (size=0x00000002) count: 1 + Export start=0x00000036 end=0x0000003e (size=0x00000008) count: 1 + Code start=0x00000040 end=0x00000048 (size=0x00000008) count: 1 +;;; STDOUT ;;) |