Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame^] | 1 | //===------ utils/wasm2yaml.cpp - obj2yaml conversion tool ------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "obj2yaml.h" |
| 11 | #include "llvm/Object/COFF.h" |
| 12 | #include "llvm/ObjectYAML/WasmYAML.h" |
| 13 | #include "llvm/Support/ErrorHandling.h" |
| 14 | #include "llvm/Support/YAMLTraits.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | class WasmDumper { |
| 21 | const object::WasmObjectFile &Obj; |
| 22 | |
| 23 | public: |
| 24 | WasmDumper(const object::WasmObjectFile &O) : Obj(O) {} |
| 25 | ErrorOr<WasmYAML::Object *> dump(); |
| 26 | }; |
| 27 | |
| 28 | ErrorOr<WasmYAML::Object *> WasmDumper::dump() { |
| 29 | auto Y = make_unique<WasmYAML::Object>(); |
| 30 | |
| 31 | // Dump header |
| 32 | Y->Header.Version = Obj.getHeader().Version; |
| 33 | |
| 34 | // Dump sections |
| 35 | for (const auto &Sec : Obj.sections()) { |
| 36 | const object::WasmSection &WasmSec = Obj.getWasmSection(Sec); |
| 37 | std::unique_ptr<WasmYAML::Section> S; |
| 38 | switch (WasmSec.Type) { |
| 39 | case wasm::WASM_SEC_CUSTOM: { |
| 40 | if (WasmSec.Name.startswith("reloc.")) { |
| 41 | // Relocations are attached the sections they apply to rather than |
| 42 | // being represented as a custom section in the YAML output. |
| 43 | continue; |
| 44 | } |
| 45 | auto CustomSec = make_unique<WasmYAML::CustomSection>(); |
| 46 | CustomSec->Name = WasmSec.Name; |
| 47 | CustomSec->Payload = yaml::BinaryRef(WasmSec.Content); |
| 48 | S = std::move(CustomSec); |
| 49 | break; |
| 50 | } |
| 51 | case wasm::WASM_SEC_TYPE: { |
| 52 | auto TypeSec = make_unique<WasmYAML::TypeSection>(); |
| 53 | uint32_t Index = 0; |
| 54 | for (const auto &FunctionSig : Obj.types()) { |
| 55 | WasmYAML::Signature Sig; |
| 56 | Sig.Index = Index++; |
| 57 | Sig.ReturnType = FunctionSig.ReturnType; |
| 58 | for (const auto &ParamType : FunctionSig.ParamTypes) |
| 59 | Sig.ParamTypes.push_back(ParamType); |
| 60 | TypeSec->Signatures.push_back(Sig); |
| 61 | } |
| 62 | S = std::move(TypeSec); |
| 63 | break; |
| 64 | } |
| 65 | case wasm::WASM_SEC_IMPORT: { |
| 66 | auto ImportSec = make_unique<WasmYAML::ImportSection>(); |
| 67 | for (auto &Import : Obj.imports()) { |
| 68 | WasmYAML::Import Ex; |
| 69 | Ex.Module = Import.Module; |
| 70 | Ex.Field = Import.Field; |
| 71 | Ex.Kind = Import.Kind; |
| 72 | if (Ex.Kind == wasm::WASM_EXTERNAL_FUNCTION) { |
| 73 | Ex.SigIndex = Import.SigIndex; |
| 74 | } else if (Ex.Kind == wasm::WASM_EXTERNAL_GLOBAL) { |
| 75 | Ex.GlobalType = Import.GlobalType; |
| 76 | Ex.GlobalMutable = Import.GlobalMutable; |
| 77 | } |
| 78 | ImportSec->Imports.push_back(Ex); |
| 79 | } |
| 80 | S = std::move(ImportSec); |
| 81 | break; |
| 82 | } |
| 83 | case wasm::WASM_SEC_FUNCTION: { |
| 84 | auto FuncSec = make_unique<WasmYAML::FunctionSection>(); |
| 85 | for (const auto &Func : Obj.functionTypes()) { |
| 86 | FuncSec->FunctionTypes.push_back(Func); |
| 87 | } |
| 88 | S = std::move(FuncSec); |
| 89 | break; |
| 90 | } |
| 91 | case wasm::WASM_SEC_TABLE: { |
| 92 | auto TableSec = make_unique<WasmYAML::TableSection>(); |
| 93 | for (auto &Table : Obj.tables()) { |
| 94 | WasmYAML::Table T; |
| 95 | T.ElemType = Table.ElemType; |
| 96 | T.TableLimits.Flags = Table.Limits.Flags; |
| 97 | T.TableLimits.Initial = Table.Limits.Initial; |
| 98 | T.TableLimits.Maximum = Table.Limits.Maximum; |
| 99 | TableSec->Tables.push_back(T); |
| 100 | } |
| 101 | S = std::move(TableSec); |
| 102 | break; |
| 103 | } |
| 104 | case wasm::WASM_SEC_MEMORY: { |
| 105 | auto MemorySec = make_unique<WasmYAML::MemorySection>(); |
| 106 | for (auto &Memory : Obj.memories()) { |
| 107 | WasmYAML::Limits L; |
| 108 | L.Flags = Memory.Flags; |
| 109 | L.Initial = Memory.Initial; |
| 110 | L.Maximum = Memory.Maximum; |
| 111 | MemorySec->Memories.push_back(L); |
| 112 | } |
| 113 | S = std::move(MemorySec); |
| 114 | break; |
| 115 | } |
| 116 | case wasm::WASM_SEC_GLOBAL: { |
| 117 | auto GlobalSec = make_unique<WasmYAML::GlobalSection>(); |
| 118 | for (auto &Global : Obj.globals()) { |
| 119 | WasmYAML::Global G; |
| 120 | G.Type = Global.Type; |
| 121 | G.Mutable = Global.Mutable; |
| 122 | G.InitExpr = Global.InitExpr; |
| 123 | GlobalSec->Globals.push_back(G); |
| 124 | } |
| 125 | S = std::move(GlobalSec); |
| 126 | break; |
| 127 | } |
| 128 | case wasm::WASM_SEC_START: { |
| 129 | auto StartSec = make_unique<WasmYAML::StartSection>(); |
| 130 | StartSec->StartFunction = Obj.startFunction(); |
| 131 | S = std::move(StartSec); |
| 132 | break; |
| 133 | } |
| 134 | case wasm::WASM_SEC_EXPORT: { |
| 135 | auto ExportSec = make_unique<WasmYAML::ExportSection>(); |
| 136 | for (auto &Export : Obj.exports()) { |
| 137 | WasmYAML::Export Ex; |
| 138 | Ex.Name = Export.Name; |
| 139 | Ex.Kind = Export.Kind; |
| 140 | Ex.Index = Export.Index; |
| 141 | ExportSec->Exports.push_back(Ex); |
| 142 | } |
| 143 | S = std::move(ExportSec); |
| 144 | break; |
| 145 | } |
| 146 | case wasm::WASM_SEC_ELEM: { |
| 147 | auto ElemSec = make_unique<WasmYAML::ElemSection>(); |
| 148 | for (auto &Segment : Obj.elements()) { |
| 149 | WasmYAML::ElemSegment Seg; |
| 150 | Seg.TableIndex = Segment.TableIndex; |
| 151 | Seg.Offset = Segment.Offset; |
| 152 | for (auto &Func : Segment.Functions) { |
| 153 | Seg.Functions.push_back(Func); |
| 154 | } |
| 155 | ElemSec->Segments.push_back(Seg); |
| 156 | } |
| 157 | S = std::move(ElemSec); |
| 158 | break; |
| 159 | } |
| 160 | case wasm::WASM_SEC_CODE: { |
| 161 | auto CodeSec = make_unique<WasmYAML::CodeSection>(); |
| 162 | for (auto &Func : Obj.functions()) { |
| 163 | WasmYAML::Function Function; |
| 164 | for (auto &Local : Func.Locals) { |
| 165 | WasmYAML::LocalDecl LocalDecl; |
| 166 | LocalDecl.Type = Local.Type; |
| 167 | LocalDecl.Count = Local.Count; |
| 168 | Function.Locals.push_back(LocalDecl); |
| 169 | } |
| 170 | Function.Body = yaml::BinaryRef(Func.Body); |
| 171 | CodeSec->Functions.push_back(Function); |
| 172 | } |
| 173 | S = std::move(CodeSec); |
| 174 | break; |
| 175 | } |
| 176 | case wasm::WASM_SEC_DATA: { |
| 177 | auto DataSec = make_unique<WasmYAML::DataSection>(); |
| 178 | for (auto &Segment : Obj.dataSegments()) { |
| 179 | WasmYAML::DataSegment Seg; |
| 180 | Seg.Index = Segment.Index; |
| 181 | Seg.Offset = Segment.Offset; |
| 182 | Seg.Content = yaml::BinaryRef(Segment.Content); |
| 183 | DataSec->Segments.push_back(Seg); |
| 184 | } |
| 185 | S = std::move(DataSec); |
| 186 | break; |
| 187 | } |
| 188 | default: |
| 189 | llvm_unreachable("Unknown section type"); |
| 190 | break; |
| 191 | } |
| 192 | for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) { |
| 193 | WasmYAML::Relocation R; |
| 194 | R.Type = Reloc.Type; |
| 195 | R.Index = Reloc.Index; |
| 196 | R.Offset = Reloc.Offset; |
| 197 | R.Addend = Reloc.Addend; |
| 198 | S->Relocations.push_back(R); |
| 199 | } |
| 200 | Y->Sections.push_back(std::move(S)); |
| 201 | } |
| 202 | |
| 203 | return Y.release(); |
| 204 | } |
| 205 | |
| 206 | } // namespace |
| 207 | |
| 208 | std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) { |
| 209 | WasmDumper Dumper(Obj); |
| 210 | ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump(); |
| 211 | if (std::error_code EC = YAMLOrErr.getError()) |
| 212 | return EC; |
| 213 | |
| 214 | std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get()); |
| 215 | yaml::Output Yout(Out); |
| 216 | Yout << *YAML; |
| 217 | |
| 218 | return std::error_code(); |
| 219 | } |