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