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