1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements Wasm object file writer information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/BinaryFormat/Wasm.h"
16 #include "llvm/Config/llvm-config.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/EndianStream.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/LEB128.h"
33 #include "llvm/Support/StringSaver.h"
34 #include <vector>
35 
36 using namespace llvm;
37 
38 #define DEBUG_TYPE "mc"
39 
40 namespace {
41 
42 // Went we ceate the indirect function table we start at 1, so that there is
43 // and emtpy slot at 0 and therefore calling a null function pointer will trap.
44 static const uint32_t InitialTableOffset = 1;
45 
46 // For patching purposes, we need to remember where each section starts, both
47 // for patching up the section size field, and for patching up references to
48 // locations within the section.
49 struct SectionBookkeeping {
50   // Where the size of the section is written.
51   uint64_t SizeOffset;
52   // Where the section header ends (without custom section name).
53   uint64_t PayloadOffset;
54   // Where the contents of the section starts.
55   uint64_t ContentsOffset;
56   uint32_t Index;
57 };
58 
59 // The signature of a wasm function or event, in a struct capable of being used
60 // as a DenseMap key.
61 // TODO: Consider using wasm::WasmSignature directly instead.
62 struct WasmSignature {
63   // Support empty and tombstone instances, needed by DenseMap.
64   enum { Plain, Empty, Tombstone } State = Plain;
65 
66   // The return types of the function.
67   SmallVector<wasm::ValType, 1> Returns;
68 
69   // The parameter types of the function.
70   SmallVector<wasm::ValType, 4> Params;
71 
72   bool operator==(const WasmSignature &Other) const {
73     return State == Other.State && Returns == Other.Returns &&
74            Params == Other.Params;
75   }
76 };
77 
78 // Traits for using WasmSignature in a DenseMap.
79 struct WasmSignatureDenseMapInfo {
80   static WasmSignature getEmptyKey() {
81     WasmSignature Sig;
82     Sig.State = WasmSignature::Empty;
83     return Sig;
84   }
85   static WasmSignature getTombstoneKey() {
86     WasmSignature Sig;
87     Sig.State = WasmSignature::Tombstone;
88     return Sig;
89   }
90   static unsigned getHashValue(const WasmSignature &Sig) {
91     uintptr_t Value = Sig.State;
92     for (wasm::ValType Ret : Sig.Returns)
93       Value += DenseMapInfo<uint32_t>::getHashValue(uint32_t(Ret));
94     for (wasm::ValType Param : Sig.Params)
95       Value += DenseMapInfo<uint32_t>::getHashValue(uint32_t(Param));
96     return Value;
97   }
98   static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) {
99     return LHS == RHS;
100   }
101 };
102 
103 // A wasm data segment.  A wasm binary contains only a single data section
104 // but that can contain many segments, each with their own virtual location
105 // in memory.  Each MCSection data created by llvm is modeled as its own
106 // wasm data segment.
107 struct WasmDataSegment {
108   MCSectionWasm *Section;
109   StringRef Name;
110   uint32_t InitFlags;
111   uint32_t Offset;
112   uint32_t Alignment;
113   uint32_t LinkerFlags;
114   SmallVector<char, 4> Data;
115 };
116 
117 // A wasm function to be written into the function section.
118 struct WasmFunction {
119   uint32_t SigIndex;
120   const MCSymbolWasm *Sym;
121 };
122 
123 // A wasm global to be written into the global section.
124 struct WasmGlobal {
125   wasm::WasmGlobalType Type;
126   uint64_t InitialValue;
127 };
128 
129 // Information about a single item which is part of a COMDAT.  For each data
130 // segment or function which is in the COMDAT, there is a corresponding
131 // WasmComdatEntry.
132 struct WasmComdatEntry {
133   unsigned Kind;
134   uint32_t Index;
135 };
136 
137 // Information about a single relocation.
138 struct WasmRelocationEntry {
139   uint64_t Offset;                   // Where is the relocation.
140   const MCSymbolWasm *Symbol;        // The symbol to relocate with.
141   int64_t Addend;                    // A value to add to the symbol.
142   unsigned Type;                     // The type of the relocation.
143   const MCSectionWasm *FixupSection; // The section the relocation is targeting.
144 
145   WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
146                       int64_t Addend, unsigned Type,
147                       const MCSectionWasm *FixupSection)
148       : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
149         FixupSection(FixupSection) {}
150 
151   bool hasAddend() const { return wasm::relocTypeHasAddend(Type); }
152 
153   void print(raw_ostream &Out) const {
154     Out << wasm::relocTypetoString(Type) << " Off=" << Offset
155         << ", Sym=" << *Symbol << ", Addend=" << Addend
156         << ", FixupSection=" << FixupSection->getName();
157   }
158 
159 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
160   LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
161 #endif
162 };
163 
164 static const uint32_t InvalidIndex = -1;
165 
166 struct WasmCustomSection {
167 
168   StringRef Name;
169   MCSectionWasm *Section;
170 
171   uint32_t OutputContentsOffset;
172   uint32_t OutputIndex;
173 
174   WasmCustomSection(StringRef Name, MCSectionWasm *Section)
175       : Name(Name), Section(Section), OutputContentsOffset(0),
176         OutputIndex(InvalidIndex) {}
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 // Write X as an (unsigned) LEB value at offset Offset in Stream, padded
187 // to allow patching.
188 static void writePatchableLEB(raw_pwrite_stream &Stream, uint32_t X,
189                               uint64_t Offset) {
190   uint8_t Buffer[5];
191   unsigned SizeLen = encodeULEB128(X, Buffer, 5);
192   assert(SizeLen == 5);
193   Stream.pwrite((char *)Buffer, SizeLen, Offset);
194 }
195 
196 // Write X as an signed LEB value at offset Offset in Stream, padded
197 // to allow patching.
198 static void writePatchableSLEB(raw_pwrite_stream &Stream, int32_t X,
199                                uint64_t Offset) {
200   uint8_t Buffer[5];
201   unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
202   assert(SizeLen == 5);
203   Stream.pwrite((char *)Buffer, SizeLen, Offset);
204 }
205 
206 // Write X as a plain integer value at offset Offset in Stream.
207 static void patchI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
208   uint8_t Buffer[4];
209   support::endian::write32le(Buffer, X);
210   Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
211 }
212 
213 class WasmObjectWriter : public MCObjectWriter {
214   support::endian::Writer W;
215 
216   /// The target specific Wasm writer instance.
217   std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
218 
219   // Relocations for fixing up references in the code section.
220   std::vector<WasmRelocationEntry> CodeRelocations;
221   uint32_t CodeSectionIndex;
222 
223   // Relocations for fixing up references in the data section.
224   std::vector<WasmRelocationEntry> DataRelocations;
225   uint32_t DataSectionIndex;
226 
227   // Index values to use for fixing up call_indirect type indices.
228   // Maps function symbols to the index of the type of the function
229   DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
230   // Maps function symbols to the table element index space. Used
231   // for TABLE_INDEX relocation types (i.e. address taken functions).
232   DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
233   // Maps function/global symbols to the function/global/event/section index
234   // space.
235   DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
236   DenseMap<const MCSymbolWasm *, uint32_t> GOTIndices;
237   // Maps data symbols to the Wasm segment and offset/size with the segment.
238   DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
239 
240   // Stores output data (index, relocations, content offset) for custom
241   // section.
242   std::vector<WasmCustomSection> CustomSections;
243   std::unique_ptr<WasmCustomSection> ProducersSection;
244   std::unique_ptr<WasmCustomSection> TargetFeaturesSection;
245   // Relocations for fixing up references in the custom sections.
246   DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
247       CustomSectionsRelocations;
248 
249   // Map from section to defining function symbol.
250   DenseMap<const MCSection *, const MCSymbol *> SectionFunctions;
251 
252   DenseMap<WasmSignature, uint32_t, WasmSignatureDenseMapInfo> SignatureIndices;
253   SmallVector<WasmSignature, 4> Signatures;
254   SmallVector<WasmDataSegment, 4> DataSegments;
255   unsigned NumFunctionImports = 0;
256   unsigned NumGlobalImports = 0;
257   unsigned NumEventImports = 0;
258   uint32_t SectionCount = 0;
259 
260   // TargetObjectWriter wrappers.
261   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
262   bool isEmscripten() const { return TargetObjectWriter->isEmscripten(); }
263 
264   void startSection(SectionBookkeeping &Section, unsigned SectionId);
265   void startCustomSection(SectionBookkeeping &Section, StringRef Name);
266   void endSection(SectionBookkeeping &Section);
267 
268 public:
269   WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
270                    raw_pwrite_stream &OS)
271       : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
272 
273 private:
274   void reset() override {
275     CodeRelocations.clear();
276     DataRelocations.clear();
277     TypeIndices.clear();
278     WasmIndices.clear();
279     GOTIndices.clear();
280     TableIndices.clear();
281     DataLocations.clear();
282     CustomSections.clear();
283     ProducersSection.reset();
284     TargetFeaturesSection.reset();
285     CustomSectionsRelocations.clear();
286     SignatureIndices.clear();
287     Signatures.clear();
288     DataSegments.clear();
289     SectionFunctions.clear();
290     NumFunctionImports = 0;
291     NumGlobalImports = 0;
292     MCObjectWriter::reset();
293   }
294 
295   void writeHeader(const MCAssembler &Asm);
296 
297   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
298                         const MCFragment *Fragment, const MCFixup &Fixup,
299                         MCValue Target, uint64_t &FixedValue) override;
300 
301   void executePostLayoutBinding(MCAssembler &Asm,
302                                 const MCAsmLayout &Layout) override;
303 
304   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
305 
306   void writeString(const StringRef Str) {
307     encodeULEB128(Str.size(), W.OS);
308     W.OS << Str;
309   }
310 
311   void writeI32(int32_t val) {
312     char Buffer[4];
313     support::endian::write32le(Buffer, val);
314     W.OS.write(Buffer, sizeof(Buffer));
315   }
316 
317   void writeI64(int64_t val) {
318     char Buffer[8];
319     support::endian::write64le(Buffer, val);
320     W.OS.write(Buffer, sizeof(Buffer));
321   }
322 
323   void writeValueType(wasm::ValType Ty) { W.OS << static_cast<char>(Ty); }
324 
325   void writeTypeSection(ArrayRef<WasmSignature> Signatures);
326   void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize,
327                           uint32_t NumElements);
328   void writeFunctionSection(ArrayRef<WasmFunction> Functions);
329   void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
330   void writeElemSection(ArrayRef<uint32_t> TableElems);
331   void writeDataCountSection();
332   void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
333                         ArrayRef<WasmFunction> Functions);
334   void writeDataSection();
335   void writeEventSection(ArrayRef<wasm::WasmEventType> Events);
336   void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals);
337   void writeRelocSection(uint32_t SectionIndex, StringRef Name,
338                          std::vector<WasmRelocationEntry> &Relocations);
339   void writeLinkingMetaDataSection(
340       ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
341       ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
342       const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
343   void writeCustomSection(WasmCustomSection &CustomSection,
344                           const MCAssembler &Asm, const MCAsmLayout &Layout);
345   void writeCustomRelocSections();
346   void
347   updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions,
348                                  const MCAsmLayout &Layout);
349 
350   uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
351   void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
352                         uint64_t ContentsOffset);
353 
354   uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
355   uint32_t getFunctionType(const MCSymbolWasm &Symbol);
356   uint32_t getEventType(const MCSymbolWasm &Symbol);
357   void registerFunctionType(const MCSymbolWasm &Symbol);
358   void registerEventType(const MCSymbolWasm &Symbol);
359 };
360 
361 } // end anonymous namespace
362 
363 // Write out a section header and a patchable section size field.
364 void WasmObjectWriter::startSection(SectionBookkeeping &Section,
365                                     unsigned SectionId) {
366   LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n");
367   W.OS << char(SectionId);
368 
369   Section.SizeOffset = W.OS.tell();
370 
371   // The section size. We don't know the size yet, so reserve enough space
372   // for any 32-bit value; we'll patch it later.
373   encodeULEB128(0, W.OS, 5);
374 
375   // The position where the section starts, for measuring its size.
376   Section.ContentsOffset = W.OS.tell();
377   Section.PayloadOffset = W.OS.tell();
378   Section.Index = SectionCount++;
379 }
380 
381 void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
382                                           StringRef Name) {
383   LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n");
384   startSection(Section, wasm::WASM_SEC_CUSTOM);
385 
386   // The position where the section header ends, for measuring its size.
387   Section.PayloadOffset = W.OS.tell();
388 
389   // Custom sections in wasm also have a string identifier.
390   writeString(Name);
391 
392   // The position where the custom section starts.
393   Section.ContentsOffset = W.OS.tell();
394 }
395 
396 // Now that the section is complete and we know how big it is, patch up the
397 // section size field at the start of the section.
398 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
399   uint64_t Size = W.OS.tell();
400   // /dev/null doesn't support seek/tell and can report offset of 0.
401   // Simply skip this patching in that case.
402   if (!Size)
403     return;
404 
405   Size -= Section.PayloadOffset;
406   if (uint32_t(Size) != Size)
407     report_fatal_error("section size does not fit in a uint32_t");
408 
409   LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n");
410 
411   // Write the final section size to the payload_len field, which follows
412   // the section id byte.
413   writePatchableLEB(static_cast<raw_pwrite_stream &>(W.OS), Size,
414                     Section.SizeOffset);
415 }
416 
417 // Emit the Wasm header.
418 void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
419   W.OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
420   W.write<uint32_t>(wasm::WasmVersion);
421 }
422 
423 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
424                                                 const MCAsmLayout &Layout) {
425   // Build a map of sections to the function that defines them, for use
426   // in recordRelocation.
427   for (const MCSymbol &S : Asm.symbols()) {
428     const auto &WS = static_cast<const MCSymbolWasm &>(S);
429     if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
430       const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());
431       auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
432       if (!Pair.second)
433         report_fatal_error("section already has a defining function: " +
434                            Sec.getName());
435     }
436   }
437 }
438 
439 void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
440                                         const MCAsmLayout &Layout,
441                                         const MCFragment *Fragment,
442                                         const MCFixup &Fixup, MCValue Target,
443                                         uint64_t &FixedValue) {
444   // The WebAssembly backend should never generate FKF_IsPCRel fixups
445   assert(!(Asm.getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
446            MCFixupKindInfo::FKF_IsPCRel));
447 
448   const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
449   uint64_t C = Target.getConstant();
450   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
451   MCContext &Ctx = Asm.getContext();
452 
453   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
454     // To get here the A - B expression must have failed evaluateAsRelocatable.
455     // This means either A or B must be undefined and in WebAssembly we can't
456     // support either of those cases.
457     const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
458     Ctx.reportError(
459         Fixup.getLoc(),
460         Twine("symbol '") + SymB.getName() +
461             "': unsupported subtraction expression used in relocation.");
462     return;
463   }
464 
465   // We either rejected the fixup or folded B into C at this point.
466   const MCSymbolRefExpr *RefA = Target.getSymA();
467   const auto *SymA = cast<MCSymbolWasm>(&RefA->getSymbol());
468 
469   // The .init_array isn't translated as data, so don't do relocations in it.
470   if (FixupSection.getName().startswith(".init_array")) {
471     SymA->setUsedInInitArray();
472     return;
473   }
474 
475   if (SymA->isVariable()) {
476     const MCExpr *Expr = SymA->getVariableValue();
477     const auto *Inner = cast<MCSymbolRefExpr>(Expr);
478     if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
479       llvm_unreachable("weakref used in reloc not yet implemented");
480   }
481 
482   // Put any constant offset in an addend. Offsets can be negative, and
483   // LLVM expects wrapping, in contrast to wasm's immediates which can't
484   // be negative and don't wrap.
485   FixedValue = 0;
486 
487   unsigned Type = TargetObjectWriter->getRelocType(Target, Fixup);
488 
489   // Absolute offset within a section or a function.
490   // Currently only supported for for metadata sections.
491   // See: test/MC/WebAssembly/blockaddress.ll
492   if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
493       Type == wasm::R_WASM_SECTION_OFFSET_I32) {
494     if (!FixupSection.getKind().isMetadata())
495       report_fatal_error("relocations for function or section offsets are "
496                          "only supported in metadata sections");
497 
498     const MCSymbol *SectionSymbol = nullptr;
499     const MCSection &SecA = SymA->getSection();
500     if (SecA.getKind().isText())
501       SectionSymbol = SectionFunctions.find(&SecA)->second;
502     else
503       SectionSymbol = SecA.getBeginSymbol();
504     if (!SectionSymbol)
505       report_fatal_error("section symbol is required for relocation");
506 
507     C += Layout.getSymbolOffset(*SymA);
508     SymA = cast<MCSymbolWasm>(SectionSymbol);
509   }
510 
511   // Relocation other than R_WASM_TYPE_INDEX_LEB are required to be
512   // against a named symbol.
513   if (Type != wasm::R_WASM_TYPE_INDEX_LEB) {
514     if (SymA->getName().empty())
515       report_fatal_error("relocations against un-named temporaries are not yet "
516                          "supported by wasm");
517 
518     SymA->setUsedInReloc();
519   }
520 
521   if (RefA->getKind() == MCSymbolRefExpr::VK_GOT)
522     SymA->setUsedInGOT();
523 
524   WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
525   LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
526 
527   if (FixupSection.isWasmData()) {
528     DataRelocations.push_back(Rec);
529   } else if (FixupSection.getKind().isText()) {
530     CodeRelocations.push_back(Rec);
531   } else if (FixupSection.getKind().isMetadata()) {
532     CustomSectionsRelocations[&FixupSection].push_back(Rec);
533   } else {
534     llvm_unreachable("unexpected section type");
535   }
536 }
537 
538 static const MCSymbolWasm *resolveSymbol(const MCSymbolWasm &Symbol) {
539   const MCSymbolWasm* Ret = &Symbol;
540   while (Ret->isVariable()) {
541     const MCExpr *Expr = Ret->getVariableValue();
542     auto *Inner = cast<MCSymbolRefExpr>(Expr);
543     Ret = cast<MCSymbolWasm>(&Inner->getSymbol());
544   }
545   return Ret;
546 }
547 
548 // Compute a value to write into the code at the location covered
549 // by RelEntry. This value isn't used by the static linker; it just serves
550 // to make the object format more readable and more likely to be directly
551 // useable.
552 uint32_t
553 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
554   if ((RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB ||
555        RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_I32) &&
556       !RelEntry.Symbol->isGlobal()) {
557     assert(GOTIndices.count(RelEntry.Symbol) > 0 && "symbol not found in GOT index space");
558     return GOTIndices[RelEntry.Symbol];
559   }
560 
561   switch (RelEntry.Type) {
562   case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
563   case wasm::R_WASM_TABLE_INDEX_SLEB:
564   case wasm::R_WASM_TABLE_INDEX_I32: {
565     // Provisional value is table address of the resolved symbol itself
566     const MCSymbolWasm *Sym = resolveSymbol(*RelEntry.Symbol);
567     assert(Sym->isFunction());
568     if (RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB)
569       return TableIndices[Sym] - InitialTableOffset;
570     else
571       return TableIndices[Sym];
572   }
573   case wasm::R_WASM_TYPE_INDEX_LEB:
574     // Provisional value is same as the index
575     return getRelocationIndexValue(RelEntry);
576   case wasm::R_WASM_FUNCTION_INDEX_LEB:
577   case wasm::R_WASM_GLOBAL_INDEX_LEB:
578   case wasm::R_WASM_GLOBAL_INDEX_I32:
579   case wasm::R_WASM_EVENT_INDEX_LEB:
580     // Provisional value is function/global/event Wasm index
581     assert(WasmIndices.count(RelEntry.Symbol) > 0 && "symbol not found in wasm index space");
582     return WasmIndices[RelEntry.Symbol];
583   case wasm::R_WASM_FUNCTION_OFFSET_I32:
584   case wasm::R_WASM_SECTION_OFFSET_I32: {
585     const auto &Section =
586         static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
587     return Section.getSectionOffset() + RelEntry.Addend;
588   }
589   case wasm::R_WASM_MEMORY_ADDR_LEB:
590   case wasm::R_WASM_MEMORY_ADDR_I32:
591   case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
592   case wasm::R_WASM_MEMORY_ADDR_SLEB: {
593     // Provisional value is address of the global
594     const MCSymbolWasm *Sym = resolveSymbol(*RelEntry.Symbol);
595     // For undefined symbols, use zero
596     if (!Sym->isDefined())
597       return 0;
598     const wasm::WasmDataReference &Ref = DataLocations[Sym];
599     const WasmDataSegment &Segment = DataSegments[Ref.Segment];
600     // Ignore overflow. LLVM allows address arithmetic to silently wrap.
601     return Segment.Offset + Ref.Offset + RelEntry.Addend;
602   }
603   default:
604     llvm_unreachable("invalid relocation type");
605   }
606 }
607 
608 static void addData(SmallVectorImpl<char> &DataBytes,
609                     MCSectionWasm &DataSection) {
610   LLVM_DEBUG(errs() << "addData: " << DataSection.getName() << "\n");
611 
612   DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
613 
614   for (const MCFragment &Frag : DataSection) {
615     if (Frag.hasInstructions())
616       report_fatal_error("only data supported in data sections");
617 
618     if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
619       if (Align->getValueSize() != 1)
620         report_fatal_error("only byte values supported for alignment");
621       // If nops are requested, use zeros, as this is the data section.
622       uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
623       uint64_t Size =
624           std::min<uint64_t>(alignTo(DataBytes.size(), Align->getAlignment()),
625                              DataBytes.size() + Align->getMaxBytesToEmit());
626       DataBytes.resize(Size, Value);
627     } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
628       int64_t NumValues;
629       if (!Fill->getNumValues().evaluateAsAbsolute(NumValues))
630         llvm_unreachable("The fill should be an assembler constant");
631       DataBytes.insert(DataBytes.end(), Fill->getValueSize() * NumValues,
632                        Fill->getValue());
633     } else if (auto *LEB = dyn_cast<MCLEBFragment>(&Frag)) {
634       const SmallVectorImpl<char> &Contents = LEB->getContents();
635       DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
636     } else {
637       const auto &DataFrag = cast<MCDataFragment>(Frag);
638       const SmallVectorImpl<char> &Contents = DataFrag.getContents();
639       DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
640     }
641   }
642 
643   LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
644 }
645 
646 uint32_t
647 WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
648   if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) {
649     if (!TypeIndices.count(RelEntry.Symbol))
650       report_fatal_error("symbol not found in type index space: " +
651                          RelEntry.Symbol->getName());
652     return TypeIndices[RelEntry.Symbol];
653   }
654 
655   return RelEntry.Symbol->getIndex();
656 }
657 
658 // Apply the portions of the relocation records that we can handle ourselves
659 // directly.
660 void WasmObjectWriter::applyRelocations(
661     ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
662   auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
663   for (const WasmRelocationEntry &RelEntry : Relocations) {
664     uint64_t Offset = ContentsOffset +
665                       RelEntry.FixupSection->getSectionOffset() +
666                       RelEntry.Offset;
667 
668     LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
669     uint32_t Value = getProvisionalValue(RelEntry);
670 
671     switch (RelEntry.Type) {
672     case wasm::R_WASM_FUNCTION_INDEX_LEB:
673     case wasm::R_WASM_TYPE_INDEX_LEB:
674     case wasm::R_WASM_GLOBAL_INDEX_LEB:
675     case wasm::R_WASM_MEMORY_ADDR_LEB:
676     case wasm::R_WASM_EVENT_INDEX_LEB:
677       writePatchableLEB(Stream, Value, Offset);
678       break;
679     case wasm::R_WASM_TABLE_INDEX_I32:
680     case wasm::R_WASM_MEMORY_ADDR_I32:
681     case wasm::R_WASM_FUNCTION_OFFSET_I32:
682     case wasm::R_WASM_SECTION_OFFSET_I32:
683     case wasm::R_WASM_GLOBAL_INDEX_I32:
684       patchI32(Stream, Value, Offset);
685       break;
686     case wasm::R_WASM_TABLE_INDEX_SLEB:
687     case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
688     case wasm::R_WASM_MEMORY_ADDR_SLEB:
689     case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
690       writePatchableSLEB(Stream, Value, Offset);
691       break;
692     default:
693       llvm_unreachable("invalid relocation type");
694     }
695   }
696 }
697 
698 void WasmObjectWriter::writeTypeSection(ArrayRef<WasmSignature> Signatures) {
699   if (Signatures.empty())
700     return;
701 
702   SectionBookkeeping Section;
703   startSection(Section, wasm::WASM_SEC_TYPE);
704 
705   encodeULEB128(Signatures.size(), W.OS);
706 
707   for (const WasmSignature &Sig : Signatures) {
708     W.OS << char(wasm::WASM_TYPE_FUNC);
709     encodeULEB128(Sig.Params.size(), W.OS);
710     for (wasm::ValType Ty : Sig.Params)
711       writeValueType(Ty);
712     encodeULEB128(Sig.Returns.size(), W.OS);
713     for (wasm::ValType Ty : Sig.Returns)
714       writeValueType(Ty);
715   }
716 
717   endSection(Section);
718 }
719 
720 void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
721                                           uint32_t DataSize,
722                                           uint32_t NumElements) {
723   if (Imports.empty())
724     return;
725 
726   uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
727 
728   SectionBookkeeping Section;
729   startSection(Section, wasm::WASM_SEC_IMPORT);
730 
731   encodeULEB128(Imports.size(), W.OS);
732   for (const wasm::WasmImport &Import : Imports) {
733     writeString(Import.Module);
734     writeString(Import.Field);
735     W.OS << char(Import.Kind);
736 
737     switch (Import.Kind) {
738     case wasm::WASM_EXTERNAL_FUNCTION:
739       encodeULEB128(Import.SigIndex, W.OS);
740       break;
741     case wasm::WASM_EXTERNAL_GLOBAL:
742       W.OS << char(Import.Global.Type);
743       W.OS << char(Import.Global.Mutable ? 1 : 0);
744       break;
745     case wasm::WASM_EXTERNAL_MEMORY:
746       encodeULEB128(0, W.OS);        // flags
747       encodeULEB128(NumPages, W.OS); // initial
748       break;
749     case wasm::WASM_EXTERNAL_TABLE:
750       W.OS << char(Import.Table.ElemType);
751       encodeULEB128(0, W.OS);           // flags
752       encodeULEB128(NumElements, W.OS); // initial
753       break;
754     case wasm::WASM_EXTERNAL_EVENT:
755       encodeULEB128(Import.Event.Attribute, W.OS);
756       encodeULEB128(Import.Event.SigIndex, W.OS);
757       break;
758     default:
759       llvm_unreachable("unsupported import kind");
760     }
761   }
762 
763   endSection(Section);
764 }
765 
766 void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
767   if (Functions.empty())
768     return;
769 
770   SectionBookkeeping Section;
771   startSection(Section, wasm::WASM_SEC_FUNCTION);
772 
773   encodeULEB128(Functions.size(), W.OS);
774   for (const WasmFunction &Func : Functions)
775     encodeULEB128(Func.SigIndex, W.OS);
776 
777   endSection(Section);
778 }
779 
780 void WasmObjectWriter::writeEventSection(ArrayRef<wasm::WasmEventType> Events) {
781   if (Events.empty())
782     return;
783 
784   SectionBookkeeping Section;
785   startSection(Section, wasm::WASM_SEC_EVENT);
786 
787   encodeULEB128(Events.size(), W.OS);
788   for (const wasm::WasmEventType &Event : Events) {
789     encodeULEB128(Event.Attribute, W.OS);
790     encodeULEB128(Event.SigIndex, W.OS);
791   }
792 
793   endSection(Section);
794 }
795 
796 void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) {
797   if (Globals.empty())
798     return;
799 
800   SectionBookkeeping Section;
801   startSection(Section, wasm::WASM_SEC_GLOBAL);
802 
803   encodeULEB128(Globals.size(), W.OS);
804   for (const wasm::WasmGlobal &Global : Globals) {
805     encodeULEB128(Global.Type.Type, W.OS);
806     W.OS << char(Global.Type.Mutable);
807     W.OS << char(Global.InitExpr.Opcode);
808     switch (Global.Type.Type) {
809     case wasm::WASM_TYPE_I32:
810       encodeSLEB128(0, W.OS);
811       break;
812     case wasm::WASM_TYPE_I64:
813       encodeSLEB128(0, W.OS);
814       break;
815     case wasm::WASM_TYPE_F32:
816       writeI32(0);
817       break;
818     case wasm::WASM_TYPE_F64:
819       writeI64(0);
820       break;
821     default:
822       llvm_unreachable("unexpected type");
823     }
824     W.OS << char(wasm::WASM_OPCODE_END);
825   }
826 
827   endSection(Section);
828 }
829 
830 void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
831   if (Exports.empty())
832     return;
833 
834   SectionBookkeeping Section;
835   startSection(Section, wasm::WASM_SEC_EXPORT);
836 
837   encodeULEB128(Exports.size(), W.OS);
838   for (const wasm::WasmExport &Export : Exports) {
839     writeString(Export.Name);
840     W.OS << char(Export.Kind);
841     encodeULEB128(Export.Index, W.OS);
842   }
843 
844   endSection(Section);
845 }
846 
847 void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
848   if (TableElems.empty())
849     return;
850 
851   SectionBookkeeping Section;
852   startSection(Section, wasm::WASM_SEC_ELEM);
853 
854   encodeULEB128(1, W.OS); // number of "segments"
855   encodeULEB128(0, W.OS); // the table index
856 
857   // init expr for starting offset
858   W.OS << char(wasm::WASM_OPCODE_I32_CONST);
859   encodeSLEB128(InitialTableOffset, W.OS);
860   W.OS << char(wasm::WASM_OPCODE_END);
861 
862   encodeULEB128(TableElems.size(), W.OS);
863   for (uint32_t Elem : TableElems)
864     encodeULEB128(Elem, W.OS);
865 
866   endSection(Section);
867 }
868 
869 void WasmObjectWriter::writeDataCountSection() {
870   if (DataSegments.empty())
871     return;
872 
873   SectionBookkeeping Section;
874   startSection(Section, wasm::WASM_SEC_DATACOUNT);
875   encodeULEB128(DataSegments.size(), W.OS);
876   endSection(Section);
877 }
878 
879 void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
880                                         const MCAsmLayout &Layout,
881                                         ArrayRef<WasmFunction> Functions) {
882   if (Functions.empty())
883     return;
884 
885   SectionBookkeeping Section;
886   startSection(Section, wasm::WASM_SEC_CODE);
887   CodeSectionIndex = Section.Index;
888 
889   encodeULEB128(Functions.size(), W.OS);
890 
891   for (const WasmFunction &Func : Functions) {
892     auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
893 
894     int64_t Size = 0;
895     if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
896       report_fatal_error(".size expression must be evaluatable");
897 
898     encodeULEB128(Size, W.OS);
899     FuncSection.setSectionOffset(W.OS.tell() - Section.ContentsOffset);
900     Asm.writeSectionData(W.OS, &FuncSection, Layout);
901   }
902 
903   // Apply fixups.
904   applyRelocations(CodeRelocations, Section.ContentsOffset);
905 
906   endSection(Section);
907 }
908 
909 void WasmObjectWriter::writeDataSection() {
910   if (DataSegments.empty())
911     return;
912 
913   SectionBookkeeping Section;
914   startSection(Section, wasm::WASM_SEC_DATA);
915   DataSectionIndex = Section.Index;
916 
917   encodeULEB128(DataSegments.size(), W.OS); // count
918 
919   for (const WasmDataSegment &Segment : DataSegments) {
920     encodeULEB128(Segment.InitFlags, W.OS); // flags
921     if (Segment.InitFlags & wasm::WASM_SEGMENT_HAS_MEMINDEX)
922       encodeULEB128(0, W.OS); // memory index
923     if ((Segment.InitFlags & wasm::WASM_SEGMENT_IS_PASSIVE) == 0) {
924       W.OS << char(wasm::WASM_OPCODE_I32_CONST);
925       encodeSLEB128(Segment.Offset, W.OS); // offset
926       W.OS << char(wasm::WASM_OPCODE_END);
927     }
928     encodeULEB128(Segment.Data.size(), W.OS); // size
929     Segment.Section->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
930     W.OS << Segment.Data; // data
931   }
932 
933   // Apply fixups.
934   applyRelocations(DataRelocations, Section.ContentsOffset);
935 
936   endSection(Section);
937 }
938 
939 void WasmObjectWriter::writeRelocSection(
940     uint32_t SectionIndex, StringRef Name,
941     std::vector<WasmRelocationEntry> &Relocs) {
942   // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
943   // for descriptions of the reloc sections.
944 
945   if (Relocs.empty())
946     return;
947 
948   // First, ensure the relocations are sorted in offset order.  In general they
949   // should already be sorted since `recordRelocation` is called in offset
950   // order, but for the code section we combine many MC sections into single
951   // wasm section, and this order is determined by the order of Asm.Symbols()
952   // not the sections order.
953   llvm::stable_sort(
954       Relocs, [](const WasmRelocationEntry &A, const WasmRelocationEntry &B) {
955         return (A.Offset + A.FixupSection->getSectionOffset()) <
956                (B.Offset + B.FixupSection->getSectionOffset());
957       });
958 
959   SectionBookkeeping Section;
960   startCustomSection(Section, std::string("reloc.") + Name.str());
961 
962   encodeULEB128(SectionIndex, W.OS);
963   encodeULEB128(Relocs.size(), W.OS);
964   for (const WasmRelocationEntry &RelEntry : Relocs) {
965     uint64_t Offset =
966         RelEntry.Offset + RelEntry.FixupSection->getSectionOffset();
967     uint32_t Index = getRelocationIndexValue(RelEntry);
968 
969     W.OS << char(RelEntry.Type);
970     encodeULEB128(Offset, W.OS);
971     encodeULEB128(Index, W.OS);
972     if (RelEntry.hasAddend())
973       encodeSLEB128(RelEntry.Addend, W.OS);
974   }
975 
976   endSection(Section);
977 }
978 
979 void WasmObjectWriter::writeCustomRelocSections() {
980   for (const auto &Sec : CustomSections) {
981     auto &Relocations = CustomSectionsRelocations[Sec.Section];
982     writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
983   }
984 }
985 
986 void WasmObjectWriter::writeLinkingMetaDataSection(
987     ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
988     ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
989     const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
990   SectionBookkeeping Section;
991   startCustomSection(Section, "linking");
992   encodeULEB128(wasm::WasmMetadataVersion, W.OS);
993 
994   SectionBookkeeping SubSection;
995   if (SymbolInfos.size() != 0) {
996     startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
997     encodeULEB128(SymbolInfos.size(), W.OS);
998     for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
999       encodeULEB128(Sym.Kind, W.OS);
1000       encodeULEB128(Sym.Flags, W.OS);
1001       switch (Sym.Kind) {
1002       case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1003       case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1004       case wasm::WASM_SYMBOL_TYPE_EVENT:
1005         encodeULEB128(Sym.ElementIndex, W.OS);
1006         if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
1007             (Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
1008           writeString(Sym.Name);
1009         break;
1010       case wasm::WASM_SYMBOL_TYPE_DATA:
1011         writeString(Sym.Name);
1012         if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
1013           encodeULEB128(Sym.DataRef.Segment, W.OS);
1014           encodeULEB128(Sym.DataRef.Offset, W.OS);
1015           encodeULEB128(Sym.DataRef.Size, W.OS);
1016         }
1017         break;
1018       case wasm::WASM_SYMBOL_TYPE_SECTION: {
1019         const uint32_t SectionIndex =
1020             CustomSections[Sym.ElementIndex].OutputIndex;
1021         encodeULEB128(SectionIndex, W.OS);
1022         break;
1023       }
1024       default:
1025         llvm_unreachable("unexpected kind");
1026       }
1027     }
1028     endSection(SubSection);
1029   }
1030 
1031   if (DataSegments.size()) {
1032     startSection(SubSection, wasm::WASM_SEGMENT_INFO);
1033     encodeULEB128(DataSegments.size(), W.OS);
1034     for (const WasmDataSegment &Segment : DataSegments) {
1035       writeString(Segment.Name);
1036       encodeULEB128(Segment.Alignment, W.OS);
1037       encodeULEB128(Segment.LinkerFlags, W.OS);
1038     }
1039     endSection(SubSection);
1040   }
1041 
1042   if (!InitFuncs.empty()) {
1043     startSection(SubSection, wasm::WASM_INIT_FUNCS);
1044     encodeULEB128(InitFuncs.size(), W.OS);
1045     for (auto &StartFunc : InitFuncs) {
1046       encodeULEB128(StartFunc.first, W.OS);  // priority
1047       encodeULEB128(StartFunc.second, W.OS); // function index
1048     }
1049     endSection(SubSection);
1050   }
1051 
1052   if (Comdats.size()) {
1053     startSection(SubSection, wasm::WASM_COMDAT_INFO);
1054     encodeULEB128(Comdats.size(), W.OS);
1055     for (const auto &C : Comdats) {
1056       writeString(C.first);
1057       encodeULEB128(0, W.OS); // flags for future use
1058       encodeULEB128(C.second.size(), W.OS);
1059       for (const WasmComdatEntry &Entry : C.second) {
1060         encodeULEB128(Entry.Kind, W.OS);
1061         encodeULEB128(Entry.Index, W.OS);
1062       }
1063     }
1064     endSection(SubSection);
1065   }
1066 
1067   endSection(Section);
1068 }
1069 
1070 void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
1071                                           const MCAssembler &Asm,
1072                                           const MCAsmLayout &Layout) {
1073   SectionBookkeeping Section;
1074   auto *Sec = CustomSection.Section;
1075   startCustomSection(Section, CustomSection.Name);
1076 
1077   Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
1078   Asm.writeSectionData(W.OS, Sec, Layout);
1079 
1080   CustomSection.OutputContentsOffset = Section.ContentsOffset;
1081   CustomSection.OutputIndex = Section.Index;
1082 
1083   endSection(Section);
1084 
1085   // Apply fixups.
1086   auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1087   applyRelocations(Relocations, CustomSection.OutputContentsOffset);
1088 }
1089 
1090 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) {
1091   assert(Symbol.isFunction());
1092   assert(TypeIndices.count(&Symbol));
1093   return TypeIndices[&Symbol];
1094 }
1095 
1096 uint32_t WasmObjectWriter::getEventType(const MCSymbolWasm &Symbol) {
1097   assert(Symbol.isEvent());
1098   assert(TypeIndices.count(&Symbol));
1099   return TypeIndices[&Symbol];
1100 }
1101 
1102 void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) {
1103   assert(Symbol.isFunction());
1104 
1105   WasmSignature S;
1106   const MCSymbolWasm *ResolvedSym = resolveSymbol(Symbol);
1107   if (auto *Sig = ResolvedSym->getSignature()) {
1108     S.Returns = Sig->Returns;
1109     S.Params = Sig->Params;
1110   }
1111 
1112   auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
1113   if (Pair.second)
1114     Signatures.push_back(S);
1115   TypeIndices[&Symbol] = Pair.first->second;
1116 
1117   LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol
1118                     << " new:" << Pair.second << "\n");
1119   LLVM_DEBUG(dbgs() << "  -> type index: " << Pair.first->second << "\n");
1120 }
1121 
1122 void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) {
1123   assert(Symbol.isEvent());
1124 
1125   // TODO Currently we don't generate imported exceptions, but if we do, we
1126   // should have a way of infering types of imported exceptions.
1127   WasmSignature S;
1128   if (auto *Sig = Symbol.getSignature()) {
1129     S.Returns = Sig->Returns;
1130     S.Params = Sig->Params;
1131   }
1132 
1133   auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
1134   if (Pair.second)
1135     Signatures.push_back(S);
1136   TypeIndices[&Symbol] = Pair.first->second;
1137 
1138   LLVM_DEBUG(dbgs() << "registerEventType: " << Symbol << " new:" << Pair.second
1139                     << "\n");
1140   LLVM_DEBUG(dbgs() << "  -> type index: " << Pair.first->second << "\n");
1141 }
1142 
1143 static bool isInSymtab(const MCSymbolWasm &Sym) {
1144   if (Sym.isUsedInReloc() || Sym.isUsedInInitArray())
1145     return true;
1146 
1147   if (Sym.isComdat() && !Sym.isDefined())
1148     return false;
1149 
1150   if (Sym.isTemporary())
1151     return false;
1152 
1153   if (Sym.isSection())
1154     return false;
1155 
1156   return true;
1157 }
1158 
1159 uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
1160                                        const MCAsmLayout &Layout) {
1161   uint64_t StartOffset = W.OS.tell();
1162 
1163   LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
1164 
1165   // Collect information from the available symbols.
1166   SmallVector<WasmFunction, 4> Functions;
1167   SmallVector<uint32_t, 4> TableElems;
1168   SmallVector<wasm::WasmImport, 4> Imports;
1169   SmallVector<wasm::WasmExport, 4> Exports;
1170   SmallVector<wasm::WasmEventType, 1> Events;
1171   SmallVector<wasm::WasmGlobal, 1> Globals;
1172   SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
1173   SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
1174   std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
1175   uint32_t DataSize = 0;
1176 
1177   // For now, always emit the memory import, since loads and stores are not
1178   // valid without it. In the future, we could perhaps be more clever and omit
1179   // it if there are no loads or stores.
1180   wasm::WasmImport MemImport;
1181   MemImport.Module = "env";
1182   MemImport.Field = "__linear_memory";
1183   MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
1184   Imports.push_back(MemImport);
1185 
1186   // For now, always emit the table section, since indirect calls are not
1187   // valid without it. In the future, we could perhaps be more clever and omit
1188   // it if there are no indirect calls.
1189   wasm::WasmImport TableImport;
1190   TableImport.Module = "env";
1191   TableImport.Field = "__indirect_function_table";
1192   TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
1193   TableImport.Table.ElemType = wasm::WASM_TYPE_FUNCREF;
1194   Imports.push_back(TableImport);
1195 
1196   // Populate SignatureIndices, and Imports and WasmIndices for undefined
1197   // symbols.  This must be done before populating WasmIndices for defined
1198   // symbols.
1199   for (const MCSymbol &S : Asm.symbols()) {
1200     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1201 
1202     // Register types for all functions, including those with private linkage
1203     // (because wasm always needs a type signature).
1204     if (WS.isFunction())
1205       registerFunctionType(WS);
1206 
1207     if (WS.isEvent())
1208       registerEventType(WS);
1209 
1210     if (WS.isTemporary())
1211       continue;
1212 
1213     // If the symbol is not defined in this translation unit, import it.
1214     if (!WS.isDefined() && !WS.isComdat()) {
1215       if (WS.isFunction()) {
1216         wasm::WasmImport Import;
1217         Import.Module = WS.getImportModule();
1218         Import.Field = WS.getImportName();
1219         Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1220         Import.SigIndex = getFunctionType(WS);
1221         Imports.push_back(Import);
1222         assert(WasmIndices.count(&WS) == 0);
1223         WasmIndices[&WS] = NumFunctionImports++;
1224       } else if (WS.isGlobal()) {
1225         if (WS.isWeak())
1226           report_fatal_error("undefined global symbol cannot be weak");
1227 
1228         wasm::WasmImport Import;
1229         Import.Field = WS.getImportName();
1230         Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1231         Import.Module = WS.getImportModule();
1232         Import.Global = WS.getGlobalType();
1233         Imports.push_back(Import);
1234         assert(WasmIndices.count(&WS) == 0);
1235         WasmIndices[&WS] = NumGlobalImports++;
1236       } else if (WS.isEvent()) {
1237         if (WS.isWeak())
1238           report_fatal_error("undefined event symbol cannot be weak");
1239 
1240         wasm::WasmImport Import;
1241         Import.Module = WS.getImportModule();
1242         Import.Field = WS.getImportName();
1243         Import.Kind = wasm::WASM_EXTERNAL_EVENT;
1244         Import.Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION;
1245         Import.Event.SigIndex = getEventType(WS);
1246         Imports.push_back(Import);
1247         assert(WasmIndices.count(&WS) == 0);
1248         WasmIndices[&WS] = NumEventImports++;
1249       }
1250     }
1251   }
1252 
1253   // Add imports for GOT globals
1254   for (const MCSymbol &S : Asm.symbols()) {
1255     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1256     if (WS.isUsedInGOT()) {
1257       wasm::WasmImport Import;
1258       if (WS.isFunction())
1259         Import.Module = "GOT.func";
1260       else
1261         Import.Module = "GOT.mem";
1262       Import.Field = WS.getName();
1263       Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1264       Import.Global = {wasm::WASM_TYPE_I32, true};
1265       Imports.push_back(Import);
1266       assert(GOTIndices.count(&WS) == 0);
1267       GOTIndices[&WS] = NumGlobalImports++;
1268     }
1269   }
1270 
1271   // Populate DataSegments and CustomSections, which must be done before
1272   // populating DataLocations.
1273   for (MCSection &Sec : Asm) {
1274     auto &Section = static_cast<MCSectionWasm &>(Sec);
1275     StringRef SectionName = Section.getName();
1276 
1277     // .init_array sections are handled specially elsewhere.
1278     if (SectionName.startswith(".init_array"))
1279       continue;
1280 
1281     // Code is handled separately
1282     if (Section.getKind().isText())
1283       continue;
1284 
1285     if (Section.isWasmData()) {
1286       uint32_t SegmentIndex = DataSegments.size();
1287       DataSize = alignTo(DataSize, Section.getAlignment());
1288       DataSegments.emplace_back();
1289       WasmDataSegment &Segment = DataSegments.back();
1290       Segment.Name = SectionName;
1291       Segment.InitFlags =
1292           Section.getPassive() ? (uint32_t)wasm::WASM_SEGMENT_IS_PASSIVE : 0;
1293       Segment.Offset = DataSize;
1294       Segment.Section = &Section;
1295       addData(Segment.Data, Section);
1296       Segment.Alignment = Log2_32(Section.getAlignment());
1297       Segment.LinkerFlags = 0;
1298       DataSize += Segment.Data.size();
1299       Section.setSegmentIndex(SegmentIndex);
1300 
1301       if (const MCSymbolWasm *C = Section.getGroup()) {
1302         Comdats[C->getName()].emplace_back(
1303             WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
1304       }
1305     } else {
1306       // Create custom sections
1307       assert(Sec.getKind().isMetadata());
1308 
1309       StringRef Name = SectionName;
1310 
1311       // For user-defined custom sections, strip the prefix
1312       if (Name.startswith(".custom_section."))
1313         Name = Name.substr(strlen(".custom_section."));
1314 
1315       MCSymbol *Begin = Sec.getBeginSymbol();
1316       if (Begin) {
1317         WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size();
1318         if (SectionName != Begin->getName())
1319           report_fatal_error("section name and begin symbol should match: " +
1320                              Twine(SectionName));
1321       }
1322 
1323       // Separate out the producers and target features sections
1324       if (Name == "producers") {
1325         ProducersSection = std::make_unique<WasmCustomSection>(Name, &Section);
1326         continue;
1327       }
1328       if (Name == "target_features") {
1329         TargetFeaturesSection =
1330             std::make_unique<WasmCustomSection>(Name, &Section);
1331         continue;
1332       }
1333 
1334       CustomSections.emplace_back(Name, &Section);
1335     }
1336   }
1337 
1338   // Populate WasmIndices and DataLocations for defined symbols.
1339   for (const MCSymbol &S : Asm.symbols()) {
1340     // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1341     // or used in relocations.
1342     if (S.isTemporary() && S.getName().empty())
1343       continue;
1344 
1345     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1346     LLVM_DEBUG(
1347         dbgs() << "MCSymbol: " << toString(WS.getType()) << " '" << S << "'"
1348                << " isDefined=" << S.isDefined() << " isExternal="
1349                << S.isExternal() << " isTemporary=" << S.isTemporary()
1350                << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden()
1351                << " isVariable=" << WS.isVariable() << "\n");
1352 
1353     if (WS.isVariable())
1354       continue;
1355     if (WS.isComdat() && !WS.isDefined())
1356       continue;
1357 
1358     if (WS.isFunction()) {
1359       unsigned Index;
1360       if (WS.isDefined()) {
1361         if (WS.getOffset() != 0)
1362           report_fatal_error(
1363               "function sections must contain one function each");
1364 
1365         if (WS.getSize() == nullptr)
1366           report_fatal_error(
1367               "function symbols must have a size set with .size");
1368 
1369         // A definition. Write out the function body.
1370         Index = NumFunctionImports + Functions.size();
1371         WasmFunction Func;
1372         Func.SigIndex = getFunctionType(WS);
1373         Func.Sym = &WS;
1374         WasmIndices[&WS] = Index;
1375         Functions.push_back(Func);
1376 
1377         auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
1378         if (const MCSymbolWasm *C = Section.getGroup()) {
1379           Comdats[C->getName()].emplace_back(
1380               WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
1381         }
1382 
1383         if (WS.hasExportName()) {
1384           wasm::WasmExport Export;
1385           Export.Name = WS.getExportName();
1386           Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1387           Export.Index = Index;
1388           Exports.push_back(Export);
1389         }
1390       } else {
1391         // An import; the index was assigned above.
1392         Index = WasmIndices.find(&WS)->second;
1393       }
1394 
1395       LLVM_DEBUG(dbgs() << "  -> function index: " << Index << "\n");
1396 
1397     } else if (WS.isData()) {
1398       if (!isInSymtab(WS))
1399         continue;
1400 
1401       if (!WS.isDefined()) {
1402         LLVM_DEBUG(dbgs() << "  -> segment index: -1"
1403                           << "\n");
1404         continue;
1405       }
1406 
1407       if (!WS.getSize())
1408         report_fatal_error("data symbols must have a size set with .size: " +
1409                            WS.getName());
1410 
1411       int64_t Size = 0;
1412       if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1413         report_fatal_error(".size expression must be evaluatable");
1414 
1415       auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1416       if (!DataSection.isWasmData())
1417         report_fatal_error("data symbols must live in a data section: " +
1418                            WS.getName());
1419 
1420       // For each data symbol, export it in the symtab as a reference to the
1421       // corresponding Wasm data segment.
1422       wasm::WasmDataReference Ref = wasm::WasmDataReference{
1423           DataSection.getSegmentIndex(),
1424           static_cast<uint32_t>(Layout.getSymbolOffset(WS)),
1425           static_cast<uint32_t>(Size)};
1426       DataLocations[&WS] = Ref;
1427       LLVM_DEBUG(dbgs() << "  -> segment index: " << Ref.Segment << "\n");
1428 
1429     } else if (WS.isGlobal()) {
1430       // A "true" Wasm global (currently just __stack_pointer)
1431       if (WS.isDefined()) {
1432         assert(WasmIndices.count(&WS) == 0);
1433         wasm::WasmGlobal Global;
1434         Global.Type = WS.getGlobalType();
1435         Global.Index = NumGlobalImports + Globals.size();
1436         switch (Global.Type.Type) {
1437         case wasm::WASM_TYPE_I32:
1438           Global.InitExpr.Opcode = wasm::WASM_OPCODE_I32_CONST;
1439           break;
1440         case wasm::WASM_TYPE_I64:
1441           Global.InitExpr.Opcode = wasm::WASM_OPCODE_I64_CONST;
1442           break;
1443         case wasm::WASM_TYPE_F32:
1444           Global.InitExpr.Opcode = wasm::WASM_OPCODE_F32_CONST;
1445           break;
1446         case wasm::WASM_TYPE_F64:
1447           Global.InitExpr.Opcode = wasm::WASM_OPCODE_F64_CONST;
1448           break;
1449         default:
1450           llvm_unreachable("unexpected type");
1451         }
1452         WasmIndices[&WS] = Global.Index;
1453         Globals.push_back(Global);
1454       } else {
1455         // An import; the index was assigned above
1456         LLVM_DEBUG(dbgs() << "  -> global index: "
1457                           << WasmIndices.find(&WS)->second << "\n");
1458       }
1459     } else if (WS.isEvent()) {
1460       // C++ exception symbol (__cpp_exception)
1461       unsigned Index;
1462       if (WS.isDefined()) {
1463         assert(WasmIndices.count(&WS) == 0);
1464         Index = NumEventImports + Events.size();
1465         wasm::WasmEventType Event;
1466         Event.SigIndex = getEventType(WS);
1467         Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION;
1468         WasmIndices[&WS] = Index;
1469         Events.push_back(Event);
1470       } else {
1471         // An import; the index was assigned above.
1472         assert(WasmIndices.count(&WS) > 0);
1473       }
1474       LLVM_DEBUG(dbgs() << "  -> event index: " << WasmIndices.find(&WS)->second
1475                         << "\n");
1476 
1477     } else {
1478       assert(WS.isSection());
1479     }
1480   }
1481 
1482   // Populate WasmIndices and DataLocations for aliased symbols.  We need to
1483   // process these in a separate pass because we need to have processed the
1484   // target of the alias before the alias itself and the symbols are not
1485   // necessarily ordered in this way.
1486   for (const MCSymbol &S : Asm.symbols()) {
1487     if (!S.isVariable())
1488       continue;
1489 
1490     assert(S.isDefined());
1491 
1492     // Find the target symbol of this weak alias and export that index
1493     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1494     const MCSymbolWasm *ResolvedSym = resolveSymbol(WS);
1495     LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym
1496                       << "'\n");
1497 
1498     if (ResolvedSym->isFunction()) {
1499       assert(WasmIndices.count(ResolvedSym) > 0);
1500       uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
1501       assert(WasmIndices.count(&WS) == 0);
1502       WasmIndices[&WS] = WasmIndex;
1503       LLVM_DEBUG(dbgs() << "  -> index:" << WasmIndex << "\n");
1504     } else if (ResolvedSym->isData()) {
1505       assert(DataLocations.count(ResolvedSym) > 0);
1506       const wasm::WasmDataReference &Ref =
1507           DataLocations.find(ResolvedSym)->second;
1508       DataLocations[&WS] = Ref;
1509       LLVM_DEBUG(dbgs() << "  -> index:" << Ref.Segment << "\n");
1510     } else {
1511       report_fatal_error("don't yet support global/event aliases");
1512     }
1513   }
1514 
1515   // Finally, populate the symbol table itself, in its "natural" order.
1516   for (const MCSymbol &S : Asm.symbols()) {
1517     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1518     if (!isInSymtab(WS)) {
1519       WS.setIndex(InvalidIndex);
1520       continue;
1521     }
1522     LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
1523 
1524     uint32_t Flags = 0;
1525     if (WS.isWeak())
1526       Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
1527     if (WS.isHidden())
1528       Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
1529     if (!WS.isExternal() && WS.isDefined())
1530       Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
1531     if (WS.isUndefined())
1532       Flags |= wasm::WASM_SYMBOL_UNDEFINED;
1533     if (WS.isNoStrip()) {
1534       Flags |= wasm::WASM_SYMBOL_NO_STRIP;
1535       if (isEmscripten()) {
1536         Flags |= wasm::WASM_SYMBOL_EXPORTED;
1537       }
1538     }
1539     if (WS.hasImportName())
1540       Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME;
1541     if (WS.hasExportName())
1542       Flags |= wasm::WASM_SYMBOL_EXPORTED;
1543 
1544     wasm::WasmSymbolInfo Info;
1545     Info.Name = WS.getName();
1546     Info.Kind = WS.getType();
1547     Info.Flags = Flags;
1548     if (!WS.isData()) {
1549       assert(WasmIndices.count(&WS) > 0);
1550       Info.ElementIndex = WasmIndices.find(&WS)->second;
1551     } else if (WS.isDefined()) {
1552       assert(DataLocations.count(&WS) > 0);
1553       Info.DataRef = DataLocations.find(&WS)->second;
1554     }
1555     WS.setIndex(SymbolInfos.size());
1556     SymbolInfos.emplace_back(Info);
1557   }
1558 
1559   {
1560     auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
1561       // Functions referenced by a relocation need to put in the table.  This is
1562       // purely to make the object file's provisional values readable, and is
1563       // ignored by the linker, which re-calculates the relocations itself.
1564       if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 &&
1565           Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB &&
1566           Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB)
1567         return;
1568       assert(Rel.Symbol->isFunction());
1569       const MCSymbolWasm &WS = *resolveSymbol(*Rel.Symbol);
1570       uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
1571       uint32_t TableIndex = TableElems.size() + InitialTableOffset;
1572       if (TableIndices.try_emplace(&WS, TableIndex).second) {
1573         LLVM_DEBUG(dbgs() << "  -> adding " << WS.getName()
1574                           << " to table: " << TableIndex << "\n");
1575         TableElems.push_back(FunctionIndex);
1576         registerFunctionType(WS);
1577       }
1578     };
1579 
1580     for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1581       HandleReloc(RelEntry);
1582     for (const WasmRelocationEntry &RelEntry : DataRelocations)
1583       HandleReloc(RelEntry);
1584   }
1585 
1586   // Translate .init_array section contents into start functions.
1587   for (const MCSection &S : Asm) {
1588     const auto &WS = static_cast<const MCSectionWasm &>(S);
1589     if (WS.getName().startswith(".fini_array"))
1590       report_fatal_error(".fini_array sections are unsupported");
1591     if (!WS.getName().startswith(".init_array"))
1592       continue;
1593     if (WS.getFragmentList().empty())
1594       continue;
1595 
1596     // init_array is expected to contain a single non-empty data fragment
1597     if (WS.getFragmentList().size() != 3)
1598       report_fatal_error("only one .init_array section fragment supported");
1599 
1600     auto IT = WS.begin();
1601     const MCFragment &EmptyFrag = *IT;
1602     if (EmptyFrag.getKind() != MCFragment::FT_Data)
1603       report_fatal_error(".init_array section should be aligned");
1604 
1605     IT = std::next(IT);
1606     const MCFragment &AlignFrag = *IT;
1607     if (AlignFrag.getKind() != MCFragment::FT_Align)
1608       report_fatal_error(".init_array section should be aligned");
1609     if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
1610       report_fatal_error(".init_array section should be aligned for pointers");
1611 
1612     const MCFragment &Frag = *std::next(IT);
1613     if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1614       report_fatal_error("only data supported in .init_array section");
1615 
1616     uint16_t Priority = UINT16_MAX;
1617     unsigned PrefixLength = strlen(".init_array");
1618     if (WS.getName().size() > PrefixLength) {
1619       if (WS.getName()[PrefixLength] != '.')
1620         report_fatal_error(
1621             ".init_array section priority should start with '.'");
1622       if (WS.getName().substr(PrefixLength + 1).getAsInteger(10, Priority))
1623         report_fatal_error("invalid .init_array section priority");
1624     }
1625     const auto &DataFrag = cast<MCDataFragment>(Frag);
1626     const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1627     for (const uint8_t *
1628              P = (const uint8_t *)Contents.data(),
1629             *End = (const uint8_t *)Contents.data() + Contents.size();
1630          P != End; ++P) {
1631       if (*P != 0)
1632         report_fatal_error("non-symbolic data in .init_array section");
1633     }
1634     for (const MCFixup &Fixup : DataFrag.getFixups()) {
1635       assert(Fixup.getKind() ==
1636              MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1637       const MCExpr *Expr = Fixup.getValue();
1638       auto *SymRef = dyn_cast<MCSymbolRefExpr>(Expr);
1639       if (!SymRef)
1640         report_fatal_error("fixups in .init_array should be symbol references");
1641       const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol());
1642       if (TargetSym.getIndex() == InvalidIndex)
1643         report_fatal_error("symbols in .init_array should exist in symtab");
1644       if (!TargetSym.isFunction())
1645         report_fatal_error("symbols in .init_array should be for functions");
1646       InitFuncs.push_back(
1647           std::make_pair(Priority, TargetSym.getIndex()));
1648     }
1649   }
1650 
1651   // Write out the Wasm header.
1652   writeHeader(Asm);
1653 
1654   writeTypeSection(Signatures);
1655   writeImportSection(Imports, DataSize, TableElems.size());
1656   writeFunctionSection(Functions);
1657   // Skip the "table" section; we import the table instead.
1658   // Skip the "memory" section; we import the memory instead.
1659   writeEventSection(Events);
1660   writeGlobalSection(Globals);
1661   writeExportSection(Exports);
1662   writeElemSection(TableElems);
1663   writeDataCountSection();
1664   writeCodeSection(Asm, Layout, Functions);
1665   writeDataSection();
1666   for (auto &CustomSection : CustomSections)
1667     writeCustomSection(CustomSection, Asm, Layout);
1668   writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
1669   writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
1670   writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
1671   writeCustomRelocSections();
1672   if (ProducersSection)
1673     writeCustomSection(*ProducersSection, Asm, Layout);
1674   if (TargetFeaturesSection)
1675     writeCustomSection(*TargetFeaturesSection, Asm, Layout);
1676 
1677   // TODO: Translate the .comment section to the output.
1678   return W.OS.tell() - StartOffset;
1679 }
1680 
1681 std::unique_ptr<MCObjectWriter>
1682 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1683                              raw_pwrite_stream &OS) {
1684   return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
1685 }
1686