Skip to content

Commit

Permalink
Add --strip that removes debug info (#1787)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kripken authored Dec 3, 2018
1 parent 3d98b5b commit d53c648
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions build-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions src/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SET(passes_SOURCES
Print.cpp
PrintCallGraph.cpp
StackIR.cpp
Strip.cpp
RedundantSetElimination.cpp
RelooperJumpThreading.cpp
ReReloop.cpp
Expand Down
60 changes: 60 additions & 0 deletions src/passes/Strip.cpp
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/passes/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Pass* createSimplifyLocalsNoNestingPass();
Pass* createSimplifyLocalsNoTeePass();
Pass* createSimplifyLocalsNoStructurePass();
Pass* createSimplifyLocalsNoTeeNoStructurePass();
Pass* createStripPass();
Pass* createSouperifyPass();
Pass* createSouperifySingleUsePass();
Pass* createSpillPointersPass();
Expand Down
1 change: 1 addition & 0 deletions src/tools/wasm-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,9 @@ class Function : public Importable {
Name getLocalNameOrGeneric(Index index);

bool hasLocalName(Index index) const;

void clearNames();
void clearDebugInfo();
};

// The kind of an import or export.
Expand Down Expand Up @@ -792,6 +795,8 @@ class Module {
void removeGlobal(Name name);

void updateMaps();

void clearDebugInfo();
};

} // namespace wasm
Expand Down
15 changes: 15 additions & 0 deletions src/wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -811,4 +822,8 @@ void Module::updateMaps() {
}
}

void Module::clearDebugInfo() {
debugInfoFileNames.clear();
}

} // namespace wasm
15 changes: 15 additions & 0 deletions test/passes/strip.bin.txt
Original file line number Diff line number Diff line change
@@ -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
)
Binary file added test/passes/strip.wasm
Binary file not shown.

0 comments on commit d53c648

Please sign in to comment.