Skip to content

Commit ddb5acd

Browse files
authored
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.
1 parent 5f1afa5 commit ddb5acd

16 files changed

+97
-13
lines changed

src/passes/Strip.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616

1717
//
18-
// Similar to strip-ing a native binary, this removes debug info
19-
// and related things like source map URLs, names section, etc.
18+
// Similar to strip-ing a native binary, this family of passes can
19+
// removes debug info and other things.
2020
//
2121

22+
#include <functional>
23+
2224
#include "wasm.h"
2325
#include "wasm-binary.h"
2426
#include "pass.h"
@@ -28,19 +30,20 @@ using namespace std;
2830
namespace wasm {
2931

3032
struct Strip : public Pass {
33+
// A function that returns true if the method should be removed.
34+
typedef std::function<bool (UserSection&)> Decider;
35+
Decider decider;
36+
37+
Strip(Decider decider) : decider(decider) {}
38+
3139
void run(PassRunner* runner, Module* module) override {
3240
// Remove name and debug sections.
3341
auto& sections = module->userSections;
3442
sections.erase(
3543
std::remove_if(
3644
sections.begin(),
3745
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-
}
46+
decider
4447
),
4548
sections.end()
4649
);
@@ -53,8 +56,19 @@ struct Strip : public Pass {
5356
}
5457
};
5558

56-
Pass *createStripPass() {
57-
return new Strip();
59+
Pass *createStripDebugPass() {
60+
return new Strip([&](const UserSection& curr) {
61+
return curr.name == BinaryConsts::UserSections::Name ||
62+
curr.name == BinaryConsts::UserSections::SourceMapUrl ||
63+
curr.name.find(".debug") == 0 ||
64+
curr.name.find("reloc..debug") == 0;
65+
});
66+
}
67+
68+
Pass *createStripProducersPass() {
69+
return new Strip([&](const UserSection& curr) {
70+
return curr.name == BinaryConsts::UserSections::Producers;
71+
});
5872
}
5973

6074
} // namespace wasm

src/passes/pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ void PassRegistry::registerPasses() {
133133
registerPass("souperify-single-use", "emit Souper IR in text form (single-use nodes only)", createSouperifySingleUsePass);
134134
registerPass("spill-pointers", "spill pointers to the C stack (useful for Boehm-style GC)", createSpillPointersPass);
135135
registerPass("ssa", "ssa-ify variables so that they have a single assignment", createSSAifyPass);
136-
registerPass("strip", "strip debug info (including the names section)", createStripPass);
136+
registerPass("strip", "deprecated; same as strip-debug", createStripDebugPass);
137+
registerPass("strip-debug", "strip debug info (including the names section)", createStripDebugPass);
138+
registerPass("strip-producers", "strip the wasm producers section", createStripProducersPass);
137139
registerPass("trap-mode-clamp", "replace trapping operations with clamping semantics", createTrapModeClamp);
138140
registerPass("trap-mode-js", "replace trapping operations with js semantics", createTrapModeJS);
139141
registerPass("untee", "removes local.tees, replacing them with sets and gets", createUnteePass);

src/passes/passes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ Pass* createSimplifyLocalsNoNestingPass();
8484
Pass* createSimplifyLocalsNoTeePass();
8585
Pass* createSimplifyLocalsNoStructurePass();
8686
Pass* createSimplifyLocalsNoTeeNoStructurePass();
87-
Pass* createStripPass();
87+
Pass* createStripDebugPass();
88+
Pass* createStripProducersPass();
8889
Pass* createSouperifyPass();
8990
Pass* createSouperifySingleUsePass();
9091
Pass* createSpillPointersPass();

src/wasm-binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ extern const char* Name;
344344
extern const char* SourceMapUrl;
345345
extern const char* Dylink;
346346
extern const char* Linking;
347+
extern const char* Producers;
347348

348349
enum Subsection {
349350
NameFunction = 1,

src/wasm/wasm-binary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ void WasmBinaryBuilder::readUserSection(size_t payloadLen) {
719719
} else {
720720
// an unfamiliar custom section
721721
if (sectionName.equals(BinaryConsts::UserSections::Linking)) {
722-
std::cerr << "warning: linking section is present, which binaryen cannot handle yet - relocations will be invalidated!\n";
722+
std::cerr << "warning: linking section is present, so this is not a standard wasm file - binaryen cannot handle this properly!\n";
723723
}
724724
wasm.userSections.resize(wasm.userSections.size() + 1);
725725
auto& section = wasm.userSections.back();

src/wasm/wasm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const char* Name = "name";
3131
const char* SourceMapUrl = "sourceMappingURL";
3232
const char* Dylink = "dylink";
3333
const char* Linking = "linking";
34+
const char* Producers = "producers";
3435
}
3536
}
3637

test/metadatas.wasm

261 Bytes
Binary file not shown.

test/metadatas.wasm.fromBinary

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(type $0 (func))
3+
(export "a" (func $0))
4+
(func $0 (; 0 ;) (type $0)
5+
(nop)
6+
)
7+
;; custom section "emscripten_metadata", size 7
8+
;; custom section "producers", size 187
9+
)
10+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
total
2+
[funcs] : 1
3+
[total] : 1
4+
nop : 1
5+
total
6+
[funcs] : 1
7+
[total] : 1
8+
nop : 1
9+
(module
10+
(type $0 (func))
11+
(export "a" (func $0))
12+
(func $0 (; 0 ;) (type $0)
13+
(nop)
14+
)
15+
;; custom section "emscripten_metadata", size 7
16+
;; custom section "producers", size 187
17+
)
261 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)