|
| 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 |
0 commit comments