diff options
author | Alon Zakai <alonzakai@gmail.com> | 2019-01-31 09:35:32 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-31 09:35:32 -0800 |
commit | ddb5acd34238967a2487a70926849c89f38a35e2 (patch) | |
tree | 35be190a17fc8b1d9a0824dd5a4376f46d53c9a0 | |
parent | 5f1afa58d22dce1088c63eff690283d8c615feee (diff) | |
download | binaryen-ddb5acd34238967a2487a70926849c89f38a35e2.tar.gz binaryen-ddb5acd34238967a2487a70926849c89f38a35e2.tar.bz2 binaryen-ddb5acd34238967a2487a70926849c89f38a35e2.zip |
Strip the producers section in --strip-producers (#1875)
WebAssembly/tool-conventions#93 has a summary of emscripten's current thinking on this. For Binaryen, we don't want to do anything to the producers section by default, but do want it to be possible to optionally remove it. To achieve that, this PR
* creates a --strip-producers pass that removes that section.
* creates a --strip-debug pass that removes debug info, same as the old --strip, which is still around but deprecated.
A followup in emscripten will use this pass by default.
-rw-r--r-- | src/passes/Strip.cpp | 34 | ||||
-rw-r--r-- | src/passes/pass.cpp | 4 | ||||
-rw-r--r-- | src/passes/passes.h | 3 | ||||
-rw-r--r-- | src/wasm-binary.h | 1 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 2 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 1 | ||||
-rw-r--r-- | test/metadatas.wasm | bin | 0 -> 261 bytes | |||
-rw-r--r-- | test/metadatas.wasm.fromBinary | 10 | ||||
-rw-r--r-- | test/passes/metrics_strip-debug_metrics.bin.txt | 17 | ||||
-rw-r--r-- | test/passes/metrics_strip-debug_metrics.wasm | bin | 0 -> 261 bytes | |||
-rw-r--r-- | test/passes/metrics_strip-producers_metrics.bin.txt | 16 | ||||
-rw-r--r-- | test/passes/metrics_strip-producers_metrics.wasm | bin | 0 -> 261 bytes | |||
-rw-r--r-- | test/passes/strip-debug.bin.txt (renamed from test/passes/strip.bin.txt) | 0 | ||||
-rw-r--r-- | test/passes/strip-debug.wasm (renamed from test/passes/strip.wasm) | bin | 771 -> 771 bytes | |||
-rw-r--r-- | test/passes/strip-producers.bin.txt | 22 | ||||
-rw-r--r-- | test/passes/strip-producers.wasm | bin | 0 -> 771 bytes |
16 files changed, 97 insertions, 13 deletions
diff --git a/src/passes/Strip.cpp b/src/passes/Strip.cpp index d2ebbe129..8ab712cb1 100644 --- a/src/passes/Strip.cpp +++ b/src/passes/Strip.cpp @@ -15,10 +15,12 @@ */ // -// Similar to strip-ing a native binary, this removes debug info -// and related things like source map URLs, names section, etc. +// Similar to strip-ing a native binary, this family of passes can +// removes debug info and other things. // +#include <functional> + #include "wasm.h" #include "wasm-binary.h" #include "pass.h" @@ -28,6 +30,12 @@ using namespace std; namespace wasm { struct Strip : public Pass { + // A function that returns true if the method should be removed. + typedef std::function<bool (UserSection&)> Decider; + Decider decider; + + Strip(Decider decider) : decider(decider) {} + void run(PassRunner* runner, Module* module) override { // Remove name and debug sections. auto& sections = module->userSections; @@ -35,12 +43,7 @@ struct Strip : public Pass { std::remove_if( sections.begin(), sections.end(), - [&](const UserSection& curr) { - return curr.name == BinaryConsts::UserSections::Name || - curr.name == BinaryConsts::UserSections::SourceMapUrl || - curr.name.find(".debug") == 0 || - curr.name.find("reloc..debug") == 0; - } + decider ), sections.end() ); @@ -53,8 +56,19 @@ struct Strip : public Pass { } }; -Pass *createStripPass() { - return new Strip(); +Pass *createStripDebugPass() { + return new Strip([&](const UserSection& curr) { + return curr.name == BinaryConsts::UserSections::Name || + curr.name == BinaryConsts::UserSections::SourceMapUrl || + curr.name.find(".debug") == 0 || + curr.name.find("reloc..debug") == 0; + }); +} + +Pass *createStripProducersPass() { + return new Strip([&](const UserSection& curr) { + return curr.name == BinaryConsts::UserSections::Producers; + }); } } // namespace wasm diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index cae69860a..2872575c2 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -133,7 +133,9 @@ void PassRegistry::registerPasses() { registerPass("souperify-single-use", "emit Souper IR in text form (single-use nodes only)", createSouperifySingleUsePass); registerPass("spill-pointers", "spill pointers to the C stack (useful for Boehm-style GC)", createSpillPointersPass); registerPass("ssa", "ssa-ify variables so that they have a single assignment", createSSAifyPass); - registerPass("strip", "strip debug info (including the names section)", createStripPass); + registerPass("strip", "deprecated; same as strip-debug", createStripDebugPass); + registerPass("strip-debug", "strip debug info (including the names section)", createStripDebugPass); + registerPass("strip-producers", "strip the wasm producers section", createStripProducersPass); registerPass("trap-mode-clamp", "replace trapping operations with clamping semantics", createTrapModeClamp); registerPass("trap-mode-js", "replace trapping operations with js semantics", createTrapModeJS); registerPass("untee", "removes local.tees, replacing them with sets and gets", createUnteePass); diff --git a/src/passes/passes.h b/src/passes/passes.h index b04303429..15493f313 100644 --- a/src/passes/passes.h +++ b/src/passes/passes.h @@ -84,7 +84,8 @@ Pass* createSimplifyLocalsNoNestingPass(); Pass* createSimplifyLocalsNoTeePass(); Pass* createSimplifyLocalsNoStructurePass(); Pass* createSimplifyLocalsNoTeeNoStructurePass(); -Pass* createStripPass(); +Pass* createStripDebugPass(); +Pass* createStripProducersPass(); Pass* createSouperifyPass(); Pass* createSouperifySingleUsePass(); Pass* createSpillPointersPass(); diff --git a/src/wasm-binary.h b/src/wasm-binary.h index a5b1e10b4..2402375dc 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -344,6 +344,7 @@ extern const char* Name; extern const char* SourceMapUrl; extern const char* Dylink; extern const char* Linking; +extern const char* Producers; enum Subsection { NameFunction = 1, diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 187328e58..90a588bb9 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -719,7 +719,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) { } else { // an unfamiliar custom section if (sectionName.equals(BinaryConsts::UserSections::Linking)) { - std::cerr << "warning: linking section is present, which binaryen cannot handle yet - relocations will be invalidated!\n"; + std::cerr << "warning: linking section is present, so this is not a standard wasm file - binaryen cannot handle this properly!\n"; } wasm.userSections.resize(wasm.userSections.size() + 1); auto& section = wasm.userSections.back(); diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 757bc1828..0d5f3d5b5 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -31,6 +31,7 @@ const char* Name = "name"; const char* SourceMapUrl = "sourceMappingURL"; const char* Dylink = "dylink"; const char* Linking = "linking"; +const char* Producers = "producers"; } } diff --git a/test/metadatas.wasm b/test/metadatas.wasm Binary files differnew file mode 100644 index 000000000..ea797306f --- /dev/null +++ b/test/metadatas.wasm diff --git a/test/metadatas.wasm.fromBinary b/test/metadatas.wasm.fromBinary new file mode 100644 index 000000000..2e6531678 --- /dev/null +++ b/test/metadatas.wasm.fromBinary @@ -0,0 +1,10 @@ +(module + (type $0 (func)) + (export "a" (func $0)) + (func $0 (; 0 ;) (type $0) + (nop) + ) + ;; custom section "emscripten_metadata", size 7 + ;; custom section "producers", size 187 +) + diff --git a/test/passes/metrics_strip-debug_metrics.bin.txt b/test/passes/metrics_strip-debug_metrics.bin.txt new file mode 100644 index 000000000..dbb3dcc37 --- /dev/null +++ b/test/passes/metrics_strip-debug_metrics.bin.txt @@ -0,0 +1,17 @@ +total + [funcs] : 1 + [total] : 1 + nop : 1 +total + [funcs] : 1 + [total] : 1 + nop : 1 +(module + (type $0 (func)) + (export "a" (func $0)) + (func $0 (; 0 ;) (type $0) + (nop) + ) + ;; custom section "emscripten_metadata", size 7 + ;; custom section "producers", size 187 +) diff --git a/test/passes/metrics_strip-debug_metrics.wasm b/test/passes/metrics_strip-debug_metrics.wasm Binary files differnew file mode 100644 index 000000000..ea797306f --- /dev/null +++ b/test/passes/metrics_strip-debug_metrics.wasm diff --git a/test/passes/metrics_strip-producers_metrics.bin.txt b/test/passes/metrics_strip-producers_metrics.bin.txt new file mode 100644 index 000000000..6ddfa5892 --- /dev/null +++ b/test/passes/metrics_strip-producers_metrics.bin.txt @@ -0,0 +1,16 @@ +total + [funcs] : 1 + [total] : 1 + nop : 1 +total + [funcs] : 1 + [total] : 1 + nop : 1 +(module + (type $0 (func)) + (export "a" (func $0)) + (func $0 (; 0 ;) (type $0) + (nop) + ) + ;; custom section "emscripten_metadata", size 7 +) diff --git a/test/passes/metrics_strip-producers_metrics.wasm b/test/passes/metrics_strip-producers_metrics.wasm Binary files differnew file mode 100644 index 000000000..ea797306f --- /dev/null +++ b/test/passes/metrics_strip-producers_metrics.wasm diff --git a/test/passes/strip.bin.txt b/test/passes/strip-debug.bin.txt index 7160e69f2..7160e69f2 100644 --- a/test/passes/strip.bin.txt +++ b/test/passes/strip-debug.bin.txt diff --git a/test/passes/strip.wasm b/test/passes/strip-debug.wasm Binary files differindex 59065683c..59065683c 100644 --- a/test/passes/strip.wasm +++ b/test/passes/strip-debug.wasm diff --git a/test/passes/strip-producers.bin.txt b/test/passes/strip-producers.bin.txt new file mode 100644 index 000000000..dcc8ab9b4 --- /dev/null +++ b/test/passes/strip-producers.bin.txt @@ -0,0 +1,22 @@ +(module + (type $0 (func (result i32))) + (import "env" "__linear_memory" (memory $0 0)) + (import "env" "__indirect_function_table" (table $timport$1 0 funcref)) + (func $0 (; 0 ;) (type $0) (result i32) + (local $0 i32) + (local.set $0 + (i32.const 1) + ) + (return + (local.get $0) + ) + ) + ;; custom section ".debug_str", size 246 + ;; custom section ".debug_abbrev", size 52 + ;; custom section ".debug_info", size 69 + ;; custom section ".debug_macinfo", size 1 + ;; custom section ".debug_line", size 56 + ;; custom section "zinking", size 28 + ;; custom section "reloc..debug_info", size 47 + ;; custom section "reloc..debug_line", size 6 +) diff --git a/test/passes/strip-producers.wasm b/test/passes/strip-producers.wasm Binary files differnew file mode 100644 index 000000000..59065683c --- /dev/null +++ b/test/passes/strip-producers.wasm |