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