OSDN Git Service

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