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