Skip to content

Commit d53c648

Browse files
authored
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.
1 parent 3d98b5b commit d53c648

File tree

10 files changed

+100
-0
lines changed

10 files changed

+100
-0
lines changed

build-js.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ echo "building shared bitcode"
138138
$BINARYEN_SRC/passes/SpillPointers.cpp \
139139
$BINARYEN_SRC/passes/SSAify.cpp \
140140
$BINARYEN_SRC/passes/StackIR.cpp \
141+
$BINARYEN_SRC/passes/Strip.cpp \
141142
$BINARYEN_SRC/passes/TrapMode.cpp \
142143
$BINARYEN_SRC/passes/Untee.cpp \
143144
$BINARYEN_SRC/passes/Vacuum.cpp \

src/passes/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SET(passes_SOURCES
3737
Print.cpp
3838
PrintCallGraph.cpp
3939
StackIR.cpp
40+
Strip.cpp
4041
RedundantSetElimination.cpp
4142
RelooperJumpThreading.cpp
4243
ReReloop.cpp

src/passes/Strip.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2018 WebAssembly Community Group participants
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//
18+
// Similar to strip-ing a native binary, this removes debug info
19+
// and related things like source map URLs, names section, etc.
20+
//
21+
22+
#include "wasm.h"
23+
#include "wasm-binary.h"
24+
#include "pass.h"
25+
26+
using namespace std;
27+
28+
namespace wasm {
29+
30+
struct Strip : public Pass {
31+
void run(PassRunner* runner, Module* module) override {
32+
// Remove name and debug sections.
33+
auto& sections = module->userSections;
34+
sections.erase(
35+
std::remove_if(
36+
sections.begin(),
37+
sections.end(),
38+
[&](const UserSection& curr) {
39+
return curr.name == BinaryConsts::UserSections::Name ||
40+
curr.name == BinaryConsts::UserSections::SourceMapUrl ||
41+
curr.name.find(".debug") == 0 ||
42+
curr.name.find("reloc..debug") == 0;
43+
}
44+
),
45+
sections.end()
46+
);
47+
// Clean up internal data structures.
48+
module->clearDebugInfo();
49+
for (auto& func : module->functions) {
50+
func->clearNames();
51+
func->clearDebugInfo();
52+
}
53+
}
54+
};
55+
56+
Pass *createStripPass() {
57+
return new Strip();
58+
}
59+
60+
} // namespace wasm

src/passes/pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void PassRegistry::registerPasses() {
131131
registerPass("souperify-single-use", "emit Souper IR in text form (single-use nodes only)", createSouperifySingleUsePass);
132132
registerPass("spill-pointers", "spill pointers to the C stack (useful for Boehm-style GC)", createSpillPointersPass);
133133
registerPass("ssa", "ssa-ify variables so that they have a single assignment", createSSAifyPass);
134+
registerPass("strip", "strip debug info (including the names section)", createStripPass);
134135
registerPass("trap-mode-clamp", "replace trapping operations with clamping semantics", createTrapModeClamp);
135136
registerPass("trap-mode-js", "replace trapping operations with js semantics", createTrapModeJS);
136137
registerPass("untee", "removes tee_locals, replacing them with sets and gets", createUnteePass);

src/passes/passes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Pass* createSimplifyLocalsNoNestingPass();
8282
Pass* createSimplifyLocalsNoTeePass();
8383
Pass* createSimplifyLocalsNoStructurePass();
8484
Pass* createSimplifyLocalsNoTeeNoStructurePass();
85+
Pass* createStripPass();
8586
Pass* createSouperifyPass();
8687
Pass* createSouperifySingleUsePass();
8788
Pass* createSpillPointersPass();

src/tools/wasm-reduce.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ struct Reducer : public WalkerPass<PostWalker<Reducer, UnifiedExpressionVisitor<
259259
"--reorder-functions",
260260
"--reorder-locals",
261261
"--simplify-locals --vacuum",
262+
"--strip",
262263
"--vacuum"
263264
};
264265
auto oldSize = file_size(working);

src/wasm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ class Function : public Importable {
642642
Name getLocalNameOrGeneric(Index index);
643643

644644
bool hasLocalName(Index index) const;
645+
646+
void clearNames();
647+
void clearDebugInfo();
645648
};
646649

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

794797
void updateMaps();
798+
799+
void clearDebugInfo();
795800
};
796801

797802
} // namespace wasm

src/wasm/wasm.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,17 @@ Type Function::getLocalType(Index index) {
638638
}
639639
}
640640

641+
void Function::clearNames() {
642+
localNames.clear();
643+
}
644+
645+
void Function::clearDebugInfo() {
646+
localIndices.clear();
647+
debugLocations.clear();
648+
prologLocation.clear();
649+
epilogLocation.clear();
650+
}
651+
641652
FunctionType* Module::getFunctionType(Name name) {
642653
auto iter = functionTypesMap.find(name);
643654
if (iter == functionTypesMap.end()) {
@@ -811,4 +822,8 @@ void Module::updateMaps() {
811822
}
812823
}
813824

825+
void Module::clearDebugInfo() {
826+
debugInfoFileNames.clear();
827+
}
828+
814829
} // namespace wasm

test/passes/strip.bin.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(module
2+
(type $0 (func (result i32)))
3+
(import "env" "__linear_memory" (memory $0 0))
4+
(import "env" "__indirect_function_table" (table $timport$1 0 anyfunc))
5+
(func $0 (; 0 ;) (type $0) (result i32)
6+
(local $0 i32)
7+
(set_local $0
8+
(i32.const 1)
9+
)
10+
(return
11+
(get_local $0)
12+
)
13+
)
14+
;; custom section "zinking", size 28
15+
)

test/passes/strip.wasm

771 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)