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 using object::WasmSection; 18 19 namespace { 20 21 class WasmDumper { 22 const object::WasmObjectFile &Obj; 23 24 public: 25 WasmDumper(const object::WasmObjectFile &O) : Obj(O) {} 26 27 ErrorOr<WasmYAML::Object *> dump(); 28 29 std::unique_ptr<WasmYAML::CustomSection> 30 dumpCustomSection(const WasmSection &WasmSec); 31 }; 32 33 } // namespace 34 35 static WasmYAML::Table make_table(const wasm::WasmTable &Table) { 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 44 static WasmYAML::Limits make_limits(const wasm::WasmLimits &Limits) { 45 WasmYAML::Limits L; 46 L.Flags = Limits.Flags; 47 L.Initial = Limits.Initial; 48 L.Maximum = Limits.Maximum; 49 return L; 50 } 51 52 std::unique_ptr<WasmYAML::CustomSection> 53 WasmDumper::dumpCustomSection(const WasmSection &WasmSec) { 54 std::unique_ptr<WasmYAML::CustomSection> CustomSec; 55 if (WasmSec.Name == "dylink") { 56 std::unique_ptr<WasmYAML::DylinkSection> DylinkSec = 57 make_unique<WasmYAML::DylinkSection>(); 58 const wasm::WasmDylinkInfo& Info = Obj.dylinkInfo(); 59 DylinkSec->MemorySize = Info.MemorySize; 60 DylinkSec->MemoryAlignment = Info.MemoryAlignment; 61 DylinkSec->TableSize = Info.TableSize; 62 DylinkSec->TableAlignment = Info.TableAlignment; 63 CustomSec = std::move(DylinkSec); 64 } else if (WasmSec.Name == "name") { 65 std::unique_ptr<WasmYAML::NameSection> NameSec = 66 make_unique<WasmYAML::NameSection>(); 67 for (const llvm::wasm::WasmFunctionName &Func : Obj.debugNames()) { 68 WasmYAML::NameEntry NameEntry; 69 NameEntry.Name = Func.Name; 70 NameEntry.Index = Func.Index; 71 NameSec->FunctionNames.push_back(NameEntry); 72 } 73 CustomSec = std::move(NameSec); 74 } else if (WasmSec.Name == "linking") { 75 std::unique_ptr<WasmYAML::LinkingSection> LinkingSec = 76 make_unique<WasmYAML::LinkingSection>(); 77 LinkingSec->Version = Obj.linkingData().Version; 78 79 ArrayRef<StringRef> Comdats = Obj.linkingData().Comdats; 80 for (StringRef ComdatName : Comdats) 81 LinkingSec->Comdats.emplace_back(WasmYAML::Comdat{ComdatName, {}}); 82 for (auto &Func : Obj.functions()) { 83 if (Func.Comdat != UINT32_MAX) { 84 LinkingSec->Comdats[Func.Comdat].Entries.emplace_back( 85 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_FUNCTION, Func.Index}); 86 } 87 } 88 89 uint32_t SegmentIndex = 0; 90 for (const object::WasmSegment &Segment : Obj.dataSegments()) { 91 if (!Segment.Data.Name.empty()) { 92 WasmYAML::SegmentInfo SegmentInfo; 93 SegmentInfo.Name = Segment.Data.Name; 94 SegmentInfo.Index = SegmentIndex; 95 SegmentInfo.Alignment = Segment.Data.Alignment; 96 SegmentInfo.Flags = Segment.Data.Flags; 97 LinkingSec->SegmentInfos.push_back(SegmentInfo); 98 } 99 if (Segment.Data.Comdat != UINT32_MAX) { 100 LinkingSec->Comdats[Segment.Data.Comdat].Entries.emplace_back( 101 WasmYAML::ComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex}); 102 } 103 SegmentIndex++; 104 } 105 106 uint32_t SymbolIndex = 0; 107 for (const wasm::WasmSymbolInfo &Symbol : Obj.linkingData().SymbolTable) { 108 WasmYAML::SymbolInfo Info; 109 Info.Index = SymbolIndex++; 110 Info.Kind = static_cast<uint32_t>(Symbol.Kind); 111 Info.Name = Symbol.Name; 112 Info.Flags = Symbol.Flags; 113 switch (Symbol.Kind) { 114 case wasm::WASM_SYMBOL_TYPE_DATA: 115 Info.DataRef = Symbol.DataRef; 116 break; 117 case wasm::WASM_SYMBOL_TYPE_FUNCTION: 118 case wasm::WASM_SYMBOL_TYPE_GLOBAL: 119 case wasm::WASM_SYMBOL_TYPE_EVENT: 120 Info.ElementIndex = Symbol.ElementIndex; 121 break; 122 case wasm::WASM_SYMBOL_TYPE_SECTION: 123 Info.ElementIndex = Symbol.ElementIndex; 124 break; 125 } 126 LinkingSec->SymbolTable.emplace_back(Info); 127 } 128 129 for (const wasm::WasmInitFunc &Func : Obj.linkingData().InitFunctions) { 130 WasmYAML::InitFunction F{Func.Priority, Func.Symbol}; 131 LinkingSec->InitFunctions.emplace_back(F); 132 } 133 134 CustomSec = std::move(LinkingSec); 135 } else { 136 CustomSec = make_unique<WasmYAML::CustomSection>(WasmSec.Name); 137 } 138 CustomSec->Payload = yaml::BinaryRef(WasmSec.Content); 139 return CustomSec; 140 } 141 142 ErrorOr<WasmYAML::Object *> WasmDumper::dump() { 143 auto Y = make_unique<WasmYAML::Object>(); 144 145 // Dump header 146 Y->Header.Version = Obj.getHeader().Version; 147 148 // Dump sections 149 for (const auto &Sec : Obj.sections()) { 150 const WasmSection &WasmSec = Obj.getWasmSection(Sec); 151 std::unique_ptr<WasmYAML::Section> S; 152 switch (WasmSec.Type) { 153 case wasm::WASM_SEC_CUSTOM: { 154 if (WasmSec.Name.startswith("reloc.")) { 155 // Relocations are attached the sections they apply to rather than 156 // being represented as a custom section in the YAML output. 157 continue; 158 } 159 S = dumpCustomSection(WasmSec); 160 break; 161 } 162 case wasm::WASM_SEC_TYPE: { 163 auto TypeSec = make_unique<WasmYAML::TypeSection>(); 164 uint32_t Index = 0; 165 for (const auto &FunctionSig : Obj.types()) { 166 WasmYAML::Signature Sig; 167 Sig.Index = Index++; 168 Sig.ReturnType = wasm::WASM_TYPE_NORESULT; 169 assert(FunctionSig.Returns.size() <= 1 && 170 "Functions with multiple returns are not supported"); 171 if (FunctionSig.Returns.size()) 172 Sig.ReturnType = static_cast<uint32_t>(FunctionSig.Returns[0]); 173 for (const auto &ParamType : FunctionSig.Params) 174 Sig.ParamTypes.push_back(static_cast<uint32_t>(ParamType)); 175 TypeSec->Signatures.push_back(Sig); 176 } 177 S = std::move(TypeSec); 178 break; 179 } 180 case wasm::WASM_SEC_IMPORT: { 181 auto ImportSec = make_unique<WasmYAML::ImportSection>(); 182 for (auto &Import : Obj.imports()) { 183 WasmYAML::Import Im; 184 Im.Module = Import.Module; 185 Im.Field = Import.Field; 186 Im.Kind = Import.Kind; 187 switch (Im.Kind) { 188 case wasm::WASM_EXTERNAL_FUNCTION: 189 Im.SigIndex = Import.SigIndex; 190 break; 191 case wasm::WASM_EXTERNAL_GLOBAL: 192 Im.GlobalImport.Type = Import.Global.Type; 193 Im.GlobalImport.Mutable = Import.Global.Mutable; 194 break; 195 case wasm::WASM_EXTERNAL_EVENT: 196 Im.EventImport.Attribute = Import.Event.Attribute; 197 Im.EventImport.SigIndex = Import.Event.SigIndex; 198 break; 199 case wasm::WASM_EXTERNAL_TABLE: 200 Im.TableImport = make_table(Import.Table); 201 break; 202 case wasm::WASM_EXTERNAL_MEMORY: 203 Im.Memory = make_limits(Import.Memory); 204 break; 205 } 206 ImportSec->Imports.push_back(Im); 207 } 208 S = std::move(ImportSec); 209 break; 210 } 211 case wasm::WASM_SEC_FUNCTION: { 212 auto FuncSec = make_unique<WasmYAML::FunctionSection>(); 213 for (const auto &Func : Obj.functionTypes()) { 214 FuncSec->FunctionTypes.push_back(Func); 215 } 216 S = std::move(FuncSec); 217 break; 218 } 219 case wasm::WASM_SEC_TABLE: { 220 auto TableSec = make_unique<WasmYAML::TableSection>(); 221 for (const wasm::WasmTable &Table : Obj.tables()) { 222 TableSec->Tables.push_back(make_table(Table)); 223 } 224 S = std::move(TableSec); 225 break; 226 } 227 case wasm::WASM_SEC_MEMORY: { 228 auto MemorySec = make_unique<WasmYAML::MemorySection>(); 229 for (const wasm::WasmLimits &Memory : Obj.memories()) { 230 MemorySec->Memories.push_back(make_limits(Memory)); 231 } 232 S = std::move(MemorySec); 233 break; 234 } 235 case wasm::WASM_SEC_GLOBAL: { 236 auto GlobalSec = make_unique<WasmYAML::GlobalSection>(); 237 for (auto &Global : Obj.globals()) { 238 WasmYAML::Global G; 239 G.Index = Global.Index; 240 G.Type = Global.Type.Type; 241 G.Mutable = Global.Type.Mutable; 242 G.InitExpr = Global.InitExpr; 243 GlobalSec->Globals.push_back(G); 244 } 245 S = std::move(GlobalSec); 246 break; 247 } 248 case wasm::WASM_SEC_EVENT: { 249 auto EventSec = make_unique<WasmYAML::EventSection>(); 250 for (auto &Event : Obj.events()) { 251 WasmYAML::Event E; 252 E.Index = Event.Index; 253 E.Attribute = Event.Type.Attribute; 254 E.SigIndex = Event.Type.SigIndex; 255 EventSec->Events.push_back(E); 256 } 257 S = std::move(EventSec); 258 break; 259 } 260 case wasm::WASM_SEC_START: { 261 auto StartSec = make_unique<WasmYAML::StartSection>(); 262 StartSec->StartFunction = Obj.startFunction(); 263 S = std::move(StartSec); 264 break; 265 } 266 case wasm::WASM_SEC_EXPORT: { 267 auto ExportSec = make_unique<WasmYAML::ExportSection>(); 268 for (auto &Export : Obj.exports()) { 269 WasmYAML::Export Ex; 270 Ex.Name = Export.Name; 271 Ex.Kind = Export.Kind; 272 Ex.Index = Export.Index; 273 ExportSec->Exports.push_back(Ex); 274 } 275 S = std::move(ExportSec); 276 break; 277 } 278 case wasm::WASM_SEC_ELEM: { 279 auto ElemSec = make_unique<WasmYAML::ElemSection>(); 280 for (auto &Segment : Obj.elements()) { 281 WasmYAML::ElemSegment Seg; 282 Seg.TableIndex = Segment.TableIndex; 283 Seg.Offset = Segment.Offset; 284 for (auto &Func : Segment.Functions) { 285 Seg.Functions.push_back(Func); 286 } 287 ElemSec->Segments.push_back(Seg); 288 } 289 S = std::move(ElemSec); 290 break; 291 } 292 case wasm::WASM_SEC_CODE: { 293 auto CodeSec = make_unique<WasmYAML::CodeSection>(); 294 for (auto &Func : Obj.functions()) { 295 WasmYAML::Function Function; 296 Function.Index = Func.Index; 297 for (auto &Local : Func.Locals) { 298 WasmYAML::LocalDecl LocalDecl; 299 LocalDecl.Type = Local.Type; 300 LocalDecl.Count = Local.Count; 301 Function.Locals.push_back(LocalDecl); 302 } 303 Function.Body = yaml::BinaryRef(Func.Body); 304 CodeSec->Functions.push_back(Function); 305 } 306 S = std::move(CodeSec); 307 break; 308 } 309 case wasm::WASM_SEC_DATA: { 310 auto DataSec = make_unique<WasmYAML::DataSection>(); 311 for (const object::WasmSegment &Segment : Obj.dataSegments()) { 312 WasmYAML::DataSegment Seg; 313 Seg.SectionOffset = Segment.SectionOffset; 314 Seg.MemoryIndex = Segment.Data.MemoryIndex; 315 Seg.Offset = Segment.Data.Offset; 316 Seg.Content = yaml::BinaryRef(Segment.Data.Content); 317 DataSec->Segments.push_back(Seg); 318 } 319 S = std::move(DataSec); 320 break; 321 } 322 default: 323 llvm_unreachable("Unknown section type"); 324 break; 325 } 326 for (const wasm::WasmRelocation &Reloc : WasmSec.Relocations) { 327 WasmYAML::Relocation R; 328 R.Type = Reloc.Type; 329 R.Index = Reloc.Index; 330 R.Offset = Reloc.Offset; 331 R.Addend = Reloc.Addend; 332 S->Relocations.push_back(R); 333 } 334 Y->Sections.push_back(std::move(S)); 335 } 336 337 return Y.release(); 338 } 339 340 std::error_code wasm2yaml(raw_ostream &Out, const object::WasmObjectFile &Obj) { 341 WasmDumper Dumper(Obj); 342 ErrorOr<WasmYAML::Object *> YAMLOrErr = Dumper.dump(); 343 if (std::error_code EC = YAMLOrErr.getError()) 344 return EC; 345 346 std::unique_ptr<WasmYAML::Object> YAML(YAMLOrErr.get()); 347 yaml::Output Yout(Out); 348 Yout << *YAML; 349 350 return std::error_code(); 351 } 352