summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild-js.sh1
-rw-r--r--src/passes/CMakeLists.txt1
-rw-r--r--src/passes/Strip.cpp60
-rw-r--r--src/passes/pass.cpp1
-rw-r--r--src/passes/passes.h1
-rw-r--r--src/tools/wasm-reduce.cpp1
-rw-r--r--src/wasm.h5
-rw-r--r--src/wasm/wasm.cpp15
-rw-r--r--test/passes/strip.bin.txt15
-rw-r--r--test/passes/strip.wasmbin0 -> 771 bytes
10 files changed, 100 insertions, 0 deletions
diff --git a/build-js.sh b/build-js.sh
index 4b9e56ecb..9aa4105f6 100755
--- a/build-js.sh
+++ b/build-js.sh
@@ -138,6 +138,7 @@ echo "building shared bitcode"
$BINARYEN_SRC/passes/SpillPointers.cpp \
$BINARYEN_SRC/passes/SSAify.cpp \
$BINARYEN_SRC/passes/StackIR.cpp \
+ $BINARYEN_SRC/passes/Strip.cpp \
$BINARYEN_SRC/passes/TrapMode.cpp \
$BINARYEN_SRC/passes/Untee.cpp \
$BINARYEN_SRC/passes/Vacuum.cpp \
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt
index c6507c7e9..b4e396750 100644
--- a/src/passes/CMakeLists.txt
+++ b/src/passes/CMakeLists.txt
@@ -37,6 +37,7 @@ SET(passes_SOURCES
Print.cpp
PrintCallGraph.cpp
StackIR.cpp
+ Strip.cpp
RedundantSetElimination.cpp
RelooperJumpThreading.cpp
ReReloop.cpp
diff --git a/src/passes/Strip.cpp b/src/passes/Strip.cpp
new file mode 100644
index 000000000..d2ebbe129
--- /dev/null
+++ b/src/passes/Strip.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2018 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//
+// Similar to strip-ing a native binary, this removes debug info
+// and related things like source map URLs, names section, etc.
+//
+
+#include "wasm.h"
+#include "wasm-binary.h"
+#include "pass.h"
+
+using namespace std;
+
+namespace wasm {
+
+struct Strip : public Pass {
+ void run(PassRunner* runner, Module* module) override {
+ // Remove name and debug sections.
+ auto& sections = module->userSections;
+ sections.erase(
+ 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;
+ }
+ ),
+ sections.end()
+ );
+ // Clean up internal data structures.
+ module->clearDebugInfo();
+ for (auto& func : module->functions) {
+ func->clearNames();
+ func->clearDebugInfo();
+ }
+ }
+};
+
+Pass *createStripPass() {
+ return new Strip();
+}
+
+} // namespace wasm
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index d2478eaba..e89fed83f 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -131,6 +131,7 @@ 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("trap-mode-clamp", "replace trapping operations with clamping semantics", createTrapModeClamp);
registerPass("trap-mode-js", "replace trapping operations with js semantics", createTrapModeJS);
registerPass("untee", "removes tee_locals, replacing them with sets and gets", createUnteePass);
diff --git a/src/passes/passes.h b/src/passes/passes.h
index ec40a60f2..8f5ca7e0a 100644
--- a/src/passes/passes.h
+++ b/src/passes/passes.h
@@ -82,6 +82,7 @@ Pass* createSimplifyLocalsNoNestingPass();
Pass* createSimplifyLocalsNoTeePass();
Pass* createSimplifyLocalsNoStructurePass();
Pass* createSimplifyLocalsNoTeeNoStructurePass();
+Pass* createStripPass();
Pass* createSouperifyPass();
Pass* createSouperifySingleUsePass();
Pass* createSpillPointersPass();
diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp
index b7dbc79f3..5c1f98d88 100644
--- a/src/tools/wasm-reduce.cpp
+++ b/src/tools/wasm-reduce.cpp
@@ -259,6 +259,7 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
"--reorder-functions",
"--reorder-locals",
"--simplify-locals --vacuum",
+ "--strip",
"--vacuum"
};
auto oldSize = file_size(working);
diff --git a/src/wasm.h b/src/wasm.h
index 2e09ef088..424f89d0b 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -642,6 +642,9 @@ public:
Name getLocalNameOrGeneric(Index index);
bool hasLocalName(Index index) const;
+
+ void clearNames();
+ void clearDebugInfo();
};
// The kind of an import or export.
@@ -792,6 +795,8 @@ public:
void removeGlobal(Name name);
void updateMaps();
+
+ void clearDebugInfo();
};
} // namespace wasm
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp
index 3373a3f20..e0a9cff93 100644
--- a/src/wasm/wasm.cpp
+++ b/src/wasm/wasm.cpp
@@ -638,6 +638,17 @@ Type Function::getLocalType(Index index) {
}
}
+void Function::clearNames() {
+ localNames.clear();
+}
+
+void Function::clearDebugInfo() {
+ localIndices.clear();
+ debugLocations.clear();
+ prologLocation.clear();
+ epilogLocation.clear();
+}
+
FunctionType* Module::getFunctionType(Name name) {
auto iter = functionTypesMap.find(name);
if (iter == functionTypesMap.end()) {
@@ -811,4 +822,8 @@ void Module::updateMaps() {
}
}
+void Module::clearDebugInfo() {
+ debugInfoFileNames.clear();
+}
+
} // namespace wasm
diff --git a/test/passes/strip.bin.txt b/test/passes/strip.bin.txt
new file mode 100644
index 000000000..e11a0a6f7
--- /dev/null
+++ b/test/passes/strip.bin.txt
@@ -0,0 +1,15 @@
+(module
+ (type $0 (func (result i32)))
+ (import "env" "__linear_memory" (memory $0 0))
+ (import "env" "__indirect_function_table" (table $timport$1 0 anyfunc))
+ (func $0 (; 0 ;) (type $0) (result i32)
+ (local $0 i32)
+ (set_local $0
+ (i32.const 1)
+ )
+ (return
+ (get_local $0)
+ )
+ )
+ ;; custom section "zinking", size 28
+)
diff --git a/test/passes/strip.wasm b/test/passes/strip.wasm
new file mode 100644
index 000000000..59065683c
--- /dev/null
+++ b/test/passes/strip.wasm
Binary files differ