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