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