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