10b57cec5SDimitry Andric //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements Wasm object file writer information. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 140b57cec5SDimitry Andric #include "llvm/BinaryFormat/Wasm.h" 15e8d8bef9SDimitry Andric #include "llvm/BinaryFormat/WasmTraits.h" 160b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 170b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h" 180b57cec5SDimitry Andric #include "llvm/MC/MCAssembler.h" 190b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 200b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h" 210b57cec5SDimitry Andric #include "llvm/MC/MCFixupKindInfo.h" 220b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h" 230b57cec5SDimitry Andric #include "llvm/MC/MCSectionWasm.h" 240b57cec5SDimitry Andric #include "llvm/MC/MCSymbolWasm.h" 250b57cec5SDimitry Andric #include "llvm/MC/MCValue.h" 260b57cec5SDimitry Andric #include "llvm/MC/MCWasmObjectWriter.h" 270b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 280b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 295ffd83dbSDimitry Andric #include "llvm/Support/EndianStream.h" 300b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 310b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 320b57cec5SDimitry Andric #include <vector> 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #define DEBUG_TYPE "mc" 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric namespace { 390b57cec5SDimitry Andric 40e8d8bef9SDimitry Andric // When we create the indirect function table we start at 1, so that there is 41e8d8bef9SDimitry Andric // and empty slot at 0 and therefore calling a null function pointer will trap. 420b57cec5SDimitry Andric static const uint32_t InitialTableOffset = 1; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric // For patching purposes, we need to remember where each section starts, both 450b57cec5SDimitry Andric // for patching up the section size field, and for patching up references to 460b57cec5SDimitry Andric // locations within the section. 470b57cec5SDimitry Andric struct SectionBookkeeping { 480b57cec5SDimitry Andric // Where the size of the section is written. 490b57cec5SDimitry Andric uint64_t SizeOffset; 500b57cec5SDimitry Andric // Where the section header ends (without custom section name). 510b57cec5SDimitry Andric uint64_t PayloadOffset; 520b57cec5SDimitry Andric // Where the contents of the section starts. 530b57cec5SDimitry Andric uint64_t ContentsOffset; 540b57cec5SDimitry Andric uint32_t Index; 550b57cec5SDimitry Andric }; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric // A wasm data segment. A wasm binary contains only a single data section 580b57cec5SDimitry Andric // but that can contain many segments, each with their own virtual location 590b57cec5SDimitry Andric // in memory. Each MCSection data created by llvm is modeled as its own 600b57cec5SDimitry Andric // wasm data segment. 610b57cec5SDimitry Andric struct WasmDataSegment { 620b57cec5SDimitry Andric MCSectionWasm *Section; 630b57cec5SDimitry Andric StringRef Name; 640b57cec5SDimitry Andric uint32_t InitFlags; 655ffd83dbSDimitry Andric uint64_t Offset; 660b57cec5SDimitry Andric uint32_t Alignment; 67fe6060f1SDimitry Andric uint32_t LinkingFlags; 680b57cec5SDimitry Andric SmallVector<char, 4> Data; 690b57cec5SDimitry Andric }; 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric // A wasm function to be written into the function section. 720b57cec5SDimitry Andric struct WasmFunction { 730b57cec5SDimitry Andric uint32_t SigIndex; 74bdd1243dSDimitry Andric MCSection *Section; 750b57cec5SDimitry Andric }; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric // A wasm global to be written into the global section. 780b57cec5SDimitry Andric struct WasmGlobal { 790b57cec5SDimitry Andric wasm::WasmGlobalType Type; 800b57cec5SDimitry Andric uint64_t InitialValue; 810b57cec5SDimitry Andric }; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // Information about a single item which is part of a COMDAT. For each data 840b57cec5SDimitry Andric // segment or function which is in the COMDAT, there is a corresponding 850b57cec5SDimitry Andric // WasmComdatEntry. 860b57cec5SDimitry Andric struct WasmComdatEntry { 870b57cec5SDimitry Andric unsigned Kind; 880b57cec5SDimitry Andric uint32_t Index; 890b57cec5SDimitry Andric }; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric // Information about a single relocation. 920b57cec5SDimitry Andric struct WasmRelocationEntry { 930b57cec5SDimitry Andric uint64_t Offset; // Where is the relocation. 940b57cec5SDimitry Andric const MCSymbolWasm *Symbol; // The symbol to relocate with. 950b57cec5SDimitry Andric int64_t Addend; // A value to add to the symbol. 960b57cec5SDimitry Andric unsigned Type; // The type of the relocation. 970b57cec5SDimitry Andric const MCSectionWasm *FixupSection; // The section the relocation is targeting. 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol, 1000b57cec5SDimitry Andric int64_t Addend, unsigned Type, 1010b57cec5SDimitry Andric const MCSectionWasm *FixupSection) 1020b57cec5SDimitry Andric : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type), 1030b57cec5SDimitry Andric FixupSection(FixupSection) {} 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric bool hasAddend() const { return wasm::relocTypeHasAddend(Type); } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric void print(raw_ostream &Out) const { 1080b57cec5SDimitry Andric Out << wasm::relocTypetoString(Type) << " Off=" << Offset 1090b57cec5SDimitry Andric << ", Sym=" << *Symbol << ", Addend=" << Addend 1105ffd83dbSDimitry Andric << ", FixupSection=" << FixupSection->getName(); 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1140b57cec5SDimitry Andric LLVM_DUMP_METHOD void dump() const { print(dbgs()); } 1150b57cec5SDimitry Andric #endif 1160b57cec5SDimitry Andric }; 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric static const uint32_t InvalidIndex = -1; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric struct WasmCustomSection { 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric StringRef Name; 1230b57cec5SDimitry Andric MCSectionWasm *Section; 1240b57cec5SDimitry Andric 12581ad6265SDimitry Andric uint32_t OutputContentsOffset = 0; 12681ad6265SDimitry Andric uint32_t OutputIndex = InvalidIndex; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric WasmCustomSection(StringRef Name, MCSectionWasm *Section) 12981ad6265SDimitry Andric : Name(Name), Section(Section) {} 1300b57cec5SDimitry Andric }; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric #if !defined(NDEBUG) 1330b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) { 1340b57cec5SDimitry Andric Rel.print(OS); 1350b57cec5SDimitry Andric return OS; 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric #endif 1380b57cec5SDimitry Andric 139fb03ea46SDimitry Andric // Write Value as an (unsigned) LEB value at offset Offset in Stream, padded 1400b57cec5SDimitry Andric // to allow patching. 141fb03ea46SDimitry Andric template <typename T, int W> 142fb03ea46SDimitry Andric void writePatchableULEB(raw_pwrite_stream &Stream, T Value, uint64_t Offset) { 1435ffd83dbSDimitry Andric uint8_t Buffer[W]; 144fb03ea46SDimitry Andric unsigned SizeLen = encodeULEB128(Value, Buffer, W); 1455ffd83dbSDimitry Andric assert(SizeLen == W); 1460b57cec5SDimitry Andric Stream.pwrite((char *)Buffer, SizeLen, Offset); 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 149fb03ea46SDimitry Andric // Write Value as an signed LEB value at offset Offset in Stream, padded 1500b57cec5SDimitry Andric // to allow patching. 151fb03ea46SDimitry Andric template <typename T, int W> 152fb03ea46SDimitry Andric void writePatchableSLEB(raw_pwrite_stream &Stream, T Value, uint64_t Offset) { 1535ffd83dbSDimitry Andric uint8_t Buffer[W]; 154fb03ea46SDimitry Andric unsigned SizeLen = encodeSLEB128(Value, Buffer, W); 1555ffd83dbSDimitry Andric assert(SizeLen == W); 1560b57cec5SDimitry Andric Stream.pwrite((char *)Buffer, SizeLen, Offset); 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric 159fb03ea46SDimitry Andric static void writePatchableU32(raw_pwrite_stream &Stream, uint32_t Value, 160fb03ea46SDimitry Andric uint64_t Offset) { 161fb03ea46SDimitry Andric writePatchableULEB<uint32_t, 5>(Stream, Value, Offset); 162fb03ea46SDimitry Andric } 163fb03ea46SDimitry Andric 164fb03ea46SDimitry Andric static void writePatchableS32(raw_pwrite_stream &Stream, int32_t Value, 165fb03ea46SDimitry Andric uint64_t Offset) { 166fb03ea46SDimitry Andric writePatchableSLEB<int32_t, 5>(Stream, Value, Offset); 167fb03ea46SDimitry Andric } 168fb03ea46SDimitry Andric 169fb03ea46SDimitry Andric static void writePatchableU64(raw_pwrite_stream &Stream, uint64_t Value, 170fb03ea46SDimitry Andric uint64_t Offset) { 171fb03ea46SDimitry Andric writePatchableSLEB<uint64_t, 10>(Stream, Value, Offset); 172fb03ea46SDimitry Andric } 173fb03ea46SDimitry Andric 174fb03ea46SDimitry Andric static void writePatchableS64(raw_pwrite_stream &Stream, int64_t Value, 175fb03ea46SDimitry Andric uint64_t Offset) { 176fb03ea46SDimitry Andric writePatchableSLEB<int64_t, 10>(Stream, Value, Offset); 177fb03ea46SDimitry Andric } 178fb03ea46SDimitry Andric 179fb03ea46SDimitry Andric // Write Value as a plain integer value at offset Offset in Stream. 180fb03ea46SDimitry Andric static void patchI32(raw_pwrite_stream &Stream, uint32_t Value, 181fb03ea46SDimitry Andric uint64_t Offset) { 1820b57cec5SDimitry Andric uint8_t Buffer[4]; 183fb03ea46SDimitry Andric support::endian::write32le(Buffer, Value); 1840b57cec5SDimitry Andric Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset); 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric 187fb03ea46SDimitry Andric static void patchI64(raw_pwrite_stream &Stream, uint64_t Value, 188fb03ea46SDimitry Andric uint64_t Offset) { 1895ffd83dbSDimitry Andric uint8_t Buffer[8]; 190fb03ea46SDimitry Andric support::endian::write64le(Buffer, Value); 1915ffd83dbSDimitry Andric Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset); 1925ffd83dbSDimitry Andric } 1935ffd83dbSDimitry Andric 194e8d8bef9SDimitry Andric bool isDwoSection(const MCSection &Sec) { 1955f757f3fSDimitry Andric return Sec.getName().ends_with(".dwo"); 196e8d8bef9SDimitry Andric } 197e8d8bef9SDimitry Andric 1980b57cec5SDimitry Andric class WasmObjectWriter : public MCObjectWriter { 19906c3fb27SDimitry Andric support::endian::Writer *W = nullptr; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric /// The target specific Wasm writer instance. 2020b57cec5SDimitry Andric std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter; 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // Relocations for fixing up references in the code section. 2050b57cec5SDimitry Andric std::vector<WasmRelocationEntry> CodeRelocations; 2060b57cec5SDimitry Andric // Relocations for fixing up references in the data section. 2070b57cec5SDimitry Andric std::vector<WasmRelocationEntry> DataRelocations; 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // Index values to use for fixing up call_indirect type indices. 2100b57cec5SDimitry Andric // Maps function symbols to the index of the type of the function 2110b57cec5SDimitry Andric DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices; 2120b57cec5SDimitry Andric // Maps function symbols to the table element index space. Used 2130b57cec5SDimitry Andric // for TABLE_INDEX relocation types (i.e. address taken functions). 2140b57cec5SDimitry Andric DenseMap<const MCSymbolWasm *, uint32_t> TableIndices; 215e8d8bef9SDimitry Andric // Maps function/global/table symbols to the 216fe6060f1SDimitry Andric // function/global/table/tag/section index space. 2170b57cec5SDimitry Andric DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices; 2180b57cec5SDimitry Andric DenseMap<const MCSymbolWasm *, uint32_t> GOTIndices; 2190b57cec5SDimitry Andric // Maps data symbols to the Wasm segment and offset/size with the segment. 2200b57cec5SDimitry Andric DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations; 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric // Stores output data (index, relocations, content offset) for custom 2230b57cec5SDimitry Andric // section. 2240b57cec5SDimitry Andric std::vector<WasmCustomSection> CustomSections; 2250b57cec5SDimitry Andric std::unique_ptr<WasmCustomSection> ProducersSection; 2260b57cec5SDimitry Andric std::unique_ptr<WasmCustomSection> TargetFeaturesSection; 2270b57cec5SDimitry Andric // Relocations for fixing up references in the custom sections. 2280b57cec5SDimitry Andric DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>> 2290b57cec5SDimitry Andric CustomSectionsRelocations; 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric // Map from section to defining function symbol. 2320b57cec5SDimitry Andric DenseMap<const MCSection *, const MCSymbol *> SectionFunctions; 2330b57cec5SDimitry Andric 234e8d8bef9SDimitry Andric DenseMap<wasm::WasmSignature, uint32_t> SignatureIndices; 235e8d8bef9SDimitry Andric SmallVector<wasm::WasmSignature, 4> Signatures; 2360b57cec5SDimitry Andric SmallVector<WasmDataSegment, 4> DataSegments; 2370b57cec5SDimitry Andric unsigned NumFunctionImports = 0; 2380b57cec5SDimitry Andric unsigned NumGlobalImports = 0; 239e8d8bef9SDimitry Andric unsigned NumTableImports = 0; 240fe6060f1SDimitry Andric unsigned NumTagImports = 0; 2410b57cec5SDimitry Andric uint32_t SectionCount = 0; 2420b57cec5SDimitry Andric 243e8d8bef9SDimitry Andric enum class DwoMode { 244e8d8bef9SDimitry Andric AllSections, 245e8d8bef9SDimitry Andric NonDwoOnly, 246e8d8bef9SDimitry Andric DwoOnly, 247e8d8bef9SDimitry Andric }; 248e8d8bef9SDimitry Andric bool IsSplitDwarf = false; 249e8d8bef9SDimitry Andric raw_pwrite_stream *OS = nullptr; 250e8d8bef9SDimitry Andric raw_pwrite_stream *DwoOS = nullptr; 251e8d8bef9SDimitry Andric 252e8d8bef9SDimitry Andric // TargetObjectWriter wranppers. 2530b57cec5SDimitry Andric bool is64Bit() const { return TargetObjectWriter->is64Bit(); } 2548bcb0991SDimitry Andric bool isEmscripten() const { return TargetObjectWriter->isEmscripten(); } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric void startSection(SectionBookkeeping &Section, unsigned SectionId); 2570b57cec5SDimitry Andric void startCustomSection(SectionBookkeeping &Section, StringRef Name); 2580b57cec5SDimitry Andric void endSection(SectionBookkeeping &Section); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric public: 2610b57cec5SDimitry Andric WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, 262e8d8bef9SDimitry Andric raw_pwrite_stream &OS_) 263e8d8bef9SDimitry Andric : TargetObjectWriter(std::move(MOTW)), OS(&OS_) {} 264e8d8bef9SDimitry Andric 265e8d8bef9SDimitry Andric WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, 266e8d8bef9SDimitry Andric raw_pwrite_stream &OS_, raw_pwrite_stream &DwoOS_) 267e8d8bef9SDimitry Andric : TargetObjectWriter(std::move(MOTW)), IsSplitDwarf(true), OS(&OS_), 268e8d8bef9SDimitry Andric DwoOS(&DwoOS_) {} 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric private: 2710b57cec5SDimitry Andric void reset() override { 2720b57cec5SDimitry Andric CodeRelocations.clear(); 2730b57cec5SDimitry Andric DataRelocations.clear(); 2740b57cec5SDimitry Andric TypeIndices.clear(); 2750b57cec5SDimitry Andric WasmIndices.clear(); 2760b57cec5SDimitry Andric GOTIndices.clear(); 2770b57cec5SDimitry Andric TableIndices.clear(); 2780b57cec5SDimitry Andric DataLocations.clear(); 2790b57cec5SDimitry Andric CustomSections.clear(); 2800b57cec5SDimitry Andric ProducersSection.reset(); 2810b57cec5SDimitry Andric TargetFeaturesSection.reset(); 2820b57cec5SDimitry Andric CustomSectionsRelocations.clear(); 2830b57cec5SDimitry Andric SignatureIndices.clear(); 2840b57cec5SDimitry Andric Signatures.clear(); 2850b57cec5SDimitry Andric DataSegments.clear(); 2860b57cec5SDimitry Andric SectionFunctions.clear(); 2870b57cec5SDimitry Andric NumFunctionImports = 0; 2880b57cec5SDimitry Andric NumGlobalImports = 0; 289e8d8bef9SDimitry Andric NumTableImports = 0; 2900b57cec5SDimitry Andric MCObjectWriter::reset(); 2910b57cec5SDimitry Andric } 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric void writeHeader(const MCAssembler &Asm); 2940b57cec5SDimitry Andric 2950fca6ea1SDimitry Andric void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment, 2960fca6ea1SDimitry Andric const MCFixup &Fixup, MCValue Target, 2970fca6ea1SDimitry Andric uint64_t &FixedValue) override; 2980b57cec5SDimitry Andric 2990fca6ea1SDimitry Andric void executePostLayoutBinding(MCAssembler &Asm) override; 300e8d8bef9SDimitry Andric void prepareImports(SmallVectorImpl<wasm::WasmImport> &Imports, 3010fca6ea1SDimitry Andric MCAssembler &Asm); 3020fca6ea1SDimitry Andric uint64_t writeObject(MCAssembler &Asm) override; 3030b57cec5SDimitry Andric 3040fca6ea1SDimitry Andric uint64_t writeOneObject(MCAssembler &Asm, DwoMode Mode); 305e8d8bef9SDimitry Andric 3060b57cec5SDimitry Andric void writeString(const StringRef Str) { 307e8d8bef9SDimitry Andric encodeULEB128(Str.size(), W->OS); 308e8d8bef9SDimitry Andric W->OS << Str; 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 311349cc55cSDimitry Andric void writeStringWithAlignment(const StringRef Str, unsigned Alignment); 312349cc55cSDimitry Andric 3135ffd83dbSDimitry Andric void writeI32(int32_t val) { 3145ffd83dbSDimitry Andric char Buffer[4]; 3155ffd83dbSDimitry Andric support::endian::write32le(Buffer, val); 316e8d8bef9SDimitry Andric W->OS.write(Buffer, sizeof(Buffer)); 3175ffd83dbSDimitry Andric } 3185ffd83dbSDimitry Andric 3195ffd83dbSDimitry Andric void writeI64(int64_t val) { 3205ffd83dbSDimitry Andric char Buffer[8]; 3215ffd83dbSDimitry Andric support::endian::write64le(Buffer, val); 322e8d8bef9SDimitry Andric W->OS.write(Buffer, sizeof(Buffer)); 3235ffd83dbSDimitry Andric } 3245ffd83dbSDimitry Andric 325e8d8bef9SDimitry Andric void writeValueType(wasm::ValType Ty) { W->OS << static_cast<char>(Ty); } 3260b57cec5SDimitry Andric 327e8d8bef9SDimitry Andric void writeTypeSection(ArrayRef<wasm::WasmSignature> Signatures); 3285ffd83dbSDimitry Andric void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint64_t DataSize, 3290b57cec5SDimitry Andric uint32_t NumElements); 3300b57cec5SDimitry Andric void writeFunctionSection(ArrayRef<WasmFunction> Functions); 3310b57cec5SDimitry Andric void writeExportSection(ArrayRef<wasm::WasmExport> Exports); 332fe6060f1SDimitry Andric void writeElemSection(const MCSymbolWasm *IndirectFunctionTable, 333fe6060f1SDimitry Andric ArrayRef<uint32_t> TableElems); 3340b57cec5SDimitry Andric void writeDataCountSection(); 3350fca6ea1SDimitry Andric uint32_t writeCodeSection(const MCAssembler &Asm, 3360b57cec5SDimitry Andric ArrayRef<WasmFunction> Functions); 3370fca6ea1SDimitry Andric uint32_t writeDataSection(const MCAssembler &Asm); 338349cc55cSDimitry Andric void writeTagSection(ArrayRef<uint32_t> TagTypes); 3395ffd83dbSDimitry Andric void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals); 340e8d8bef9SDimitry Andric void writeTableSection(ArrayRef<wasm::WasmTable> Tables); 3410b57cec5SDimitry Andric void writeRelocSection(uint32_t SectionIndex, StringRef Name, 3420b57cec5SDimitry Andric std::vector<WasmRelocationEntry> &Relocations); 3430b57cec5SDimitry Andric void writeLinkingMetaDataSection( 3440b57cec5SDimitry Andric ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, 3450b57cec5SDimitry Andric ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, 3460b57cec5SDimitry Andric const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats); 3470b57cec5SDimitry Andric void writeCustomSection(WasmCustomSection &CustomSection, 3480fca6ea1SDimitry Andric const MCAssembler &Asm); 3490b57cec5SDimitry Andric void writeCustomRelocSections(); 3500b57cec5SDimitry Andric 3510fca6ea1SDimitry Andric uint64_t getProvisionalValue(const MCAssembler &Asm, 3520fca6ea1SDimitry Andric const WasmRelocationEntry &RelEntry); 3530b57cec5SDimitry Andric void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations, 3540fca6ea1SDimitry Andric uint64_t ContentsOffset, const MCAssembler &Asm); 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry); 3570b57cec5SDimitry Andric uint32_t getFunctionType(const MCSymbolWasm &Symbol); 358fe6060f1SDimitry Andric uint32_t getTagType(const MCSymbolWasm &Symbol); 3590b57cec5SDimitry Andric void registerFunctionType(const MCSymbolWasm &Symbol); 360fe6060f1SDimitry Andric void registerTagType(const MCSymbolWasm &Symbol); 3610b57cec5SDimitry Andric }; 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric } // end anonymous namespace 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric // Write out a section header and a patchable section size field. 3660b57cec5SDimitry Andric void WasmObjectWriter::startSection(SectionBookkeeping &Section, 3670b57cec5SDimitry Andric unsigned SectionId) { 3680b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n"); 369e8d8bef9SDimitry Andric W->OS << char(SectionId); 3700b57cec5SDimitry Andric 371e8d8bef9SDimitry Andric Section.SizeOffset = W->OS.tell(); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric // The section size. We don't know the size yet, so reserve enough space 3740b57cec5SDimitry Andric // for any 32-bit value; we'll patch it later. 375e8d8bef9SDimitry Andric encodeULEB128(0, W->OS, 5); 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric // The position where the section starts, for measuring its size. 378e8d8bef9SDimitry Andric Section.ContentsOffset = W->OS.tell(); 379e8d8bef9SDimitry Andric Section.PayloadOffset = W->OS.tell(); 3800b57cec5SDimitry Andric Section.Index = SectionCount++; 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 383349cc55cSDimitry Andric // Write a string with extra paddings for trailing alignment 384349cc55cSDimitry Andric // TODO: support alignment at asm and llvm level? 385349cc55cSDimitry Andric void WasmObjectWriter::writeStringWithAlignment(const StringRef Str, 386349cc55cSDimitry Andric unsigned Alignment) { 387349cc55cSDimitry Andric 388349cc55cSDimitry Andric // Calculate the encoded size of str length and add pads based on it and 389349cc55cSDimitry Andric // alignment. 390349cc55cSDimitry Andric raw_null_ostream NullOS; 391349cc55cSDimitry Andric uint64_t StrSizeLength = encodeULEB128(Str.size(), NullOS); 392349cc55cSDimitry Andric uint64_t Offset = W->OS.tell() + StrSizeLength + Str.size(); 393349cc55cSDimitry Andric uint64_t Paddings = offsetToAlignment(Offset, Align(Alignment)); 394349cc55cSDimitry Andric Offset += Paddings; 395349cc55cSDimitry Andric 396349cc55cSDimitry Andric // LEB128 greater than 5 bytes is invalid 397349cc55cSDimitry Andric assert((StrSizeLength + Paddings) <= 5 && "too long string to align"); 398349cc55cSDimitry Andric 399349cc55cSDimitry Andric encodeSLEB128(Str.size(), W->OS, StrSizeLength + Paddings); 400349cc55cSDimitry Andric W->OS << Str; 401349cc55cSDimitry Andric 402349cc55cSDimitry Andric assert(W->OS.tell() == Offset && "invalid padding"); 403349cc55cSDimitry Andric } 404349cc55cSDimitry Andric 4050b57cec5SDimitry Andric void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section, 4060b57cec5SDimitry Andric StringRef Name) { 4070b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n"); 4080b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_CUSTOM); 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric // The position where the section header ends, for measuring its size. 411e8d8bef9SDimitry Andric Section.PayloadOffset = W->OS.tell(); 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // Custom sections in wasm also have a string identifier. 414349cc55cSDimitry Andric if (Name != "__clangast") { 4150b57cec5SDimitry Andric writeString(Name); 416349cc55cSDimitry Andric } else { 417349cc55cSDimitry Andric // The on-disk hashtable in clangast needs to be aligned by 4 bytes. 418349cc55cSDimitry Andric writeStringWithAlignment(Name, 4); 419349cc55cSDimitry Andric } 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // The position where the custom section starts. 422e8d8bef9SDimitry Andric Section.ContentsOffset = W->OS.tell(); 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric // Now that the section is complete and we know how big it is, patch up the 4260b57cec5SDimitry Andric // section size field at the start of the section. 4270b57cec5SDimitry Andric void WasmObjectWriter::endSection(SectionBookkeeping &Section) { 428e8d8bef9SDimitry Andric uint64_t Size = W->OS.tell(); 4290b57cec5SDimitry Andric // /dev/null doesn't support seek/tell and can report offset of 0. 4300b57cec5SDimitry Andric // Simply skip this patching in that case. 4310b57cec5SDimitry Andric if (!Size) 4320b57cec5SDimitry Andric return; 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric Size -= Section.PayloadOffset; 4350b57cec5SDimitry Andric if (uint32_t(Size) != Size) 4360b57cec5SDimitry Andric report_fatal_error("section size does not fit in a uint32_t"); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n"); 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric // Write the final section size to the payload_len field, which follows 4410b57cec5SDimitry Andric // the section id byte. 442fb03ea46SDimitry Andric writePatchableU32(static_cast<raw_pwrite_stream &>(W->OS), Size, 4430b57cec5SDimitry Andric Section.SizeOffset); 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric // Emit the Wasm header. 4470b57cec5SDimitry Andric void WasmObjectWriter::writeHeader(const MCAssembler &Asm) { 448e8d8bef9SDimitry Andric W->OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic)); 449e8d8bef9SDimitry Andric W->write<uint32_t>(wasm::WasmVersion); 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric 4520fca6ea1SDimitry Andric void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm) { 453fe6060f1SDimitry Andric // Some compilation units require the indirect function table to be present 454fe6060f1SDimitry Andric // but don't explicitly reference it. This is the case for call_indirect 455fe6060f1SDimitry Andric // without the reference-types feature, and also function bitcasts in all 456fe6060f1SDimitry Andric // cases. In those cases the __indirect_function_table has the 457fe6060f1SDimitry Andric // WASM_SYMBOL_NO_STRIP attribute. Here we make sure this symbol makes it to 458fe6060f1SDimitry Andric // the assembler, if needed. 459fe6060f1SDimitry Andric if (auto *Sym = Asm.getContext().lookupSymbol("__indirect_function_table")) { 460fe6060f1SDimitry Andric const auto *WasmSym = static_cast<const MCSymbolWasm *>(Sym); 461fe6060f1SDimitry Andric if (WasmSym->isNoStrip()) 462e8d8bef9SDimitry Andric Asm.registerSymbol(*Sym); 463fe6060f1SDimitry Andric } 464e8d8bef9SDimitry Andric 4650b57cec5SDimitry Andric // Build a map of sections to the function that defines them, for use 4660b57cec5SDimitry Andric // in recordRelocation. 4670b57cec5SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 4680b57cec5SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 4690b57cec5SDimitry Andric if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) { 4700b57cec5SDimitry Andric const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection()); 4710b57cec5SDimitry Andric auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S)); 4720b57cec5SDimitry Andric if (!Pair.second) 4730b57cec5SDimitry Andric report_fatal_error("section already has a defining function: " + 4745ffd83dbSDimitry Andric Sec.getName()); 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric void WasmObjectWriter::recordRelocation(MCAssembler &Asm, 4800b57cec5SDimitry Andric const MCFragment *Fragment, 4810b57cec5SDimitry Andric const MCFixup &Fixup, MCValue Target, 4820b57cec5SDimitry Andric uint64_t &FixedValue) { 4838bcb0991SDimitry Andric // The WebAssembly backend should never generate FKF_IsPCRel fixups 4848bcb0991SDimitry Andric assert(!(Asm.getBackend().getFixupKindInfo(Fixup.getKind()).Flags & 4858bcb0991SDimitry Andric MCFixupKindInfo::FKF_IsPCRel)); 4868bcb0991SDimitry Andric 4870b57cec5SDimitry Andric const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent()); 4880b57cec5SDimitry Andric uint64_t C = Target.getConstant(); 4890fca6ea1SDimitry Andric uint64_t FixupOffset = Asm.getFragmentOffset(*Fragment) + Fixup.getOffset(); 4900b57cec5SDimitry Andric MCContext &Ctx = Asm.getContext(); 491fe6060f1SDimitry Andric bool IsLocRel = false; 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric if (const MCSymbolRefExpr *RefB = Target.getSymB()) { 494fe6060f1SDimitry Andric 4958bcb0991SDimitry Andric const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol()); 496fe6060f1SDimitry Andric 4970fca6ea1SDimitry Andric if (FixupSection.isText()) { 498fe6060f1SDimitry Andric Ctx.reportError(Fixup.getLoc(), 4990b57cec5SDimitry Andric Twine("symbol '") + SymB.getName() + 500fe6060f1SDimitry Andric "' unsupported subtraction expression used in " 501fe6060f1SDimitry Andric "relocation in code section."); 5020b57cec5SDimitry Andric return; 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 505fe6060f1SDimitry Andric if (SymB.isUndefined()) { 506fe6060f1SDimitry Andric Ctx.reportError(Fixup.getLoc(), 507fe6060f1SDimitry Andric Twine("symbol '") + SymB.getName() + 508fe6060f1SDimitry Andric "' can not be undefined in a subtraction expression"); 509fe6060f1SDimitry Andric return; 510fe6060f1SDimitry Andric } 511fe6060f1SDimitry Andric const MCSection &SecB = SymB.getSection(); 512fe6060f1SDimitry Andric if (&SecB != &FixupSection) { 513fe6060f1SDimitry Andric Ctx.reportError(Fixup.getLoc(), 514fe6060f1SDimitry Andric Twine("symbol '") + SymB.getName() + 515fe6060f1SDimitry Andric "' can not be placed in a different section"); 516fe6060f1SDimitry Andric return; 517fe6060f1SDimitry Andric } 518fe6060f1SDimitry Andric IsLocRel = true; 5190fca6ea1SDimitry Andric C += FixupOffset - Asm.getSymbolOffset(SymB); 520fe6060f1SDimitry Andric } 521fe6060f1SDimitry Andric 5220b57cec5SDimitry Andric // We either rejected the fixup or folded B into C at this point. 5230b57cec5SDimitry Andric const MCSymbolRefExpr *RefA = Target.getSymA(); 5248bcb0991SDimitry Andric const auto *SymA = cast<MCSymbolWasm>(&RefA->getSymbol()); 5250b57cec5SDimitry Andric 5265ffd83dbSDimitry Andric // The .init_array isn't translated as data, so don't do relocations in it. 5275f757f3fSDimitry Andric if (FixupSection.getName().starts_with(".init_array")) { 5285ffd83dbSDimitry Andric SymA->setUsedInInitArray(); 5295ffd83dbSDimitry Andric return; 5305ffd83dbSDimitry Andric } 5315ffd83dbSDimitry Andric 5328bcb0991SDimitry Andric if (SymA->isVariable()) { 5330b57cec5SDimitry Andric const MCExpr *Expr = SymA->getVariableValue(); 5345ffd83dbSDimitry Andric if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) 5350b57cec5SDimitry Andric if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) 5360b57cec5SDimitry Andric llvm_unreachable("weakref used in reloc not yet implemented"); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric // Put any constant offset in an addend. Offsets can be negative, and 5400b57cec5SDimitry Andric // LLVM expects wrapping, in contrast to wasm's immediates which can't 5410b57cec5SDimitry Andric // be negative and don't wrap. 5420b57cec5SDimitry Andric FixedValue = 0; 5430b57cec5SDimitry Andric 544fe6060f1SDimitry Andric unsigned Type = 545fe6060f1SDimitry Andric TargetObjectWriter->getRelocType(Target, Fixup, FixupSection, IsLocRel); 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric // Absolute offset within a section or a function. 5485f757f3fSDimitry Andric // Currently only supported for metadata sections. 5490b57cec5SDimitry Andric // See: test/MC/WebAssembly/blockaddress.ll 550fe6060f1SDimitry Andric if ((Type == wasm::R_WASM_FUNCTION_OFFSET_I32 || 551e8d8bef9SDimitry Andric Type == wasm::R_WASM_FUNCTION_OFFSET_I64 || 552fe6060f1SDimitry Andric Type == wasm::R_WASM_SECTION_OFFSET_I32) && 553fe6060f1SDimitry Andric SymA->isDefined()) { 554fe6060f1SDimitry Andric // SymA can be a temp data symbol that represents a function (in which case 555fe6060f1SDimitry Andric // it needs to be replaced by the section symbol), [XXX and it apparently 556fe6060f1SDimitry Andric // later gets changed again to a func symbol?] or it can be a real 557fe6060f1SDimitry Andric // function symbol, in which case it can be left as-is. 558fe6060f1SDimitry Andric 5590fca6ea1SDimitry Andric if (!FixupSection.isMetadata()) 5600b57cec5SDimitry Andric report_fatal_error("relocations for function or section offsets are " 5610b57cec5SDimitry Andric "only supported in metadata sections"); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric const MCSymbol *SectionSymbol = nullptr; 5640b57cec5SDimitry Andric const MCSection &SecA = SymA->getSection(); 5650fca6ea1SDimitry Andric if (SecA.isText()) { 566fe6060f1SDimitry Andric auto SecSymIt = SectionFunctions.find(&SecA); 567fe6060f1SDimitry Andric if (SecSymIt == SectionFunctions.end()) 568fe6060f1SDimitry Andric report_fatal_error("section doesn\'t have defining symbol"); 569fe6060f1SDimitry Andric SectionSymbol = SecSymIt->second; 570fe6060f1SDimitry Andric } else { 5710b57cec5SDimitry Andric SectionSymbol = SecA.getBeginSymbol(); 572fe6060f1SDimitry Andric } 5730b57cec5SDimitry Andric if (!SectionSymbol) 5740b57cec5SDimitry Andric report_fatal_error("section symbol is required for relocation"); 5750b57cec5SDimitry Andric 5760fca6ea1SDimitry Andric C += Asm.getSymbolOffset(*SymA); 5770b57cec5SDimitry Andric SymA = cast<MCSymbolWasm>(SectionSymbol); 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric 580e8d8bef9SDimitry Andric if (Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB || 581fe6060f1SDimitry Andric Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB64 || 582e8d8bef9SDimitry Andric Type == wasm::R_WASM_TABLE_INDEX_SLEB || 583e8d8bef9SDimitry Andric Type == wasm::R_WASM_TABLE_INDEX_SLEB64 || 584e8d8bef9SDimitry Andric Type == wasm::R_WASM_TABLE_INDEX_I32 || 585e8d8bef9SDimitry Andric Type == wasm::R_WASM_TABLE_INDEX_I64) { 586e8d8bef9SDimitry Andric // TABLE_INDEX relocs implicitly use the default indirect function table. 587fe6060f1SDimitry Andric // We require the function table to have already been defined. 588e8d8bef9SDimitry Andric auto TableName = "__indirect_function_table"; 589e8d8bef9SDimitry Andric MCSymbolWasm *Sym = cast_or_null<MCSymbolWasm>(Ctx.lookupSymbol(TableName)); 590fe6060f1SDimitry Andric if (!Sym) { 591fe6060f1SDimitry Andric report_fatal_error("missing indirect function table symbol"); 592e8d8bef9SDimitry Andric } else { 593fe6060f1SDimitry Andric if (!Sym->isFunctionTable()) 594fe6060f1SDimitry Andric report_fatal_error("__indirect_function_table symbol has wrong type"); 595fe6060f1SDimitry Andric // Ensure that __indirect_function_table reaches the output. 596fe6060f1SDimitry Andric Sym->setNoStrip(); 597e8d8bef9SDimitry Andric Asm.registerSymbol(*Sym); 598e8d8bef9SDimitry Andric } 599fe6060f1SDimitry Andric } 600e8d8bef9SDimitry Andric 6010b57cec5SDimitry Andric // Relocation other than R_WASM_TYPE_INDEX_LEB are required to be 6020b57cec5SDimitry Andric // against a named symbol. 6030b57cec5SDimitry Andric if (Type != wasm::R_WASM_TYPE_INDEX_LEB) { 6040b57cec5SDimitry Andric if (SymA->getName().empty()) 6050b57cec5SDimitry Andric report_fatal_error("relocations against un-named temporaries are not yet " 6060b57cec5SDimitry Andric "supported by wasm"); 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric SymA->setUsedInReloc(); 6090b57cec5SDimitry Andric } 6100b57cec5SDimitry Andric 611349cc55cSDimitry Andric switch (RefA->getKind()) { 612349cc55cSDimitry Andric case MCSymbolRefExpr::VK_GOT: 613349cc55cSDimitry Andric case MCSymbolRefExpr::VK_WASM_GOT_TLS: 6140b57cec5SDimitry Andric SymA->setUsedInGOT(); 615349cc55cSDimitry Andric break; 616349cc55cSDimitry Andric default: 617349cc55cSDimitry Andric break; 618349cc55cSDimitry Andric } 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); 6210b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n"); 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric if (FixupSection.isWasmData()) { 6240b57cec5SDimitry Andric DataRelocations.push_back(Rec); 6250fca6ea1SDimitry Andric } else if (FixupSection.isText()) { 6260b57cec5SDimitry Andric CodeRelocations.push_back(Rec); 6270fca6ea1SDimitry Andric } else if (FixupSection.isMetadata()) { 6280b57cec5SDimitry Andric CustomSectionsRelocations[&FixupSection].push_back(Rec); 6290b57cec5SDimitry Andric } else { 6300b57cec5SDimitry Andric llvm_unreachable("unexpected section type"); 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric // Compute a value to write into the code at the location covered 6350b57cec5SDimitry Andric // by RelEntry. This value isn't used by the static linker; it just serves 6360b57cec5SDimitry Andric // to make the object format more readable and more likely to be directly 6370b57cec5SDimitry Andric // useable. 6385ffd83dbSDimitry Andric uint64_t 6390fca6ea1SDimitry Andric WasmObjectWriter::getProvisionalValue(const MCAssembler &Asm, 6400fca6ea1SDimitry Andric const WasmRelocationEntry &RelEntry) { 6415ffd83dbSDimitry Andric if ((RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB || 6425ffd83dbSDimitry Andric RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_I32) && 6435ffd83dbSDimitry Andric !RelEntry.Symbol->isGlobal()) { 6440b57cec5SDimitry Andric assert(GOTIndices.count(RelEntry.Symbol) > 0 && "symbol not found in GOT index space"); 6450b57cec5SDimitry Andric return GOTIndices[RelEntry.Symbol]; 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric switch (RelEntry.Type) { 6490b57cec5SDimitry Andric case wasm::R_WASM_TABLE_INDEX_REL_SLEB: 650fe6060f1SDimitry Andric case wasm::R_WASM_TABLE_INDEX_REL_SLEB64: 6510b57cec5SDimitry Andric case wasm::R_WASM_TABLE_INDEX_SLEB: 652e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_INDEX_SLEB64: 653e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_INDEX_I32: 654e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_INDEX_I64: { 6550b57cec5SDimitry Andric // Provisional value is table address of the resolved symbol itself 6565ffd83dbSDimitry Andric const MCSymbolWasm *Base = 6570fca6ea1SDimitry Andric cast<MCSymbolWasm>(Asm.getBaseSymbol(*RelEntry.Symbol)); 6585ffd83dbSDimitry Andric assert(Base->isFunction()); 659fe6060f1SDimitry Andric if (RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB || 660fe6060f1SDimitry Andric RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB64) 6615ffd83dbSDimitry Andric return TableIndices[Base] - InitialTableOffset; 6625ffd83dbSDimitry Andric else 6635ffd83dbSDimitry Andric return TableIndices[Base]; 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric case wasm::R_WASM_TYPE_INDEX_LEB: 6660b57cec5SDimitry Andric // Provisional value is same as the index 6670b57cec5SDimitry Andric return getRelocationIndexValue(RelEntry); 6680b57cec5SDimitry Andric case wasm::R_WASM_FUNCTION_INDEX_LEB: 66906c3fb27SDimitry Andric case wasm::R_WASM_FUNCTION_INDEX_I32: 6700b57cec5SDimitry Andric case wasm::R_WASM_GLOBAL_INDEX_LEB: 6715ffd83dbSDimitry Andric case wasm::R_WASM_GLOBAL_INDEX_I32: 672fe6060f1SDimitry Andric case wasm::R_WASM_TAG_INDEX_LEB: 673e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_NUMBER_LEB: 674fe6060f1SDimitry Andric // Provisional value is function/global/tag Wasm index 6750b57cec5SDimitry Andric assert(WasmIndices.count(RelEntry.Symbol) > 0 && "symbol not found in wasm index space"); 6760b57cec5SDimitry Andric return WasmIndices[RelEntry.Symbol]; 6770b57cec5SDimitry Andric case wasm::R_WASM_FUNCTION_OFFSET_I32: 678e8d8bef9SDimitry Andric case wasm::R_WASM_FUNCTION_OFFSET_I64: 6790b57cec5SDimitry Andric case wasm::R_WASM_SECTION_OFFSET_I32: { 680fe6060f1SDimitry Andric if (!RelEntry.Symbol->isDefined()) 681fe6060f1SDimitry Andric return 0; 6820b57cec5SDimitry Andric const auto &Section = 6830b57cec5SDimitry Andric static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection()); 6840b57cec5SDimitry Andric return Section.getSectionOffset() + RelEntry.Addend; 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_LEB: 6875ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_LEB64: 6885ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_SLEB: 6895ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_SLEB64: 6900b57cec5SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: 6915ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: 6925ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_I32: 693e8d8bef9SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_I64: 694fe6060f1SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: 695fe6060f1SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64: 696fe6060f1SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32: { 697e8d8bef9SDimitry Andric // Provisional value is address of the global plus the offset 6980b57cec5SDimitry Andric // For undefined symbols, use zero 699fe6060f1SDimitry Andric if (!RelEntry.Symbol->isDefined()) 7000b57cec5SDimitry Andric return 0; 701fe6060f1SDimitry Andric const wasm::WasmDataReference &SymRef = DataLocations[RelEntry.Symbol]; 702fe6060f1SDimitry Andric const WasmDataSegment &Segment = DataSegments[SymRef.Segment]; 7030b57cec5SDimitry Andric // Ignore overflow. LLVM allows address arithmetic to silently wrap. 704fe6060f1SDimitry Andric return Segment.Offset + SymRef.Offset + RelEntry.Addend; 7050b57cec5SDimitry Andric } 7060b57cec5SDimitry Andric default: 7070b57cec5SDimitry Andric llvm_unreachable("invalid relocation type"); 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric static void addData(SmallVectorImpl<char> &DataBytes, 7120b57cec5SDimitry Andric MCSectionWasm &DataSection) { 7135ffd83dbSDimitry Andric LLVM_DEBUG(errs() << "addData: " << DataSection.getName() << "\n"); 7140b57cec5SDimitry Andric 715bdd1243dSDimitry Andric DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlign())); 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric for (const MCFragment &Frag : DataSection) { 7180b57cec5SDimitry Andric if (Frag.hasInstructions()) 7190b57cec5SDimitry Andric report_fatal_error("only data supported in data sections"); 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) { 7220b57cec5SDimitry Andric if (Align->getValueSize() != 1) 7230b57cec5SDimitry Andric report_fatal_error("only byte values supported for alignment"); 7240b57cec5SDimitry Andric // If nops are requested, use zeros, as this is the data section. 7250b57cec5SDimitry Andric uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue(); 7260b57cec5SDimitry Andric uint64_t Size = 7270b57cec5SDimitry Andric std::min<uint64_t>(alignTo(DataBytes.size(), Align->getAlignment()), 7280b57cec5SDimitry Andric DataBytes.size() + Align->getMaxBytesToEmit()); 7290b57cec5SDimitry Andric DataBytes.resize(Size, Value); 7300b57cec5SDimitry Andric } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) { 7310b57cec5SDimitry Andric int64_t NumValues; 7320b57cec5SDimitry Andric if (!Fill->getNumValues().evaluateAsAbsolute(NumValues)) 7330b57cec5SDimitry Andric llvm_unreachable("The fill should be an assembler constant"); 7340b57cec5SDimitry Andric DataBytes.insert(DataBytes.end(), Fill->getValueSize() * NumValues, 7350b57cec5SDimitry Andric Fill->getValue()); 7360b57cec5SDimitry Andric } else if (auto *LEB = dyn_cast<MCLEBFragment>(&Frag)) { 7370b57cec5SDimitry Andric const SmallVectorImpl<char> &Contents = LEB->getContents(); 738e8d8bef9SDimitry Andric llvm::append_range(DataBytes, Contents); 7390b57cec5SDimitry Andric } else { 7400b57cec5SDimitry Andric const auto &DataFrag = cast<MCDataFragment>(Frag); 7410b57cec5SDimitry Andric const SmallVectorImpl<char> &Contents = DataFrag.getContents(); 742e8d8bef9SDimitry Andric llvm::append_range(DataBytes, Contents); 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric } 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n"); 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric uint32_t 7500b57cec5SDimitry Andric WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) { 7510b57cec5SDimitry Andric if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) { 7520b57cec5SDimitry Andric if (!TypeIndices.count(RelEntry.Symbol)) 7530b57cec5SDimitry Andric report_fatal_error("symbol not found in type index space: " + 7540b57cec5SDimitry Andric RelEntry.Symbol->getName()); 7550b57cec5SDimitry Andric return TypeIndices[RelEntry.Symbol]; 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric return RelEntry.Symbol->getIndex(); 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric // Apply the portions of the relocation records that we can handle ourselves 7620b57cec5SDimitry Andric // directly. 7630b57cec5SDimitry Andric void WasmObjectWriter::applyRelocations( 7645ffd83dbSDimitry Andric ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset, 7650fca6ea1SDimitry Andric const MCAssembler &Asm) { 766e8d8bef9SDimitry Andric auto &Stream = static_cast<raw_pwrite_stream &>(W->OS); 7670b57cec5SDimitry Andric for (const WasmRelocationEntry &RelEntry : Relocations) { 7680b57cec5SDimitry Andric uint64_t Offset = ContentsOffset + 7690b57cec5SDimitry Andric RelEntry.FixupSection->getSectionOffset() + 7700b57cec5SDimitry Andric RelEntry.Offset; 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n"); 7730fca6ea1SDimitry Andric uint64_t Value = getProvisionalValue(Asm, RelEntry); 7740b57cec5SDimitry Andric 7750b57cec5SDimitry Andric switch (RelEntry.Type) { 7760b57cec5SDimitry Andric case wasm::R_WASM_FUNCTION_INDEX_LEB: 7770b57cec5SDimitry Andric case wasm::R_WASM_TYPE_INDEX_LEB: 7780b57cec5SDimitry Andric case wasm::R_WASM_GLOBAL_INDEX_LEB: 7790b57cec5SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_LEB: 780fe6060f1SDimitry Andric case wasm::R_WASM_TAG_INDEX_LEB: 781e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_NUMBER_LEB: 782fb03ea46SDimitry Andric writePatchableU32(Stream, Value, Offset); 7835ffd83dbSDimitry Andric break; 7845ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_LEB64: 785fb03ea46SDimitry Andric writePatchableU64(Stream, Value, Offset); 7860b57cec5SDimitry Andric break; 7870b57cec5SDimitry Andric case wasm::R_WASM_TABLE_INDEX_I32: 7880b57cec5SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_I32: 7890b57cec5SDimitry Andric case wasm::R_WASM_FUNCTION_OFFSET_I32: 79006c3fb27SDimitry Andric case wasm::R_WASM_FUNCTION_INDEX_I32: 7910b57cec5SDimitry Andric case wasm::R_WASM_SECTION_OFFSET_I32: 7925ffd83dbSDimitry Andric case wasm::R_WASM_GLOBAL_INDEX_I32: 793fe6060f1SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32: 7945ffd83dbSDimitry Andric patchI32(Stream, Value, Offset); 7955ffd83dbSDimitry Andric break; 796e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_INDEX_I64: 7975ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_I64: 798e8d8bef9SDimitry Andric case wasm::R_WASM_FUNCTION_OFFSET_I64: 7995ffd83dbSDimitry Andric patchI64(Stream, Value, Offset); 8000b57cec5SDimitry Andric break; 8010b57cec5SDimitry Andric case wasm::R_WASM_TABLE_INDEX_SLEB: 8020b57cec5SDimitry Andric case wasm::R_WASM_TABLE_INDEX_REL_SLEB: 8030b57cec5SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_SLEB: 8040b57cec5SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: 805e8d8bef9SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: 806fb03ea46SDimitry Andric writePatchableS32(Stream, Value, Offset); 8075ffd83dbSDimitry Andric break; 808e8d8bef9SDimitry Andric case wasm::R_WASM_TABLE_INDEX_SLEB64: 809fe6060f1SDimitry Andric case wasm::R_WASM_TABLE_INDEX_REL_SLEB64: 8105ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_SLEB64: 8115ffd83dbSDimitry Andric case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64: 812fe6060f1SDimitry Andric case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64: 813fb03ea46SDimitry Andric writePatchableS64(Stream, Value, Offset); 8140b57cec5SDimitry Andric break; 8150b57cec5SDimitry Andric default: 8160b57cec5SDimitry Andric llvm_unreachable("invalid relocation type"); 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric } 8190b57cec5SDimitry Andric } 8200b57cec5SDimitry Andric 821e8d8bef9SDimitry Andric void WasmObjectWriter::writeTypeSection( 822e8d8bef9SDimitry Andric ArrayRef<wasm::WasmSignature> Signatures) { 8230b57cec5SDimitry Andric if (Signatures.empty()) 8240b57cec5SDimitry Andric return; 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric SectionBookkeeping Section; 8270b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_TYPE); 8280b57cec5SDimitry Andric 829e8d8bef9SDimitry Andric encodeULEB128(Signatures.size(), W->OS); 8300b57cec5SDimitry Andric 831e8d8bef9SDimitry Andric for (const wasm::WasmSignature &Sig : Signatures) { 832e8d8bef9SDimitry Andric W->OS << char(wasm::WASM_TYPE_FUNC); 833e8d8bef9SDimitry Andric encodeULEB128(Sig.Params.size(), W->OS); 8340b57cec5SDimitry Andric for (wasm::ValType Ty : Sig.Params) 8350b57cec5SDimitry Andric writeValueType(Ty); 836e8d8bef9SDimitry Andric encodeULEB128(Sig.Returns.size(), W->OS); 8370b57cec5SDimitry Andric for (wasm::ValType Ty : Sig.Returns) 8380b57cec5SDimitry Andric writeValueType(Ty); 8390b57cec5SDimitry Andric } 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric endSection(Section); 8420b57cec5SDimitry Andric } 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports, 8455ffd83dbSDimitry Andric uint64_t DataSize, 8460b57cec5SDimitry Andric uint32_t NumElements) { 8470b57cec5SDimitry Andric if (Imports.empty()) 8480b57cec5SDimitry Andric return; 8490b57cec5SDimitry Andric 8505ffd83dbSDimitry Andric uint64_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize; 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric SectionBookkeeping Section; 8530b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_IMPORT); 8540b57cec5SDimitry Andric 855e8d8bef9SDimitry Andric encodeULEB128(Imports.size(), W->OS); 8560b57cec5SDimitry Andric for (const wasm::WasmImport &Import : Imports) { 8570b57cec5SDimitry Andric writeString(Import.Module); 8580b57cec5SDimitry Andric writeString(Import.Field); 859e8d8bef9SDimitry Andric W->OS << char(Import.Kind); 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric switch (Import.Kind) { 8620b57cec5SDimitry Andric case wasm::WASM_EXTERNAL_FUNCTION: 863e8d8bef9SDimitry Andric encodeULEB128(Import.SigIndex, W->OS); 8640b57cec5SDimitry Andric break; 8650b57cec5SDimitry Andric case wasm::WASM_EXTERNAL_GLOBAL: 866e8d8bef9SDimitry Andric W->OS << char(Import.Global.Type); 867e8d8bef9SDimitry Andric W->OS << char(Import.Global.Mutable ? 1 : 0); 8680b57cec5SDimitry Andric break; 8690b57cec5SDimitry Andric case wasm::WASM_EXTERNAL_MEMORY: 870e8d8bef9SDimitry Andric encodeULEB128(Import.Memory.Flags, W->OS); 871e8d8bef9SDimitry Andric encodeULEB128(NumPages, W->OS); // initial 8720b57cec5SDimitry Andric break; 8730b57cec5SDimitry Andric case wasm::WASM_EXTERNAL_TABLE: 874e8d8bef9SDimitry Andric W->OS << char(Import.Table.ElemType); 8750fca6ea1SDimitry Andric encodeULEB128(Import.Table.Limits.Flags, W->OS); 876e8d8bef9SDimitry Andric encodeULEB128(NumElements, W->OS); // initial 8770b57cec5SDimitry Andric break; 878fe6060f1SDimitry Andric case wasm::WASM_EXTERNAL_TAG: 879349cc55cSDimitry Andric W->OS << char(0); // Reserved 'attribute' field 880349cc55cSDimitry Andric encodeULEB128(Import.SigIndex, W->OS); 8810b57cec5SDimitry Andric break; 8820b57cec5SDimitry Andric default: 8830b57cec5SDimitry Andric llvm_unreachable("unsupported import kind"); 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric endSection(Section); 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) { 8910b57cec5SDimitry Andric if (Functions.empty()) 8920b57cec5SDimitry Andric return; 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric SectionBookkeeping Section; 8950b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_FUNCTION); 8960b57cec5SDimitry Andric 897e8d8bef9SDimitry Andric encodeULEB128(Functions.size(), W->OS); 8980b57cec5SDimitry Andric for (const WasmFunction &Func : Functions) 899e8d8bef9SDimitry Andric encodeULEB128(Func.SigIndex, W->OS); 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric endSection(Section); 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric 904349cc55cSDimitry Andric void WasmObjectWriter::writeTagSection(ArrayRef<uint32_t> TagTypes) { 905349cc55cSDimitry Andric if (TagTypes.empty()) 9060b57cec5SDimitry Andric return; 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric SectionBookkeeping Section; 909fe6060f1SDimitry Andric startSection(Section, wasm::WASM_SEC_TAG); 9100b57cec5SDimitry Andric 911349cc55cSDimitry Andric encodeULEB128(TagTypes.size(), W->OS); 912349cc55cSDimitry Andric for (uint32_t Index : TagTypes) { 913349cc55cSDimitry Andric W->OS << char(0); // Reserved 'attribute' field 914349cc55cSDimitry Andric encodeULEB128(Index, W->OS); 9150b57cec5SDimitry Andric } 9160b57cec5SDimitry Andric 9170b57cec5SDimitry Andric endSection(Section); 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric 9205ffd83dbSDimitry Andric void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) { 9215ffd83dbSDimitry Andric if (Globals.empty()) 9225ffd83dbSDimitry Andric return; 9235ffd83dbSDimitry Andric 9245ffd83dbSDimitry Andric SectionBookkeeping Section; 9255ffd83dbSDimitry Andric startSection(Section, wasm::WASM_SEC_GLOBAL); 9265ffd83dbSDimitry Andric 927e8d8bef9SDimitry Andric encodeULEB128(Globals.size(), W->OS); 9285ffd83dbSDimitry Andric for (const wasm::WasmGlobal &Global : Globals) { 929e8d8bef9SDimitry Andric encodeULEB128(Global.Type.Type, W->OS); 930e8d8bef9SDimitry Andric W->OS << char(Global.Type.Mutable); 93181ad6265SDimitry Andric if (Global.InitExpr.Extended) { 93281ad6265SDimitry Andric llvm_unreachable("extected init expressions not supported"); 93381ad6265SDimitry Andric } else { 93481ad6265SDimitry Andric W->OS << char(Global.InitExpr.Inst.Opcode); 9355ffd83dbSDimitry Andric switch (Global.Type.Type) { 9365ffd83dbSDimitry Andric case wasm::WASM_TYPE_I32: 937e8d8bef9SDimitry Andric encodeSLEB128(0, W->OS); 9385ffd83dbSDimitry Andric break; 9395ffd83dbSDimitry Andric case wasm::WASM_TYPE_I64: 940e8d8bef9SDimitry Andric encodeSLEB128(0, W->OS); 9415ffd83dbSDimitry Andric break; 9425ffd83dbSDimitry Andric case wasm::WASM_TYPE_F32: 9435ffd83dbSDimitry Andric writeI32(0); 9445ffd83dbSDimitry Andric break; 9455ffd83dbSDimitry Andric case wasm::WASM_TYPE_F64: 9465ffd83dbSDimitry Andric writeI64(0); 9475ffd83dbSDimitry Andric break; 9485ffd83dbSDimitry Andric case wasm::WASM_TYPE_EXTERNREF: 9495ffd83dbSDimitry Andric writeValueType(wasm::ValType::EXTERNREF); 9505ffd83dbSDimitry Andric break; 9515ffd83dbSDimitry Andric default: 9525ffd83dbSDimitry Andric llvm_unreachable("unexpected type"); 9535ffd83dbSDimitry Andric } 95481ad6265SDimitry Andric } 955e8d8bef9SDimitry Andric W->OS << char(wasm::WASM_OPCODE_END); 9565ffd83dbSDimitry Andric } 9575ffd83dbSDimitry Andric 9585ffd83dbSDimitry Andric endSection(Section); 9595ffd83dbSDimitry Andric } 9605ffd83dbSDimitry Andric 961e8d8bef9SDimitry Andric void WasmObjectWriter::writeTableSection(ArrayRef<wasm::WasmTable> Tables) { 962e8d8bef9SDimitry Andric if (Tables.empty()) 963e8d8bef9SDimitry Andric return; 964e8d8bef9SDimitry Andric 965e8d8bef9SDimitry Andric SectionBookkeeping Section; 966e8d8bef9SDimitry Andric startSection(Section, wasm::WASM_SEC_TABLE); 967e8d8bef9SDimitry Andric 968e8d8bef9SDimitry Andric encodeULEB128(Tables.size(), W->OS); 969e8d8bef9SDimitry Andric for (const wasm::WasmTable &Table : Tables) { 9700fca6ea1SDimitry Andric assert(Table.Type.ElemType != wasm::ValType::OTHERREF && 9710fca6ea1SDimitry Andric "Cannot encode general ref-typed tables"); 9727a6dacacSDimitry Andric encodeULEB128((uint32_t)Table.Type.ElemType, W->OS); 973e8d8bef9SDimitry Andric encodeULEB128(Table.Type.Limits.Flags, W->OS); 974fe6060f1SDimitry Andric encodeULEB128(Table.Type.Limits.Minimum, W->OS); 975e8d8bef9SDimitry Andric if (Table.Type.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX) 976e8d8bef9SDimitry Andric encodeULEB128(Table.Type.Limits.Maximum, W->OS); 977e8d8bef9SDimitry Andric } 978e8d8bef9SDimitry Andric endSection(Section); 979e8d8bef9SDimitry Andric } 980e8d8bef9SDimitry Andric 9810b57cec5SDimitry Andric void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) { 9820b57cec5SDimitry Andric if (Exports.empty()) 9830b57cec5SDimitry Andric return; 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric SectionBookkeeping Section; 9860b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_EXPORT); 9870b57cec5SDimitry Andric 988e8d8bef9SDimitry Andric encodeULEB128(Exports.size(), W->OS); 9890b57cec5SDimitry Andric for (const wasm::WasmExport &Export : Exports) { 9900b57cec5SDimitry Andric writeString(Export.Name); 991e8d8bef9SDimitry Andric W->OS << char(Export.Kind); 992e8d8bef9SDimitry Andric encodeULEB128(Export.Index, W->OS); 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric endSection(Section); 9960b57cec5SDimitry Andric } 9970b57cec5SDimitry Andric 998fe6060f1SDimitry Andric void WasmObjectWriter::writeElemSection( 999fe6060f1SDimitry Andric const MCSymbolWasm *IndirectFunctionTable, ArrayRef<uint32_t> TableElems) { 10000b57cec5SDimitry Andric if (TableElems.empty()) 10010b57cec5SDimitry Andric return; 10020b57cec5SDimitry Andric 1003fe6060f1SDimitry Andric assert(IndirectFunctionTable); 1004fe6060f1SDimitry Andric 10050b57cec5SDimitry Andric SectionBookkeeping Section; 10060b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_ELEM); 10070b57cec5SDimitry Andric 1008e8d8bef9SDimitry Andric encodeULEB128(1, W->OS); // number of "segments" 1009fe6060f1SDimitry Andric 1010fe6060f1SDimitry Andric assert(WasmIndices.count(IndirectFunctionTable)); 1011fe6060f1SDimitry Andric uint32_t TableNumber = WasmIndices.find(IndirectFunctionTable)->second; 1012fe6060f1SDimitry Andric uint32_t Flags = 0; 1013fe6060f1SDimitry Andric if (TableNumber) 1014fe6060f1SDimitry Andric Flags |= wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER; 1015fe6060f1SDimitry Andric encodeULEB128(Flags, W->OS); 1016fe6060f1SDimitry Andric if (Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) 1017fe6060f1SDimitry Andric encodeULEB128(TableNumber, W->OS); // the table number 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric // init expr for starting offset 10200fca6ea1SDimitry Andric W->OS << char(is64Bit() ? wasm::WASM_OPCODE_I64_CONST 10210fca6ea1SDimitry Andric : wasm::WASM_OPCODE_I32_CONST); 1022e8d8bef9SDimitry Andric encodeSLEB128(InitialTableOffset, W->OS); 1023e8d8bef9SDimitry Andric W->OS << char(wasm::WASM_OPCODE_END); 10240b57cec5SDimitry Andric 1025fe6060f1SDimitry Andric if (Flags & wasm::WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) { 1026fe6060f1SDimitry Andric // We only write active function table initializers, for which the elem kind 1027fe6060f1SDimitry Andric // is specified to be written as 0x00 and interpreted to mean "funcref". 1028fe6060f1SDimitry Andric const uint8_t ElemKind = 0; 1029fe6060f1SDimitry Andric W->OS << ElemKind; 1030fe6060f1SDimitry Andric } 1031fe6060f1SDimitry Andric 1032e8d8bef9SDimitry Andric encodeULEB128(TableElems.size(), W->OS); 10330b57cec5SDimitry Andric for (uint32_t Elem : TableElems) 1034e8d8bef9SDimitry Andric encodeULEB128(Elem, W->OS); 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric endSection(Section); 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric void WasmObjectWriter::writeDataCountSection() { 10400b57cec5SDimitry Andric if (DataSegments.empty()) 10410b57cec5SDimitry Andric return; 10420b57cec5SDimitry Andric 10430b57cec5SDimitry Andric SectionBookkeeping Section; 10440b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_DATACOUNT); 1045e8d8bef9SDimitry Andric encodeULEB128(DataSegments.size(), W->OS); 10460b57cec5SDimitry Andric endSection(Section); 10470b57cec5SDimitry Andric } 10480b57cec5SDimitry Andric 10495ffd83dbSDimitry Andric uint32_t WasmObjectWriter::writeCodeSection(const MCAssembler &Asm, 10500b57cec5SDimitry Andric ArrayRef<WasmFunction> Functions) { 10510b57cec5SDimitry Andric if (Functions.empty()) 10525ffd83dbSDimitry Andric return 0; 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric SectionBookkeeping Section; 10550b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_CODE); 10560b57cec5SDimitry Andric 1057e8d8bef9SDimitry Andric encodeULEB128(Functions.size(), W->OS); 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric for (const WasmFunction &Func : Functions) { 1060bdd1243dSDimitry Andric auto *FuncSection = static_cast<MCSectionWasm *>(Func.Section); 10610b57cec5SDimitry Andric 10620fca6ea1SDimitry Andric int64_t Size = Asm.getSectionAddressSize(*FuncSection); 1063e8d8bef9SDimitry Andric encodeULEB128(Size, W->OS); 1064bdd1243dSDimitry Andric FuncSection->setSectionOffset(W->OS.tell() - Section.ContentsOffset); 10650fca6ea1SDimitry Andric Asm.writeSectionData(W->OS, FuncSection); 10660b57cec5SDimitry Andric } 10670b57cec5SDimitry Andric 10680b57cec5SDimitry Andric // Apply fixups. 10690fca6ea1SDimitry Andric applyRelocations(CodeRelocations, Section.ContentsOffset, Asm); 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric endSection(Section); 10725ffd83dbSDimitry Andric return Section.Index; 10730b57cec5SDimitry Andric } 10740b57cec5SDimitry Andric 10750fca6ea1SDimitry Andric uint32_t WasmObjectWriter::writeDataSection(const MCAssembler &Asm) { 10760b57cec5SDimitry Andric if (DataSegments.empty()) 10775ffd83dbSDimitry Andric return 0; 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric SectionBookkeeping Section; 10800b57cec5SDimitry Andric startSection(Section, wasm::WASM_SEC_DATA); 10810b57cec5SDimitry Andric 1082e8d8bef9SDimitry Andric encodeULEB128(DataSegments.size(), W->OS); // count 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric for (const WasmDataSegment &Segment : DataSegments) { 1085e8d8bef9SDimitry Andric encodeULEB128(Segment.InitFlags, W->OS); // flags 1086e8d8bef9SDimitry Andric if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX) 1087e8d8bef9SDimitry Andric encodeULEB128(0, W->OS); // memory index 1088e8d8bef9SDimitry Andric if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) { 1089fe6060f1SDimitry Andric W->OS << char(is64Bit() ? wasm::WASM_OPCODE_I64_CONST 10905ffd83dbSDimitry Andric : wasm::WASM_OPCODE_I32_CONST); 1091e8d8bef9SDimitry Andric encodeSLEB128(Segment.Offset, W->OS); // offset 1092e8d8bef9SDimitry Andric W->OS << char(wasm::WASM_OPCODE_END); 10930b57cec5SDimitry Andric } 1094e8d8bef9SDimitry Andric encodeULEB128(Segment.Data.size(), W->OS); // size 1095e8d8bef9SDimitry Andric Segment.Section->setSectionOffset(W->OS.tell() - Section.ContentsOffset); 1096e8d8bef9SDimitry Andric W->OS << Segment.Data; // data 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric // Apply fixups. 11000fca6ea1SDimitry Andric applyRelocations(DataRelocations, Section.ContentsOffset, Asm); 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric endSection(Section); 11035ffd83dbSDimitry Andric return Section.Index; 11040b57cec5SDimitry Andric } 11050b57cec5SDimitry Andric 11060b57cec5SDimitry Andric void WasmObjectWriter::writeRelocSection( 11070b57cec5SDimitry Andric uint32_t SectionIndex, StringRef Name, 11080b57cec5SDimitry Andric std::vector<WasmRelocationEntry> &Relocs) { 1109349cc55cSDimitry Andric // See: https://github.com/WebAssembly/tool-conventions/blob/main/Linking.md 11100b57cec5SDimitry Andric // for descriptions of the reloc sections. 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric if (Relocs.empty()) 11130b57cec5SDimitry Andric return; 11140b57cec5SDimitry Andric 11150b57cec5SDimitry Andric // First, ensure the relocations are sorted in offset order. In general they 11160b57cec5SDimitry Andric // should already be sorted since `recordRelocation` is called in offset 11170b57cec5SDimitry Andric // order, but for the code section we combine many MC sections into single 11180b57cec5SDimitry Andric // wasm section, and this order is determined by the order of Asm.Symbols() 11190b57cec5SDimitry Andric // not the sections order. 11200b57cec5SDimitry Andric llvm::stable_sort( 11210b57cec5SDimitry Andric Relocs, [](const WasmRelocationEntry &A, const WasmRelocationEntry &B) { 11220b57cec5SDimitry Andric return (A.Offset + A.FixupSection->getSectionOffset()) < 11230b57cec5SDimitry Andric (B.Offset + B.FixupSection->getSectionOffset()); 11240b57cec5SDimitry Andric }); 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andric SectionBookkeeping Section; 11270b57cec5SDimitry Andric startCustomSection(Section, std::string("reloc.") + Name.str()); 11280b57cec5SDimitry Andric 1129e8d8bef9SDimitry Andric encodeULEB128(SectionIndex, W->OS); 1130e8d8bef9SDimitry Andric encodeULEB128(Relocs.size(), W->OS); 11310b57cec5SDimitry Andric for (const WasmRelocationEntry &RelEntry : Relocs) { 11320b57cec5SDimitry Andric uint64_t Offset = 11330b57cec5SDimitry Andric RelEntry.Offset + RelEntry.FixupSection->getSectionOffset(); 11340b57cec5SDimitry Andric uint32_t Index = getRelocationIndexValue(RelEntry); 11350b57cec5SDimitry Andric 1136e8d8bef9SDimitry Andric W->OS << char(RelEntry.Type); 1137e8d8bef9SDimitry Andric encodeULEB128(Offset, W->OS); 1138e8d8bef9SDimitry Andric encodeULEB128(Index, W->OS); 11390b57cec5SDimitry Andric if (RelEntry.hasAddend()) 1140e8d8bef9SDimitry Andric encodeSLEB128(RelEntry.Addend, W->OS); 11410b57cec5SDimitry Andric } 11420b57cec5SDimitry Andric 11430b57cec5SDimitry Andric endSection(Section); 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric 11460b57cec5SDimitry Andric void WasmObjectWriter::writeCustomRelocSections() { 11470b57cec5SDimitry Andric for (const auto &Sec : CustomSections) { 11480b57cec5SDimitry Andric auto &Relocations = CustomSectionsRelocations[Sec.Section]; 11490b57cec5SDimitry Andric writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations); 11500b57cec5SDimitry Andric } 11510b57cec5SDimitry Andric } 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric void WasmObjectWriter::writeLinkingMetaDataSection( 11540b57cec5SDimitry Andric ArrayRef<wasm::WasmSymbolInfo> SymbolInfos, 11550b57cec5SDimitry Andric ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs, 11560b57cec5SDimitry Andric const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) { 11570b57cec5SDimitry Andric SectionBookkeeping Section; 11580b57cec5SDimitry Andric startCustomSection(Section, "linking"); 1159e8d8bef9SDimitry Andric encodeULEB128(wasm::WasmMetadataVersion, W->OS); 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric SectionBookkeeping SubSection; 11620b57cec5SDimitry Andric if (SymbolInfos.size() != 0) { 11630b57cec5SDimitry Andric startSection(SubSection, wasm::WASM_SYMBOL_TABLE); 1164e8d8bef9SDimitry Andric encodeULEB128(SymbolInfos.size(), W->OS); 11650b57cec5SDimitry Andric for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) { 1166e8d8bef9SDimitry Andric encodeULEB128(Sym.Kind, W->OS); 1167e8d8bef9SDimitry Andric encodeULEB128(Sym.Flags, W->OS); 11680b57cec5SDimitry Andric switch (Sym.Kind) { 11690b57cec5SDimitry Andric case wasm::WASM_SYMBOL_TYPE_FUNCTION: 11700b57cec5SDimitry Andric case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1171fe6060f1SDimitry Andric case wasm::WASM_SYMBOL_TYPE_TAG: 1172e8d8bef9SDimitry Andric case wasm::WASM_SYMBOL_TYPE_TABLE: 1173e8d8bef9SDimitry Andric encodeULEB128(Sym.ElementIndex, W->OS); 11740b57cec5SDimitry Andric if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 || 11750b57cec5SDimitry Andric (Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0) 11760b57cec5SDimitry Andric writeString(Sym.Name); 11770b57cec5SDimitry Andric break; 11780b57cec5SDimitry Andric case wasm::WASM_SYMBOL_TYPE_DATA: 11790b57cec5SDimitry Andric writeString(Sym.Name); 11800b57cec5SDimitry Andric if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) { 1181e8d8bef9SDimitry Andric encodeULEB128(Sym.DataRef.Segment, W->OS); 1182e8d8bef9SDimitry Andric encodeULEB128(Sym.DataRef.Offset, W->OS); 1183e8d8bef9SDimitry Andric encodeULEB128(Sym.DataRef.Size, W->OS); 11840b57cec5SDimitry Andric } 11850b57cec5SDimitry Andric break; 11860b57cec5SDimitry Andric case wasm::WASM_SYMBOL_TYPE_SECTION: { 11870b57cec5SDimitry Andric const uint32_t SectionIndex = 11880b57cec5SDimitry Andric CustomSections[Sym.ElementIndex].OutputIndex; 1189e8d8bef9SDimitry Andric encodeULEB128(SectionIndex, W->OS); 11900b57cec5SDimitry Andric break; 11910b57cec5SDimitry Andric } 11920b57cec5SDimitry Andric default: 11930b57cec5SDimitry Andric llvm_unreachable("unexpected kind"); 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric } 11960b57cec5SDimitry Andric endSection(SubSection); 11970b57cec5SDimitry Andric } 11980b57cec5SDimitry Andric 11990b57cec5SDimitry Andric if (DataSegments.size()) { 12000b57cec5SDimitry Andric startSection(SubSection, wasm::WASM_SEGMENT_INFO); 1201e8d8bef9SDimitry Andric encodeULEB128(DataSegments.size(), W->OS); 12020b57cec5SDimitry Andric for (const WasmDataSegment &Segment : DataSegments) { 12030b57cec5SDimitry Andric writeString(Segment.Name); 1204e8d8bef9SDimitry Andric encodeULEB128(Segment.Alignment, W->OS); 1205fe6060f1SDimitry Andric encodeULEB128(Segment.LinkingFlags, W->OS); 12060b57cec5SDimitry Andric } 12070b57cec5SDimitry Andric endSection(SubSection); 12080b57cec5SDimitry Andric } 12090b57cec5SDimitry Andric 12100b57cec5SDimitry Andric if (!InitFuncs.empty()) { 12110b57cec5SDimitry Andric startSection(SubSection, wasm::WASM_INIT_FUNCS); 1212e8d8bef9SDimitry Andric encodeULEB128(InitFuncs.size(), W->OS); 12130b57cec5SDimitry Andric for (auto &StartFunc : InitFuncs) { 1214e8d8bef9SDimitry Andric encodeULEB128(StartFunc.first, W->OS); // priority 1215e8d8bef9SDimitry Andric encodeULEB128(StartFunc.second, W->OS); // function index 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric endSection(SubSection); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric 12200b57cec5SDimitry Andric if (Comdats.size()) { 12210b57cec5SDimitry Andric startSection(SubSection, wasm::WASM_COMDAT_INFO); 1222e8d8bef9SDimitry Andric encodeULEB128(Comdats.size(), W->OS); 12230b57cec5SDimitry Andric for (const auto &C : Comdats) { 12240b57cec5SDimitry Andric writeString(C.first); 1225e8d8bef9SDimitry Andric encodeULEB128(0, W->OS); // flags for future use 1226e8d8bef9SDimitry Andric encodeULEB128(C.second.size(), W->OS); 12270b57cec5SDimitry Andric for (const WasmComdatEntry &Entry : C.second) { 1228e8d8bef9SDimitry Andric encodeULEB128(Entry.Kind, W->OS); 1229e8d8bef9SDimitry Andric encodeULEB128(Entry.Index, W->OS); 12300b57cec5SDimitry Andric } 12310b57cec5SDimitry Andric } 12320b57cec5SDimitry Andric endSection(SubSection); 12330b57cec5SDimitry Andric } 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric endSection(Section); 12360b57cec5SDimitry Andric } 12370b57cec5SDimitry Andric 12380b57cec5SDimitry Andric void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection, 12390fca6ea1SDimitry Andric const MCAssembler &Asm) { 12400b57cec5SDimitry Andric SectionBookkeeping Section; 12410b57cec5SDimitry Andric auto *Sec = CustomSection.Section; 12420b57cec5SDimitry Andric startCustomSection(Section, CustomSection.Name); 12430b57cec5SDimitry Andric 1244e8d8bef9SDimitry Andric Sec->setSectionOffset(W->OS.tell() - Section.ContentsOffset); 12450fca6ea1SDimitry Andric Asm.writeSectionData(W->OS, Sec); 12460b57cec5SDimitry Andric 12470b57cec5SDimitry Andric CustomSection.OutputContentsOffset = Section.ContentsOffset; 12480b57cec5SDimitry Andric CustomSection.OutputIndex = Section.Index; 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric endSection(Section); 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric // Apply fixups. 12530b57cec5SDimitry Andric auto &Relocations = CustomSectionsRelocations[CustomSection.Section]; 12540fca6ea1SDimitry Andric applyRelocations(Relocations, CustomSection.OutputContentsOffset, Asm); 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) { 12580b57cec5SDimitry Andric assert(Symbol.isFunction()); 12590b57cec5SDimitry Andric assert(TypeIndices.count(&Symbol)); 12600b57cec5SDimitry Andric return TypeIndices[&Symbol]; 12610b57cec5SDimitry Andric } 12620b57cec5SDimitry Andric 1263fe6060f1SDimitry Andric uint32_t WasmObjectWriter::getTagType(const MCSymbolWasm &Symbol) { 1264fe6060f1SDimitry Andric assert(Symbol.isTag()); 12650b57cec5SDimitry Andric assert(TypeIndices.count(&Symbol)); 12660b57cec5SDimitry Andric return TypeIndices[&Symbol]; 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) { 12700b57cec5SDimitry Andric assert(Symbol.isFunction()); 12710b57cec5SDimitry Andric 1272e8d8bef9SDimitry Andric wasm::WasmSignature S; 12735ffd83dbSDimitry Andric 12745ffd83dbSDimitry Andric if (auto *Sig = Symbol.getSignature()) { 12750b57cec5SDimitry Andric S.Returns = Sig->Returns; 12760b57cec5SDimitry Andric S.Params = Sig->Params; 12770b57cec5SDimitry Andric } 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size())); 12800b57cec5SDimitry Andric if (Pair.second) 12810b57cec5SDimitry Andric Signatures.push_back(S); 12820b57cec5SDimitry Andric TypeIndices[&Symbol] = Pair.first->second; 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol 12850b57cec5SDimitry Andric << " new:" << Pair.second << "\n"); 12860b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); 12870b57cec5SDimitry Andric } 12880b57cec5SDimitry Andric 1289fe6060f1SDimitry Andric void WasmObjectWriter::registerTagType(const MCSymbolWasm &Symbol) { 1290fe6060f1SDimitry Andric assert(Symbol.isTag()); 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric // TODO Currently we don't generate imported exceptions, but if we do, we 12930b57cec5SDimitry Andric // should have a way of infering types of imported exceptions. 1294e8d8bef9SDimitry Andric wasm::WasmSignature S; 12950b57cec5SDimitry Andric if (auto *Sig = Symbol.getSignature()) { 12960b57cec5SDimitry Andric S.Returns = Sig->Returns; 12970b57cec5SDimitry Andric S.Params = Sig->Params; 12980b57cec5SDimitry Andric } 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size())); 13010b57cec5SDimitry Andric if (Pair.second) 13020b57cec5SDimitry Andric Signatures.push_back(S); 13030b57cec5SDimitry Andric TypeIndices[&Symbol] = Pair.first->second; 13040b57cec5SDimitry Andric 1305fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "registerTagType: " << Symbol << " new:" << Pair.second 13060b57cec5SDimitry Andric << "\n"); 13070b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n"); 13080b57cec5SDimitry Andric } 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric static bool isInSymtab(const MCSymbolWasm &Sym) { 13115ffd83dbSDimitry Andric if (Sym.isUsedInReloc() || Sym.isUsedInInitArray()) 13120b57cec5SDimitry Andric return true; 13130b57cec5SDimitry Andric 13140b57cec5SDimitry Andric if (Sym.isComdat() && !Sym.isDefined()) 13150b57cec5SDimitry Andric return false; 13160b57cec5SDimitry Andric 13175ffd83dbSDimitry Andric if (Sym.isTemporary()) 13180b57cec5SDimitry Andric return false; 13190b57cec5SDimitry Andric 13200b57cec5SDimitry Andric if (Sym.isSection()) 13210b57cec5SDimitry Andric return false; 13220b57cec5SDimitry Andric 1323fe6060f1SDimitry Andric if (Sym.omitFromLinkingSection()) 1324fe6060f1SDimitry Andric return false; 1325fe6060f1SDimitry Andric 13260b57cec5SDimitry Andric return true; 13270b57cec5SDimitry Andric } 13280b57cec5SDimitry Andric 1329*6c05f3a7SDimitry Andric static bool isSectionReferenced(MCAssembler &Asm, MCSectionWasm &Section) { 1330*6c05f3a7SDimitry Andric StringRef SectionName = Section.getName(); 1331*6c05f3a7SDimitry Andric 1332*6c05f3a7SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 1333*6c05f3a7SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 1334*6c05f3a7SDimitry Andric if (WS.isData() && WS.isInSection()) { 1335*6c05f3a7SDimitry Andric auto &RefSection = static_cast<MCSectionWasm &>(WS.getSection()); 1336*6c05f3a7SDimitry Andric if (RefSection.getName() == SectionName) { 1337*6c05f3a7SDimitry Andric return true; 1338*6c05f3a7SDimitry Andric } 1339*6c05f3a7SDimitry Andric } 1340*6c05f3a7SDimitry Andric } 1341*6c05f3a7SDimitry Andric 1342*6c05f3a7SDimitry Andric return false; 1343*6c05f3a7SDimitry Andric } 1344*6c05f3a7SDimitry Andric 1345e8d8bef9SDimitry Andric void WasmObjectWriter::prepareImports( 13460fca6ea1SDimitry Andric SmallVectorImpl<wasm::WasmImport> &Imports, MCAssembler &Asm) { 13470b57cec5SDimitry Andric // For now, always emit the memory import, since loads and stores are not 13480b57cec5SDimitry Andric // valid without it. In the future, we could perhaps be more clever and omit 13490b57cec5SDimitry Andric // it if there are no loads or stores. 13500b57cec5SDimitry Andric wasm::WasmImport MemImport; 13510b57cec5SDimitry Andric MemImport.Module = "env"; 13520b57cec5SDimitry Andric MemImport.Field = "__linear_memory"; 13530b57cec5SDimitry Andric MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY; 13545ffd83dbSDimitry Andric MemImport.Memory.Flags = is64Bit() ? wasm::WASM_LIMITS_FLAG_IS_64 13555ffd83dbSDimitry Andric : wasm::WASM_LIMITS_FLAG_NONE; 13560b57cec5SDimitry Andric Imports.push_back(MemImport); 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric // Populate SignatureIndices, and Imports and WasmIndices for undefined 13590b57cec5SDimitry Andric // symbols. This must be done before populating WasmIndices for defined 13600b57cec5SDimitry Andric // symbols. 13610b57cec5SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 13620b57cec5SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 13630b57cec5SDimitry Andric 13640b57cec5SDimitry Andric // Register types for all functions, including those with private linkage 13650b57cec5SDimitry Andric // (because wasm always needs a type signature). 13665ffd83dbSDimitry Andric if (WS.isFunction()) { 13670fca6ea1SDimitry Andric const auto *BS = Asm.getBaseSymbol(S); 1368e8d8bef9SDimitry Andric if (!BS) 1369e8d8bef9SDimitry Andric report_fatal_error(Twine(S.getName()) + 1370e8d8bef9SDimitry Andric ": absolute addressing not supported!"); 1371e8d8bef9SDimitry Andric registerFunctionType(*cast<MCSymbolWasm>(BS)); 13725ffd83dbSDimitry Andric } 13730b57cec5SDimitry Andric 1374fe6060f1SDimitry Andric if (WS.isTag()) 1375fe6060f1SDimitry Andric registerTagType(WS); 13760b57cec5SDimitry Andric 13770b57cec5SDimitry Andric if (WS.isTemporary()) 13780b57cec5SDimitry Andric continue; 13790b57cec5SDimitry Andric 13800b57cec5SDimitry Andric // If the symbol is not defined in this translation unit, import it. 13810b57cec5SDimitry Andric if (!WS.isDefined() && !WS.isComdat()) { 13820b57cec5SDimitry Andric if (WS.isFunction()) { 13830b57cec5SDimitry Andric wasm::WasmImport Import; 13840b57cec5SDimitry Andric Import.Module = WS.getImportModule(); 13850b57cec5SDimitry Andric Import.Field = WS.getImportName(); 13860b57cec5SDimitry Andric Import.Kind = wasm::WASM_EXTERNAL_FUNCTION; 13870b57cec5SDimitry Andric Import.SigIndex = getFunctionType(WS); 13880b57cec5SDimitry Andric Imports.push_back(Import); 13890b57cec5SDimitry Andric assert(WasmIndices.count(&WS) == 0); 13900b57cec5SDimitry Andric WasmIndices[&WS] = NumFunctionImports++; 13910b57cec5SDimitry Andric } else if (WS.isGlobal()) { 13920b57cec5SDimitry Andric if (WS.isWeak()) 13930b57cec5SDimitry Andric report_fatal_error("undefined global symbol cannot be weak"); 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andric wasm::WasmImport Import; 13960b57cec5SDimitry Andric Import.Field = WS.getImportName(); 13970b57cec5SDimitry Andric Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; 13980b57cec5SDimitry Andric Import.Module = WS.getImportModule(); 13990b57cec5SDimitry Andric Import.Global = WS.getGlobalType(); 14000b57cec5SDimitry Andric Imports.push_back(Import); 14010b57cec5SDimitry Andric assert(WasmIndices.count(&WS) == 0); 14020b57cec5SDimitry Andric WasmIndices[&WS] = NumGlobalImports++; 1403fe6060f1SDimitry Andric } else if (WS.isTag()) { 14040b57cec5SDimitry Andric if (WS.isWeak()) 1405fe6060f1SDimitry Andric report_fatal_error("undefined tag symbol cannot be weak"); 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric wasm::WasmImport Import; 14080b57cec5SDimitry Andric Import.Module = WS.getImportModule(); 14090b57cec5SDimitry Andric Import.Field = WS.getImportName(); 1410fe6060f1SDimitry Andric Import.Kind = wasm::WASM_EXTERNAL_TAG; 1411349cc55cSDimitry Andric Import.SigIndex = getTagType(WS); 14120b57cec5SDimitry Andric Imports.push_back(Import); 14130b57cec5SDimitry Andric assert(WasmIndices.count(&WS) == 0); 1414fe6060f1SDimitry Andric WasmIndices[&WS] = NumTagImports++; 1415e8d8bef9SDimitry Andric } else if (WS.isTable()) { 1416e8d8bef9SDimitry Andric if (WS.isWeak()) 1417e8d8bef9SDimitry Andric report_fatal_error("undefined table symbol cannot be weak"); 1418e8d8bef9SDimitry Andric 1419e8d8bef9SDimitry Andric wasm::WasmImport Import; 1420e8d8bef9SDimitry Andric Import.Module = WS.getImportModule(); 1421e8d8bef9SDimitry Andric Import.Field = WS.getImportName(); 1422e8d8bef9SDimitry Andric Import.Kind = wasm::WASM_EXTERNAL_TABLE; 1423fe6060f1SDimitry Andric Import.Table = WS.getTableType(); 1424e8d8bef9SDimitry Andric Imports.push_back(Import); 1425e8d8bef9SDimitry Andric assert(WasmIndices.count(&WS) == 0); 1426e8d8bef9SDimitry Andric WasmIndices[&WS] = NumTableImports++; 14270b57cec5SDimitry Andric } 14280b57cec5SDimitry Andric } 14290b57cec5SDimitry Andric } 14300b57cec5SDimitry Andric 14310b57cec5SDimitry Andric // Add imports for GOT globals 14320b57cec5SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 14330b57cec5SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 14340b57cec5SDimitry Andric if (WS.isUsedInGOT()) { 14350b57cec5SDimitry Andric wasm::WasmImport Import; 14360b57cec5SDimitry Andric if (WS.isFunction()) 14370b57cec5SDimitry Andric Import.Module = "GOT.func"; 14380b57cec5SDimitry Andric else 14390b57cec5SDimitry Andric Import.Module = "GOT.mem"; 14400b57cec5SDimitry Andric Import.Field = WS.getName(); 14410b57cec5SDimitry Andric Import.Kind = wasm::WASM_EXTERNAL_GLOBAL; 14420b57cec5SDimitry Andric Import.Global = {wasm::WASM_TYPE_I32, true}; 14430b57cec5SDimitry Andric Imports.push_back(Import); 14440b57cec5SDimitry Andric assert(GOTIndices.count(&WS) == 0); 14450b57cec5SDimitry Andric GOTIndices[&WS] = NumGlobalImports++; 14460b57cec5SDimitry Andric } 14470b57cec5SDimitry Andric } 1448e8d8bef9SDimitry Andric } 1449e8d8bef9SDimitry Andric 14500fca6ea1SDimitry Andric uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm) { 14515f757f3fSDimitry Andric support::endian::Writer MainWriter(*OS, llvm::endianness::little); 1452e8d8bef9SDimitry Andric W = &MainWriter; 1453e8d8bef9SDimitry Andric if (IsSplitDwarf) { 14540fca6ea1SDimitry Andric uint64_t TotalSize = writeOneObject(Asm, DwoMode::NonDwoOnly); 1455e8d8bef9SDimitry Andric assert(DwoOS); 14565f757f3fSDimitry Andric support::endian::Writer DwoWriter(*DwoOS, llvm::endianness::little); 1457e8d8bef9SDimitry Andric W = &DwoWriter; 14580fca6ea1SDimitry Andric return TotalSize + writeOneObject(Asm, DwoMode::DwoOnly); 1459e8d8bef9SDimitry Andric } else { 14600fca6ea1SDimitry Andric return writeOneObject(Asm, DwoMode::AllSections); 1461e8d8bef9SDimitry Andric } 1462e8d8bef9SDimitry Andric } 1463e8d8bef9SDimitry Andric 1464e8d8bef9SDimitry Andric uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm, 1465e8d8bef9SDimitry Andric DwoMode Mode) { 1466e8d8bef9SDimitry Andric uint64_t StartOffset = W->OS.tell(); 1467e8d8bef9SDimitry Andric SectionCount = 0; 1468e8d8bef9SDimitry Andric CustomSections.clear(); 1469e8d8bef9SDimitry Andric 1470e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n"); 1471e8d8bef9SDimitry Andric 1472e8d8bef9SDimitry Andric // Collect information from the available symbols. 1473e8d8bef9SDimitry Andric SmallVector<WasmFunction, 4> Functions; 1474e8d8bef9SDimitry Andric SmallVector<uint32_t, 4> TableElems; 1475e8d8bef9SDimitry Andric SmallVector<wasm::WasmImport, 4> Imports; 1476e8d8bef9SDimitry Andric SmallVector<wasm::WasmExport, 4> Exports; 1477349cc55cSDimitry Andric SmallVector<uint32_t, 2> TagTypes; 1478e8d8bef9SDimitry Andric SmallVector<wasm::WasmGlobal, 1> Globals; 1479e8d8bef9SDimitry Andric SmallVector<wasm::WasmTable, 1> Tables; 1480e8d8bef9SDimitry Andric SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos; 1481e8d8bef9SDimitry Andric SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs; 1482e8d8bef9SDimitry Andric std::map<StringRef, std::vector<WasmComdatEntry>> Comdats; 1483e8d8bef9SDimitry Andric uint64_t DataSize = 0; 14840fca6ea1SDimitry Andric if (Mode != DwoMode::DwoOnly) 14850fca6ea1SDimitry Andric prepareImports(Imports, Asm); 14860b57cec5SDimitry Andric 14870b57cec5SDimitry Andric // Populate DataSegments and CustomSections, which must be done before 14880b57cec5SDimitry Andric // populating DataLocations. 14890b57cec5SDimitry Andric for (MCSection &Sec : Asm) { 14900b57cec5SDimitry Andric auto &Section = static_cast<MCSectionWasm &>(Sec); 14915ffd83dbSDimitry Andric StringRef SectionName = Section.getName(); 14920b57cec5SDimitry Andric 1493e8d8bef9SDimitry Andric if (Mode == DwoMode::NonDwoOnly && isDwoSection(Sec)) 1494e8d8bef9SDimitry Andric continue; 1495e8d8bef9SDimitry Andric if (Mode == DwoMode::DwoOnly && !isDwoSection(Sec)) 1496e8d8bef9SDimitry Andric continue; 1497e8d8bef9SDimitry Andric 1498e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Processing Section " << SectionName << " group " 1499e8d8bef9SDimitry Andric << Section.getGroup() << "\n";); 1500e8d8bef9SDimitry Andric 1501*6c05f3a7SDimitry Andric // .init_array sections are handled specially elsewhere, include them in 1502*6c05f3a7SDimitry Andric // data segments if and only if referenced by a symbol. 1503*6c05f3a7SDimitry Andric if (SectionName.starts_with(".init_array") && 1504*6c05f3a7SDimitry Andric !isSectionReferenced(Asm, Section)) 15050b57cec5SDimitry Andric continue; 15060b57cec5SDimitry Andric 15070b57cec5SDimitry Andric // Code is handled separately 15080fca6ea1SDimitry Andric if (Section.isText()) 15090b57cec5SDimitry Andric continue; 15100b57cec5SDimitry Andric 15110b57cec5SDimitry Andric if (Section.isWasmData()) { 15120b57cec5SDimitry Andric uint32_t SegmentIndex = DataSegments.size(); 1513bdd1243dSDimitry Andric DataSize = alignTo(DataSize, Section.getAlign()); 15140b57cec5SDimitry Andric DataSegments.emplace_back(); 15150b57cec5SDimitry Andric WasmDataSegment &Segment = DataSegments.back(); 15160b57cec5SDimitry Andric Segment.Name = SectionName; 1517e8d8bef9SDimitry Andric Segment.InitFlags = Section.getPassive() 1518e8d8bef9SDimitry Andric ? (uint32_t)wasm::WASM_DATA_SEGMENT_IS_PASSIVE 1519e8d8bef9SDimitry Andric : 0; 15200b57cec5SDimitry Andric Segment.Offset = DataSize; 15210b57cec5SDimitry Andric Segment.Section = &Section; 15220b57cec5SDimitry Andric addData(Segment.Data, Section); 1523bdd1243dSDimitry Andric Segment.Alignment = Log2(Section.getAlign()); 1524fe6060f1SDimitry Andric Segment.LinkingFlags = Section.getSegmentFlags(); 15250b57cec5SDimitry Andric DataSize += Segment.Data.size(); 15260b57cec5SDimitry Andric Section.setSegmentIndex(SegmentIndex); 15270b57cec5SDimitry Andric 15280b57cec5SDimitry Andric if (const MCSymbolWasm *C = Section.getGroup()) { 15290b57cec5SDimitry Andric Comdats[C->getName()].emplace_back( 15300b57cec5SDimitry Andric WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex}); 15310b57cec5SDimitry Andric } 15320b57cec5SDimitry Andric } else { 15330b57cec5SDimitry Andric // Create custom sections 15340fca6ea1SDimitry Andric assert(Section.isMetadata()); 15350b57cec5SDimitry Andric 15360b57cec5SDimitry Andric StringRef Name = SectionName; 15370b57cec5SDimitry Andric 15380b57cec5SDimitry Andric // For user-defined custom sections, strip the prefix 1539647cbc5dSDimitry Andric Name.consume_front(".custom_section."); 15400b57cec5SDimitry Andric 15410b57cec5SDimitry Andric MCSymbol *Begin = Sec.getBeginSymbol(); 15420b57cec5SDimitry Andric if (Begin) { 1543e8d8bef9SDimitry Andric assert(WasmIndices.count(cast<MCSymbolWasm>(Begin)) == 0); 15440b57cec5SDimitry Andric WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size(); 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric 15470b57cec5SDimitry Andric // Separate out the producers and target features sections 15480b57cec5SDimitry Andric if (Name == "producers") { 15498bcb0991SDimitry Andric ProducersSection = std::make_unique<WasmCustomSection>(Name, &Section); 15500b57cec5SDimitry Andric continue; 15510b57cec5SDimitry Andric } 15520b57cec5SDimitry Andric if (Name == "target_features") { 15530b57cec5SDimitry Andric TargetFeaturesSection = 15548bcb0991SDimitry Andric std::make_unique<WasmCustomSection>(Name, &Section); 15550b57cec5SDimitry Andric continue; 15560b57cec5SDimitry Andric } 15570b57cec5SDimitry Andric 1558e8d8bef9SDimitry Andric // Custom sections can also belong to COMDAT groups. In this case the 1559e8d8bef9SDimitry Andric // decriptor's "index" field is the section index (in the final object 1560e8d8bef9SDimitry Andric // file), but that is not known until after layout, so it must be fixed up 1561e8d8bef9SDimitry Andric // later 1562e8d8bef9SDimitry Andric if (const MCSymbolWasm *C = Section.getGroup()) { 1563e8d8bef9SDimitry Andric Comdats[C->getName()].emplace_back( 1564e8d8bef9SDimitry Andric WasmComdatEntry{wasm::WASM_COMDAT_SECTION, 1565e8d8bef9SDimitry Andric static_cast<uint32_t>(CustomSections.size())}); 1566e8d8bef9SDimitry Andric } 1567e8d8bef9SDimitry Andric 15680b57cec5SDimitry Andric CustomSections.emplace_back(Name, &Section); 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric } 15710b57cec5SDimitry Andric 1572e8d8bef9SDimitry Andric if (Mode != DwoMode::DwoOnly) { 15730b57cec5SDimitry Andric // Populate WasmIndices and DataLocations for defined symbols. 15740b57cec5SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 15750b57cec5SDimitry Andric // Ignore unnamed temporary symbols, which aren't ever exported, imported, 15760b57cec5SDimitry Andric // or used in relocations. 15770b57cec5SDimitry Andric if (S.isTemporary() && S.getName().empty()) 15780b57cec5SDimitry Andric continue; 15790b57cec5SDimitry Andric 15800b57cec5SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 158181ad6265SDimitry Andric LLVM_DEBUG( 158281ad6265SDimitry Andric dbgs() << "MCSymbol: " 158381ad6265SDimitry Andric << toString(WS.getType().value_or(wasm::WASM_SYMBOL_TYPE_DATA)) 1584fe6060f1SDimitry Andric << " '" << S << "'" 15850b57cec5SDimitry Andric << " isDefined=" << S.isDefined() << " isExternal=" 15860b57cec5SDimitry Andric << S.isExternal() << " isTemporary=" << S.isTemporary() 15870b57cec5SDimitry Andric << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden() 15880b57cec5SDimitry Andric << " isVariable=" << WS.isVariable() << "\n"); 15890b57cec5SDimitry Andric 15900b57cec5SDimitry Andric if (WS.isVariable()) 15910b57cec5SDimitry Andric continue; 15920b57cec5SDimitry Andric if (WS.isComdat() && !WS.isDefined()) 15930b57cec5SDimitry Andric continue; 15940b57cec5SDimitry Andric 15950b57cec5SDimitry Andric if (WS.isFunction()) { 15960b57cec5SDimitry Andric unsigned Index; 15970b57cec5SDimitry Andric if (WS.isDefined()) { 15980b57cec5SDimitry Andric if (WS.getOffset() != 0) 15990b57cec5SDimitry Andric report_fatal_error( 16000b57cec5SDimitry Andric "function sections must contain one function each"); 16010b57cec5SDimitry Andric 16020b57cec5SDimitry Andric // A definition. Write out the function body. 16030b57cec5SDimitry Andric Index = NumFunctionImports + Functions.size(); 16040b57cec5SDimitry Andric WasmFunction Func; 16050b57cec5SDimitry Andric Func.SigIndex = getFunctionType(WS); 1606bdd1243dSDimitry Andric Func.Section = &WS.getSection(); 1607e8d8bef9SDimitry Andric assert(WasmIndices.count(&WS) == 0); 16080b57cec5SDimitry Andric WasmIndices[&WS] = Index; 16090b57cec5SDimitry Andric Functions.push_back(Func); 16100b57cec5SDimitry Andric 16110b57cec5SDimitry Andric auto &Section = static_cast<MCSectionWasm &>(WS.getSection()); 16120b57cec5SDimitry Andric if (const MCSymbolWasm *C = Section.getGroup()) { 16130b57cec5SDimitry Andric Comdats[C->getName()].emplace_back( 16140b57cec5SDimitry Andric WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index}); 16150b57cec5SDimitry Andric } 1616480093f4SDimitry Andric 1617480093f4SDimitry Andric if (WS.hasExportName()) { 1618480093f4SDimitry Andric wasm::WasmExport Export; 1619480093f4SDimitry Andric Export.Name = WS.getExportName(); 1620480093f4SDimitry Andric Export.Kind = wasm::WASM_EXTERNAL_FUNCTION; 1621480093f4SDimitry Andric Export.Index = Index; 1622480093f4SDimitry Andric Exports.push_back(Export); 1623480093f4SDimitry Andric } 16240b57cec5SDimitry Andric } else { 16250b57cec5SDimitry Andric // An import; the index was assigned above. 16260b57cec5SDimitry Andric Index = WasmIndices.find(&WS)->second; 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric 16290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n"); 16300b57cec5SDimitry Andric 16310b57cec5SDimitry Andric } else if (WS.isData()) { 16320b57cec5SDimitry Andric if (!isInSymtab(WS)) 16330b57cec5SDimitry Andric continue; 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric if (!WS.isDefined()) { 16360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> segment index: -1" 16370b57cec5SDimitry Andric << "\n"); 16380b57cec5SDimitry Andric continue; 16390b57cec5SDimitry Andric } 16400b57cec5SDimitry Andric 16410b57cec5SDimitry Andric if (!WS.getSize()) 16420b57cec5SDimitry Andric report_fatal_error("data symbols must have a size set with .size: " + 16430b57cec5SDimitry Andric WS.getName()); 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric int64_t Size = 0; 16460fca6ea1SDimitry Andric if (!WS.getSize()->evaluateAsAbsolute(Size, Asm)) 16470b57cec5SDimitry Andric report_fatal_error(".size expression must be evaluatable"); 16480b57cec5SDimitry Andric 16490b57cec5SDimitry Andric auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection()); 16508bcb0991SDimitry Andric if (!DataSection.isWasmData()) 16518bcb0991SDimitry Andric report_fatal_error("data symbols must live in a data section: " + 16528bcb0991SDimitry Andric WS.getName()); 16530b57cec5SDimitry Andric 16540b57cec5SDimitry Andric // For each data symbol, export it in the symtab as a reference to the 16550b57cec5SDimitry Andric // corresponding Wasm data segment. 16560b57cec5SDimitry Andric wasm::WasmDataReference Ref = wasm::WasmDataReference{ 16570fca6ea1SDimitry Andric DataSection.getSegmentIndex(), Asm.getSymbolOffset(WS), 16585ffd83dbSDimitry Andric static_cast<uint64_t>(Size)}; 1659e8d8bef9SDimitry Andric assert(DataLocations.count(&WS) == 0); 16600b57cec5SDimitry Andric DataLocations[&WS] = Ref; 16610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n"); 16620b57cec5SDimitry Andric 16630b57cec5SDimitry Andric } else if (WS.isGlobal()) { 16640b57cec5SDimitry Andric // A "true" Wasm global (currently just __stack_pointer) 16655ffd83dbSDimitry Andric if (WS.isDefined()) { 16665ffd83dbSDimitry Andric wasm::WasmGlobal Global; 16675ffd83dbSDimitry Andric Global.Type = WS.getGlobalType(); 16685ffd83dbSDimitry Andric Global.Index = NumGlobalImports + Globals.size(); 166981ad6265SDimitry Andric Global.InitExpr.Extended = false; 16705ffd83dbSDimitry Andric switch (Global.Type.Type) { 16715ffd83dbSDimitry Andric case wasm::WASM_TYPE_I32: 167281ad6265SDimitry Andric Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_I32_CONST; 16735ffd83dbSDimitry Andric break; 16745ffd83dbSDimitry Andric case wasm::WASM_TYPE_I64: 167581ad6265SDimitry Andric Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_I64_CONST; 16765ffd83dbSDimitry Andric break; 16775ffd83dbSDimitry Andric case wasm::WASM_TYPE_F32: 167881ad6265SDimitry Andric Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_F32_CONST; 16795ffd83dbSDimitry Andric break; 16805ffd83dbSDimitry Andric case wasm::WASM_TYPE_F64: 168181ad6265SDimitry Andric Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_F64_CONST; 16825ffd83dbSDimitry Andric break; 16835ffd83dbSDimitry Andric case wasm::WASM_TYPE_EXTERNREF: 168481ad6265SDimitry Andric Global.InitExpr.Inst.Opcode = wasm::WASM_OPCODE_REF_NULL; 16855ffd83dbSDimitry Andric break; 16865ffd83dbSDimitry Andric default: 16875ffd83dbSDimitry Andric llvm_unreachable("unexpected type"); 16885ffd83dbSDimitry Andric } 1689e8d8bef9SDimitry Andric assert(WasmIndices.count(&WS) == 0); 16905ffd83dbSDimitry Andric WasmIndices[&WS] = Global.Index; 16915ffd83dbSDimitry Andric Globals.push_back(Global); 16925ffd83dbSDimitry Andric } else { 16930b57cec5SDimitry Andric // An import; the index was assigned above 16940b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> global index: " 16950b57cec5SDimitry Andric << WasmIndices.find(&WS)->second << "\n"); 16965ffd83dbSDimitry Andric } 1697e8d8bef9SDimitry Andric } else if (WS.isTable()) { 1698e8d8bef9SDimitry Andric if (WS.isDefined()) { 1699e8d8bef9SDimitry Andric wasm::WasmTable Table; 1700e8d8bef9SDimitry Andric Table.Index = NumTableImports + Tables.size(); 1701fe6060f1SDimitry Andric Table.Type = WS.getTableType(); 1702e8d8bef9SDimitry Andric assert(WasmIndices.count(&WS) == 0); 1703e8d8bef9SDimitry Andric WasmIndices[&WS] = Table.Index; 1704e8d8bef9SDimitry Andric Tables.push_back(Table); 1705e8d8bef9SDimitry Andric } 1706e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << " -> table index: " 1707e8d8bef9SDimitry Andric << WasmIndices.find(&WS)->second << "\n"); 1708fe6060f1SDimitry Andric } else if (WS.isTag()) { 1709349cc55cSDimitry Andric // C++ exception symbol (__cpp_exception) or longjmp symbol 1710349cc55cSDimitry Andric // (__c_longjmp) 17110b57cec5SDimitry Andric unsigned Index; 17120b57cec5SDimitry Andric if (WS.isDefined()) { 1713349cc55cSDimitry Andric Index = NumTagImports + TagTypes.size(); 1714349cc55cSDimitry Andric uint32_t SigIndex = getTagType(WS); 1715e8d8bef9SDimitry Andric assert(WasmIndices.count(&WS) == 0); 17160b57cec5SDimitry Andric WasmIndices[&WS] = Index; 1717349cc55cSDimitry Andric TagTypes.push_back(SigIndex); 17180b57cec5SDimitry Andric } else { 17190b57cec5SDimitry Andric // An import; the index was assigned above. 17200b57cec5SDimitry Andric assert(WasmIndices.count(&WS) > 0); 17210b57cec5SDimitry Andric } 1722fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << " -> tag index: " << WasmIndices.find(&WS)->second 1723fe6060f1SDimitry Andric << "\n"); 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric } else { 17260b57cec5SDimitry Andric assert(WS.isSection()); 17270b57cec5SDimitry Andric } 17280b57cec5SDimitry Andric } 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andric // Populate WasmIndices and DataLocations for aliased symbols. We need to 17310b57cec5SDimitry Andric // process these in a separate pass because we need to have processed the 17320b57cec5SDimitry Andric // target of the alias before the alias itself and the symbols are not 17330b57cec5SDimitry Andric // necessarily ordered in this way. 17340b57cec5SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 17350b57cec5SDimitry Andric if (!S.isVariable()) 17360b57cec5SDimitry Andric continue; 17370b57cec5SDimitry Andric 17380b57cec5SDimitry Andric assert(S.isDefined()); 17390b57cec5SDimitry Andric 17400fca6ea1SDimitry Andric const auto *BS = Asm.getBaseSymbol(S); 1741e8d8bef9SDimitry Andric if (!BS) 1742e8d8bef9SDimitry Andric report_fatal_error(Twine(S.getName()) + 1743e8d8bef9SDimitry Andric ": absolute addressing not supported!"); 1744e8d8bef9SDimitry Andric const MCSymbolWasm *Base = cast<MCSymbolWasm>(BS); 17455ffd83dbSDimitry Andric 17460b57cec5SDimitry Andric // Find the target symbol of this weak alias and export that index 17470b57cec5SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 1748e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *Base 1749e8d8bef9SDimitry Andric << "'\n"); 17500b57cec5SDimitry Andric 17515ffd83dbSDimitry Andric if (Base->isFunction()) { 17525ffd83dbSDimitry Andric assert(WasmIndices.count(Base) > 0); 17535ffd83dbSDimitry Andric uint32_t WasmIndex = WasmIndices.find(Base)->second; 17540b57cec5SDimitry Andric assert(WasmIndices.count(&WS) == 0); 17550b57cec5SDimitry Andric WasmIndices[&WS] = WasmIndex; 17560b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n"); 17575ffd83dbSDimitry Andric } else if (Base->isData()) { 17585ffd83dbSDimitry Andric auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection()); 17590fca6ea1SDimitry Andric uint64_t Offset = Asm.getSymbolOffset(S); 17605ffd83dbSDimitry Andric int64_t Size = 0; 17615ffd83dbSDimitry Andric // For data symbol alias we use the size of the base symbol as the 17625ffd83dbSDimitry Andric // size of the alias. When an offset from the base is involved this 17635ffd83dbSDimitry Andric // can result in a offset + size goes past the end of the data section 17645ffd83dbSDimitry Andric // which out object format doesn't support. So we must clamp it. 17650fca6ea1SDimitry Andric if (!Base->getSize()->evaluateAsAbsolute(Size, Asm)) 17665ffd83dbSDimitry Andric report_fatal_error(".size expression must be evaluatable"); 17675ffd83dbSDimitry Andric const WasmDataSegment &Segment = 17685ffd83dbSDimitry Andric DataSegments[DataSection.getSegmentIndex()]; 17695ffd83dbSDimitry Andric Size = 17705ffd83dbSDimitry Andric std::min(static_cast<uint64_t>(Size), Segment.Data.size() - Offset); 17715ffd83dbSDimitry Andric wasm::WasmDataReference Ref = wasm::WasmDataReference{ 17725ffd83dbSDimitry Andric DataSection.getSegmentIndex(), 17730fca6ea1SDimitry Andric static_cast<uint32_t>(Asm.getSymbolOffset(S)), 17745ffd83dbSDimitry Andric static_cast<uint32_t>(Size)}; 17750b57cec5SDimitry Andric DataLocations[&WS] = Ref; 17760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n"); 17770b57cec5SDimitry Andric } else { 1778fe6060f1SDimitry Andric report_fatal_error("don't yet support global/tag aliases"); 17790b57cec5SDimitry Andric } 17800b57cec5SDimitry Andric } 1781e8d8bef9SDimitry Andric } 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric // Finally, populate the symbol table itself, in its "natural" order. 17840b57cec5SDimitry Andric for (const MCSymbol &S : Asm.symbols()) { 17850b57cec5SDimitry Andric const auto &WS = static_cast<const MCSymbolWasm &>(S); 17860b57cec5SDimitry Andric if (!isInSymtab(WS)) { 17870b57cec5SDimitry Andric WS.setIndex(InvalidIndex); 17880b57cec5SDimitry Andric continue; 17890b57cec5SDimitry Andric } 17900b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n"); 17910b57cec5SDimitry Andric 17920b57cec5SDimitry Andric uint32_t Flags = 0; 17930b57cec5SDimitry Andric if (WS.isWeak()) 17940b57cec5SDimitry Andric Flags |= wasm::WASM_SYMBOL_BINDING_WEAK; 17950b57cec5SDimitry Andric if (WS.isHidden()) 17960b57cec5SDimitry Andric Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN; 17970b57cec5SDimitry Andric if (!WS.isExternal() && WS.isDefined()) 17980b57cec5SDimitry Andric Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL; 17990b57cec5SDimitry Andric if (WS.isUndefined()) 18000b57cec5SDimitry Andric Flags |= wasm::WASM_SYMBOL_UNDEFINED; 18018bcb0991SDimitry Andric if (WS.isNoStrip()) { 18028bcb0991SDimitry Andric Flags |= wasm::WASM_SYMBOL_NO_STRIP; 18038bcb0991SDimitry Andric if (isEmscripten()) { 18040b57cec5SDimitry Andric Flags |= wasm::WASM_SYMBOL_EXPORTED; 18058bcb0991SDimitry Andric } 18068bcb0991SDimitry Andric } 1807480093f4SDimitry Andric if (WS.hasImportName()) 18080b57cec5SDimitry Andric Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME; 1809480093f4SDimitry Andric if (WS.hasExportName()) 1810480093f4SDimitry Andric Flags |= wasm::WASM_SYMBOL_EXPORTED; 1811349cc55cSDimitry Andric if (WS.isTLS()) 1812349cc55cSDimitry Andric Flags |= wasm::WASM_SYMBOL_TLS; 18130b57cec5SDimitry Andric 18140b57cec5SDimitry Andric wasm::WasmSymbolInfo Info; 18150b57cec5SDimitry Andric Info.Name = WS.getName(); 181681ad6265SDimitry Andric Info.Kind = WS.getType().value_or(wasm::WASM_SYMBOL_TYPE_DATA); 18170b57cec5SDimitry Andric Info.Flags = Flags; 18180b57cec5SDimitry Andric if (!WS.isData()) { 18190b57cec5SDimitry Andric assert(WasmIndices.count(&WS) > 0); 18200b57cec5SDimitry Andric Info.ElementIndex = WasmIndices.find(&WS)->second; 18210b57cec5SDimitry Andric } else if (WS.isDefined()) { 18220b57cec5SDimitry Andric assert(DataLocations.count(&WS) > 0); 18230b57cec5SDimitry Andric Info.DataRef = DataLocations.find(&WS)->second; 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric WS.setIndex(SymbolInfos.size()); 18260b57cec5SDimitry Andric SymbolInfos.emplace_back(Info); 18270b57cec5SDimitry Andric } 18280b57cec5SDimitry Andric 18290b57cec5SDimitry Andric { 18300b57cec5SDimitry Andric auto HandleReloc = [&](const WasmRelocationEntry &Rel) { 18310b57cec5SDimitry Andric // Functions referenced by a relocation need to put in the table. This is 18320b57cec5SDimitry Andric // purely to make the object file's provisional values readable, and is 18330b57cec5SDimitry Andric // ignored by the linker, which re-calculates the relocations itself. 18340b57cec5SDimitry Andric if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 && 1835e8d8bef9SDimitry Andric Rel.Type != wasm::R_WASM_TABLE_INDEX_I64 && 18365ffd83dbSDimitry Andric Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB && 1837e8d8bef9SDimitry Andric Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB64 && 1838fe6060f1SDimitry Andric Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB && 1839fe6060f1SDimitry Andric Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB64) 18400b57cec5SDimitry Andric return; 18410b57cec5SDimitry Andric assert(Rel.Symbol->isFunction()); 18425ffd83dbSDimitry Andric const MCSymbolWasm *Base = 18430fca6ea1SDimitry Andric cast<MCSymbolWasm>(Asm.getBaseSymbol(*Rel.Symbol)); 18445ffd83dbSDimitry Andric uint32_t FunctionIndex = WasmIndices.find(Base)->second; 18450b57cec5SDimitry Andric uint32_t TableIndex = TableElems.size() + InitialTableOffset; 18465ffd83dbSDimitry Andric if (TableIndices.try_emplace(Base, TableIndex).second) { 18475ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << " -> adding " << Base->getName() 18480b57cec5SDimitry Andric << " to table: " << TableIndex << "\n"); 18490b57cec5SDimitry Andric TableElems.push_back(FunctionIndex); 18505ffd83dbSDimitry Andric registerFunctionType(*Base); 18510b57cec5SDimitry Andric } 18520b57cec5SDimitry Andric }; 18530b57cec5SDimitry Andric 18540b57cec5SDimitry Andric for (const WasmRelocationEntry &RelEntry : CodeRelocations) 18550b57cec5SDimitry Andric HandleReloc(RelEntry); 18560b57cec5SDimitry Andric for (const WasmRelocationEntry &RelEntry : DataRelocations) 18570b57cec5SDimitry Andric HandleReloc(RelEntry); 18580b57cec5SDimitry Andric } 18590b57cec5SDimitry Andric 18600b57cec5SDimitry Andric // Translate .init_array section contents into start functions. 18610b57cec5SDimitry Andric for (const MCSection &S : Asm) { 18620b57cec5SDimitry Andric const auto &WS = static_cast<const MCSectionWasm &>(S); 18635f757f3fSDimitry Andric if (WS.getName().starts_with(".fini_array")) 18640b57cec5SDimitry Andric report_fatal_error(".fini_array sections are unsupported"); 18655f757f3fSDimitry Andric if (!WS.getName().starts_with(".init_array")) 18660b57cec5SDimitry Andric continue; 18670b57cec5SDimitry Andric auto IT = WS.begin(); 18680fca6ea1SDimitry Andric if (IT == WS.end()) 18690fca6ea1SDimitry Andric continue; 18700b57cec5SDimitry Andric const MCFragment &EmptyFrag = *IT; 18710b57cec5SDimitry Andric if (EmptyFrag.getKind() != MCFragment::FT_Data) 18720b57cec5SDimitry Andric report_fatal_error(".init_array section should be aligned"); 18730b57cec5SDimitry Andric 1874*6c05f3a7SDimitry Andric const MCFragment *nextFrag = EmptyFrag.getNext(); 1875*6c05f3a7SDimitry Andric while (nextFrag != nullptr) { 1876*6c05f3a7SDimitry Andric const MCFragment &AlignFrag = *nextFrag; 18770b57cec5SDimitry Andric if (AlignFrag.getKind() != MCFragment::FT_Align) 18780b57cec5SDimitry Andric report_fatal_error(".init_array section should be aligned"); 187981ad6265SDimitry Andric if (cast<MCAlignFragment>(AlignFrag).getAlignment() != 188081ad6265SDimitry Andric Align(is64Bit() ? 8 : 4)) 1881*6c05f3a7SDimitry Andric report_fatal_error( 1882*6c05f3a7SDimitry Andric ".init_array section should be aligned for pointers"); 18830b57cec5SDimitry Andric 18840fca6ea1SDimitry Andric const MCFragment &Frag = *AlignFrag.getNext(); 1885*6c05f3a7SDimitry Andric nextFrag = Frag.getNext(); 18860b57cec5SDimitry Andric if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data) 18870b57cec5SDimitry Andric report_fatal_error("only data supported in .init_array section"); 18880b57cec5SDimitry Andric 18890b57cec5SDimitry Andric uint16_t Priority = UINT16_MAX; 18900b57cec5SDimitry Andric unsigned PrefixLength = strlen(".init_array"); 18915ffd83dbSDimitry Andric if (WS.getName().size() > PrefixLength) { 18925ffd83dbSDimitry Andric if (WS.getName()[PrefixLength] != '.') 18930b57cec5SDimitry Andric report_fatal_error( 18940b57cec5SDimitry Andric ".init_array section priority should start with '.'"); 18955ffd83dbSDimitry Andric if (WS.getName().substr(PrefixLength + 1).getAsInteger(10, Priority)) 18960b57cec5SDimitry Andric report_fatal_error("invalid .init_array section priority"); 18970b57cec5SDimitry Andric } 18980b57cec5SDimitry Andric const auto &DataFrag = cast<MCDataFragment>(Frag); 18990b57cec5SDimitry Andric const SmallVectorImpl<char> &Contents = DataFrag.getContents(); 19000b57cec5SDimitry Andric for (const uint8_t * 19010b57cec5SDimitry Andric P = (const uint8_t *)Contents.data(), 19020b57cec5SDimitry Andric *End = (const uint8_t *)Contents.data() + Contents.size(); 19030b57cec5SDimitry Andric P != End; ++P) { 19040b57cec5SDimitry Andric if (*P != 0) 19050b57cec5SDimitry Andric report_fatal_error("non-symbolic data in .init_array section"); 19060b57cec5SDimitry Andric } 19070b57cec5SDimitry Andric for (const MCFixup &Fixup : DataFrag.getFixups()) { 19080b57cec5SDimitry Andric assert(Fixup.getKind() == 19090b57cec5SDimitry Andric MCFixup::getKindForSize(is64Bit() ? 8 : 4, false)); 19100b57cec5SDimitry Andric const MCExpr *Expr = Fixup.getValue(); 19110b57cec5SDimitry Andric auto *SymRef = dyn_cast<MCSymbolRefExpr>(Expr); 19120b57cec5SDimitry Andric if (!SymRef) 1913*6c05f3a7SDimitry Andric report_fatal_error( 1914*6c05f3a7SDimitry Andric "fixups in .init_array should be symbol references"); 19150b57cec5SDimitry Andric const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol()); 19160b57cec5SDimitry Andric if (TargetSym.getIndex() == InvalidIndex) 19175ffd83dbSDimitry Andric report_fatal_error("symbols in .init_array should exist in symtab"); 19180b57cec5SDimitry Andric if (!TargetSym.isFunction()) 19190b57cec5SDimitry Andric report_fatal_error("symbols in .init_array should be for functions"); 1920*6c05f3a7SDimitry Andric InitFuncs.push_back(std::make_pair(Priority, TargetSym.getIndex())); 1921*6c05f3a7SDimitry Andric } 19220b57cec5SDimitry Andric } 19230b57cec5SDimitry Andric } 19240b57cec5SDimitry Andric 19250b57cec5SDimitry Andric // Write out the Wasm header. 19260b57cec5SDimitry Andric writeHeader(Asm); 19270b57cec5SDimitry Andric 1928e8d8bef9SDimitry Andric uint32_t CodeSectionIndex, DataSectionIndex; 1929e8d8bef9SDimitry Andric if (Mode != DwoMode::DwoOnly) { 19300b57cec5SDimitry Andric writeTypeSection(Signatures); 19310b57cec5SDimitry Andric writeImportSection(Imports, DataSize, TableElems.size()); 19320b57cec5SDimitry Andric writeFunctionSection(Functions); 1933e8d8bef9SDimitry Andric writeTableSection(Tables); 19340b57cec5SDimitry Andric // Skip the "memory" section; we import the memory instead. 1935349cc55cSDimitry Andric writeTagSection(TagTypes); 19365ffd83dbSDimitry Andric writeGlobalSection(Globals); 19370b57cec5SDimitry Andric writeExportSection(Exports); 1938fe6060f1SDimitry Andric const MCSymbol *IndirectFunctionTable = 1939fe6060f1SDimitry Andric Asm.getContext().lookupSymbol("__indirect_function_table"); 1940fe6060f1SDimitry Andric writeElemSection(cast_or_null<const MCSymbolWasm>(IndirectFunctionTable), 1941fe6060f1SDimitry Andric TableElems); 19420b57cec5SDimitry Andric writeDataCountSection(); 1943e8d8bef9SDimitry Andric 19440fca6ea1SDimitry Andric CodeSectionIndex = writeCodeSection(Asm, Functions); 19450fca6ea1SDimitry Andric DataSectionIndex = writeDataSection(Asm); 1946e8d8bef9SDimitry Andric } 1947e8d8bef9SDimitry Andric 1948e8d8bef9SDimitry Andric // The Sections in the COMDAT list have placeholder indices (their index among 1949e8d8bef9SDimitry Andric // custom sections, rather than among all sections). Fix them up here. 1950e8d8bef9SDimitry Andric for (auto &Group : Comdats) { 1951e8d8bef9SDimitry Andric for (auto &Entry : Group.second) { 1952e8d8bef9SDimitry Andric if (Entry.Kind == wasm::WASM_COMDAT_SECTION) { 1953e8d8bef9SDimitry Andric Entry.Index += SectionCount; 1954e8d8bef9SDimitry Andric } 1955e8d8bef9SDimitry Andric } 1956e8d8bef9SDimitry Andric } 19570b57cec5SDimitry Andric for (auto &CustomSection : CustomSections) 19580fca6ea1SDimitry Andric writeCustomSection(CustomSection, Asm); 1959e8d8bef9SDimitry Andric 1960e8d8bef9SDimitry Andric if (Mode != DwoMode::DwoOnly) { 19610b57cec5SDimitry Andric writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats); 1962e8d8bef9SDimitry Andric 19630b57cec5SDimitry Andric writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations); 19640b57cec5SDimitry Andric writeRelocSection(DataSectionIndex, "DATA", DataRelocations); 1965e8d8bef9SDimitry Andric } 19660b57cec5SDimitry Andric writeCustomRelocSections(); 19670b57cec5SDimitry Andric if (ProducersSection) 19680fca6ea1SDimitry Andric writeCustomSection(*ProducersSection, Asm); 19690b57cec5SDimitry Andric if (TargetFeaturesSection) 19700fca6ea1SDimitry Andric writeCustomSection(*TargetFeaturesSection, Asm); 19710b57cec5SDimitry Andric 19720b57cec5SDimitry Andric // TODO: Translate the .comment section to the output. 1973e8d8bef9SDimitry Andric return W->OS.tell() - StartOffset; 19740b57cec5SDimitry Andric } 19750b57cec5SDimitry Andric 19760b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> 19770b57cec5SDimitry Andric llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, 19780b57cec5SDimitry Andric raw_pwrite_stream &OS) { 19798bcb0991SDimitry Andric return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS); 19800b57cec5SDimitry Andric } 1981e8d8bef9SDimitry Andric 1982e8d8bef9SDimitry Andric std::unique_ptr<MCObjectWriter> 1983e8d8bef9SDimitry Andric llvm::createWasmDwoObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW, 1984e8d8bef9SDimitry Andric raw_pwrite_stream &OS, 1985e8d8bef9SDimitry Andric raw_pwrite_stream &DwoOS) { 1986e8d8bef9SDimitry Andric return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS, DwoOS); 1987e8d8bef9SDimitry Andric } 1988