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