OSDN Git Service

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