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 family of passes can |
19 | // removes debug info and other things. |
20 | // |
21 | |
22 | #include <functional> |
23 | |
24 | #include "pass.h" |
25 | #include "wasm-binary.h" |
26 | #include "wasm.h" |
27 | |
28 | namespace wasm { |
29 | |
30 | struct Strip : public Pass { |
31 | bool requiresNonNullableLocalFixups() override { return false; } |
32 | |
33 | // A function that returns true if the method should be removed. |
34 | using Decider = std::function<bool(CustomSection&)>; |
35 | Decider decider; |
36 | |
37 | Strip(Decider decider) : decider(decider) {} |
38 | |
39 | void run(Module* module) override { |
40 | // Remove name and debug sections. |
41 | auto& sections = module->customSections; |
42 | sections.erase(std::remove_if(sections.begin(), sections.end(), decider), |
43 | sections.end()); |
44 | // If we're cleaning up debug info, clear on the function and module too. |
45 | CustomSection temp; |
46 | temp.name = BinaryConsts::CustomSections::Name; |
47 | if (decider(temp)) { |
48 | module->clearDebugInfo(); |
49 | for (auto& func : module->functions) { |
50 | func->clearNames(); |
51 | func->clearDebugInfo(); |
52 | } |
53 | } |
54 | } |
55 | }; |
56 | |
57 | Pass* createStripDebugPass() { |
58 | return new Strip([&](const CustomSection& curr) { |
59 | return curr.name == BinaryConsts::CustomSections::Name || |
60 | curr.name == BinaryConsts::CustomSections::SourceMapUrl || |
61 | curr.name.find(".debug" ) == 0 || curr.name.find("reloc..debug" ) == 0; |
62 | }); |
63 | } |
64 | |
65 | Pass* createStripDWARFPass() { |
66 | return new Strip([&](const CustomSection& curr) { |
67 | return curr.name.find(".debug" ) == 0 || curr.name.find("reloc..debug" ) == 0; |
68 | }); |
69 | } |
70 | |
71 | Pass* createStripProducersPass() { |
72 | return new Strip([&](const CustomSection& curr) { |
73 | return curr.name == BinaryConsts::CustomSections::Producers; |
74 | }); |
75 | } |
76 | |
77 | } // namespace wasm |
78 | |