OSDN Git Service

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