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