1 //===- yaml2wasm - Convert YAML to a Wasm object file --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// The Wasm component of yaml2obj.
11 ///
12 //===----------------------------------------------------------------------===//
13 //
14 
15 #include "llvm/Object/Wasm.h"
16 #include "llvm/ObjectYAML/ObjectYAML.h"
17 #include "llvm/ObjectYAML/yaml2obj.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/LEB128.h"
20 
21 using namespace llvm;
22 
23 namespace {
24 /// This parses a yaml stream that represents a Wasm object file.
25 /// See docs/yaml2obj for the yaml scheema.
26 class WasmWriter {
27 public:
28   WasmWriter(WasmYAML::Object &Obj, yaml::ErrorHandler EH)
29       : Obj(Obj), ErrHandler(EH) {}
30   bool writeWasm(raw_ostream &OS);
31 
32 private:
33   void writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
34                          uint32_t SectionIndex);
35 
36   void writeInitExpr(raw_ostream &OS, const wasm::WasmInitExpr &InitExpr);
37 
38   void writeSectionContent(raw_ostream &OS, WasmYAML::CustomSection &Section);
39   void writeSectionContent(raw_ostream &OS, WasmYAML::TypeSection &Section);
40   void writeSectionContent(raw_ostream &OS, WasmYAML::ImportSection &Section);
41   void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section);
42   void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section);
43   void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section);
44   void writeSectionContent(raw_ostream &OS, WasmYAML::EventSection &Section);
45   void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section);
46   void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section);
47   void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section);
48   void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section);
49   void writeSectionContent(raw_ostream &OS, WasmYAML::CodeSection &Section);
50   void writeSectionContent(raw_ostream &OS, WasmYAML::DataSection &Section);
51   void writeSectionContent(raw_ostream &OS, WasmYAML::DataCountSection &Section);
52 
53   // Custom section types
54   void writeSectionContent(raw_ostream &OS, WasmYAML::DylinkSection &Section);
55   void writeSectionContent(raw_ostream &OS, WasmYAML::NameSection &Section);
56   void writeSectionContent(raw_ostream &OS, WasmYAML::LinkingSection &Section);
57   void writeSectionContent(raw_ostream &OS, WasmYAML::ProducersSection &Section);
58   void writeSectionContent(raw_ostream &OS,
59                           WasmYAML::TargetFeaturesSection &Section);
60   WasmYAML::Object &Obj;
61   uint32_t NumImportedFunctions = 0;
62   uint32_t NumImportedGlobals = 0;
63   uint32_t NumImportedTables = 0;
64   uint32_t NumImportedEvents = 0;
65 
66   bool HasError = false;
67   yaml::ErrorHandler ErrHandler;
68   void reportError(const Twine &Msg);
69 };
70 
71 class SubSectionWriter {
72   raw_ostream &OS;
73   std::string OutString;
74   raw_string_ostream StringStream;
75 
76 public:
77   SubSectionWriter(raw_ostream &OS) : OS(OS), StringStream(OutString) {}
78 
79   void done() {
80     StringStream.flush();
81     encodeULEB128(OutString.size(), OS);
82     OS << OutString;
83     OutString.clear();
84   }
85 
86   raw_ostream &getStream() { return StringStream; }
87 };
88 
89 } // end anonymous namespace
90 
91 static int writeUint64(raw_ostream &OS, uint64_t Value) {
92   char Data[sizeof(Value)];
93   support::endian::write64le(Data, Value);
94   OS.write(Data, sizeof(Data));
95   return 0;
96 }
97 
98 static int writeUint32(raw_ostream &OS, uint32_t Value) {
99   char Data[sizeof(Value)];
100   support::endian::write32le(Data, Value);
101   OS.write(Data, sizeof(Data));
102   return 0;
103 }
104 
105 static int writeUint8(raw_ostream &OS, uint8_t Value) {
106   char Data[sizeof(Value)];
107   memcpy(Data, &Value, sizeof(Data));
108   OS.write(Data, sizeof(Data));
109   return 0;
110 }
111 
112 static int writeStringRef(const StringRef &Str, raw_ostream &OS) {
113   encodeULEB128(Str.size(), OS);
114   OS << Str;
115   return 0;
116 }
117 
118 static int writeLimits(const WasmYAML::Limits &Lim, raw_ostream &OS) {
119   writeUint8(OS, Lim.Flags);
120   encodeULEB128(Lim.Initial, OS);
121   if (Lim.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
122     encodeULEB128(Lim.Maximum, OS);
123   return 0;
124 }
125 
126 void WasmWriter::reportError(const Twine &Msg) {
127   ErrHandler(Msg);
128   HasError = true;
129 }
130 
131 void WasmWriter::writeInitExpr(raw_ostream &OS,
132                                const wasm::WasmInitExpr &InitExpr) {
133   writeUint8(OS, InitExpr.Opcode);
134   switch (InitExpr.Opcode) {
135   case wasm::WASM_OPCODE_I32_CONST:
136     encodeSLEB128(InitExpr.Value.Int32, OS);
137     break;
138   case wasm::WASM_OPCODE_I64_CONST:
139     encodeSLEB128(InitExpr.Value.Int64, OS);
140     break;
141   case wasm::WASM_OPCODE_F32_CONST:
142     writeUint32(OS, InitExpr.Value.Float32);
143     break;
144   case wasm::WASM_OPCODE_F64_CONST:
145     writeUint64(OS, InitExpr.Value.Float64);
146     break;
147   case wasm::WASM_OPCODE_GLOBAL_GET:
148     encodeULEB128(InitExpr.Value.Global, OS);
149     break;
150   default:
151     reportError("unknown opcode in init_expr: " + Twine(InitExpr.Opcode));
152     return;
153   }
154   writeUint8(OS, wasm::WASM_OPCODE_END);
155 }
156 
157 void WasmWriter::writeSectionContent(raw_ostream &OS,
158                                      WasmYAML::DylinkSection &Section) {
159   writeStringRef(Section.Name, OS);
160   encodeULEB128(Section.MemorySize, OS);
161   encodeULEB128(Section.MemoryAlignment, OS);
162   encodeULEB128(Section.TableSize, OS);
163   encodeULEB128(Section.TableAlignment, OS);
164   encodeULEB128(Section.Needed.size(), OS);
165   for (StringRef Needed : Section.Needed)
166     writeStringRef(Needed, OS);
167 }
168 
169 void WasmWriter::writeSectionContent(raw_ostream &OS,
170                                      WasmYAML::LinkingSection &Section) {
171   writeStringRef(Section.Name, OS);
172   encodeULEB128(Section.Version, OS);
173 
174   SubSectionWriter SubSection(OS);
175 
176   // SYMBOL_TABLE subsection
177   if (Section.SymbolTable.size()) {
178     writeUint8(OS, wasm::WASM_SYMBOL_TABLE);
179 
180     encodeULEB128(Section.SymbolTable.size(), SubSection.getStream());
181 #ifndef NDEBUG
182     uint32_t SymbolIndex = 0;
183 #endif
184     for (const WasmYAML::SymbolInfo &Info : Section.SymbolTable) {
185       assert(Info.Index == SymbolIndex++);
186       writeUint8(SubSection.getStream(), Info.Kind);
187       encodeULEB128(Info.Flags, SubSection.getStream());
188       switch (Info.Kind) {
189       case wasm::WASM_SYMBOL_TYPE_FUNCTION:
190       case wasm::WASM_SYMBOL_TYPE_GLOBAL:
191       case wasm::WASM_SYMBOL_TYPE_TABLE:
192       case wasm::WASM_SYMBOL_TYPE_EVENT:
193         encodeULEB128(Info.ElementIndex, SubSection.getStream());
194         if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
195             (Info.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
196           writeStringRef(Info.Name, SubSection.getStream());
197         break;
198       case wasm::WASM_SYMBOL_TYPE_DATA:
199         writeStringRef(Info.Name, SubSection.getStream());
200         if ((Info.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
201           encodeULEB128(Info.DataRef.Segment, SubSection.getStream());
202           encodeULEB128(Info.DataRef.Offset, SubSection.getStream());
203           encodeULEB128(Info.DataRef.Size, SubSection.getStream());
204         }
205         break;
206       case wasm::WASM_SYMBOL_TYPE_SECTION:
207         encodeULEB128(Info.ElementIndex, SubSection.getStream());
208         break;
209       default:
210         llvm_unreachable("unexpected kind");
211       }
212     }
213 
214     SubSection.done();
215   }
216 
217   // SEGMENT_NAMES subsection
218   if (Section.SegmentInfos.size()) {
219     writeUint8(OS, wasm::WASM_SEGMENT_INFO);
220     encodeULEB128(Section.SegmentInfos.size(), SubSection.getStream());
221     for (const WasmYAML::SegmentInfo &SegmentInfo : Section.SegmentInfos) {
222       writeStringRef(SegmentInfo.Name, SubSection.getStream());
223       encodeULEB128(SegmentInfo.Alignment, SubSection.getStream());
224       encodeULEB128(SegmentInfo.Flags, SubSection.getStream());
225     }
226     SubSection.done();
227   }
228 
229   // INIT_FUNCS subsection
230   if (Section.InitFunctions.size()) {
231     writeUint8(OS, wasm::WASM_INIT_FUNCS);
232     encodeULEB128(Section.InitFunctions.size(), SubSection.getStream());
233     for (const WasmYAML::InitFunction &Func : Section.InitFunctions) {
234       encodeULEB128(Func.Priority, SubSection.getStream());
235       encodeULEB128(Func.Symbol, SubSection.getStream());
236     }
237     SubSection.done();
238   }
239 
240   // COMDAT_INFO subsection
241   if (Section.Comdats.size()) {
242     writeUint8(OS, wasm::WASM_COMDAT_INFO);
243     encodeULEB128(Section.Comdats.size(), SubSection.getStream());
244     for (const auto &C : Section.Comdats) {
245       writeStringRef(C.Name, SubSection.getStream());
246       encodeULEB128(0, SubSection.getStream()); // flags for future use
247       encodeULEB128(C.Entries.size(), SubSection.getStream());
248       for (const WasmYAML::ComdatEntry &Entry : C.Entries) {
249         writeUint8(SubSection.getStream(), Entry.Kind);
250         encodeULEB128(Entry.Index, SubSection.getStream());
251       }
252     }
253     SubSection.done();
254   }
255 }
256 
257 void WasmWriter::writeSectionContent(raw_ostream &OS,
258                                      WasmYAML::NameSection &Section) {
259   writeStringRef(Section.Name, OS);
260   if (Section.FunctionNames.size()) {
261     writeUint8(OS, wasm::WASM_NAMES_FUNCTION);
262 
263     SubSectionWriter SubSection(OS);
264 
265     encodeULEB128(Section.FunctionNames.size(), SubSection.getStream());
266     for (const WasmYAML::NameEntry &NameEntry : Section.FunctionNames) {
267       encodeULEB128(NameEntry.Index, SubSection.getStream());
268       writeStringRef(NameEntry.Name, SubSection.getStream());
269     }
270 
271     SubSection.done();
272   }
273 }
274 
275 void WasmWriter::writeSectionContent(raw_ostream &OS,
276                                      WasmYAML::ProducersSection &Section) {
277   writeStringRef(Section.Name, OS);
278   int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) +
279                int(!Section.SDKs.empty());
280   if (Fields == 0)
281     return;
282   encodeULEB128(Fields, OS);
283   for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages),
284                       std::make_pair(StringRef("processed-by"), &Section.Tools),
285                       std::make_pair(StringRef("sdk"), &Section.SDKs)}) {
286     if (Field.second->empty())
287       continue;
288     writeStringRef(Field.first, OS);
289     encodeULEB128(Field.second->size(), OS);
290     for (auto &Entry : *Field.second) {
291       writeStringRef(Entry.Name, OS);
292       writeStringRef(Entry.Version, OS);
293     }
294   }
295 }
296 
297 void WasmWriter::writeSectionContent(raw_ostream &OS,
298                                      WasmYAML::TargetFeaturesSection &Section) {
299   writeStringRef(Section.Name, OS);
300   encodeULEB128(Section.Features.size(), OS);
301   for (auto &E : Section.Features) {
302     writeUint8(OS, E.Prefix);
303     writeStringRef(E.Name, OS);
304   }
305 }
306 
307 void WasmWriter::writeSectionContent(raw_ostream &OS,
308                                      WasmYAML::CustomSection &Section) {
309   if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
310     writeSectionContent(OS, *S);
311   } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
312     writeSectionContent(OS, *S);
313   } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
314     writeSectionContent(OS, *S);
315   } else if (auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) {
316     writeSectionContent(OS, *S);
317   } else if (auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) {
318     writeSectionContent(OS, *S);
319   } else {
320     writeStringRef(Section.Name, OS);
321     Section.Payload.writeAsBinary(OS);
322   }
323 }
324 
325 void WasmWriter::writeSectionContent(raw_ostream &OS,
326                                     WasmYAML::TypeSection &Section) {
327   encodeULEB128(Section.Signatures.size(), OS);
328   uint32_t ExpectedIndex = 0;
329   for (const WasmYAML::Signature &Sig : Section.Signatures) {
330     if (Sig.Index != ExpectedIndex) {
331       reportError("unexpected type index: " + Twine(Sig.Index));
332       return;
333     }
334     ++ExpectedIndex;
335     writeUint8(OS, Sig.Form);
336     encodeULEB128(Sig.ParamTypes.size(), OS);
337     for (auto ParamType : Sig.ParamTypes)
338       writeUint8(OS, ParamType);
339     encodeULEB128(Sig.ReturnTypes.size(), OS);
340     for (auto ReturnType : Sig.ReturnTypes)
341       writeUint8(OS, ReturnType);
342   }
343 }
344 
345 void WasmWriter::writeSectionContent(raw_ostream &OS,
346                                     WasmYAML::ImportSection &Section) {
347   encodeULEB128(Section.Imports.size(), OS);
348   for (const WasmYAML::Import &Import : Section.Imports) {
349     writeStringRef(Import.Module, OS);
350     writeStringRef(Import.Field, OS);
351     writeUint8(OS, Import.Kind);
352     switch (Import.Kind) {
353     case wasm::WASM_EXTERNAL_FUNCTION:
354       encodeULEB128(Import.SigIndex, OS);
355       NumImportedFunctions++;
356       break;
357     case wasm::WASM_EXTERNAL_GLOBAL:
358       writeUint8(OS, Import.GlobalImport.Type);
359       writeUint8(OS, Import.GlobalImport.Mutable);
360       NumImportedGlobals++;
361       break;
362     case wasm::WASM_EXTERNAL_EVENT:
363       writeUint32(OS, Import.EventImport.Attribute);
364       writeUint32(OS, Import.EventImport.SigIndex);
365       NumImportedEvents++;
366       break;
367     case wasm::WASM_EXTERNAL_MEMORY:
368       writeLimits(Import.Memory, OS);
369       break;
370     case wasm::WASM_EXTERNAL_TABLE:
371       writeUint8(OS, Import.TableImport.ElemType);
372       writeLimits(Import.TableImport.TableLimits, OS);
373       NumImportedTables++;
374       break;
375     default:
376       reportError("unknown import type: " +Twine(Import.Kind));
377       return;
378     }
379   }
380 }
381 
382 void WasmWriter::writeSectionContent(raw_ostream &OS,
383                                      WasmYAML::FunctionSection &Section) {
384   encodeULEB128(Section.FunctionTypes.size(), OS);
385   for (uint32_t FuncType : Section.FunctionTypes)
386     encodeULEB128(FuncType, OS);
387 }
388 
389 void WasmWriter::writeSectionContent(raw_ostream &OS,
390                                     WasmYAML::ExportSection &Section) {
391   encodeULEB128(Section.Exports.size(), OS);
392   for (const WasmYAML::Export &Export : Section.Exports) {
393     writeStringRef(Export.Name, OS);
394     writeUint8(OS, Export.Kind);
395     encodeULEB128(Export.Index, OS);
396   }
397 }
398 
399 void WasmWriter::writeSectionContent(raw_ostream &OS,
400                                      WasmYAML::StartSection &Section) {
401   encodeULEB128(Section.StartFunction, OS);
402 }
403 
404 void WasmWriter::writeSectionContent(raw_ostream &OS,
405                                      WasmYAML::TableSection &Section) {
406   encodeULEB128(Section.Tables.size(), OS);
407   uint32_t ExpectedIndex = NumImportedTables;
408   for (auto &Table : Section.Tables) {
409     if (Table.Index != ExpectedIndex) {
410       reportError("unexpected table index: " + Twine(Table.Index));
411       return;
412     }
413     ++ExpectedIndex;
414     writeUint8(OS, Table.ElemType);
415     writeLimits(Table.TableLimits, OS);
416   }
417 }
418 
419 void WasmWriter::writeSectionContent(raw_ostream &OS,
420                                      WasmYAML::MemorySection &Section) {
421   encodeULEB128(Section.Memories.size(), OS);
422   for (const WasmYAML::Limits &Mem : Section.Memories)
423     writeLimits(Mem, OS);
424 }
425 
426 void WasmWriter::writeSectionContent(raw_ostream &OS,
427                                      WasmYAML::EventSection &Section) {
428   encodeULEB128(Section.Events.size(), OS);
429   uint32_t ExpectedIndex = NumImportedEvents;
430   for (auto &Event : Section.Events) {
431     if (Event.Index != ExpectedIndex) {
432       reportError("unexpected event index: " + Twine(Event.Index));
433       return;
434     }
435     ++ExpectedIndex;
436     encodeULEB128(Event.Attribute, OS);
437     encodeULEB128(Event.SigIndex, OS);
438   }
439 }
440 
441 void WasmWriter::writeSectionContent(raw_ostream &OS,
442                                      WasmYAML::GlobalSection &Section) {
443   encodeULEB128(Section.Globals.size(), OS);
444   uint32_t ExpectedIndex = NumImportedGlobals;
445   for (auto &Global : Section.Globals) {
446     if (Global.Index != ExpectedIndex) {
447       reportError("unexpected global index: " + Twine(Global.Index));
448       return;
449     }
450     ++ExpectedIndex;
451     writeUint8(OS, Global.Type);
452     writeUint8(OS, Global.Mutable);
453     writeInitExpr(OS, Global.InitExpr);
454   }
455 }
456 
457 void WasmWriter::writeSectionContent(raw_ostream &OS,
458                                      WasmYAML::ElemSection &Section) {
459   encodeULEB128(Section.Segments.size(), OS);
460   for (auto &Segment : Section.Segments) {
461     encodeULEB128(Segment.TableIndex, OS);
462     writeInitExpr(OS, Segment.Offset);
463 
464     encodeULEB128(Segment.Functions.size(), OS);
465     for (auto &Function : Segment.Functions)
466       encodeULEB128(Function, OS);
467   }
468 }
469 
470 void WasmWriter::writeSectionContent(raw_ostream &OS,
471                                     WasmYAML::CodeSection &Section) {
472   encodeULEB128(Section.Functions.size(), OS);
473   uint32_t ExpectedIndex = NumImportedFunctions;
474   for (auto &Func : Section.Functions) {
475     std::string OutString;
476     raw_string_ostream StringStream(OutString);
477     if (Func.Index != ExpectedIndex) {
478       reportError("unexpected function index: " + Twine(Func.Index));
479       return;
480     }
481     ++ExpectedIndex;
482 
483     encodeULEB128(Func.Locals.size(), StringStream);
484     for (auto &LocalDecl : Func.Locals) {
485       encodeULEB128(LocalDecl.Count, StringStream);
486       writeUint8(StringStream, LocalDecl.Type);
487     }
488 
489     Func.Body.writeAsBinary(StringStream);
490 
491     // Write the section size followed by the content
492     StringStream.flush();
493     encodeULEB128(OutString.size(), OS);
494     OS << OutString;
495   }
496 }
497 
498 void WasmWriter::writeSectionContent(raw_ostream &OS,
499                                      WasmYAML::DataSection &Section) {
500   encodeULEB128(Section.Segments.size(), OS);
501   for (auto &Segment : Section.Segments) {
502     encodeULEB128(Segment.InitFlags, OS);
503     if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX)
504       encodeULEB128(Segment.MemoryIndex, OS);
505     if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0)
506       writeInitExpr(OS, Segment.Offset);
507     encodeULEB128(Segment.Content.binary_size(), OS);
508     Segment.Content.writeAsBinary(OS);
509   }
510 }
511 
512 void WasmWriter::writeSectionContent(raw_ostream &OS,
513                                      WasmYAML::DataCountSection &Section) {
514   encodeULEB128(Section.Count, OS);
515 }
516 
517 void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
518                                   uint32_t SectionIndex) {
519   switch (Sec.Type) {
520   case wasm::WASM_SEC_CODE:
521     writeStringRef("reloc.CODE", OS);
522     break;
523   case wasm::WASM_SEC_DATA:
524     writeStringRef("reloc.DATA", OS);
525     break;
526   case wasm::WASM_SEC_CUSTOM: {
527     auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec);
528     writeStringRef(("reloc." + CustomSection->Name).str(), OS);
529     break;
530   }
531   default:
532     llvm_unreachable("not yet implemented");
533   }
534 
535   encodeULEB128(SectionIndex, OS);
536   encodeULEB128(Sec.Relocations.size(), OS);
537 
538   for (auto Reloc : Sec.Relocations) {
539     writeUint8(OS, Reloc.Type);
540     encodeULEB128(Reloc.Offset, OS);
541     encodeULEB128(Reloc.Index, OS);
542     switch (Reloc.Type) {
543     case wasm::R_WASM_MEMORY_ADDR_LEB:
544     case wasm::R_WASM_MEMORY_ADDR_LEB64:
545     case wasm::R_WASM_MEMORY_ADDR_SLEB:
546     case wasm::R_WASM_MEMORY_ADDR_SLEB64:
547     case wasm::R_WASM_MEMORY_ADDR_I32:
548     case wasm::R_WASM_MEMORY_ADDR_I64:
549     case wasm::R_WASM_FUNCTION_OFFSET_I32:
550     case wasm::R_WASM_SECTION_OFFSET_I32:
551       encodeULEB128(Reloc.Addend, OS);
552     }
553   }
554 }
555 
556 bool WasmWriter::writeWasm(raw_ostream &OS) {
557   // Write headers
558   OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
559   writeUint32(OS, Obj.Header.Version);
560 
561   // Write each section
562   llvm::object::WasmSectionOrderChecker Checker;
563   for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
564     StringRef SecName = "";
565     if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
566       SecName = S->Name;
567     if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
568       reportError("out of order section type: " + Twine(Sec->Type));
569       return false;
570     }
571     encodeULEB128(Sec->Type, OS);
572     std::string OutString;
573     raw_string_ostream StringStream(OutString);
574     if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
575       writeSectionContent(StringStream, *S);
576     else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get()))
577       writeSectionContent(StringStream, *S);
578     else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get()))
579       writeSectionContent(StringStream, *S);
580     else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get()))
581       writeSectionContent(StringStream, *S);
582     else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get()))
583       writeSectionContent(StringStream, *S);
584     else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
585       writeSectionContent(StringStream, *S);
586     else if (auto S = dyn_cast<WasmYAML::EventSection>(Sec.get()))
587       writeSectionContent(StringStream, *S);
588     else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
589       writeSectionContent(StringStream, *S);
590     else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
591       writeSectionContent(StringStream, *S);
592     else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
593       writeSectionContent(StringStream, *S);
594     else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get()))
595       writeSectionContent(StringStream, *S);
596     else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get()))
597       writeSectionContent(StringStream, *S);
598     else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get()))
599       writeSectionContent(StringStream, *S);
600     else if (auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get()))
601       writeSectionContent(StringStream, *S);
602     else
603       reportError("unknown section type: " + Twine(Sec->Type));
604 
605     if (HasError)
606       return false;
607 
608     StringStream.flush();
609 
610     // Write the section size followed by the content
611     encodeULEB128(OutString.size(), OS);
612     OS << OutString;
613   }
614 
615   // write reloc sections for any section that have relocations
616   uint32_t SectionIndex = 0;
617   for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
618     if (Sec->Relocations.empty()) {
619       SectionIndex++;
620       continue;
621     }
622 
623     writeUint8(OS, wasm::WASM_SEC_CUSTOM);
624     std::string OutString;
625     raw_string_ostream StringStream(OutString);
626     writeRelocSection(StringStream, *Sec, SectionIndex++);
627     StringStream.flush();
628 
629     encodeULEB128(OutString.size(), OS);
630     OS << OutString;
631   }
632 
633   return true;
634 }
635 
636 namespace llvm {
637 namespace yaml {
638 
639 bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
640   WasmWriter Writer(Doc, EH);
641   return Writer.writeWasm(Out);
642 }
643 
644 } // namespace yaml
645 } // namespace llvm
646