|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | +#include "wasm-debug.h" |
| 18 | +#include "wasm.h" |
| 19 | + |
| 20 | +#ifdef BUILD_LLVM_DWARF |
| 21 | +#include "llvm/ObjectYAML/DWARFEmitter.h" |
| 22 | +#include "llvm/ObjectYAML/DWARFYAML.h" |
| 23 | +#include "llvm/include/llvm/DebugInfo/DWARFContext.h" |
| 24 | + |
| 25 | +std::error_code dwarf2yaml(llvm::DWARFContext& DCtx, llvm::DWARFYAML::Data& Y); |
| 26 | +#endif |
| 27 | + |
| 28 | +namespace wasm { |
| 29 | + |
| 30 | +namespace Debug { |
| 31 | + |
| 32 | +bool isDWARFSection(Name name) { return name.startsWith(".debug_"); } |
| 33 | + |
| 34 | +bool hasDWARFSections(const Module& wasm) { |
| 35 | + for (auto& section : wasm.userSections) { |
| 36 | + if (isDWARFSection(section.name)) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + } |
| 40 | + return false; |
| 41 | +} |
| 42 | + |
| 43 | +#ifdef BUILD_LLVM_DWARF |
| 44 | + |
| 45 | +struct BinaryenDWARFInfo { |
| 46 | + llvm::StringMap<std::unique_ptr<llvm::MemoryBuffer>> sections; |
| 47 | + std::unique_ptr<llvm::DWARFContext> context; |
| 48 | + |
| 49 | + BinaryenDWARFInfo(const Module& wasm) { |
| 50 | + // Get debug sections from the wasm. |
| 51 | + for (auto& section : wasm.userSections) { |
| 52 | + if (Name(section.name).startsWith(".debug_")) { |
| 53 | + // TODO: efficiency |
| 54 | + sections[section.name.substr(1)] = llvm::MemoryBuffer::getMemBufferCopy( |
| 55 | + llvm::StringRef(section.data.data(), section.data.size())); |
| 56 | + } |
| 57 | + } |
| 58 | + // Parse debug sections. |
| 59 | + uint8_t addrSize = 4; |
| 60 | + context = llvm::DWARFContext::create(sections, addrSize); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +void dumpDWARF(const Module& wasm) { |
| 65 | + BinaryenDWARFInfo info(wasm); |
| 66 | + std::cout << "DWARF debug info\n"; |
| 67 | + std::cout << "================\n\n"; |
| 68 | + for (auto& section : wasm.userSections) { |
| 69 | + if (Name(section.name).startsWith(".debug_")) { |
| 70 | + std::cout << "Contains section " << section.name << " (" |
| 71 | + << section.data.size() << " bytes)\n"; |
| 72 | + } |
| 73 | + } |
| 74 | + llvm::DIDumpOptions options; |
| 75 | + options.Verbose = true; |
| 76 | + info.context->dump(llvm::outs(), options); |
| 77 | +} |
| 78 | + |
| 79 | +// |
| 80 | +// Big picture: We use a DWARFContext to read data, then DWARFYAML support |
| 81 | +// code to write it. That is not the main LLVM Dwarf code used for writing |
| 82 | +// object files, but it avoids us create a "fake" MC layer, and provides a |
| 83 | +// simple way to write out the debug info. Likely the level of info represented |
| 84 | +// in the DWARFYAML::Data object is sufficient for Binaryen's needs, but if not, |
| 85 | +// we may need a different approach. |
| 86 | +// |
| 87 | +// In more detail: |
| 88 | +// |
| 89 | +// 1. Binary sections => DWARFContext: |
| 90 | +// |
| 91 | +// llvm::DWARFContext::create(sections..) |
| 92 | +// |
| 93 | +// 2. DWARFContext => DWARFYAML::Data |
| 94 | +// |
| 95 | +// std::error_code dwarf2yaml(DWARFContext &DCtx, DWARFYAML::Data &Y) { |
| 96 | +// |
| 97 | +// 3. DWARFYAML::Data => binary sections |
| 98 | +// |
| 99 | +// StringMap<std::unique_ptr<MemoryBuffer>> |
| 100 | +// EmitDebugSections(llvm::DWARFYAML::Data &DI, bool ApplyFixups); |
| 101 | +// |
| 102 | +// For modifying data, like line numberes, we can in theory do that either on |
| 103 | +// the DWARFContext or DWARFYAML::Data; unclear which is best, but modifying |
| 104 | +// the DWARFContext may save us doing fixups in EmitDebugSections. |
| 105 | +// |
| 106 | + |
| 107 | +void writeDWARFSections(Module& wasm) { |
| 108 | + BinaryenDWARFInfo info(wasm); |
| 109 | + |
| 110 | + // Convert to Data representation, which YAML can use to write. |
| 111 | + llvm::DWARFYAML::Data Data; |
| 112 | + if (dwarf2yaml(*info.context, Data)) { |
| 113 | + Fatal() << "Failed to parse DWARF to YAML"; |
| 114 | + } |
| 115 | + |
| 116 | + // TODO: Actually update, and remove sections we don't know how to update yet? |
| 117 | + |
| 118 | + // Convert to binary sections. |
| 119 | + auto newSections = EmitDebugSections( |
| 120 | + Data, |
| 121 | + false /* ApplyFixups, should be true if we modify Data, presumably? */); |
| 122 | + |
| 123 | + // Update the custom sections in the wasm. |
| 124 | + // TODO: efficiency |
| 125 | + for (auto& section : wasm.userSections) { |
| 126 | + if (Name(section.name).startsWith(".debug_")) { |
| 127 | + auto llvmName = section.name.substr(1); |
| 128 | + if (newSections.count(llvmName)) { |
| 129 | + auto llvmData = newSections[llvmName]->getBuffer(); |
| 130 | + section.data.resize(llvmData.size()); |
| 131 | + std::copy(llvmData.begin(), llvmData.end(), section.data.data()); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +#else // BUILD_LLVM_DWARF |
| 138 | + |
| 139 | +void dumpDWARF(const Module& wasm) { |
| 140 | + std::cerr << "warning: no DWARF dumping support present\n"; |
| 141 | +} |
| 142 | + |
| 143 | +void writeDWARFSections(Module& wasm) { |
| 144 | + std::cerr << "warning: no DWARF updating support present\n"; |
| 145 | +} |
| 146 | + |
| 147 | +#endif // BUILD_LLVM_DWARF |
| 148 | + |
| 149 | +} // namespace Debug |
| 150 | + |
| 151 | +} // namespace wasm |
0 commit comments