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; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 17 | using object::WasmSection; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class WasmDumper { |
| 22 | const object::WasmObjectFile &Obj; |
| 23 | |
| 24 | public: |
| 25 | WasmDumper(const object::WasmObjectFile &O) : Obj(O) {} |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 26 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 27 | ErrorOr<WasmYAML::Object *> dump(); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 28 | |
| 29 | std::unique_ptr<WasmYAML::CustomSection> |
| 30 | dumpCustomSection(const WasmSection &WasmSec); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 33 | } // namespace |
| 34 | |
| 35 | static WasmYAML::Table make_table(const wasm::WasmTable &Table) { |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 36 | WasmYAML::Table T; |
| 37 | T.ElemType = Table.ElemType; |
| 38 | T.TableLimits.Flags = Table.Limits.Flags; |
| 39 | T.TableLimits.Initial = Table.Limits.Initial; |
| 40 | T.TableLimits.Maximum = Table.Limits.Maximum; |
| 41 | return T; |
| 42 | } |
| 43 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 44 | static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) { |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 45 | WasmYAML::Limits L; |
| 46 | L.Flags = Limits.Flags; |
| 47 | L.Initial = Limits.Initial; |
| 48 | L.Maximum = Limits.Maximum; |
| 49 | return L; |
| 50 | } |
| 51 | |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 52 | std::unique_ptr<WasmYAML::CustomSection> WasmDumper::dumpCustomSection(const WasmSection &WasmSec) { |
| 53 | std::unique_ptr<WasmYAML::CustomSection> CustomSec; |
| 54 | if (WasmSec.Name == "name") { |
| 55 | std::unique_ptr<WasmYAML::NameSection> NameSec = make_unique<WasmYAML::NameSection>(); |
| 56 | for (const object::SymbolRef& Sym: Obj.symbols()) { |
Sam Clegg | 31a2c80 | 2017-09-20 21:17:04 +0000 | [diff] [blame] | 57 | const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym); |
| 58 | if (Symbol.Type != object::WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME) |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 59 | continue; |
| 60 | WasmYAML::NameEntry NameEntry; |
Sam Clegg | 31a2c80 | 2017-09-20 21:17:04 +0000 | [diff] [blame] | 61 | NameEntry.Name = Symbol.Name; |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 62 | NameEntry.Index = Sym.getValue(); |
| 63 | NameSec->FunctionNames.push_back(NameEntry); |
| 64 | } |
| 65 | CustomSec = std::move(NameSec); |
| 66 | } else if (WasmSec.Name == "linking") { |
| 67 | std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = make_unique<WasmYAML::LinkingSection>(); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 68 | size_t Index = 0; |
| 69 | for (const object::WasmSegment &Segment : Obj.dataSegments()) { |
| 70 | if (!Segment.Data.Name.empty()) { |
Sam Clegg | 63ebb81 | 2017-09-29 16:50:08 +0000 | [diff] [blame] | 71 | WasmYAML::SegmentInfo SegmentInfo; |
| 72 | SegmentInfo.Name = Segment.Data.Name; |
| 73 | SegmentInfo.Index = Index; |
| 74 | SegmentInfo.Alignment = Segment.Data.Alignment; |
| 75 | SegmentInfo.Flags = Segment.Data.Flags; |
| 76 | LinkingSec->SegmentInfos.push_back(SegmentInfo); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 77 | } |
| 78 | Index++; |
| 79 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 80 | for (const object::SymbolRef& Sym: Obj.symbols()) { |
| 81 | const object::WasmSymbol Symbol = Obj.getWasmSymbol(Sym); |
| 82 | if (Symbol.Flags != 0) { |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 83 | WasmYAML::SymbolInfo Info{Symbol.Name, Symbol.Flags}; |
| 84 | LinkingSec->SymbolInfos.emplace_back(Info); |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Sam Clegg | 9e1ade9 | 2017-06-27 20:27:59 +0000 | [diff] [blame] | 87 | LinkingSec->DataSize = Obj.linkingData().DataSize; |
Sam Clegg | 4273998 | 2017-12-14 21:10:03 +0000 | [diff] [blame] | 88 | for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) { |
| 89 | WasmYAML::InitFunction F{Func.Priority, Func.FunctionIndex}; |
| 90 | LinkingSec->InitFunctions.emplace_back(F); |
| 91 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 92 | CustomSec = std::move(LinkingSec); |
| 93 | } else { |
| 94 | CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name); |
| 95 | } |
| 96 | CustomSec->Payload = yaml::BinaryRef(WasmSec.Content); |
| 97 | return CustomSec; |
| 98 | } |
| 99 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 100 | ErrorOr<WasmYAML::Object *> WasmDumper::dump() { |
| 101 | auto Y = make_unique<WasmYAML::Object>(); |
| 102 | |
| 103 | // Dump header |
| 104 | Y->Header.Version = Obj.getHeader().Version; |
| 105 | |
| 106 | // Dump sections |
| 107 | for (const auto &Sec : Obj.sections()) { |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 108 | const WasmSection &WasmSec = Obj.getWasmSection(Sec); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 109 | std::unique_ptr<WasmYAML::Section> S; |
| 110 | switch (WasmSec.Type) { |
| 111 | case wasm::WASM_SEC_CUSTOM: { |
| 112 | if (WasmSec.Name.startswith("reloc.")) { |
| 113 | // Relocations are attached the sections they apply to rather than |
| 114 | // being represented as a custom section in the YAML output. |
| 115 | continue; |
| 116 | } |
Sam Clegg | b7787fd | 2017-06-20 04:04:59 +0000 | [diff] [blame] | 117 | S = dumpCustomSection(WasmSec); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 118 | break; |
| 119 | } |
| 120 | case wasm::WASM_SEC_TYPE: { |
| 121 | auto TypeSec = make_unique<WasmYAML::TypeSection>(); |
| 122 | uint32_t Index = 0; |
| 123 | for (const auto &FunctionSig : Obj.types()) { |
| 124 | WasmYAML::Signature Sig; |
| 125 | Sig.Index = Index++; |
| 126 | Sig.ReturnType = FunctionSig.ReturnType; |
| 127 | for (const auto &ParamType : FunctionSig.ParamTypes) |
| 128 | Sig.ParamTypes.push_back(ParamType); |
| 129 | TypeSec->Signatures.push_back(Sig); |
| 130 | } |
| 131 | S = std::move(TypeSec); |
| 132 | break; |
| 133 | } |
| 134 | case wasm::WASM_SEC_IMPORT: { |
| 135 | auto ImportSec = make_unique<WasmYAML::ImportSection>(); |
| 136 | for (auto &Import : Obj.imports()) { |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 137 | WasmYAML::Import Im; |
| 138 | Im.Module = Import.Module; |
| 139 | Im.Field = Import.Field; |
| 140 | Im.Kind = Import.Kind; |
| 141 | switch (Im.Kind) { |
| 142 | case wasm::WASM_EXTERNAL_FUNCTION: |
| 143 | Im.SigIndex = Import.SigIndex; |
| 144 | break; |
| 145 | case wasm::WASM_EXTERNAL_GLOBAL: |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 146 | Im.GlobalImport.Type = Import.Global.Type; |
| 147 | Im.GlobalImport.Mutable = Import.Global.Mutable; |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 148 | break; |
| 149 | case wasm::WASM_EXTERNAL_TABLE: |
Sam Clegg | 41db519 | 2017-05-10 00:14:04 +0000 | [diff] [blame] | 150 | Im.TableImport = make_table(Import.Table); |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 151 | break; |
| 152 | case wasm::WASM_EXTERNAL_MEMORY: |
| 153 | Im.Memory = make_limits(Import.Memory); |
| 154 | break; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 155 | } |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 156 | ImportSec->Imports.push_back(Im); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 157 | } |
| 158 | S = std::move(ImportSec); |
| 159 | break; |
| 160 | } |
| 161 | case wasm::WASM_SEC_FUNCTION: { |
| 162 | auto FuncSec = make_unique<WasmYAML::FunctionSection>(); |
| 163 | for (const auto &Func : Obj.functionTypes()) { |
| 164 | FuncSec->FunctionTypes.push_back(Func); |
| 165 | } |
| 166 | S = std::move(FuncSec); |
| 167 | break; |
| 168 | } |
| 169 | case wasm::WASM_SEC_TABLE: { |
| 170 | auto TableSec = make_unique<WasmYAML::TableSection>(); |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 171 | for (const wasm::WasmTable &Table : Obj.tables()) { |
| 172 | TableSec->Tables.push_back(make_table(Table)); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 173 | } |
| 174 | S = std::move(TableSec); |
| 175 | break; |
| 176 | } |
| 177 | case wasm::WASM_SEC_MEMORY: { |
| 178 | auto MemorySec = make_unique<WasmYAML::MemorySection>(); |
Sam Clegg | 2ffff5a | 2017-05-09 23:48:41 +0000 | [diff] [blame] | 179 | for (const wasm::WasmLimits &Memory : Obj.memories()) { |
| 180 | MemorySec->Memories.push_back(make_limits(Memory)); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 181 | } |
| 182 | S = std::move(MemorySec); |
| 183 | break; |
| 184 | } |
| 185 | case wasm::WASM_SEC_GLOBAL: { |
| 186 | auto GlobalSec = make_unique<WasmYAML::GlobalSection>(); |
| 187 | for (auto &Global : Obj.globals()) { |
| 188 | WasmYAML::Global G; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame^] | 189 | G.Index = Global.Index; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 190 | G.Type = Global.Type; |
| 191 | G.Mutable = Global.Mutable; |
| 192 | G.InitExpr = Global.InitExpr; |
| 193 | GlobalSec->Globals.push_back(G); |
| 194 | } |
| 195 | S = std::move(GlobalSec); |
| 196 | break; |
| 197 | } |
| 198 | case wasm::WASM_SEC_START: { |
| 199 | auto StartSec = make_unique<WasmYAML::StartSection>(); |
| 200 | StartSec->StartFunction = Obj.startFunction(); |
| 201 | S = std::move(StartSec); |
| 202 | break; |
| 203 | } |
| 204 | case wasm::WASM_SEC_EXPORT: { |
| 205 | auto ExportSec = make_unique<WasmYAML::ExportSection>(); |
| 206 | for (auto &Export : Obj.exports()) { |
| 207 | WasmYAML::Export Ex; |
| 208 | Ex.Name = Export.Name; |
| 209 | Ex.Kind = Export.Kind; |
| 210 | Ex.Index = Export.Index; |
| 211 | ExportSec->Exports.push_back(Ex); |
| 212 | } |
| 213 | S = std::move(ExportSec); |
| 214 | break; |
| 215 | } |
| 216 | case wasm::WASM_SEC_ELEM: { |
| 217 | auto ElemSec = make_unique<WasmYAML::ElemSection>(); |
| 218 | for (auto &Segment : Obj.elements()) { |
| 219 | WasmYAML::ElemSegment Seg; |
| 220 | Seg.TableIndex = Segment.TableIndex; |
| 221 | Seg.Offset = Segment.Offset; |
| 222 | for (auto &Func : Segment.Functions) { |
| 223 | Seg.Functions.push_back(Func); |
| 224 | } |
| 225 | ElemSec->Segments.push_back(Seg); |
| 226 | } |
| 227 | S = std::move(ElemSec); |
| 228 | break; |
| 229 | } |
| 230 | case wasm::WASM_SEC_CODE: { |
| 231 | auto CodeSec = make_unique<WasmYAML::CodeSection>(); |
| 232 | for (auto &Func : Obj.functions()) { |
| 233 | WasmYAML::Function Function; |
Sam Clegg | e53af7f | 2018-01-09 21:38:53 +0000 | [diff] [blame^] | 234 | Function.Index = Func.Index; |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 235 | for (auto &Local : Func.Locals) { |
| 236 | WasmYAML::LocalDecl LocalDecl; |
| 237 | LocalDecl.Type = Local.Type; |
| 238 | LocalDecl.Count = Local.Count; |
| 239 | Function.Locals.push_back(LocalDecl); |
| 240 | } |
| 241 | Function.Body = yaml::BinaryRef(Func.Body); |
| 242 | CodeSec->Functions.push_back(Function); |
| 243 | } |
| 244 | S = std::move(CodeSec); |
| 245 | break; |
| 246 | } |
| 247 | case wasm::WASM_SEC_DATA: { |
| 248 | auto DataSec = make_unique<WasmYAML::DataSection>(); |
Sam Clegg | d95ed95 | 2017-09-20 19:03:35 +0000 | [diff] [blame] | 249 | for (const object::WasmSegment &Segment : Obj.dataSegments()) { |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 250 | WasmYAML::DataSegment Seg; |
Sam Clegg | 9c07f94 | 2017-07-12 00:24:54 +0000 | [diff] [blame] | 251 | Seg.SectionOffset = Segment.SectionOffset; |
| 252 | Seg.MemoryIndex = Segment.Data.MemoryIndex; |
| 253 | Seg.Offset = Segment.Data.Offset; |
| 254 | Seg.Content = yaml::BinaryRef(Segment.Data.Content); |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 255 | DataSec->Segments.push_back(Seg); |
| 256 | } |
| 257 | S = std::move(DataSec); |
| 258 | break; |
| 259 | } |
| 260 | default: |
| 261 | llvm_unreachable("Unknown section type"); |
| 262 | break; |
| 263 | } |
| 264 | for (const wasm::WasmRelocation &Reloc: WasmSec.Relocations) { |
| 265 | WasmYAML::Relocation R; |
| 266 | R.Type = Reloc.Type; |
| 267 | R.Index = Reloc.Index; |
| 268 | R.Offset = Reloc.Offset; |
| 269 | R.Addend = Reloc.Addend; |
| 270 | S->Relocations.push_back(R); |
| 271 | } |
| 272 | Y->Sections.push_back(std::move(S)); |
| 273 | } |
| 274 | |
| 275 | return Y.release(); |
| 276 | } |
| 277 | |
Derek Schuff | d3d84fd | 2017-03-30 19:44:09 +0000 | [diff] [blame] | 278 | std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) { |
| 279 | WasmDumper Dumper(Obj); |
| 280 | ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump(); |
| 281 | if (std::error_code EC = YAMLOrErr.getError()) |
| 282 | return EC; |
| 283 | |
| 284 | std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get()); |
| 285 | yaml::Output Yout(Out); |
| 286 | Yout << *YAML; |
| 287 | |
| 288 | return std::error_code(); |
| 289 | } |