summaryrefslogtreecommitdiff
path: root/src/passes/Strip.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-12-03 15:35:35 -0800
committerGitHub <noreply@github.com>2018-12-03 15:35:35 -0800
commitd53c64875ff0367dbc28ccea3d3299809fd8ee36 (patch)
tree770e88d288355451fe1db3bbaebbb27c0532fecd /src/passes/Strip.cpp
parent3d98b5bb767a2bacf791ad000e75d4ed8df1a385 (diff)
downloadbinaryen-d53c64875ff0367dbc28ccea3d3299809fd8ee36.tar.gz
binaryen-d53c64875ff0367dbc28ccea3d3299809fd8ee36.tar.bz2
binaryen-d53c64875ff0367dbc28ccea3d3299809fd8ee36.zip
Add --strip that removes debug info (#1787)
This is sort of like --strip on a native binary. The more specific use case for us is e.g. you link with a library that has -g in its CFLAGS, but you don't want debug info in your final executable (I hit this with poppler now). We can make emcc pass this to binaryen if emcc is not building an output with intended debug info.
Diffstat (limited to 'src/passes/Strip.cpp')
-rw-r--r--src/passes/Strip.cpp60
1 files changed, 60 insertions, 0 deletions
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