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