summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tools/wasm-strip.cc44
-rw-r--r--test/strip/out_file.txt33
2 files changed, 59 insertions, 18 deletions
diff --git a/src/tools/wasm-strip.cc b/src/tools/wasm-strip.cc
index d80251f9..f7abd5ff 100644
--- a/src/tools/wasm-strip.cc
+++ b/src/tools/wasm-strip.cc
@@ -25,6 +25,7 @@
using namespace wabt;
static std::string s_filename;
+static std::string s_outfile;
static const char s_description[] =
R"( Remove sections of a WebAssembly binary file.
@@ -42,6 +43,8 @@ static void ParseOptions(int argc, char** argv) {
s_filename = argument;
ConvertBackslashToSlash(&s_filename);
});
+ parser.AddOption('o', "output", "FILE", "output wasm binary file",
+ [](const char* argument) { s_outfile = argument; });
parser.Parse(argc, argv);
}
@@ -86,25 +89,30 @@ int ProgramMain(int argc, char** argv) {
std::vector<uint8_t> file_data;
result = ReadFile(s_filename.c_str(), &file_data);
- if (Succeeded(result)) {
- Errors errors;
- Features features;
- features.EnableAll();
- const bool kReadDebugNames = false;
- const bool kStopOnFirstError = true;
- const bool kFailOnCustomSectionError = false;
- ReadBinaryOptions options(features, nullptr, kReadDebugNames,
- kStopOnFirstError, kFailOnCustomSectionError);
-
- BinaryReaderStrip reader(&errors);
- result = ReadBinary(file_data.data(), file_data.size(), &reader, options);
- FormatErrorsToFile(errors, Location::Type::Binary);
-
- if (Succeeded(result)) {
- result = reader.WriteToFile(s_filename);
- }
+ if (Failed(result)) {
+ return Result::Error;
+ }
+
+ Errors errors;
+ Features features;
+ features.EnableAll();
+ const bool kReadDebugNames = false;
+ const bool kStopOnFirstError = true;
+ const bool kFailOnCustomSectionError = false;
+ ReadBinaryOptions options(features, nullptr, kReadDebugNames,
+ kStopOnFirstError, kFailOnCustomSectionError);
+
+ BinaryReaderStrip reader(&errors);
+ result = ReadBinary(file_data.data(), file_data.size(), &reader, options);
+ FormatErrorsToFile(errors, Location::Type::Binary);
+ if (Failed(result)) {
+ return Result::Error;
+ }
+
+ if (s_outfile.empty()) {
+ s_outfile = s_filename;
}
- return result != Result::Ok;
+ return reader.WriteToFile(s_outfile);
}
int main(int argc, char** argv) {
diff --git a/test/strip/out_file.txt b/test/strip/out_file.txt
new file mode 100644
index 00000000..6add5d85
--- /dev/null
+++ b/test/strip/out_file.txt
@@ -0,0 +1,33 @@
+;;; RUN: %(gen_wasm_py)s %(in_file)s -o %(temp_file)s.wasm
+;;; RUN: %(wasm-strip)s %(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 ;;;
+
+out_file_stripped.wasm: file format wasm 0x1
+
+Sections:
+
+ Type start=0x0000000a end=0x0000000f (size=0x00000005) count: 1
+ Function start=0x00000011 end=0x00000013 (size=0x00000002) count: 1
+ Export start=0x00000015 end=0x0000001d (size=0x00000008) count: 1
+ Code start=0x0000001f end=0x00000027 (size=0x00000008) count: 1
+;;; STDOUT ;;)