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   if (Section.GlobalNames.size()) {
274     writeUint8(OS, wasm::WASM_NAMES_GLOBAL);
275 
276     SubSectionWriter SubSection(OS);
277 
278     encodeULEB128(Section.GlobalNames.size(), SubSection.getStream());
279     for (const WasmYAML::NameEntry &NameEntry : Section.GlobalNames) {
280       encodeULEB128(NameEntry.Index, SubSection.getStream());
281       writeStringRef(NameEntry.Name, SubSection.getStream());
282     }
283 
284     SubSection.done();
285   }
286   if (Section.DataSegmentNames.size()) {
287     writeUint8(OS, wasm::WASM_NAMES_DATA_SEGMENT);
288 
289     SubSectionWriter SubSection(OS);
290 
291     encodeULEB128(Section.DataSegmentNames.size(), SubSection.getStream());
292     for (const WasmYAML::NameEntry &NameEntry : Section.DataSegmentNames) {
293       encodeULEB128(NameEntry.Index, SubSection.getStream());
294       writeStringRef(NameEntry.Name, SubSection.getStream());
295     }
296 
297     SubSection.done();
298   }
299 }
300 
301 void WasmWriter::writeSectionContent(raw_ostream &OS,
302                                      WasmYAML::ProducersSection &Section) {
303   writeStringRef(Section.Name, OS);
304   int Fields = int(!Section.Languages.empty()) + int(!Section.Tools.empty()) +
305                int(!Section.SDKs.empty());
306   if (Fields == 0)
307     return;
308   encodeULEB128(Fields, OS);
309   for (auto &Field : {std::make_pair(StringRef("language"), &Section.Languages),
310                       std::make_pair(StringRef("processed-by"), &Section.Tools),
311                       std::make_pair(StringRef("sdk"), &Section.SDKs)}) {
312     if (Field.second->empty())
313       continue;
314     writeStringRef(Field.first, OS);
315     encodeULEB128(Field.second->size(), OS);
316     for (auto &Entry : *Field.second) {
317       writeStringRef(Entry.Name, OS);
318       writeStringRef(Entry.Version, OS);
319     }
320   }
321 }
322 
323 void WasmWriter::writeSectionContent(raw_ostream &OS,
324                                      WasmYAML::TargetFeaturesSection &Section) {
325   writeStringRef(Section.Name, OS);
326   encodeULEB128(Section.Features.size(), OS);
327   for (auto &E : Section.Features) {
328     writeUint8(OS, E.Prefix);
329     writeStringRef(E.Name, OS);
330   }
331 }
332 
333 void WasmWriter::writeSectionContent(raw_ostream &OS,
334                                      WasmYAML::CustomSection &Section) {
335   if (auto S = dyn_cast<WasmYAML::DylinkSection>(&Section)) {
336     writeSectionContent(OS, *S);
337   } else if (auto S = dyn_cast<WasmYAML::NameSection>(&Section)) {
338     writeSectionContent(OS, *S);
339   } else if (auto S = dyn_cast<WasmYAML::LinkingSection>(&Section)) {
340     writeSectionContent(OS, *S);
341   } else if (auto S = dyn_cast<WasmYAML::ProducersSection>(&Section)) {
342     writeSectionContent(OS, *S);
343   } else if (auto S = dyn_cast<WasmYAML::TargetFeaturesSection>(&Section)) {
344     writeSectionContent(OS, *S);
345   } else {
346     writeStringRef(Section.Name, OS);
347     Section.Payload.writeAsBinary(OS);
348   }
349 }
350 
351 void WasmWriter::writeSectionContent(raw_ostream &OS,
352                                     WasmYAML::TypeSection &Section) {
353   encodeULEB128(Section.Signatures.size(), OS);
354   uint32_t ExpectedIndex = 0;
355   for (const WasmYAML::Signature &Sig : Section.Signatures) {
356     if (Sig.Index != ExpectedIndex) {
357       reportError("unexpected type index: " + Twine(Sig.Index));
358       return;
359     }
360     ++ExpectedIndex;
361     writeUint8(OS, Sig.Form);
362     encodeULEB128(Sig.ParamTypes.size(), OS);
363     for (auto ParamType : Sig.ParamTypes)
364       writeUint8(OS, ParamType);
365     encodeULEB128(Sig.ReturnTypes.size(), OS);
366     for (auto ReturnType : Sig.ReturnTypes)
367       writeUint8(OS, ReturnType);
368   }
369 }
370 
371 void WasmWriter::writeSectionContent(raw_ostream &OS,
372                                     WasmYAML::ImportSection &Section) {
373   encodeULEB128(Section.Imports.size(), OS);
374   for (const WasmYAML::Import &Import : Section.Imports) {
375     writeStringRef(Import.Module, OS);
376     writeStringRef(Import.Field, OS);
377     writeUint8(OS, Import.Kind);
378     switch (Import.Kind) {
379     case wasm::WASM_EXTERNAL_FUNCTION:
380       encodeULEB128(Import.SigIndex, OS);
381       NumImportedFunctions++;
382       break;
383     case wasm::WASM_EXTERNAL_GLOBAL:
384       writeUint8(OS, Import.GlobalImport.Type);
385       writeUint8(OS, Import.GlobalImport.Mutable);
386       NumImportedGlobals++;
387       break;
388     case wasm::WASM_EXTERNAL_EVENT:
389       writeUint32(OS, Import.EventImport.Attribute);
390       writeUint32(OS, Import.EventImport.SigIndex);
391       NumImportedEvents++;
392       break;
393     case wasm::WASM_EXTERNAL_MEMORY:
394       writeLimits(Import.Memory, OS);
395       break;
396     case wasm::WASM_EXTERNAL_TABLE:
397       writeUint8(OS, Import.TableImport.ElemType);
398       writeLimits(Import.TableImport.TableLimits, OS);
399       NumImportedTables++;
400       break;
401     default:
402       reportError("unknown import type: " +Twine(Import.Kind));
403       return;
404     }
405   }
406 }
407 
408 void WasmWriter::writeSectionContent(raw_ostream &OS,
409                                      WasmYAML::FunctionSection &Section) {
410   encodeULEB128(Section.FunctionTypes.size(), OS);
411   for (uint32_t FuncType : Section.FunctionTypes)
412     encodeULEB128(FuncType, OS);
413 }
414 
415 void WasmWriter::writeSectionContent(raw_ostream &OS,
416                                     WasmYAML::ExportSection &Section) {
417   encodeULEB128(Section.Exports.size(), OS);
418   for (const WasmYAML::Export &Export : Section.Exports) {
419     writeStringRef(Export.Name, OS);
420     writeUint8(OS, Export.Kind);
421     encodeULEB128(Export.Index, OS);
422   }
423 }
424 
425 void WasmWriter::writeSectionContent(raw_ostream &OS,
426                                      WasmYAML::StartSection &Section) {
427   encodeULEB128(Section.StartFunction, OS);
428 }
429 
430 void WasmWriter::writeSectionContent(raw_ostream &OS,
431                                      WasmYAML::TableSection &Section) {
432   encodeULEB128(Section.Tables.size(), OS);
433   uint32_t ExpectedIndex = NumImportedTables;
434   for (auto &Table : Section.Tables) {
435     if (Table.Index != ExpectedIndex) {
436       reportError("unexpected table index: " + Twine(Table.Index));
437       return;
438     }
439     ++ExpectedIndex;
440     writeUint8(OS, Table.ElemType);
441     writeLimits(Table.TableLimits, OS);
442   }
443 }
444 
445 void WasmWriter::writeSectionContent(raw_ostream &OS,
446                                      WasmYAML::MemorySection &Section) {
447   encodeULEB128(Section.Memories.size(), OS);
448   for (const WasmYAML::Limits &Mem : Section.Memories)
449     writeLimits(Mem, OS);
450 }
451 
452 void WasmWriter::writeSectionContent(raw_ostream &OS,
453                                      WasmYAML::EventSection &Section) {
454   encodeULEB128(Section.Events.size(), OS);
455   uint32_t ExpectedIndex = NumImportedEvents;
456   for (auto &Event : Section.Events) {
457     if (Event.Index != ExpectedIndex) {
458       reportError("unexpected event index: " + Twine(Event.Index));
459       return;
460     }
461     ++ExpectedIndex;
462     encodeULEB128(Event.Attribute, OS);
463     encodeULEB128(Event.SigIndex, OS);
464   }
465 }
466 
467 void WasmWriter::writeSectionContent(raw_ostream &OS,
468                                      WasmYAML::GlobalSection &Section) {
469   encodeULEB128(Section.Globals.size(), OS);
470   uint32_t ExpectedIndex = NumImportedGlobals;
471   for (auto &Global : Section.Globals) {
472     if (Global.Index != ExpectedIndex) {
473       reportError("unexpected global index: " + Twine(Global.Index));
474       return;
475     }
476     ++ExpectedIndex;
477     writeUint8(OS, Global.Type);
478     writeUint8(OS, Global.Mutable);
479     writeInitExpr(OS, Global.InitExpr);
480   }
481 }
482 
483 void WasmWriter::writeSectionContent(raw_ostream &OS,
484                                      WasmYAML::ElemSection &Section) {
485   encodeULEB128(Section.Segments.size(), OS);
486   for (auto &Segment : Section.Segments) {
487     encodeULEB128(Segment.TableIndex, OS);
488     writeInitExpr(OS, Segment.Offset);
489 
490     encodeULEB128(Segment.Functions.size(), OS);
491     for (auto &Function : Segment.Functions)
492       encodeULEB128(Function, OS);
493   }
494 }
495 
496 void WasmWriter::writeSectionContent(raw_ostream &OS,
497                                     WasmYAML::CodeSection &Section) {
498   encodeULEB128(Section.Functions.size(), OS);
499   uint32_t ExpectedIndex = NumImportedFunctions;
500   for (auto &Func : Section.Functions) {
501     std::string OutString;
502     raw_string_ostream StringStream(OutString);
503     if (Func.Index != ExpectedIndex) {
504       reportError("unexpected function index: " + Twine(Func.Index));
505       return;
506     }
507     ++ExpectedIndex;
508 
509     encodeULEB128(Func.Locals.size(), StringStream);
510     for (auto &LocalDecl : Func.Locals) {
511       encodeULEB128(LocalDecl.Count, StringStream);
512       writeUint8(StringStream, LocalDecl.Type);
513     }
514 
515     Func.Body.writeAsBinary(StringStream);
516 
517     // Write the section size followed by the content
518     StringStream.flush();
519     encodeULEB128(OutString.size(), OS);
520     OS << OutString;
521   }
522 }
523 
524 void WasmWriter::writeSectionContent(raw_ostream &OS,
525                                      WasmYAML::DataSection &Section) {
526   encodeULEB128(Section.Segments.size(), OS);
527   for (auto &Segment : Section.Segments) {
528     encodeULEB128(Segment.InitFlags, OS);
529     if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
530       encodeULEB128(Segment.MemoryIndex, OS);
531     if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0)
532       writeInitExpr(OS, Segment.Offset);
533     encodeULEB128(Segment.Content.binary_size(), OS);
534     Segment.Content.writeAsBinary(OS);
535   }
536 }
537 
538 void WasmWriter::writeSectionContent(raw_ostream &OS,
539                                      WasmYAML::DataCountSection &Section) {
540   encodeULEB128(Section.Count, OS);
541 }
542 
543 void WasmWriter::writeRelocSection(raw_ostream &OS, WasmYAML::Section &Sec,
544                                   uint32_t SectionIndex) {
545   switch (Sec.Type) {
546   case wasm::WASM_SEC_CODE:
547     writeStringRef("reloc.CODE", OS);
548     break;
549   case wasm::WASM_SEC_DATA:
550     writeStringRef("reloc.DATA", OS);
551     break;
552   case wasm::WASM_SEC_CUSTOM: {
553     auto *CustomSection = cast<WasmYAML::CustomSection>(&Sec);
554     writeStringRef(("reloc." + CustomSection->Name).str(), OS);
555     break;
556   }
557   default:
558     llvm_unreachable("not yet implemented");
559   }
560 
561   encodeULEB128(SectionIndex, OS);
562   encodeULEB128(Sec.Relocations.size(), OS);
563 
564   for (auto Reloc : Sec.Relocations) {
565     writeUint8(OS, Reloc.Type);
566     encodeULEB128(Reloc.Offset, OS);
567     encodeULEB128(Reloc.Index, OS);
568     switch (Reloc.Type) {
569     case wasm::R_WASM_MEMORY_ADDR_LEB:
570     case wasm::R_WASM_MEMORY_ADDR_LEB64:
571     case wasm::R_WASM_MEMORY_ADDR_SLEB:
572     case wasm::R_WASM_MEMORY_ADDR_SLEB64:
573     case wasm::R_WASM_MEMORY_ADDR_I32:
574     case wasm::R_WASM_MEMORY_ADDR_I64:
575     case wasm::R_WASM_FUNCTION_OFFSET_I32:
576     case wasm::R_WASM_FUNCTION_OFFSET_I64:
577     case wasm::R_WASM_SECTION_OFFSET_I32:
578       encodeULEB128(Reloc.Addend, OS);
579     }
580   }
581 }
582 
583 bool WasmWriter::writeWasm(raw_ostream &OS) {
584   // Write headers
585   OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
586   writeUint32(OS, Obj.Header.Version);
587 
588   // Write each section
589   llvm::object::WasmSectionOrderChecker Checker;
590   for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
591     StringRef SecName = "";
592     if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
593       SecName = S->Name;
594     if (!Checker.isValidSectionOrder(Sec->Type, SecName)) {
595       reportError("out of order section type: " + Twine(Sec->Type));
596       return false;
597     }
598     encodeULEB128(Sec->Type, OS);
599     std::string OutString;
600     raw_string_ostream StringStream(OutString);
601     if (auto S = dyn_cast<WasmYAML::CustomSection>(Sec.get()))
602       writeSectionContent(StringStream, *S);
603     else if (auto S = dyn_cast<WasmYAML::TypeSection>(Sec.get()))
604       writeSectionContent(StringStream, *S);
605     else if (auto S = dyn_cast<WasmYAML::ImportSection>(Sec.get()))
606       writeSectionContent(StringStream, *S);
607     else if (auto S = dyn_cast<WasmYAML::FunctionSection>(Sec.get()))
608       writeSectionContent(StringStream, *S);
609     else if (auto S = dyn_cast<WasmYAML::TableSection>(Sec.get()))
610       writeSectionContent(StringStream, *S);
611     else if (auto S = dyn_cast<WasmYAML::MemorySection>(Sec.get()))
612       writeSectionContent(StringStream, *S);
613     else if (auto S = dyn_cast<WasmYAML::EventSection>(Sec.get()))
614       writeSectionContent(StringStream, *S);
615     else if (auto S = dyn_cast<WasmYAML::GlobalSection>(Sec.get()))
616       writeSectionContent(StringStream, *S);
617     else if (auto S = dyn_cast<WasmYAML::ExportSection>(Sec.get()))
618       writeSectionContent(StringStream, *S);
619     else if (auto S = dyn_cast<WasmYAML::StartSection>(Sec.get()))
620       writeSectionContent(StringStream, *S);
621     else if (auto S = dyn_cast<WasmYAML::ElemSection>(Sec.get()))
622       writeSectionContent(StringStream, *S);
623     else if (auto S = dyn_cast<WasmYAML::CodeSection>(Sec.get()))
624       writeSectionContent(StringStream, *S);
625     else if (auto S = dyn_cast<WasmYAML::DataSection>(Sec.get()))
626       writeSectionContent(StringStream, *S);
627     else if (auto S = dyn_cast<WasmYAML::DataCountSection>(Sec.get()))
628       writeSectionContent(StringStream, *S);
629     else
630       reportError("unknown section type: " + Twine(Sec->Type));
631 
632     if (HasError)
633       return false;
634 
635     StringStream.flush();
636 
637     // Write the section size followed by the content
638     encodeULEB128(OutString.size(), OS);
639     OS << OutString;
640   }
641 
642   // write reloc sections for any section that have relocations
643   uint32_t SectionIndex = 0;
644   for (const std::unique_ptr<WasmYAML::Section> &Sec : Obj.Sections) {
645     if (Sec->Relocations.empty()) {
646       SectionIndex++;
647       continue;
648     }
649 
650     writeUint8(OS, wasm::WASM_SEC_CUSTOM);
651     std::string OutString;
652     raw_string_ostream StringStream(OutString);
653     writeRelocSection(StringStream, *Sec, SectionIndex++);
654     StringStream.flush();
655 
656     encodeULEB128(OutString.size(), OS);
657     OS << OutString;
658   }
659 
660   return true;
661 }
662 
663 namespace llvm {
664 namespace yaml {
665 
666 bool yaml2wasm(WasmYAML::Object &Doc, raw_ostream &Out, ErrorHandler EH) {
667   WasmWriter Writer(Doc, EH);
668   return Writer.writeWasm(Out);
669 }
670 
671 } // namespace yaml
672 } // namespace llvm
673