xref: /llvm-project/llvm/lib/MC/WasmObjectWriter.cpp (revision de19f7b6d46f1c38e10e604154f0fdaaffde9ebd)
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().ends_with(".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().starts_with(".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 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(Import.Table.Limits.Flags, W->OS);
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     assert(Table.Type.ElemType != wasm::ValType::OTHERREF &&
976            "Cannot encode general ref-typed tables");
977     encodeULEB128((uint32_t)Table.Type.ElemType, W->OS);
978     encodeULEB128(Table.Type.Limits.Flags, W->OS);
979     encodeULEB128(Table.Type.Limits.Minimum, W->OS);
980     if (Table.Type.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
981       encodeULEB128(Table.Type.Limits.Maximum, W->OS);
982   }
983   endSection(Section);
984 }
985 
986 void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
987   if (Exports.empty())
988     return;
989 
990   SectionBookkeeping Section;
991   startSection(Section, wasm::WASM_SEC_EXPORT);
992 
993   encodeULEB128(Exports.size(), W->OS);
994   for (const wasm::WasmExport &Export : Exports) {
995     writeString(Export.Name);
996     W->OS << char(Export.Kind);
997     encodeULEB128(Export.Index, W->OS);
998   }
999 
1000   endSection(Section);
1001 }
1002 
1003 void WasmObjectWriter::writeElemSection(
1004     const MCSymbolWasm *IndirectFunctionTable, ArrayRef<uint32_t> TableElems) {
1005   if (TableElems.empty())
1006     return;
1007 
1008   assert(IndirectFunctionTable);
1009 
1010   SectionBookkeeping Section;
1011   startSection(Section, wasm::WASM_SEC_ELEM);
1012 
1013   encodeULEB128(1, W->OS); // number of "segments"
1014 
1015   assert(WasmIndices.count(IndirectFunctionTable));
1016   uint32_t TableNumber = WasmIndices.find(IndirectFunctionTable)->second;
1017   uint32_t Flags = 0;
1018   if (TableNumber)
1019     Flags |= wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER;
1020   encodeULEB128(Flags, W->OS);
1021   if (Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
1022     encodeULEB128(TableNumber, W->OS); // the table number
1023 
1024   // init expr for starting offset
1025   W->OS << char(is64Bit() ? wasm::WASM_OPCODE_I64_CONST
1026                           : wasm::WASM_OPCODE_I32_CONST);
1027   encodeSLEB128(InitialTableOffset, W->OS);
1028   W->OS << char(wasm::WASM_OPCODE_END);
1029 
1030   if (Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) {
1031     // We only write active function table initializers, for which the elem kind
1032     // is specified to be written as 0x00 and interpreted to mean "funcref".
1033     const uint8_t ElemKind = 0;
1034     W->OS << ElemKind;
1035   }
1036 
1037   encodeULEB128(TableElems.size(), W->OS);
1038   for (uint32_t Elem : TableElems)
1039     encodeULEB128(Elem, W->OS);
1040 
1041   endSection(Section);
1042 }
1043 
1044 void WasmObjectWriter::writeDataCountSection() {
1045   if (DataSegments.empty())
1046     return;
1047 
1048   SectionBookkeeping Section;
1049   startSection(Section, wasm::WASM_SEC_DATACOUNT);
1050   encodeULEB128(DataSegments.size(), W->OS);
1051   endSection(Section);
1052 }
1053 
1054 uint32_t WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
1055                                             const MCAsmLayout &Layout,
1056                                             ArrayRef<WasmFunction> Functions) {
1057   if (Functions.empty())
1058     return 0;
1059 
1060   SectionBookkeeping Section;
1061   startSection(Section, wasm::WASM_SEC_CODE);
1062 
1063   encodeULEB128(Functions.size(), W->OS);
1064 
1065   for (const WasmFunction &Func : Functions) {
1066     auto *FuncSection = static_cast<MCSectionWasm *>(Func.Section);
1067 
1068     int64_t Size = Layout.getSectionAddressSize(FuncSection);
1069     encodeULEB128(Size, W->OS);
1070     FuncSection->setSectionOffset(W->OS.tell() - Section.ContentsOffset);
1071     Asm.writeSectionData(W->OS, FuncSection, Layout);
1072   }
1073 
1074   // Apply fixups.
1075   applyRelocations(CodeRelocations, Section.ContentsOffset, Layout);
1076 
1077   endSection(Section);
1078   return Section.Index;
1079 }
1080 
1081 uint32_t WasmObjectWriter::writeDataSection(const MCAsmLayout &Layout) {
1082   if (DataSegments.empty())
1083     return 0;
1084 
1085   SectionBookkeeping Section;
1086   startSection(Section, wasm::WASM_SEC_DATA);
1087 
1088   encodeULEB128(DataSegments.size(), W->OS); // count
1089 
1090   for (const WasmDataSegment &Segment : DataSegments) {
1091     encodeULEB128(Segment.InitFlags, W->OS); // flags
1092     if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
1093       encodeULEB128(0, W->OS); // memory index
1094     if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
1095       W->OS << char(is64Bit() ? wasm::WASM_OPCODE_I64_CONST
1096                               : wasm::WASM_OPCODE_I32_CONST);
1097       encodeSLEB128(Segment.Offset, W->OS); // offset
1098       W->OS << char(wasm::WASM_OPCODE_END);
1099     }
1100     encodeULEB128(Segment.Data.size(), W->OS); // size
1101     Segment.Section->setSectionOffset(W->OS.tell() - Section.ContentsOffset);
1102     W->OS << Segment.Data; // data
1103   }
1104 
1105   // Apply fixups.
1106   applyRelocations(DataRelocations, Section.ContentsOffset, Layout);
1107 
1108   endSection(Section);
1109   return Section.Index;
1110 }
1111 
1112 void WasmObjectWriter::writeRelocSection(
1113     uint32_t SectionIndex, StringRef Name,
1114     std::vector<WasmRelocationEntry> &Relocs) {
1115   // See: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md
1116   // for descriptions of the reloc sections.
1117 
1118   if (Relocs.empty())
1119     return;
1120 
1121   // First, ensure the relocations are sorted in offset order.  In general they
1122   // should already be sorted since `recordRelocation` is called in offset
1123   // order, but for the code section we combine many MC sections into single
1124   // wasm section, and this order is determined by the order of Asm.Symbols()
1125   // not the sections order.
1126   llvm::stable_sort(
1127       Relocs, [](const WasmRelocationEntry &A, const WasmRelocationEntry &B) {
1128         return (A.Offset + A.FixupSection->getSectionOffset()) <
1129                (B.Offset + B.FixupSection->getSectionOffset());
1130       });
1131 
1132   SectionBookkeeping Section;
1133   startCustomSection(Section, std::string("reloc.") + Name.str());
1134 
1135   encodeULEB128(SectionIndex, W->OS);
1136   encodeULEB128(Relocs.size(), W->OS);
1137   for (const WasmRelocationEntry &RelEntry : Relocs) {
1138     uint64_t Offset =
1139         RelEntry.Offset + RelEntry.FixupSection->getSectionOffset();
1140     uint32_t Index = getRelocationIndexValue(RelEntry);
1141 
1142     W->OS << char(RelEntry.Type);
1143     encodeULEB128(Offset, W->OS);
1144     encodeULEB128(Index, W->OS);
1145     if (RelEntry.hasAddend())
1146       encodeSLEB128(RelEntry.Addend, W->OS);
1147   }
1148 
1149   endSection(Section);
1150 }
1151 
1152 void WasmObjectWriter::writeCustomRelocSections() {
1153   for (const auto &Sec : CustomSections) {
1154     auto &Relocations = CustomSectionsRelocations[Sec.Section];
1155     writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
1156   }
1157 }
1158 
1159 void WasmObjectWriter::writeLinkingMetaDataSection(
1160     ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
1161     ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
1162     const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
1163   SectionBookkeeping Section;
1164   startCustomSection(Section, "linking");
1165   encodeULEB128(wasm::WasmMetadataVersion, W->OS);
1166 
1167   SectionBookkeeping SubSection;
1168   if (SymbolInfos.size() != 0) {
1169     startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
1170     encodeULEB128(SymbolInfos.size(), W->OS);
1171     for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
1172       encodeULEB128(Sym.Kind, W->OS);
1173       encodeULEB128(Sym.Flags, W->OS);
1174       switch (Sym.Kind) {
1175       case wasm::WASM_SYMBOL_TYPE_FUNCTION:
1176       case wasm::WASM_SYMBOL_TYPE_GLOBAL:
1177       case wasm::WASM_SYMBOL_TYPE_TAG:
1178       case wasm::WASM_SYMBOL_TYPE_TABLE:
1179         encodeULEB128(Sym.ElementIndex, W->OS);
1180         if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
1181             (Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
1182           writeString(Sym.Name);
1183         break;
1184       case wasm::WASM_SYMBOL_TYPE_DATA:
1185         writeString(Sym.Name);
1186         if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
1187           encodeULEB128(Sym.DataRef.Segment, W->OS);
1188           encodeULEB128(Sym.DataRef.Offset, W->OS);
1189           encodeULEB128(Sym.DataRef.Size, W->OS);
1190         }
1191         break;
1192       case wasm::WASM_SYMBOL_TYPE_SECTION: {
1193         const uint32_t SectionIndex =
1194             CustomSections[Sym.ElementIndex].OutputIndex;
1195         encodeULEB128(SectionIndex, W->OS);
1196         break;
1197       }
1198       default:
1199         llvm_unreachable("unexpected kind");
1200       }
1201     }
1202     endSection(SubSection);
1203   }
1204 
1205   if (DataSegments.size()) {
1206     startSection(SubSection, wasm::WASM_SEGMENT_INFO);
1207     encodeULEB128(DataSegments.size(), W->OS);
1208     for (const WasmDataSegment &Segment : DataSegments) {
1209       writeString(Segment.Name);
1210       encodeULEB128(Segment.Alignment, W->OS);
1211       encodeULEB128(Segment.LinkingFlags, W->OS);
1212     }
1213     endSection(SubSection);
1214   }
1215 
1216   if (!InitFuncs.empty()) {
1217     startSection(SubSection, wasm::WASM_INIT_FUNCS);
1218     encodeULEB128(InitFuncs.size(), W->OS);
1219     for (auto &StartFunc : InitFuncs) {
1220       encodeULEB128(StartFunc.first, W->OS);  // priority
1221       encodeULEB128(StartFunc.second, W->OS); // function index
1222     }
1223     endSection(SubSection);
1224   }
1225 
1226   if (Comdats.size()) {
1227     startSection(SubSection, wasm::WASM_COMDAT_INFO);
1228     encodeULEB128(Comdats.size(), W->OS);
1229     for (const auto &C : Comdats) {
1230       writeString(C.first);
1231       encodeULEB128(0, W->OS); // flags for future use
1232       encodeULEB128(C.second.size(), W->OS);
1233       for (const WasmComdatEntry &Entry : C.second) {
1234         encodeULEB128(Entry.Kind, W->OS);
1235         encodeULEB128(Entry.Index, W->OS);
1236       }
1237     }
1238     endSection(SubSection);
1239   }
1240 
1241   endSection(Section);
1242 }
1243 
1244 void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
1245                                           const MCAssembler &Asm,
1246                                           const MCAsmLayout &Layout) {
1247   SectionBookkeeping Section;
1248   auto *Sec = CustomSection.Section;
1249   startCustomSection(Section, CustomSection.Name);
1250 
1251   Sec->setSectionOffset(W->OS.tell() - Section.ContentsOffset);
1252   Asm.writeSectionData(W->OS, Sec, Layout);
1253 
1254   CustomSection.OutputContentsOffset = Section.ContentsOffset;
1255   CustomSection.OutputIndex = Section.Index;
1256 
1257   endSection(Section);
1258 
1259   // Apply fixups.
1260   auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1261   applyRelocations(Relocations, CustomSection.OutputContentsOffset, Layout);
1262 }
1263 
1264 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) {
1265   assert(Symbol.isFunction());
1266   assert(TypeIndices.count(&Symbol));
1267   return TypeIndices[&Symbol];
1268 }
1269 
1270 uint32_t WasmObjectWriter::getTagType(const MCSymbolWasm &Symbol) {
1271   assert(Symbol.isTag());
1272   assert(TypeIndices.count(&Symbol));
1273   return TypeIndices[&Symbol];
1274 }
1275 
1276 void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) {
1277   assert(Symbol.isFunction());
1278 
1279   wasm::WasmSignature S;
1280 
1281   if (auto *Sig = Symbol.getSignature()) {
1282     S.Returns = Sig->Returns;
1283     S.Params = Sig->Params;
1284   }
1285 
1286   auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
1287   if (Pair.second)
1288     Signatures.push_back(S);
1289   TypeIndices[&Symbol] = Pair.first->second;
1290 
1291   LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol
1292                     << " new:" << Pair.second << "\n");
1293   LLVM_DEBUG(dbgs() << "  -> type index: " << Pair.first->second << "\n");
1294 }
1295 
1296 void WasmObjectWriter::registerTagType(const MCSymbolWasm &Symbol) {
1297   assert(Symbol.isTag());
1298 
1299   // TODO Currently we don't generate imported exceptions, but if we do, we
1300   // should have a way of infering types of imported exceptions.
1301   wasm::WasmSignature S;
1302   if (auto *Sig = Symbol.getSignature()) {
1303     S.Returns = Sig->Returns;
1304     S.Params = Sig->Params;
1305   }
1306 
1307   auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
1308   if (Pair.second)
1309     Signatures.push_back(S);
1310   TypeIndices[&Symbol] = Pair.first->second;
1311 
1312   LLVM_DEBUG(dbgs() << "registerTagType: " << Symbol << " new:" << Pair.second
1313                     << "\n");
1314   LLVM_DEBUG(dbgs() << "  -> type index: " << Pair.first->second << "\n");
1315 }
1316 
1317 static bool isInSymtab(const MCSymbolWasm &Sym) {
1318   if (Sym.isUsedInReloc() || Sym.isUsedInInitArray())
1319     return true;
1320 
1321   if (Sym.isComdat() && !Sym.isDefined())
1322     return false;
1323 
1324   if (Sym.isTemporary())
1325     return false;
1326 
1327   if (Sym.isSection())
1328     return false;
1329 
1330   if (Sym.omitFromLinkingSection())
1331     return false;
1332 
1333   return true;
1334 }
1335 
1336 void WasmObjectWriter::prepareImports(
1337     SmallVectorImpl<wasm::WasmImport> &Imports, MCAssembler &Asm,
1338     const MCAsmLayout &Layout) {
1339   // For now, always emit the memory import, since loads and stores are not
1340   // valid without it. In the future, we could perhaps be more clever and omit
1341   // it if there are no loads or stores.
1342   wasm::WasmImport MemImport;
1343   MemImport.Module = "env";
1344   MemImport.Field = "__linear_memory";
1345   MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
1346   MemImport.Memory.Flags = is64Bit() ? wasm::WASM_LIMITS_FLAG_IS_64
1347                                      : wasm::WASM_LIMITS_FLAG_NONE;
1348   Imports.push_back(MemImport);
1349 
1350   // Populate SignatureIndices, and Imports and WasmIndices for undefined
1351   // symbols.  This must be done before populating WasmIndices for defined
1352   // symbols.
1353   for (const MCSymbol &S : Asm.symbols()) {
1354     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1355 
1356     // Register types for all functions, including those with private linkage
1357     // (because wasm always needs a type signature).
1358     if (WS.isFunction()) {
1359       const auto *BS = Layout.getBaseSymbol(S);
1360       if (!BS)
1361         report_fatal_error(Twine(S.getName()) +
1362                            ": absolute addressing not supported!");
1363       registerFunctionType(*cast<MCSymbolWasm>(BS));
1364     }
1365 
1366     if (WS.isTag())
1367       registerTagType(WS);
1368 
1369     if (WS.isTemporary())
1370       continue;
1371 
1372     // If the symbol is not defined in this translation unit, import it.
1373     if (!WS.isDefined() && !WS.isComdat()) {
1374       if (WS.isFunction()) {
1375         wasm::WasmImport Import;
1376         Import.Module = WS.getImportModule();
1377         Import.Field = WS.getImportName();
1378         Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1379         Import.SigIndex = getFunctionType(WS);
1380         Imports.push_back(Import);
1381         assert(WasmIndices.count(&WS) == 0);
1382         WasmIndices[&WS] = NumFunctionImports++;
1383       } else if (WS.isGlobal()) {
1384         if (WS.isWeak())
1385           report_fatal_error("undefined global symbol cannot be weak");
1386 
1387         wasm::WasmImport Import;
1388         Import.Field = WS.getImportName();
1389         Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1390         Import.Module = WS.getImportModule();
1391         Import.Global = WS.getGlobalType();
1392         Imports.push_back(Import);
1393         assert(WasmIndices.count(&WS) == 0);
1394         WasmIndices[&WS] = NumGlobalImports++;
1395       } else if (WS.isTag()) {
1396         if (WS.isWeak())
1397           report_fatal_error("undefined tag symbol cannot be weak");
1398 
1399         wasm::WasmImport Import;
1400         Import.Module = WS.getImportModule();
1401         Import.Field = WS.getImportName();
1402         Import.Kind = wasm::WASM_EXTERNAL_TAG;
1403         Import.SigIndex = getTagType(WS);
1404         Imports.push_back(Import);
1405         assert(WasmIndices.count(&WS) == 0);
1406         WasmIndices[&WS] = NumTagImports++;
1407       } else if (WS.isTable()) {
1408         if (WS.isWeak())
1409           report_fatal_error("undefined table symbol cannot be weak");
1410 
1411         wasm::WasmImport Import;
1412         Import.Module = WS.getImportModule();
1413         Import.Field = WS.getImportName();
1414         Import.Kind = wasm::WASM_EXTERNAL_TABLE;
1415         Import.Table = WS.getTableType();
1416         Imports.push_back(Import);
1417         assert(WasmIndices.count(&WS) == 0);
1418         WasmIndices[&WS] = NumTableImports++;
1419       }
1420     }
1421   }
1422 
1423   // Add imports for GOT globals
1424   for (const MCSymbol &S : Asm.symbols()) {
1425     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1426     if (WS.isUsedInGOT()) {
1427       wasm::WasmImport Import;
1428       if (WS.isFunction())
1429         Import.Module = "GOT.func";
1430       else
1431         Import.Module = "GOT.mem";
1432       Import.Field = WS.getName();
1433       Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1434       Import.Global = {wasm::WASM_TYPE_I32, true};
1435       Imports.push_back(Import);
1436       assert(GOTIndices.count(&WS) == 0);
1437       GOTIndices[&WS] = NumGlobalImports++;
1438     }
1439   }
1440 }
1441 
1442 uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
1443                                        const MCAsmLayout &Layout) {
1444   support::endian::Writer MainWriter(*OS, llvm::endianness::little);
1445   W = &MainWriter;
1446   if (IsSplitDwarf) {
1447     uint64_t TotalSize = writeOneObject(Asm, Layout, DwoMode::NonDwoOnly);
1448     assert(DwoOS);
1449     support::endian::Writer DwoWriter(*DwoOS, llvm::endianness::little);
1450     W = &DwoWriter;
1451     return TotalSize + writeOneObject(Asm, Layout, DwoMode::DwoOnly);
1452   } else {
1453     return writeOneObject(Asm, Layout, DwoMode::AllSections);
1454   }
1455 }
1456 
1457 uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
1458                                           const MCAsmLayout &Layout,
1459                                           DwoMode Mode) {
1460   uint64_t StartOffset = W->OS.tell();
1461   SectionCount = 0;
1462   CustomSections.clear();
1463 
1464   LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
1465 
1466   // Collect information from the available symbols.
1467   SmallVector<WasmFunction, 4> Functions;
1468   SmallVector<uint32_t, 4> TableElems;
1469   SmallVector<wasm::WasmImport, 4> Imports;
1470   SmallVector<wasm::WasmExport, 4> Exports;
1471   SmallVector<uint32_t, 2> TagTypes;
1472   SmallVector<wasm::WasmGlobal, 1> Globals;
1473   SmallVector<wasm::WasmTable, 1> Tables;
1474   SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
1475   SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
1476   std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
1477   uint64_t DataSize = 0;
1478   if (Mode != DwoMode::DwoOnly) {
1479     prepareImports(Imports, Asm, Layout);
1480   }
1481 
1482   // Populate DataSegments and CustomSections, which must be done before
1483   // populating DataLocations.
1484   for (MCSection &Sec : Asm) {
1485     auto &Section = static_cast<MCSectionWasm &>(Sec);
1486     StringRef SectionName = Section.getName();
1487 
1488     if (Mode == DwoMode::NonDwoOnly && isDwoSection(Sec))
1489       continue;
1490     if (Mode == DwoMode::DwoOnly && !isDwoSection(Sec))
1491       continue;
1492 
1493     LLVM_DEBUG(dbgs() << "Processing Section " << SectionName << "  group "
1494                       << Section.getGroup() << "\n";);
1495 
1496     // .init_array sections are handled specially elsewhere.
1497     if (SectionName.starts_with(".init_array"))
1498       continue;
1499 
1500     // Code is handled separately
1501     if (Section.getKind().isText())
1502       continue;
1503 
1504     if (Section.isWasmData()) {
1505       uint32_t SegmentIndex = DataSegments.size();
1506       DataSize = alignTo(DataSize, Section.getAlign());
1507       DataSegments.emplace_back();
1508       WasmDataSegment &Segment = DataSegments.back();
1509       Segment.Name = SectionName;
1510       Segment.InitFlags = Section.getPassive()
1511                               ? (uint32_t)wasm::WASM_DATA_SEGMENT_IS_PASSIVE
1512                               : 0;
1513       Segment.Offset = DataSize;
1514       Segment.Section = &Section;
1515       addData(Segment.Data, Section);
1516       Segment.Alignment = Log2(Section.getAlign());
1517       Segment.LinkingFlags = Section.getSegmentFlags();
1518       DataSize += Segment.Data.size();
1519       Section.setSegmentIndex(SegmentIndex);
1520 
1521       if (const MCSymbolWasm *C = Section.getGroup()) {
1522         Comdats[C->getName()].emplace_back(
1523             WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
1524       }
1525     } else {
1526       // Create custom sections
1527       assert(Sec.getKind().isMetadata());
1528 
1529       StringRef Name = SectionName;
1530 
1531       // For user-defined custom sections, strip the prefix
1532       Name.consume_front(".custom_section.");
1533 
1534       MCSymbol *Begin = Sec.getBeginSymbol();
1535       if (Begin) {
1536         assert(WasmIndices.count(cast<MCSymbolWasm>(Begin)) == 0);
1537         WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size();
1538       }
1539 
1540       // Separate out the producers and target features sections
1541       if (Name == "producers") {
1542         ProducersSection = std::make_unique<WasmCustomSection>(Name, &Section);
1543         continue;
1544       }
1545       if (Name == "target_features") {
1546         TargetFeaturesSection =
1547             std::make_unique<WasmCustomSection>(Name, &Section);
1548         continue;
1549       }
1550 
1551       // Custom sections can also belong to COMDAT groups. In this case the
1552       // decriptor's "index" field is the section index (in the final object
1553       // file), but that is not known until after layout, so it must be fixed up
1554       // later
1555       if (const MCSymbolWasm *C = Section.getGroup()) {
1556         Comdats[C->getName()].emplace_back(
1557             WasmComdatEntry{wasm::WASM_COMDAT_SECTION,
1558                             static_cast<uint32_t>(CustomSections.size())});
1559       }
1560 
1561       CustomSections.emplace_back(Name, &Section);
1562     }
1563   }
1564 
1565   if (Mode != DwoMode::DwoOnly) {
1566     // Populate WasmIndices and DataLocations for defined symbols.
1567     for (const MCSymbol &S : Asm.symbols()) {
1568       // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1569       // or used in relocations.
1570       if (S.isTemporary() && S.getName().empty())
1571         continue;
1572 
1573       const auto &WS = static_cast<const MCSymbolWasm &>(S);
1574       LLVM_DEBUG(
1575           dbgs() << "MCSymbol: "
1576                  << toString(WS.getType().value_or(wasm::WASM_SYMBOL_TYPE_DATA))
1577                  << " '" << S << "'"
1578                  << " isDefined=" << S.isDefined() << " isExternal="
1579                  << S.isExternal() << " isTemporary=" << S.isTemporary()
1580                  << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden()
1581                  << " isVariable=" << WS.isVariable() << "\n");
1582 
1583       if (WS.isVariable())
1584         continue;
1585       if (WS.isComdat() && !WS.isDefined())
1586         continue;
1587 
1588       if (WS.isFunction()) {
1589         unsigned Index;
1590         if (WS.isDefined()) {
1591           if (WS.getOffset() != 0)
1592             report_fatal_error(
1593                 "function sections must contain one function each");
1594 
1595           // A definition. Write out the function body.
1596           Index = NumFunctionImports + Functions.size();
1597           WasmFunction Func;
1598           Func.SigIndex = getFunctionType(WS);
1599           Func.Section = &WS.getSection();
1600           assert(WasmIndices.count(&WS) == 0);
1601           WasmIndices[&WS] = Index;
1602           Functions.push_back(Func);
1603 
1604           auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
1605           if (const MCSymbolWasm *C = Section.getGroup()) {
1606             Comdats[C->getName()].emplace_back(
1607                 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
1608           }
1609 
1610           if (WS.hasExportName()) {
1611             wasm::WasmExport Export;
1612             Export.Name = WS.getExportName();
1613             Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1614             Export.Index = Index;
1615             Exports.push_back(Export);
1616           }
1617         } else {
1618           // An import; the index was assigned above.
1619           Index = WasmIndices.find(&WS)->second;
1620         }
1621 
1622         LLVM_DEBUG(dbgs() << "  -> function index: " << Index << "\n");
1623 
1624       } else if (WS.isData()) {
1625         if (!isInSymtab(WS))
1626           continue;
1627 
1628         if (!WS.isDefined()) {
1629           LLVM_DEBUG(dbgs() << "  -> segment index: -1"
1630                             << "\n");
1631           continue;
1632         }
1633 
1634         if (!WS.getSize())
1635           report_fatal_error("data symbols must have a size set with .size: " +
1636                              WS.getName());
1637 
1638         int64_t Size = 0;
1639         if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1640           report_fatal_error(".size expression must be evaluatable");
1641 
1642         auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1643         if (!DataSection.isWasmData())
1644           report_fatal_error("data symbols must live in a data section: " +
1645                              WS.getName());
1646 
1647         // For each data symbol, export it in the symtab as a reference to the
1648         // corresponding Wasm data segment.
1649         wasm::WasmDataReference Ref = wasm::WasmDataReference{
1650             DataSection.getSegmentIndex(), Layout.getSymbolOffset(WS),
1651             static_cast<uint64_t>(Size)};
1652         assert(DataLocations.count(&WS) == 0);
1653         DataLocations[&WS] = Ref;
1654         LLVM_DEBUG(dbgs() << "  -> segment index: " << Ref.Segment << "\n");
1655 
1656       } else if (WS.isGlobal()) {
1657         // A "true" Wasm global (currently just __stack_pointer)
1658         if (WS.isDefined()) {
1659           wasm::WasmGlobal Global;
1660           Global.Type = WS.getGlobalType();
1661           Global.Index = NumGlobalImports + Globals.size();
1662           Global.InitExpr.Extended = false;
1663           switch (Global.Type.Type) {
1664           case wasm::WASM_TYPE_I32:
1665             Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST;
1666             break;
1667           case wasm::WASM_TYPE_I64:
1668             Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_I64_CONST;
1669             break;
1670           case wasm::WASM_TYPE_F32:
1671             Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_F32_CONST;
1672             break;
1673           case wasm::WASM_TYPE_F64:
1674             Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_F64_CONST;
1675             break;
1676           case wasm::WASM_TYPE_EXTERNREF:
1677             Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_REF_NULL;
1678             break;
1679           default:
1680             llvm_unreachable("unexpected type");
1681           }
1682           assert(WasmIndices.count(&WS) == 0);
1683           WasmIndices[&WS] = Global.Index;
1684           Globals.push_back(Global);
1685         } else {
1686           // An import; the index was assigned above
1687           LLVM_DEBUG(dbgs() << "  -> global index: "
1688                             << WasmIndices.find(&WS)->second << "\n");
1689         }
1690       } else if (WS.isTable()) {
1691         if (WS.isDefined()) {
1692           wasm::WasmTable Table;
1693           Table.Index = NumTableImports + Tables.size();
1694           Table.Type = WS.getTableType();
1695           assert(WasmIndices.count(&WS) == 0);
1696           WasmIndices[&WS] = Table.Index;
1697           Tables.push_back(Table);
1698         }
1699         LLVM_DEBUG(dbgs() << " -> table index: "
1700                           << WasmIndices.find(&WS)->second << "\n");
1701       } else if (WS.isTag()) {
1702         // C++ exception symbol (__cpp_exception) or longjmp symbol
1703         // (__c_longjmp)
1704         unsigned Index;
1705         if (WS.isDefined()) {
1706           Index = NumTagImports + TagTypes.size();
1707           uint32_t SigIndex = getTagType(WS);
1708           assert(WasmIndices.count(&WS) == 0);
1709           WasmIndices[&WS] = Index;
1710           TagTypes.push_back(SigIndex);
1711         } else {
1712           // An import; the index was assigned above.
1713           assert(WasmIndices.count(&WS) > 0);
1714         }
1715         LLVM_DEBUG(dbgs() << "  -> tag index: " << WasmIndices.find(&WS)->second
1716                           << "\n");
1717 
1718       } else {
1719         assert(WS.isSection());
1720       }
1721     }
1722 
1723     // Populate WasmIndices and DataLocations for aliased symbols.  We need to
1724     // process these in a separate pass because we need to have processed the
1725     // target of the alias before the alias itself and the symbols are not
1726     // necessarily ordered in this way.
1727     for (const MCSymbol &S : Asm.symbols()) {
1728       if (!S.isVariable())
1729         continue;
1730 
1731       assert(S.isDefined());
1732 
1733       const auto *BS = Layout.getBaseSymbol(S);
1734       if (!BS)
1735         report_fatal_error(Twine(S.getName()) +
1736                            ": absolute addressing not supported!");
1737       const MCSymbolWasm *Base = cast<MCSymbolWasm>(BS);
1738 
1739       // Find the target symbol of this weak alias and export that index
1740       const auto &WS = static_cast<const MCSymbolWasm &>(S);
1741       LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *Base
1742                         << "'\n");
1743 
1744       if (Base->isFunction()) {
1745         assert(WasmIndices.count(Base) > 0);
1746         uint32_t WasmIndex = WasmIndices.find(Base)->second;
1747         assert(WasmIndices.count(&WS) == 0);
1748         WasmIndices[&WS] = WasmIndex;
1749         LLVM_DEBUG(dbgs() << "  -> index:" << WasmIndex << "\n");
1750       } else if (Base->isData()) {
1751         auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1752         uint64_t Offset = Layout.getSymbolOffset(S);
1753         int64_t Size = 0;
1754         // For data symbol alias we use the size of the base symbol as the
1755         // size of the alias.  When an offset from the base is involved this
1756         // can result in a offset + size goes past the end of the data section
1757         // which out object format doesn't support.  So we must clamp it.
1758         if (!Base->getSize()->evaluateAsAbsolute(Size, Layout))
1759           report_fatal_error(".size expression must be evaluatable");
1760         const WasmDataSegment &Segment =
1761             DataSegments[DataSection.getSegmentIndex()];
1762         Size =
1763             std::min(static_cast<uint64_t>(Size), Segment.Data.size() - Offset);
1764         wasm::WasmDataReference Ref = wasm::WasmDataReference{
1765             DataSection.getSegmentIndex(),
1766             static_cast<uint32_t>(Layout.getSymbolOffset(S)),
1767             static_cast<uint32_t>(Size)};
1768         DataLocations[&WS] = Ref;
1769         LLVM_DEBUG(dbgs() << "  -> index:" << Ref.Segment << "\n");
1770       } else {
1771         report_fatal_error("don't yet support global/tag aliases");
1772       }
1773     }
1774   }
1775 
1776   // Finally, populate the symbol table itself, in its "natural" order.
1777   for (const MCSymbol &S : Asm.symbols()) {
1778     const auto &WS = static_cast<const MCSymbolWasm &>(S);
1779     if (!isInSymtab(WS)) {
1780       WS.setIndex(InvalidIndex);
1781       continue;
1782     }
1783     LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
1784 
1785     uint32_t Flags = 0;
1786     if (WS.isWeak())
1787       Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
1788     if (WS.isHidden())
1789       Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
1790     if (!WS.isExternal() && WS.isDefined())
1791       Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
1792     if (WS.isUndefined())
1793       Flags |= wasm::WASM_SYMBOL_UNDEFINED;
1794     if (WS.isNoStrip()) {
1795       Flags |= wasm::WASM_SYMBOL_NO_STRIP;
1796       if (isEmscripten()) {
1797         Flags |= wasm::WASM_SYMBOL_EXPORTED;
1798       }
1799     }
1800     if (WS.hasImportName())
1801       Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME;
1802     if (WS.hasExportName())
1803       Flags |= wasm::WASM_SYMBOL_EXPORTED;
1804     if (WS.isTLS())
1805       Flags |= wasm::WASM_SYMBOL_TLS;
1806 
1807     wasm::WasmSymbolInfo Info;
1808     Info.Name = WS.getName();
1809     Info.Kind = WS.getType().value_or(wasm::WASM_SYMBOL_TYPE_DATA);
1810     Info.Flags = Flags;
1811     if (!WS.isData()) {
1812       assert(WasmIndices.count(&WS) > 0);
1813       Info.ElementIndex = WasmIndices.find(&WS)->second;
1814     } else if (WS.isDefined()) {
1815       assert(DataLocations.count(&WS) > 0);
1816       Info.DataRef = DataLocations.find(&WS)->second;
1817     }
1818     WS.setIndex(SymbolInfos.size());
1819     SymbolInfos.emplace_back(Info);
1820   }
1821 
1822   {
1823     auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
1824       // Functions referenced by a relocation need to put in the table.  This is
1825       // purely to make the object file's provisional values readable, and is
1826       // ignored by the linker, which re-calculates the relocations itself.
1827       if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 &&
1828           Rel.Type != wasm::R_WASM_TABLE_INDEX_I64 &&
1829           Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB &&
1830           Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB64 &&
1831           Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB &&
1832           Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB64)
1833         return;
1834       assert(Rel.Symbol->isFunction());
1835       const MCSymbolWasm *Base =
1836           cast<MCSymbolWasm>(Layout.getBaseSymbol(*Rel.Symbol));
1837       uint32_t FunctionIndex = WasmIndices.find(Base)->second;
1838       uint32_t TableIndex = TableElems.size() + InitialTableOffset;
1839       if (TableIndices.try_emplace(Base, TableIndex).second) {
1840         LLVM_DEBUG(dbgs() << "  -> adding " << Base->getName()
1841                           << " to table: " << TableIndex << "\n");
1842         TableElems.push_back(FunctionIndex);
1843         registerFunctionType(*Base);
1844       }
1845     };
1846 
1847     for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1848       HandleReloc(RelEntry);
1849     for (const WasmRelocationEntry &RelEntry : DataRelocations)
1850       HandleReloc(RelEntry);
1851   }
1852 
1853   // Translate .init_array section contents into start functions.
1854   for (const MCSection &S : Asm) {
1855     const auto &WS = static_cast<const MCSectionWasm &>(S);
1856     if (WS.getName().starts_with(".fini_array"))
1857       report_fatal_error(".fini_array sections are unsupported");
1858     if (!WS.getName().starts_with(".init_array"))
1859       continue;
1860     auto IT = WS.begin();
1861     if (IT == WS.end())
1862       continue;
1863     const MCFragment &EmptyFrag = *IT;
1864     if (EmptyFrag.getKind() != MCFragment::FT_Data)
1865       report_fatal_error(".init_array section should be aligned");
1866 
1867     const MCFragment &AlignFrag = *EmptyFrag.getNext();
1868     if (AlignFrag.getKind() != MCFragment::FT_Align)
1869       report_fatal_error(".init_array section should be aligned");
1870     if (cast<MCAlignFragment>(AlignFrag).getAlignment() !=
1871         Align(is64Bit() ? 8 : 4))
1872       report_fatal_error(".init_array section should be aligned for pointers");
1873 
1874     const MCFragment &Frag = *AlignFrag.getNext();
1875     if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1876       report_fatal_error("only data supported in .init_array section");
1877 
1878     uint16_t Priority = UINT16_MAX;
1879     unsigned PrefixLength = strlen(".init_array");
1880     if (WS.getName().size() > PrefixLength) {
1881       if (WS.getName()[PrefixLength] != '.')
1882         report_fatal_error(
1883             ".init_array section priority should start with '.'");
1884       if (WS.getName().substr(PrefixLength + 1).getAsInteger(10, Priority))
1885         report_fatal_error("invalid .init_array section priority");
1886     }
1887     const auto &DataFrag = cast<MCDataFragment>(Frag);
1888     const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1889     for (const uint8_t *
1890              P = (const uint8_t *)Contents.data(),
1891             *End = (const uint8_t *)Contents.data() + Contents.size();
1892          P != End; ++P) {
1893       if (*P != 0)
1894         report_fatal_error("non-symbolic data in .init_array section");
1895     }
1896     for (const MCFixup &Fixup : DataFrag.getFixups()) {
1897       assert(Fixup.getKind() ==
1898              MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1899       const MCExpr *Expr = Fixup.getValue();
1900       auto *SymRef = dyn_cast<MCSymbolRefExpr>(Expr);
1901       if (!SymRef)
1902         report_fatal_error("fixups in .init_array should be symbol references");
1903       const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol());
1904       if (TargetSym.getIndex() == InvalidIndex)
1905         report_fatal_error("symbols in .init_array should exist in symtab");
1906       if (!TargetSym.isFunction())
1907         report_fatal_error("symbols in .init_array should be for functions");
1908       InitFuncs.push_back(
1909           std::make_pair(Priority, TargetSym.getIndex()));
1910     }
1911   }
1912 
1913   // Write out the Wasm header.
1914   writeHeader(Asm);
1915 
1916   uint32_t CodeSectionIndex, DataSectionIndex;
1917   if (Mode != DwoMode::DwoOnly) {
1918     writeTypeSection(Signatures);
1919     writeImportSection(Imports, DataSize, TableElems.size());
1920     writeFunctionSection(Functions);
1921     writeTableSection(Tables);
1922     // Skip the "memory" section; we import the memory instead.
1923     writeTagSection(TagTypes);
1924     writeGlobalSection(Globals);
1925     writeExportSection(Exports);
1926     const MCSymbol *IndirectFunctionTable =
1927         Asm.getContext().lookupSymbol("__indirect_function_table");
1928     writeElemSection(cast_or_null<const MCSymbolWasm>(IndirectFunctionTable),
1929                      TableElems);
1930     writeDataCountSection();
1931 
1932     CodeSectionIndex = writeCodeSection(Asm, Layout, Functions);
1933     DataSectionIndex = writeDataSection(Layout);
1934   }
1935 
1936   // The Sections in the COMDAT list have placeholder indices (their index among
1937   // custom sections, rather than among all sections). Fix them up here.
1938   for (auto &Group : Comdats) {
1939     for (auto &Entry : Group.second) {
1940       if (Entry.Kind == wasm::WASM_COMDAT_SECTION) {
1941         Entry.Index += SectionCount;
1942       }
1943     }
1944   }
1945   for (auto &CustomSection : CustomSections)
1946     writeCustomSection(CustomSection, Asm, Layout);
1947 
1948   if (Mode != DwoMode::DwoOnly) {
1949     writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
1950 
1951     writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
1952     writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
1953   }
1954   writeCustomRelocSections();
1955   if (ProducersSection)
1956     writeCustomSection(*ProducersSection, Asm, Layout);
1957   if (TargetFeaturesSection)
1958     writeCustomSection(*TargetFeaturesSection, Asm, Layout);
1959 
1960   // TODO: Translate the .comment section to the output.
1961   return W->OS.tell() - StartOffset;
1962 }
1963 
1964 std::unique_ptr<MCObjectWriter>
1965 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1966                              raw_pwrite_stream &OS) {
1967   return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
1968 }
1969 
1970 std::unique_ptr<MCObjectWriter>
1971 llvm::createWasmDwoObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1972                                 raw_pwrite_stream &OS,
1973                                 raw_pwrite_stream &DwoOS) {
1974   return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS, DwoOS);
1975 }
1976