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