1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
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 // This file implements Wasm object file writer information.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/BinaryFormat/Wasm.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCFixupKindInfo.h"
24 #include "llvm/MC/MCObjectFileInfo.h"
25 #include "llvm/MC/MCObjectWriter.h"
26 #include "llvm/MC/MCSectionWasm.h"
27 #include "llvm/MC/MCSymbolWasm.h"
28 #include "llvm/MC/MCValue.h"
29 #include "llvm/MC/MCWasmObjectWriter.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/LEB128.h"
34 #include "llvm/Support/StringSaver.h"
35 #include <vector>
36 
37 using namespace llvm;
38 
39 #define DEBUG_TYPE "mc"
40 
41 namespace {
42 
43 // For patching purposes, we need to remember where each section starts, both
44 // for patching up the section size field, and for patching up references to
45 // locations within the section.
46 struct SectionBookkeeping {
47   // Where the size of the section is written.
48   uint64_t SizeOffset;
49   // Where the contents of the section starts (after the header).
50   uint64_t ContentsOffset;
51 };
52 
53 // The signature of a wasm function, in a struct capable of being used as a
54 // DenseMap key.
55 struct WasmFunctionType {
56   // Support empty and tombstone instances, needed by DenseMap.
57   enum { Plain, Empty, Tombstone } State;
58 
59   // The return types of the function.
60   SmallVector<wasm::ValType, 1> Returns;
61 
62   // The parameter types of the function.
63   SmallVector<wasm::ValType, 4> Params;
64 
65   WasmFunctionType() : State(Plain) {}
66 
67   bool operator==(const WasmFunctionType &Other) const {
68     return State == Other.State && Returns == Other.Returns &&
69            Params == Other.Params;
70   }
71 };
72 
73 // Traits for using WasmFunctionType in a DenseMap.
74 struct WasmFunctionTypeDenseMapInfo {
75   static WasmFunctionType getEmptyKey() {
76     WasmFunctionType FuncTy;
77     FuncTy.State = WasmFunctionType::Empty;
78     return FuncTy;
79   }
80   static WasmFunctionType getTombstoneKey() {
81     WasmFunctionType FuncTy;
82     FuncTy.State = WasmFunctionType::Tombstone;
83     return FuncTy;
84   }
85   static unsigned getHashValue(const WasmFunctionType &FuncTy) {
86     uintptr_t Value = FuncTy.State;
87     for (wasm::ValType Ret : FuncTy.Returns)
88       Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
89     for (wasm::ValType Param : FuncTy.Params)
90       Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
91     return Value;
92   }
93   static bool isEqual(const WasmFunctionType &LHS,
94                       const WasmFunctionType &RHS) {
95     return LHS == RHS;
96   }
97 };
98 
99 // A wasm data segment.  A wasm binary contains only a single data section
100 // but that can contain many segments, each with their own virtual location
101 // in memory.  Each MCSection data created by llvm is modeled as its own
102 // wasm data segment.
103 struct WasmDataSegment {
104   MCSectionWasm *Section;
105   uint32_t Offset;
106   SmallVector<char, 4> Data;
107 };
108 
109 // A wasm import to be written into the import section.
110 struct WasmImport {
111   StringRef ModuleName;
112   StringRef FieldName;
113   unsigned Kind;
114   int32_t Type;
115 };
116 
117 // A wasm function to be written into the function section.
118 struct WasmFunction {
119   int32_t Type;
120   const MCSymbolWasm *Sym;
121 };
122 
123 // A wasm export to be written into the export section.
124 struct WasmExport {
125   StringRef FieldName;
126   unsigned Kind;
127   uint32_t Index;
128 };
129 
130 // A wasm global to be written into the global section.
131 struct WasmGlobal {
132   wasm::ValType Type;
133   bool IsMutable;
134   bool HasImport;
135   uint64_t InitialValue;
136   uint32_t ImportIndex;
137 };
138 
139 // Information about a single relocation.
140 struct WasmRelocationEntry {
141   uint64_t Offset;                  // Where is the relocation.
142   const MCSymbolWasm *Symbol;       // The symbol to relocate with.
143   int64_t Addend;                   // A value to add to the symbol.
144   unsigned Type;                    // The type of the relocation.
145   const MCSectionWasm *FixupSection;// The section the relocation is targeting.
146 
147   WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
148                       int64_t Addend, unsigned Type,
149                       const MCSectionWasm *FixupSection)
150       : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
151         FixupSection(FixupSection) {}
152 
153   bool hasAddend() const {
154     switch (Type) {
155     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
156     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
157     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
158       return true;
159     default:
160       return false;
161     }
162   }
163 
164   void print(raw_ostream &Out) const {
165     Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
166         << ", Type=" << Type << ", FixupSection=" << FixupSection;
167   }
168 
169 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
170   LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
171 #endif
172 };
173 
174 #if !defined(NDEBUG)
175 raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
176   Rel.print(OS);
177   return OS;
178 }
179 #endif
180 
181 class WasmObjectWriter : public MCObjectWriter {
182   /// Helper struct for containing some precomputed information on symbols.
183   struct WasmSymbolData {
184     const MCSymbolWasm *Symbol;
185     StringRef Name;
186 
187     // Support lexicographic sorting.
188     bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; }
189   };
190 
191   /// The target specific Wasm writer instance.
192   std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
193 
194   // Relocations for fixing up references in the code section.
195   std::vector<WasmRelocationEntry> CodeRelocations;
196 
197   // Relocations for fixing up references in the data section.
198   std::vector<WasmRelocationEntry> DataRelocations;
199 
200   // Index values to use for fixing up call_indirect type indices.
201   // Maps function symbols to the index of the type of the function
202   DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
203   // Maps function symbols to the table element index space. Used
204   // for TABLE_INDEX relocation types (i.e. address taken functions).
205   DenseMap<const MCSymbolWasm *, uint32_t> IndirectSymbolIndices;
206   // Maps function/global symbols to the function/global index space.
207   DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
208 
209   DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
210       FunctionTypeIndices;
211   SmallVector<WasmFunctionType, 4> FunctionTypes;
212   SmallVector<WasmGlobal, 4> Globals;
213   unsigned NumGlobalImports = 0;
214 
215   // TargetObjectWriter wrappers.
216   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
217   unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
218     return TargetObjectWriter->getRelocType(Target, Fixup);
219   }
220 
221   void startSection(SectionBookkeeping &Section, unsigned SectionId,
222                     const char *Name = nullptr);
223   void endSection(SectionBookkeeping &Section);
224 
225 public:
226   WasmObjectWriter(MCWasmObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
227       : MCObjectWriter(OS, /*IsLittleEndian=*/true), TargetObjectWriter(MOTW) {}
228 
229 private:
230   ~WasmObjectWriter() override;
231 
232   void reset() override {
233     CodeRelocations.clear();
234     DataRelocations.clear();
235     TypeIndices.clear();
236     SymbolIndices.clear();
237     IndirectSymbolIndices.clear();
238     FunctionTypeIndices.clear();
239     FunctionTypes.clear();
240     Globals.clear();
241     MCObjectWriter::reset();
242     NumGlobalImports = 0;
243   }
244 
245   void writeHeader(const MCAssembler &Asm);
246 
247   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
248                         const MCFragment *Fragment, const MCFixup &Fixup,
249                         MCValue Target, uint64_t &FixedValue) override;
250 
251   void executePostLayoutBinding(MCAssembler &Asm,
252                                 const MCAsmLayout &Layout) override;
253 
254   void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
255 
256   void writeString(const StringRef Str) {
257     encodeULEB128(Str.size(), getStream());
258     writeBytes(Str);
259   }
260 
261   void writeValueType(wasm::ValType Ty) {
262     encodeSLEB128(int32_t(Ty), getStream());
263   }
264 
265   void writeTypeSection(const SmallVector<WasmFunctionType, 4> &FunctionTypes);
266   void writeImportSection(const SmallVector<WasmImport, 4> &Imports);
267   void writeFunctionSection(const SmallVector<WasmFunction, 4> &Functions);
268   void writeTableSection(uint32_t NumElements);
269   void writeMemorySection(uint32_t DataSize);
270   void writeGlobalSection();
271   void writeExportSection(const SmallVector<WasmExport, 4> &Exports);
272   void writeElemSection(const SmallVector<uint32_t, 4> &TableElems);
273   void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
274                         const SmallVector<WasmFunction, 4> &Functions);
275   void writeDataSection(const SmallVector<WasmDataSegment, 4> &Segments);
276   void writeNameSection(const SmallVector<WasmFunction, 4> &Functions,
277                         const SmallVector<WasmImport, 4> &Imports,
278                         uint32_t NumFuncImports);
279   void writeCodeRelocSection();
280   void writeDataRelocSection();
281   void writeLinkingMetaDataSection(uint32_t DataSize, uint32_t DataAlignment,
282                                    ArrayRef<StringRef> WeakSymbols,
283                                    bool HasStackPointer,
284                                    uint32_t StackPointerGlobal);
285 
286   uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
287   void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
288                         uint64_t ContentsOffset);
289 
290   void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations);
291   uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
292   uint32_t getFunctionType(const MCSymbolWasm& Symbol);
293   uint32_t registerFunctionType(const MCSymbolWasm& Symbol);
294 };
295 
296 } // end anonymous namespace
297 
298 WasmObjectWriter::~WasmObjectWriter() {}
299 
300 // Return the padding size to write a 32-bit value into a 5-byte ULEB128.
301 static unsigned PaddingFor5ByteULEB128(uint32_t X) {
302   return X == 0 ? 4 : (4u - (31u - countLeadingZeros(X)) / 7u);
303 }
304 
305 // Return the padding size to write a 32-bit value into a 5-byte SLEB128.
306 static unsigned PaddingFor5ByteSLEB128(int32_t X) {
307   return 5 - getSLEB128Size(X);
308 }
309 
310 // Write out a section header and a patchable section size field.
311 void WasmObjectWriter::startSection(SectionBookkeeping &Section,
312                                     unsigned SectionId,
313                                     const char *Name) {
314   assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) &&
315          "Only custom sections can have names");
316 
317   DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n");
318   encodeULEB128(SectionId, getStream());
319 
320   Section.SizeOffset = getStream().tell();
321 
322   // The section size. We don't know the size yet, so reserve enough space
323   // for any 32-bit value; we'll patch it later.
324   encodeULEB128(UINT32_MAX, getStream());
325 
326   // The position where the section starts, for measuring its size.
327   Section.ContentsOffset = getStream().tell();
328 
329   // Custom sections in wasm also have a string identifier.
330   if (SectionId == wasm::WASM_SEC_CUSTOM) {
331     assert(Name);
332     writeString(StringRef(Name));
333   }
334 }
335 
336 // Now that the section is complete and we know how big it is, patch up the
337 // section size field at the start of the section.
338 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
339   uint64_t Size = getStream().tell() - Section.ContentsOffset;
340   if (uint32_t(Size) != Size)
341     report_fatal_error("section size does not fit in a uint32_t");
342 
343   DEBUG(dbgs() << "endSection size=" << Size << "\n");
344   unsigned Padding = PaddingFor5ByteULEB128(Size);
345 
346   // Write the final section size to the payload_len field, which follows
347   // the section id byte.
348   uint8_t Buffer[16];
349   unsigned SizeLen = encodeULEB128(Size, Buffer, Padding);
350   assert(SizeLen == 5);
351   getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
352 }
353 
354 // Emit the Wasm header.
355 void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
356   writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
357   writeLE32(wasm::WasmVersion);
358 }
359 
360 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
361                                                 const MCAsmLayout &Layout) {
362 }
363 
364 void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
365                                         const MCAsmLayout &Layout,
366                                         const MCFragment *Fragment,
367                                         const MCFixup &Fixup, MCValue Target,
368                                         uint64_t &FixedValue) {
369   MCAsmBackend &Backend = Asm.getBackend();
370   bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
371                  MCFixupKindInfo::FKF_IsPCRel;
372   const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
373   uint64_t C = Target.getConstant();
374   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
375   MCContext &Ctx = Asm.getContext();
376 
377   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
378     assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
379            "Should not have constructed this");
380 
381     // Let A, B and C being the components of Target and R be the location of
382     // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
383     // If it is pcrel, we want to compute (A - B + C - R).
384 
385     // In general, Wasm has no relocations for -B. It can only represent (A + C)
386     // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
387     // replace B to implement it: (A - R - K + C)
388     if (IsPCRel) {
389       Ctx.reportError(
390           Fixup.getLoc(),
391           "No relocation available to represent this relative expression");
392       return;
393     }
394 
395     const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
396 
397     if (SymB.isUndefined()) {
398       Ctx.reportError(Fixup.getLoc(),
399                       Twine("symbol '") + SymB.getName() +
400                           "' can not be undefined in a subtraction expression");
401       return;
402     }
403 
404     assert(!SymB.isAbsolute() && "Should have been folded");
405     const MCSection &SecB = SymB.getSection();
406     if (&SecB != &FixupSection) {
407       Ctx.reportError(Fixup.getLoc(),
408                       "Cannot represent a difference across sections");
409       return;
410     }
411 
412     uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
413     uint64_t K = SymBOffset - FixupOffset;
414     IsPCRel = true;
415     C -= K;
416   }
417 
418   // We either rejected the fixup or folded B into C at this point.
419   const MCSymbolRefExpr *RefA = Target.getSymA();
420   const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
421 
422   if (SymA && SymA->isVariable()) {
423     const MCExpr *Expr = SymA->getVariableValue();
424     const auto *Inner = cast<MCSymbolRefExpr>(Expr);
425     if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
426       llvm_unreachable("weakref used in reloc not yet implemented");
427   }
428 
429   // Put any constant offset in an addend. Offsets can be negative, and
430   // LLVM expects wrapping, in contrast to wasm's immediates which can't
431   // be negative and don't wrap.
432   FixedValue = 0;
433 
434   if (SymA)
435     SymA->setUsedInReloc();
436 
437   assert(!IsPCRel);
438   assert(SymA);
439 
440   unsigned Type = getRelocType(Target, Fixup);
441 
442   WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
443   DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
444 
445   if (FixupSection.hasInstructions())
446     CodeRelocations.push_back(Rec);
447   else
448     DataRelocations.push_back(Rec);
449 }
450 
451 // Write X as an (unsigned) LEB value at offset Offset in Stream, padded
452 // to allow patching.
453 static void
454 WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
455   uint8_t Buffer[5];
456   unsigned Padding = PaddingFor5ByteULEB128(X);
457   unsigned SizeLen = encodeULEB128(X, Buffer, Padding);
458   assert(SizeLen == 5);
459   Stream.pwrite((char *)Buffer, SizeLen, Offset);
460 }
461 
462 // Write X as an signed LEB value at offset Offset in Stream, padded
463 // to allow patching.
464 static void
465 WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
466   uint8_t Buffer[5];
467   unsigned Padding = PaddingFor5ByteSLEB128(X);
468   unsigned SizeLen = encodeSLEB128(X, Buffer, Padding);
469   assert(SizeLen == 5);
470   Stream.pwrite((char *)Buffer, SizeLen, Offset);
471 }
472 
473 // Write X as a plain integer value at offset Offset in Stream.
474 static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
475   uint8_t Buffer[4];
476   support::endian::write32le(Buffer, X);
477   Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
478 }
479 
480 // Compute a value to write into the code at the location covered
481 // by RelEntry. This value isn't used by the static linker, since
482 // we have addends; it just serves to make the code more readable
483 // and to make standalone wasm modules directly usable.
484 uint32_t
485 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
486   const MCSymbolWasm *Sym = RelEntry.Symbol;
487 
488   // For undefined symbols, use a hopefully invalid value.
489   if (!Sym->isDefined(/*SetUsed=*/false))
490     return UINT32_MAX;
491 
492   uint32_t GlobalIndex = SymbolIndices[Sym];
493   const WasmGlobal& Global = Globals[GlobalIndex - NumGlobalImports];
494   uint64_t Address = Global.InitialValue + RelEntry.Addend;
495 
496   // Ignore overflow. LLVM allows address arithmetic to silently wrap.
497   uint32_t Value = Address;
498 
499   return Value;
500 }
501 
502 uint32_t WasmObjectWriter::getRelocationIndexValue(
503     const WasmRelocationEntry &RelEntry) {
504   switch (RelEntry.Type) {
505   case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
506   case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
507     if (!IndirectSymbolIndices.count(RelEntry.Symbol))
508       report_fatal_error("symbol not found table index space: " +
509                          RelEntry.Symbol->getName());
510     return IndirectSymbolIndices[RelEntry.Symbol];
511   case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
512   case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
513   case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
514   case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
515   case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
516     if (!SymbolIndices.count(RelEntry.Symbol))
517       report_fatal_error("symbol not found function/global index space: " +
518                          RelEntry.Symbol->getName());
519     return SymbolIndices[RelEntry.Symbol];
520   case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
521     if (!TypeIndices.count(RelEntry.Symbol))
522       report_fatal_error("symbol not found in type index space: " +
523                          RelEntry.Symbol->getName());
524     return TypeIndices[RelEntry.Symbol];
525   default:
526     llvm_unreachable("invalid relocation type");
527   }
528 }
529 
530 // Apply the portions of the relocation records that we can handle ourselves
531 // directly.
532 void WasmObjectWriter::applyRelocations(
533     ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
534   raw_pwrite_stream &Stream = getStream();
535   for (const WasmRelocationEntry &RelEntry : Relocations) {
536     uint64_t Offset = ContentsOffset +
537                       RelEntry.FixupSection->getSectionOffset() +
538                       RelEntry.Offset;
539 
540     DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
541     switch (RelEntry.Type) {
542     case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
543     case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
544     case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
545     case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
546       uint32_t Index = getRelocationIndexValue(RelEntry);
547       WritePatchableSLEB(Stream, Index, Offset);
548       break;
549     }
550     case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
551       uint32_t Index = getRelocationIndexValue(RelEntry);
552       WriteI32(Stream, Index, Offset);
553       break;
554     }
555     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
556       uint32_t Value = getProvisionalValue(RelEntry);
557       WritePatchableSLEB(Stream, Value, Offset);
558       break;
559     }
560     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: {
561       uint32_t Value = getProvisionalValue(RelEntry);
562       WritePatchableLEB(Stream, Value, Offset);
563       break;
564     }
565     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: {
566       uint32_t Value = getProvisionalValue(RelEntry);
567       WriteI32(Stream, Value, Offset);
568       break;
569     }
570     default:
571       llvm_unreachable("invalid relocation type");
572     }
573   }
574 }
575 
576 // Write out the portions of the relocation records that the linker will
577 // need to handle.
578 void WasmObjectWriter::writeRelocations(
579     ArrayRef<WasmRelocationEntry> Relocations) {
580   raw_pwrite_stream &Stream = getStream();
581   for (const WasmRelocationEntry& RelEntry : Relocations) {
582 
583     uint64_t Offset = RelEntry.Offset +
584                       RelEntry.FixupSection->getSectionOffset();
585     uint32_t Index = getRelocationIndexValue(RelEntry);
586 
587     encodeULEB128(RelEntry.Type, Stream);
588     encodeULEB128(Offset, Stream);
589     encodeULEB128(Index, Stream);
590     if (RelEntry.hasAddend())
591       encodeSLEB128(RelEntry.Addend, Stream);
592   }
593 }
594 
595 void WasmObjectWriter::writeTypeSection(
596     const SmallVector<WasmFunctionType, 4> &FunctionTypes) {
597   if (FunctionTypes.empty())
598     return;
599 
600   SectionBookkeeping Section;
601   startSection(Section, wasm::WASM_SEC_TYPE);
602 
603   encodeULEB128(FunctionTypes.size(), getStream());
604 
605   for (const WasmFunctionType &FuncTy : FunctionTypes) {
606     encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
607     encodeULEB128(FuncTy.Params.size(), getStream());
608     for (wasm::ValType Ty : FuncTy.Params)
609       writeValueType(Ty);
610     encodeULEB128(FuncTy.Returns.size(), getStream());
611     for (wasm::ValType Ty : FuncTy.Returns)
612       writeValueType(Ty);
613   }
614 
615   endSection(Section);
616 }
617 
618 
619 void WasmObjectWriter::writeImportSection(
620     const SmallVector<WasmImport, 4> &Imports) {
621   if (Imports.empty())
622     return;
623 
624   SectionBookkeeping Section;
625   startSection(Section, wasm::WASM_SEC_IMPORT);
626 
627   encodeULEB128(Imports.size(), getStream());
628   for (const WasmImport &Import : Imports) {
629     writeString(Import.ModuleName);
630     writeString(Import.FieldName);
631 
632     encodeULEB128(Import.Kind, getStream());
633 
634     switch (Import.Kind) {
635     case wasm::WASM_EXTERNAL_FUNCTION:
636       encodeULEB128(Import.Type, getStream());
637       break;
638     case wasm::WASM_EXTERNAL_GLOBAL:
639       encodeSLEB128(int32_t(Import.Type), getStream());
640       encodeULEB128(0, getStream()); // mutability
641       break;
642     default:
643       llvm_unreachable("unsupported import kind");
644     }
645   }
646 
647   endSection(Section);
648 }
649 
650 void WasmObjectWriter::writeFunctionSection(
651     const SmallVector<WasmFunction, 4> &Functions) {
652   if (Functions.empty())
653     return;
654 
655   SectionBookkeeping Section;
656   startSection(Section, wasm::WASM_SEC_FUNCTION);
657 
658   encodeULEB128(Functions.size(), getStream());
659   for (const WasmFunction &Func : Functions)
660     encodeULEB128(Func.Type, getStream());
661 
662   endSection(Section);
663 }
664 
665 void WasmObjectWriter::writeTableSection(uint32_t NumElements) {
666   // For now, always emit the table section, since indirect calls are not
667   // valid without it. In the future, we could perhaps be more clever and omit
668   // it if there are no indirect calls.
669 
670   SectionBookkeeping Section;
671   startSection(Section, wasm::WASM_SEC_TABLE);
672 
673   encodeULEB128(1, getStream());                       // The number of tables.
674                                                        // Fixed to 1 for now.
675   encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream()); // Type of table
676   encodeULEB128(0, getStream());                       // flags
677   encodeULEB128(NumElements, getStream());             // initial
678 
679   endSection(Section);
680 }
681 
682 void WasmObjectWriter::writeMemorySection(uint32_t DataSize) {
683   // For now, always emit the memory section, since loads and stores are not
684   // valid without it. In the future, we could perhaps be more clever and omit
685   // it if there are no loads or stores.
686   SectionBookkeeping Section;
687   uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
688 
689   startSection(Section, wasm::WASM_SEC_MEMORY);
690   encodeULEB128(1, getStream()); // number of memory spaces
691 
692   encodeULEB128(0, getStream()); // flags
693   encodeULEB128(NumPages, getStream()); // initial
694 
695   endSection(Section);
696 }
697 
698 void WasmObjectWriter::writeGlobalSection() {
699   if (Globals.empty())
700     return;
701 
702   SectionBookkeeping Section;
703   startSection(Section, wasm::WASM_SEC_GLOBAL);
704 
705   encodeULEB128(Globals.size(), getStream());
706   for (const WasmGlobal &Global : Globals) {
707     writeValueType(Global.Type);
708     write8(Global.IsMutable);
709 
710     if (Global.HasImport) {
711       assert(Global.InitialValue == 0);
712       write8(wasm::WASM_OPCODE_GET_GLOBAL);
713       encodeULEB128(Global.ImportIndex, getStream());
714     } else {
715       assert(Global.ImportIndex == 0);
716       write8(wasm::WASM_OPCODE_I32_CONST);
717       encodeSLEB128(Global.InitialValue, getStream()); // offset
718     }
719     write8(wasm::WASM_OPCODE_END);
720   }
721 
722   endSection(Section);
723 }
724 
725 void WasmObjectWriter::writeExportSection(
726     const SmallVector<WasmExport, 4> &Exports) {
727   if (Exports.empty())
728     return;
729 
730   SectionBookkeeping Section;
731   startSection(Section, wasm::WASM_SEC_EXPORT);
732 
733   encodeULEB128(Exports.size(), getStream());
734   for (const WasmExport &Export : Exports) {
735     writeString(Export.FieldName);
736     encodeSLEB128(Export.Kind, getStream());
737     encodeULEB128(Export.Index, getStream());
738   }
739 
740   endSection(Section);
741 }
742 
743 void WasmObjectWriter::writeElemSection(
744     const SmallVector<uint32_t, 4> &TableElems) {
745   if (TableElems.empty())
746     return;
747 
748   SectionBookkeeping Section;
749   startSection(Section, wasm::WASM_SEC_ELEM);
750 
751   encodeULEB128(1, getStream()); // number of "segments"
752   encodeULEB128(0, getStream()); // the table index
753 
754   // init expr for starting offset
755   write8(wasm::WASM_OPCODE_I32_CONST);
756   encodeSLEB128(0, getStream());
757   write8(wasm::WASM_OPCODE_END);
758 
759   encodeULEB128(TableElems.size(), getStream());
760   for (uint32_t Elem : TableElems)
761     encodeULEB128(Elem, getStream());
762 
763   endSection(Section);
764 }
765 
766 void WasmObjectWriter::writeCodeSection(
767     const MCAssembler &Asm, const MCAsmLayout &Layout,
768     const SmallVector<WasmFunction, 4> &Functions) {
769   if (Functions.empty())
770     return;
771 
772   SectionBookkeeping Section;
773   startSection(Section, wasm::WASM_SEC_CODE);
774 
775   encodeULEB128(Functions.size(), getStream());
776 
777   for (const WasmFunction &Func : Functions) {
778     auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
779 
780     int64_t Size = 0;
781     if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
782       report_fatal_error(".size expression must be evaluatable");
783 
784     encodeULEB128(Size, getStream());
785 
786     FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
787 
788     Asm.writeSectionData(&FuncSection, Layout);
789   }
790 
791   // Apply fixups.
792   applyRelocations(CodeRelocations, Section.ContentsOffset);
793 
794   endSection(Section);
795 }
796 
797 void WasmObjectWriter::writeDataSection(
798     const SmallVector<WasmDataSegment, 4> &Segments) {
799   if (Segments.empty())
800     return;
801 
802   SectionBookkeeping Section;
803   startSection(Section, wasm::WASM_SEC_DATA);
804 
805   encodeULEB128(Segments.size(), getStream()); // count
806 
807   for (const WasmDataSegment & Segment : Segments) {
808     encodeULEB128(0, getStream()); // memory index
809     write8(wasm::WASM_OPCODE_I32_CONST);
810     encodeSLEB128(Segment.Offset, getStream()); // offset
811     write8(wasm::WASM_OPCODE_END);
812     encodeULEB128(Segment.Data.size(), getStream()); // size
813     Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
814     writeBytes(Segment.Data); // data
815   }
816 
817   // Apply fixups.
818   applyRelocations(DataRelocations, Section.ContentsOffset);
819 
820   endSection(Section);
821 }
822 
823 void WasmObjectWriter::writeNameSection(
824     const SmallVector<WasmFunction, 4> &Functions,
825     const SmallVector<WasmImport, 4> &Imports,
826     unsigned NumFuncImports) {
827   uint32_t TotalFunctions = NumFuncImports + Functions.size();
828   if (TotalFunctions == 0)
829     return;
830 
831   SectionBookkeeping Section;
832   startSection(Section, wasm::WASM_SEC_CUSTOM, "name");
833   SectionBookkeeping SubSection;
834   startSection(SubSection, wasm::WASM_NAMES_FUNCTION);
835 
836   encodeULEB128(TotalFunctions, getStream());
837   uint32_t Index = 0;
838   for (const WasmImport &Import : Imports) {
839     if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
840       encodeULEB128(Index, getStream());
841       writeString(Import.FieldName);
842       ++Index;
843     }
844   }
845   for (const WasmFunction &Func : Functions) {
846     encodeULEB128(Index, getStream());
847     writeString(Func.Sym->getName());
848     ++Index;
849   }
850 
851   endSection(SubSection);
852   endSection(Section);
853 }
854 
855 void WasmObjectWriter::writeCodeRelocSection() {
856   // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
857   // for descriptions of the reloc sections.
858 
859   if (CodeRelocations.empty())
860     return;
861 
862   SectionBookkeeping Section;
863   startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
864 
865   encodeULEB128(wasm::WASM_SEC_CODE, getStream());
866   encodeULEB128(CodeRelocations.size(), getStream());
867 
868   writeRelocations(CodeRelocations);
869 
870   endSection(Section);
871 }
872 
873 void WasmObjectWriter::writeDataRelocSection() {
874   // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
875   // for descriptions of the reloc sections.
876 
877   if (DataRelocations.empty())
878     return;
879 
880   SectionBookkeeping Section;
881   startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
882 
883   encodeULEB128(wasm::WASM_SEC_DATA, getStream());
884   encodeULEB128(DataRelocations.size(), getStream());
885 
886   writeRelocations(DataRelocations);
887 
888   endSection(Section);
889 }
890 
891 void WasmObjectWriter::writeLinkingMetaDataSection(
892     uint32_t DataSize, uint32_t DataAlignment, ArrayRef<StringRef> WeakSymbols,
893     bool HasStackPointer, uint32_t StackPointerGlobal) {
894   SectionBookkeeping Section;
895   startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
896   SectionBookkeeping SubSection;
897 
898   if (HasStackPointer) {
899     startSection(SubSection, wasm::WASM_STACK_POINTER);
900     encodeULEB128(StackPointerGlobal, getStream()); // id
901     endSection(SubSection);
902   }
903 
904   if (WeakSymbols.size() != 0) {
905     startSection(SubSection, wasm::WASM_SYMBOL_INFO);
906     encodeULEB128(WeakSymbols.size(), getStream());
907     for (const StringRef Export: WeakSymbols) {
908       writeString(Export);
909       encodeULEB128(wasm::WASM_SYMBOL_FLAG_WEAK, getStream());
910     }
911     endSection(SubSection);
912   }
913 
914   if (DataSize > 0) {
915     startSection(SubSection, wasm::WASM_DATA_SIZE);
916     encodeULEB128(DataSize, getStream());
917     endSection(SubSection);
918 
919     startSection(SubSection, wasm::WASM_DATA_ALIGNMENT);
920     encodeULEB128(DataAlignment, getStream());
921     endSection(SubSection);
922   }
923 
924   endSection(Section);
925 }
926 
927 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
928   assert(Symbol.isFunction());
929   assert(TypeIndices.count(&Symbol));
930   return TypeIndices[&Symbol];
931 }
932 
933 uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
934   assert(Symbol.isFunction());
935 
936   WasmFunctionType F;
937   if (Symbol.isVariable()) {
938     const MCExpr *Expr = Symbol.getVariableValue();
939     auto *Inner = cast<MCSymbolRefExpr>(Expr);
940     const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol());
941     F.Returns = ResolvedSym->getReturns();
942     F.Params = ResolvedSym->getParams();
943   } else {
944     F.Returns = Symbol.getReturns();
945     F.Params = Symbol.getParams();
946   }
947 
948   auto Pair =
949       FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
950   if (Pair.second)
951     FunctionTypes.push_back(F);
952   TypeIndices[&Symbol] = Pair.first->second;
953 
954   DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
955   DEBUG(dbgs() << "  -> type index: " << Pair.first->second << "\n");
956   return Pair.first->second;
957 }
958 
959 void WasmObjectWriter::writeObject(MCAssembler &Asm,
960                                    const MCAsmLayout &Layout) {
961   DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
962   MCContext &Ctx = Asm.getContext();
963   wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32;
964 
965   // Collect information from the available symbols.
966   SmallVector<WasmFunction, 4> Functions;
967   SmallVector<uint32_t, 4> TableElems;
968   SmallVector<WasmImport, 4> Imports;
969   SmallVector<WasmExport, 4> Exports;
970   SmallVector<StringRef, 4> WeakSymbols;
971   SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
972   unsigned NumFuncImports = 0;
973   SmallVector<WasmDataSegment, 4> DataSegments;
974   uint32_t DataAlignment = 1;
975   uint32_t StackPointerGlobal = 0;
976   uint32_t DataSize = 0;
977   bool HasStackPointer = false;
978 
979   // Populate the IsAddressTaken set.
980   for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
981     switch (RelEntry.Type) {
982     case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
983     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
984       IsAddressTaken.insert(RelEntry.Symbol);
985       break;
986     default:
987       break;
988     }
989   }
990   for (const WasmRelocationEntry &RelEntry : DataRelocations) {
991     switch (RelEntry.Type) {
992     case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
993     case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
994       IsAddressTaken.insert(RelEntry.Symbol);
995       break;
996     default:
997       break;
998     }
999   }
1000 
1001   // Populate FunctionTypeIndices and Imports.
1002   for (const MCSymbol &S : Asm.symbols()) {
1003     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1004 
1005     if (WS.isTemporary())
1006       continue;
1007 
1008     if (WS.isFunction())
1009       registerFunctionType(WS);
1010 
1011     // If the symbol is not defined in this translation unit, import it.
1012     if (!WS.isDefined(/*SetUsed=*/false) || WS.isVariable()) {
1013       WasmImport Import;
1014       Import.ModuleName = WS.getModuleName();
1015       Import.FieldName = WS.getName();
1016 
1017       if (WS.isFunction()) {
1018         Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1019         Import.Type = getFunctionType(WS);
1020         SymbolIndices[&WS] = NumFuncImports;
1021         ++NumFuncImports;
1022       } else {
1023         Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1024         Import.Type = int32_t(PtrType);
1025         SymbolIndices[&WS] = NumGlobalImports;
1026         ++NumGlobalImports;
1027       }
1028 
1029       Imports.push_back(Import);
1030     }
1031   }
1032 
1033   // In the special .global_variables section, we've encoded global
1034   // variables used by the function. Translate them into the Globals
1035   // list.
1036   MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", 0, 0);
1037   if (!GlobalVars->getFragmentList().empty()) {
1038     if (GlobalVars->getFragmentList().size() != 1)
1039       report_fatal_error("only one .global_variables fragment supported");
1040     const MCFragment &Frag = *GlobalVars->begin();
1041     if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1042       report_fatal_error("only data supported in .global_variables");
1043     const auto &DataFrag = cast<MCDataFragment>(Frag);
1044     if (!DataFrag.getFixups().empty())
1045       report_fatal_error("fixups not supported in .global_variables");
1046     const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1047     for (const uint8_t *p = (const uint8_t *)Contents.data(),
1048                      *end = (const uint8_t *)Contents.data() + Contents.size();
1049          p != end; ) {
1050       WasmGlobal G;
1051       if (end - p < 3)
1052         report_fatal_error("truncated global variable encoding");
1053       G.Type = wasm::ValType(int8_t(*p++));
1054       G.IsMutable = bool(*p++);
1055       G.HasImport = bool(*p++);
1056       if (G.HasImport) {
1057         G.InitialValue = 0;
1058 
1059         WasmImport Import;
1060         Import.ModuleName = (const char *)p;
1061         const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p);
1062         if (!nul)
1063           report_fatal_error("global module name must be nul-terminated");
1064         p = nul + 1;
1065         nul = (const uint8_t *)memchr(p, '\0', end - p);
1066         if (!nul)
1067           report_fatal_error("global base name must be nul-terminated");
1068         Import.FieldName = (const char *)p;
1069         p = nul + 1;
1070 
1071         Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1072         Import.Type = int32_t(G.Type);
1073 
1074         G.ImportIndex = NumGlobalImports;
1075         ++NumGlobalImports;
1076 
1077         Imports.push_back(Import);
1078       } else {
1079         unsigned n;
1080         G.InitialValue = decodeSLEB128(p, &n);
1081         G.ImportIndex = 0;
1082         if ((ptrdiff_t)n > end - p)
1083           report_fatal_error("global initial value must be valid SLEB128");
1084         p += n;
1085       }
1086       Globals.push_back(G);
1087     }
1088   }
1089 
1090   // In the special .stack_pointer section, we've encoded the stack pointer
1091   // index.
1092   MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", 0, 0);
1093   if (!StackPtr->getFragmentList().empty()) {
1094     if (StackPtr->getFragmentList().size() != 1)
1095       report_fatal_error("only one .stack_pointer fragment supported");
1096     const MCFragment &Frag = *StackPtr->begin();
1097     if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1098       report_fatal_error("only data supported in .stack_pointer");
1099     const auto &DataFrag = cast<MCDataFragment>(Frag);
1100     if (!DataFrag.getFixups().empty())
1101       report_fatal_error("fixups not supported in .stack_pointer");
1102     const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1103     if (Contents.size() != 4)
1104       report_fatal_error("only one entry supported in .stack_pointer");
1105     HasStackPointer = true;
1106     StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
1107   }
1108 
1109   // Handle regular defined and undefined symbols.
1110   for (const MCSymbol &S : Asm.symbols()) {
1111     // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1112     // or used in relocations.
1113     if (S.isTemporary() && S.getName().empty())
1114       continue;
1115 
1116     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1117     DEBUG(dbgs() << "MCSymbol: '" << S << "'"
1118                  << " isDefined=" << S.isDefined() << " isExternal="
1119                  << S.isExternal() << " isTemporary=" << S.isTemporary()
1120                  << " isFunction=" << WS.isFunction()
1121                  << " isWeak=" << WS.isWeak()
1122                  << " isVariable=" << WS.isVariable() << "\n");
1123 
1124     if (WS.isWeak())
1125       WeakSymbols.push_back(WS.getName());
1126 
1127     if (WS.isVariable())
1128       continue;
1129 
1130     unsigned Index;
1131 
1132     if (WS.isFunction()) {
1133       if (WS.isDefined(/*SetUsed=*/false)) {
1134         if (WS.getOffset() != 0)
1135           report_fatal_error(
1136               "function sections must contain one function each");
1137 
1138         if (WS.getSize() == 0)
1139           report_fatal_error(
1140               "function symbols must have a size set with .size");
1141 
1142         // A definition. Take the next available index.
1143         Index = NumFuncImports + Functions.size();
1144 
1145         // Prepare the function.
1146         WasmFunction Func;
1147         Func.Type = getFunctionType(WS);
1148         Func.Sym = &WS;
1149         SymbolIndices[&WS] = Index;
1150         Functions.push_back(Func);
1151       } else {
1152         // An import; the index was assigned above.
1153         Index = SymbolIndices.find(&WS)->second;
1154       }
1155 
1156       DEBUG(dbgs() << "  -> function index: " << Index << "\n");
1157 
1158       // If needed, prepare the function to be called indirectly.
1159       if (IsAddressTaken.count(&WS) != 0) {
1160         IndirectSymbolIndices[&WS] = TableElems.size();
1161         DEBUG(dbgs() << "  -> adding to table: " << TableElems.size() << "\n");
1162         TableElems.push_back(Index);
1163       }
1164     } else {
1165       if (WS.isTemporary() && !WS.getSize())
1166         continue;
1167 
1168       if (!WS.isDefined(/*SetUsed=*/false))
1169         continue;
1170 
1171       if (WS.getOffset() != 0)
1172         report_fatal_error("data sections must contain one variable each: " +
1173                            WS.getName());
1174       if (!WS.getSize())
1175         report_fatal_error("data symbols must have a size set with .size: " +
1176                            WS.getName());
1177 
1178       int64_t Size = 0;
1179       if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1180         report_fatal_error(".size expression must be evaluatable");
1181 
1182       auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1183 
1184       if (uint64_t(Size) != Layout.getSectionFileSize(&DataSection))
1185         report_fatal_error("data sections must contain at most one variable");
1186 
1187       DataAlignment = std::max(DataAlignment, DataSection.getAlignment());
1188 
1189       DataSegments.emplace_back();
1190       WasmDataSegment &Segment = DataSegments.back();
1191 
1192       DataSize = alignTo(DataSize, DataSection.getAlignment());
1193       Segment.Offset = DataSize;
1194       Segment.Section = &DataSection;
1195 
1196       // For each global, prepare a corresponding wasm global holding its
1197       // address.  For externals these will also be named exports.
1198       Index = NumGlobalImports + Globals.size();
1199 
1200       WasmGlobal Global;
1201       Global.Type = PtrType;
1202       Global.IsMutable = false;
1203       Global.HasImport = false;
1204       Global.InitialValue = DataSize;
1205       Global.ImportIndex = 0;
1206       SymbolIndices[&WS] = Index;
1207       DEBUG(dbgs() << "  -> global index: " << Index << "\n");
1208       Globals.push_back(Global);
1209 
1210       for (const MCFragment &Frag : DataSection) {
1211         if (Frag.hasInstructions())
1212           report_fatal_error("only data supported in data sections");
1213 
1214         if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
1215           if (Align->getValueSize() != 1)
1216             report_fatal_error("only byte values supported for alignment");
1217           // If nops are requested, use zeros, as this is the data section.
1218           uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
1219           uint64_t Size = std::min<uint64_t>(
1220               alignTo(Segment.Data.size(), Align->getAlignment()),
1221               Segment.Data.size() + Align->getMaxBytesToEmit());
1222           Segment.Data.resize(Size, Value);
1223         } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
1224           Segment.Data.insert(Segment.Data.end(), Fill->getSize(), Fill->getValue());
1225         } else {
1226           const auto &DataFrag = cast<MCDataFragment>(Frag);
1227           const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1228 
1229           Segment.Data.insert(Segment.Data.end(), Contents.begin(),
1230                               Contents.end());
1231         }
1232       }
1233       DataSize += Segment.Data.size();
1234     }
1235 
1236     // If the symbol is visible outside this translation unit, export it.
1237     if ((WS.isExternal() && WS.isDefined(/*SetUsed=*/false))) {
1238       WasmExport Export;
1239       Export.FieldName = WS.getName();
1240       Export.Index = Index;
1241       if (WS.isFunction())
1242         Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1243       else
1244         Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1245       DEBUG(dbgs() << "  -> export " << Exports.size() << "\n");
1246       Exports.push_back(Export);
1247     }
1248   }
1249 
1250   // Handle weak aliases. We need to process these in a separate pass because
1251   // we need to have processed the target of the alias before the alias itself
1252   // and the symbols are not necessarily ordered in this way.
1253   for (const MCSymbol &S : Asm.symbols()) {
1254     if (!S.isVariable())
1255       continue;
1256     assert(S.isDefined(/*SetUsed=*/false));
1257 
1258     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1259     // Find the target symbol of this weak alias and export that index
1260     const MCExpr *Expr = WS.getVariableValue();
1261     auto *Inner = cast<MCSymbolRefExpr>(Expr);
1262     const auto *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol());
1263     DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
1264     assert(SymbolIndices.count(ResolvedSym) > 0);
1265     uint32_t Index = SymbolIndices.find(ResolvedSym)->second;
1266     DEBUG(dbgs() << "  -> index:" << Index << "\n");
1267 
1268     WasmExport Export;
1269     Export.FieldName = WS.getName();
1270     Export.Index = Index;
1271     if (WS.isFunction())
1272       Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1273     else
1274       Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1275     DEBUG(dbgs() << "  -> export " << Exports.size() << "\n");
1276     Exports.push_back(Export);
1277   }
1278 
1279   // Add types for indirect function calls.
1280   for (const WasmRelocationEntry &Fixup : CodeRelocations) {
1281     if (Fixup.Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
1282       continue;
1283 
1284     registerFunctionType(*Fixup.Symbol);
1285   }
1286 
1287   // Write out the Wasm header.
1288   writeHeader(Asm);
1289 
1290   writeTypeSection(FunctionTypes);
1291   writeImportSection(Imports);
1292   writeFunctionSection(Functions);
1293   writeTableSection(TableElems.size());
1294   writeMemorySection(DataSize);
1295   writeGlobalSection();
1296   writeExportSection(Exports);
1297   // TODO: Start Section
1298   writeElemSection(TableElems);
1299   writeCodeSection(Asm, Layout, Functions);
1300   writeDataSection(DataSegments);
1301   writeNameSection(Functions, Imports, NumFuncImports);
1302   writeCodeRelocSection();
1303   writeDataRelocSection();
1304   writeLinkingMetaDataSection(DataSize, DataAlignment, WeakSymbols, HasStackPointer, StackPointerGlobal);
1305 
1306   // TODO: Translate the .comment section to the output.
1307   // TODO: Translate debug sections to the output.
1308 }
1309 
1310 MCObjectWriter *llvm::createWasmObjectWriter(MCWasmObjectTargetWriter *MOTW,
1311                                              raw_pwrite_stream &OS) {
1312   return new WasmObjectWriter(MOTW, OS);
1313 }
1314