1 //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Wasm object file writer information.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/BinaryFormat/Wasm.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCFixupKindInfo.h"
23 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSectionWasm.h"
25 #include "llvm/MC/MCSymbolWasm.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/MC/MCWasmObjectWriter.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/LEB128.h"
32 #include "llvm/Support/StringSaver.h"
37 #define DEBUG_TYPE "mc"
41 // For patching purposes, we need to remember where each section starts, both
42 // for patching up the section size field, and for patching up references to
43 // locations within the section.
44 struct SectionBookkeeping {
45 // Where the size of the section is written.
47 // Where the contents of the section starts (after the header).
48 uint64_t ContentsOffset;
51 // The signature of a wasm function, in a struct capable of being used as a
53 struct WasmFunctionType {
54 // Support empty and tombstone instances, needed by DenseMap.
55 enum { Plain, Empty, Tombstone } State;
57 // The return types of the function.
58 SmallVector<wasm::ValType, 1> Returns;
60 // The parameter types of the function.
61 SmallVector<wasm::ValType, 4> Params;
63 WasmFunctionType() : State(Plain) {}
65 bool operator==(const WasmFunctionType &Other) const {
66 return State == Other.State && Returns == Other.Returns &&
67 Params == Other.Params;
71 // Traits for using WasmFunctionType in a DenseMap.
72 struct WasmFunctionTypeDenseMapInfo {
73 static WasmFunctionType getEmptyKey() {
74 WasmFunctionType FuncTy;
75 FuncTy.State = WasmFunctionType::Empty;
78 static WasmFunctionType getTombstoneKey() {
79 WasmFunctionType FuncTy;
80 FuncTy.State = WasmFunctionType::Tombstone;
83 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
84 uintptr_t Value = FuncTy.State;
85 for (wasm::ValType Ret : FuncTy.Returns)
86 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
87 for (wasm::ValType Param : FuncTy.Params)
88 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
91 static bool isEqual(const WasmFunctionType &LHS,
92 const WasmFunctionType &RHS) {
97 // A wasm data segment. A wasm binary contains only a single data section
98 // but that can contain many segments, each with their own virtual location
99 // in memory. Each MCSection data created by llvm is modeled as its own
100 // wasm data segment.
101 struct WasmDataSegment {
102 MCSectionWasm *Section;
107 SmallVector<char, 4> Data;
110 // A wasm import to be written into the import section.
112 StringRef ModuleName;
119 // A wasm function to be written into the function section.
120 struct WasmFunction {
122 const MCSymbolWasm *Sym;
125 // A wasm export to be written into the export section.
132 // A wasm global to be written into the global section.
137 uint64_t InitialValue;
138 uint32_t ImportIndex;
141 // Information about a single relocation.
142 struct WasmRelocationEntry {
143 uint64_t Offset; // Where is the relocation.
144 const MCSymbolWasm *Symbol; // The symbol to relocate with.
145 int64_t Addend; // A value to add to the symbol.
146 unsigned Type; // The type of the relocation.
147 const MCSectionWasm *FixupSection;// The section the relocation is targeting.
149 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
150 int64_t Addend, unsigned Type,
151 const MCSectionWasm *FixupSection)
152 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
153 FixupSection(FixupSection) {}
155 bool hasAddend() const {
157 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
158 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
159 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
166 void print(raw_ostream &Out) const {
167 Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
169 << ", FixupSection=" << FixupSection->getSectionName();
172 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
173 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
178 raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
184 class WasmObjectWriter : public MCObjectWriter {
185 /// Helper struct for containing some precomputed information on symbols.
186 struct WasmSymbolData {
187 const MCSymbolWasm *Symbol;
190 // Support lexicographic sorting.
191 bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; }
194 /// The target specific Wasm writer instance.
195 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
197 // Relocations for fixing up references in the code section.
198 std::vector<WasmRelocationEntry> CodeRelocations;
200 // Relocations for fixing up references in the data section.
201 std::vector<WasmRelocationEntry> DataRelocations;
203 // Index values to use for fixing up call_indirect type indices.
204 // Maps function symbols to the index of the type of the function
205 DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
206 // Maps function symbols to the table element index space. Used
207 // for TABLE_INDEX relocation types (i.e. address taken functions).
208 DenseMap<const MCSymbolWasm *, uint32_t> IndirectSymbolIndices;
209 // Maps function/global symbols to the function/global index space.
210 DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
212 DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
214 SmallVector<WasmFunctionType, 4> FunctionTypes;
215 SmallVector<WasmGlobal, 4> Globals;
216 unsigned NumGlobalImports = 0;
218 // TargetObjectWriter wrappers.
219 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
220 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
221 return TargetObjectWriter->getRelocType(Target, Fixup);
224 void startSection(SectionBookkeeping &Section, unsigned SectionId,
225 const char *Name = nullptr);
226 void endSection(SectionBookkeeping &Section);
229 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
230 raw_pwrite_stream &OS)
231 : MCObjectWriter(OS, /*IsLittleEndian=*/true),
232 TargetObjectWriter(std::move(MOTW)) {}
235 ~WasmObjectWriter() override;
237 void reset() override {
238 CodeRelocations.clear();
239 DataRelocations.clear();
241 SymbolIndices.clear();
242 IndirectSymbolIndices.clear();
243 FunctionTypeIndices.clear();
244 FunctionTypes.clear();
246 MCObjectWriter::reset();
247 NumGlobalImports = 0;
250 void writeHeader(const MCAssembler &Asm);
252 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
253 const MCFragment *Fragment, const MCFixup &Fixup,
254 MCValue Target, uint64_t &FixedValue) override;
256 void executePostLayoutBinding(MCAssembler &Asm,
257 const MCAsmLayout &Layout) override;
259 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
261 void writeString(const StringRef Str) {
262 encodeULEB128(Str.size(), getStream());
266 void writeValueType(wasm::ValType Ty) {
267 encodeSLEB128(int32_t(Ty), getStream());
270 void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
271 void writeImportSection(ArrayRef<WasmImport> Imports, uint32_t DataSize,
272 uint32_t NumElements);
273 void writeFunctionSection(ArrayRef<WasmFunction> Functions);
274 void writeGlobalSection();
275 void writeExportSection(ArrayRef<WasmExport> Exports);
276 void writeElemSection(ArrayRef<uint32_t> TableElems);
277 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
278 ArrayRef<WasmFunction> Functions);
279 void writeDataSection(ArrayRef<WasmDataSegment> Segments);
280 void writeNameSection(ArrayRef<WasmFunction> Functions,
281 ArrayRef<WasmImport> Imports,
282 uint32_t NumFuncImports);
283 void writeCodeRelocSection();
284 void writeDataRelocSection();
285 void writeLinkingMetaDataSection(
286 ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
287 const SmallVector<std::pair<StringRef, uint32_t>, 4> &SymbolFlags,
288 const SmallVector<std::pair<uint16_t, uint32_t>, 2> &InitFuncs);
290 uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
291 void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
292 uint64_t ContentsOffset);
294 void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations);
295 uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
296 uint32_t getFunctionType(const MCSymbolWasm& Symbol);
297 uint32_t registerFunctionType(const MCSymbolWasm& Symbol);
300 } // end anonymous namespace
302 WasmObjectWriter::~WasmObjectWriter() {}
304 // Write out a section header and a patchable section size field.
305 void WasmObjectWriter::startSection(SectionBookkeeping &Section,
308 assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) &&
309 "Only custom sections can have names");
311 DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n");
312 encodeULEB128(SectionId, getStream());
314 Section.SizeOffset = getStream().tell();
316 // The section size. We don't know the size yet, so reserve enough space
317 // for any 32-bit value; we'll patch it later.
318 encodeULEB128(UINT32_MAX, getStream());
320 // The position where the section starts, for measuring its size.
321 Section.ContentsOffset = getStream().tell();
323 // Custom sections in wasm also have a string identifier.
324 if (SectionId == wasm::WASM_SEC_CUSTOM) {
326 writeString(StringRef(Name));
330 // Now that the section is complete and we know how big it is, patch up the
331 // section size field at the start of the section.
332 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
333 uint64_t Size = getStream().tell() - Section.ContentsOffset;
334 if (uint32_t(Size) != Size)
335 report_fatal_error("section size does not fit in a uint32_t");
337 DEBUG(dbgs() << "endSection size=" << Size << "\n");
339 // Write the final section size to the payload_len field, which follows
340 // the section id byte.
342 unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
343 assert(SizeLen == 5);
344 getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
347 // Emit the Wasm header.
348 void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
349 writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
350 writeLE32(wasm::WasmVersion);
353 void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
354 const MCAsmLayout &Layout) {
357 void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
358 const MCAsmLayout &Layout,
359 const MCFragment *Fragment,
360 const MCFixup &Fixup, MCValue Target,
361 uint64_t &FixedValue) {
362 MCAsmBackend &Backend = Asm.getBackend();
363 bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
364 MCFixupKindInfo::FKF_IsPCRel;
365 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
366 uint64_t C = Target.getConstant();
367 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
368 MCContext &Ctx = Asm.getContext();
370 // The .init_array isn't translated as data, so don't do relocations in it.
371 if (FixupSection.getSectionName().startswith(".init_array"))
374 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
375 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
376 "Should not have constructed this");
378 // Let A, B and C being the components of Target and R be the location of
379 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
380 // If it is pcrel, we want to compute (A - B + C - R).
382 // In general, Wasm has no relocations for -B. It can only represent (A + C)
383 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
384 // replace B to implement it: (A - R - K + C)
388 "No relocation available to represent this relative expression");
392 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
394 if (SymB.isUndefined()) {
395 Ctx.reportError(Fixup.getLoc(),
396 Twine("symbol '") + SymB.getName() +
397 "' can not be undefined in a subtraction expression");
401 assert(!SymB.isAbsolute() && "Should have been folded");
402 const MCSection &SecB = SymB.getSection();
403 if (&SecB != &FixupSection) {
404 Ctx.reportError(Fixup.getLoc(),
405 "Cannot represent a difference across sections");
409 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
410 uint64_t K = SymBOffset - FixupOffset;
415 // We either rejected the fixup or folded B into C at this point.
416 const MCSymbolRefExpr *RefA = Target.getSymA();
417 const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
419 if (SymA && SymA->isVariable()) {
420 const MCExpr *Expr = SymA->getVariableValue();
421 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
422 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
423 llvm_unreachable("weakref used in reloc not yet implemented");
426 // Put any constant offset in an addend. Offsets can be negative, and
427 // LLVM expects wrapping, in contrast to wasm's immediates which can't
428 // be negative and don't wrap.
432 SymA->setUsedInReloc();
437 unsigned Type = getRelocType(Target, Fixup);
439 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
440 DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
442 if (FixupSection.isWasmData())
443 DataRelocations.push_back(Rec);
444 else if (FixupSection.getKind().isText())
445 CodeRelocations.push_back(Rec);
446 else if (!FixupSection.getKind().isMetadata())
447 // TODO(sbc): Add support for debug sections.
448 llvm_unreachable("unexpected section type");
451 // Write X as an (unsigned) LEB value at offset Offset in Stream, padded
452 // to allow patching.
454 WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
456 unsigned SizeLen = encodeULEB128(X, Buffer, 5);
457 assert(SizeLen == 5);
458 Stream.pwrite((char *)Buffer, SizeLen, Offset);
461 // Write X as an signed LEB value at offset Offset in Stream, padded
462 // to allow patching.
464 WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
466 unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
467 assert(SizeLen == 5);
468 Stream.pwrite((char *)Buffer, SizeLen, Offset);
471 // Write X as a plain integer value at offset Offset in Stream.
472 static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
474 support::endian::write32le(Buffer, X);
475 Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
478 static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
479 if (Symbol.isVariable()) {
480 const MCExpr *Expr = Symbol.getVariableValue();
481 auto *Inner = cast<MCSymbolRefExpr>(Expr);
482 return cast<MCSymbolWasm>(&Inner->getSymbol());
487 // Compute a value to write into the code at the location covered
488 // by RelEntry. This value isn't used by the static linker, since
489 // we have addends; it just serves to make the code more readable
490 // and to make standalone wasm modules directly usable.
492 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
493 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
495 // For undefined symbols, use a hopefully invalid value.
496 if (!Sym->isDefined(/*SetUsed=*/false))
499 uint32_t GlobalIndex = SymbolIndices[Sym];
500 const WasmGlobal& Global = Globals[GlobalIndex - NumGlobalImports];
501 uint64_t Address = Global.InitialValue + RelEntry.Addend;
503 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
504 uint32_t Value = Address;
509 static void addData(SmallVectorImpl<char> &DataBytes,
510 MCSectionWasm &DataSection) {
511 DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
513 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
515 size_t LastFragmentSize = 0;
516 for (const MCFragment &Frag : DataSection) {
517 if (Frag.hasInstructions())
518 report_fatal_error("only data supported in data sections");
520 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
521 if (Align->getValueSize() != 1)
522 report_fatal_error("only byte values supported for alignment");
523 // If nops are requested, use zeros, as this is the data section.
524 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
525 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
526 Align->getAlignment()),
528 Align->getMaxBytesToEmit());
529 DataBytes.resize(Size, Value);
530 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
532 if (!Fill->getSize().evaluateAsAbsolute(Size))
533 llvm_unreachable("The fill should be an assembler constant");
534 DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
536 const auto &DataFrag = cast<MCDataFragment>(Frag);
537 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
539 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
540 LastFragmentSize = Contents.size();
544 // Don't allow empty segments, or segments that end with zero-sized
545 // fragment, otherwise the linker cannot map symbols to a unique
546 // data segment. This can be triggered by zero-sized structs
547 // See: test/MC/WebAssembly/bss.ll
548 if (LastFragmentSize == 0)
549 DataBytes.resize(DataBytes.size() + 1);
550 DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
553 uint32_t WasmObjectWriter::getRelocationIndexValue(
554 const WasmRelocationEntry &RelEntry) {
555 switch (RelEntry.Type) {
556 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
557 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
558 if (!IndirectSymbolIndices.count(RelEntry.Symbol))
559 report_fatal_error("symbol not found in table index space: " +
560 RelEntry.Symbol->getName());
561 return IndirectSymbolIndices[RelEntry.Symbol];
562 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
563 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
564 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
565 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
566 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
567 if (!SymbolIndices.count(RelEntry.Symbol))
568 report_fatal_error("symbol not found in function/global index space: " +
569 RelEntry.Symbol->getName());
570 return SymbolIndices[RelEntry.Symbol];
571 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
572 if (!TypeIndices.count(RelEntry.Symbol))
573 report_fatal_error("symbol not found in type index space: " +
574 RelEntry.Symbol->getName());
575 return TypeIndices[RelEntry.Symbol];
577 llvm_unreachable("invalid relocation type");
581 // Apply the portions of the relocation records that we can handle ourselves
583 void WasmObjectWriter::applyRelocations(
584 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
585 raw_pwrite_stream &Stream = getStream();
586 for (const WasmRelocationEntry &RelEntry : Relocations) {
587 uint64_t Offset = ContentsOffset +
588 RelEntry.FixupSection->getSectionOffset() +
591 DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
592 switch (RelEntry.Type) {
593 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
594 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
595 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
596 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
597 uint32_t Index = getRelocationIndexValue(RelEntry);
598 WritePatchableSLEB(Stream, Index, Offset);
601 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
602 uint32_t Index = getRelocationIndexValue(RelEntry);
603 WriteI32(Stream, Index, Offset);
606 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
607 uint32_t Value = getProvisionalValue(RelEntry);
608 WritePatchableSLEB(Stream, Value, Offset);
611 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB: {
612 uint32_t Value = getProvisionalValue(RelEntry);
613 WritePatchableLEB(Stream, Value, Offset);
616 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32: {
617 uint32_t Value = getProvisionalValue(RelEntry);
618 WriteI32(Stream, Value, Offset);
622 llvm_unreachable("invalid relocation type");
627 // Write out the portions of the relocation records that the linker will
629 void WasmObjectWriter::writeRelocations(
630 ArrayRef<WasmRelocationEntry> Relocations) {
631 raw_pwrite_stream &Stream = getStream();
632 for (const WasmRelocationEntry& RelEntry : Relocations) {
634 uint64_t Offset = RelEntry.Offset +
635 RelEntry.FixupSection->getSectionOffset();
636 uint32_t Index = getRelocationIndexValue(RelEntry);
638 encodeULEB128(RelEntry.Type, Stream);
639 encodeULEB128(Offset, Stream);
640 encodeULEB128(Index, Stream);
641 if (RelEntry.hasAddend())
642 encodeSLEB128(RelEntry.Addend, Stream);
646 void WasmObjectWriter::writeTypeSection(
647 ArrayRef<WasmFunctionType> FunctionTypes) {
648 if (FunctionTypes.empty())
651 SectionBookkeeping Section;
652 startSection(Section, wasm::WASM_SEC_TYPE);
654 encodeULEB128(FunctionTypes.size(), getStream());
656 for (const WasmFunctionType &FuncTy : FunctionTypes) {
657 encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
658 encodeULEB128(FuncTy.Params.size(), getStream());
659 for (wasm::ValType Ty : FuncTy.Params)
661 encodeULEB128(FuncTy.Returns.size(), getStream());
662 for (wasm::ValType Ty : FuncTy.Returns)
669 void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports,
671 uint32_t NumElements) {
675 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
677 SectionBookkeeping Section;
678 startSection(Section, wasm::WASM_SEC_IMPORT);
680 encodeULEB128(Imports.size(), getStream());
681 for (const WasmImport &Import : Imports) {
682 writeString(Import.ModuleName);
683 writeString(Import.FieldName);
685 encodeULEB128(Import.Kind, getStream());
687 switch (Import.Kind) {
688 case wasm::WASM_EXTERNAL_FUNCTION:
689 encodeULEB128(Import.Type, getStream());
691 case wasm::WASM_EXTERNAL_GLOBAL:
692 encodeSLEB128(int32_t(Import.Type), getStream());
693 encodeULEB128(int32_t(Import.IsMutable), getStream());
695 case wasm::WASM_EXTERNAL_MEMORY:
696 encodeULEB128(0, getStream()); // flags
697 encodeULEB128(NumPages, getStream()); // initial
699 case wasm::WASM_EXTERNAL_TABLE:
700 encodeSLEB128(int32_t(Import.Type), getStream());
701 encodeULEB128(0, getStream()); // flags
702 encodeULEB128(NumElements, getStream()); // initial
705 llvm_unreachable("unsupported import kind");
712 void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
713 if (Functions.empty())
716 SectionBookkeeping Section;
717 startSection(Section, wasm::WASM_SEC_FUNCTION);
719 encodeULEB128(Functions.size(), getStream());
720 for (const WasmFunction &Func : Functions)
721 encodeULEB128(Func.Type, getStream());
726 void WasmObjectWriter::writeGlobalSection() {
730 SectionBookkeeping Section;
731 startSection(Section, wasm::WASM_SEC_GLOBAL);
733 encodeULEB128(Globals.size(), getStream());
734 for (const WasmGlobal &Global : Globals) {
735 writeValueType(Global.Type);
736 write8(Global.IsMutable);
738 if (Global.HasImport) {
739 assert(Global.InitialValue == 0);
740 write8(wasm::WASM_OPCODE_GET_GLOBAL);
741 encodeULEB128(Global.ImportIndex, getStream());
743 assert(Global.ImportIndex == 0);
744 write8(wasm::WASM_OPCODE_I32_CONST);
745 encodeSLEB128(Global.InitialValue, getStream()); // offset
747 write8(wasm::WASM_OPCODE_END);
753 void WasmObjectWriter::writeExportSection(ArrayRef<WasmExport> Exports) {
757 SectionBookkeeping Section;
758 startSection(Section, wasm::WASM_SEC_EXPORT);
760 encodeULEB128(Exports.size(), getStream());
761 for (const WasmExport &Export : Exports) {
762 writeString(Export.FieldName);
763 encodeSLEB128(Export.Kind, getStream());
764 encodeULEB128(Export.Index, getStream());
770 void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
771 if (TableElems.empty())
774 SectionBookkeeping Section;
775 startSection(Section, wasm::WASM_SEC_ELEM);
777 encodeULEB128(1, getStream()); // number of "segments"
778 encodeULEB128(0, getStream()); // the table index
780 // init expr for starting offset
781 write8(wasm::WASM_OPCODE_I32_CONST);
782 encodeSLEB128(0, getStream());
783 write8(wasm::WASM_OPCODE_END);
785 encodeULEB128(TableElems.size(), getStream());
786 for (uint32_t Elem : TableElems)
787 encodeULEB128(Elem, getStream());
792 void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
793 const MCAsmLayout &Layout,
794 ArrayRef<WasmFunction> Functions) {
795 if (Functions.empty())
798 SectionBookkeeping Section;
799 startSection(Section, wasm::WASM_SEC_CODE);
801 encodeULEB128(Functions.size(), getStream());
803 for (const WasmFunction &Func : Functions) {
804 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
807 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
808 report_fatal_error(".size expression must be evaluatable");
810 encodeULEB128(Size, getStream());
811 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
812 Asm.writeSectionData(&FuncSection, Layout);
816 applyRelocations(CodeRelocations, Section.ContentsOffset);
821 void WasmObjectWriter::writeDataSection(ArrayRef<WasmDataSegment> Segments) {
822 if (Segments.empty())
825 SectionBookkeeping Section;
826 startSection(Section, wasm::WASM_SEC_DATA);
828 encodeULEB128(Segments.size(), getStream()); // count
830 for (const WasmDataSegment & Segment : Segments) {
831 encodeULEB128(0, getStream()); // memory index
832 write8(wasm::WASM_OPCODE_I32_CONST);
833 encodeSLEB128(Segment.Offset, getStream()); // offset
834 write8(wasm::WASM_OPCODE_END);
835 encodeULEB128(Segment.Data.size(), getStream()); // size
836 Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
837 writeBytes(Segment.Data); // data
841 applyRelocations(DataRelocations, Section.ContentsOffset);
846 void WasmObjectWriter::writeNameSection(
847 ArrayRef<WasmFunction> Functions,
848 ArrayRef<WasmImport> Imports,
849 unsigned NumFuncImports) {
850 uint32_t TotalFunctions = NumFuncImports + Functions.size();
851 if (TotalFunctions == 0)
854 SectionBookkeeping Section;
855 startSection(Section, wasm::WASM_SEC_CUSTOM, "name");
856 SectionBookkeeping SubSection;
857 startSection(SubSection, wasm::WASM_NAMES_FUNCTION);
859 encodeULEB128(TotalFunctions, getStream());
861 for (const WasmImport &Import : Imports) {
862 if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
863 encodeULEB128(Index, getStream());
864 writeString(Import.FieldName);
868 for (const WasmFunction &Func : Functions) {
869 encodeULEB128(Index, getStream());
870 writeString(Func.Sym->getName());
874 endSection(SubSection);
878 void WasmObjectWriter::writeCodeRelocSection() {
879 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
880 // for descriptions of the reloc sections.
882 if (CodeRelocations.empty())
885 SectionBookkeeping Section;
886 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
888 encodeULEB128(wasm::WASM_SEC_CODE, getStream());
889 encodeULEB128(CodeRelocations.size(), getStream());
891 writeRelocations(CodeRelocations);
896 void WasmObjectWriter::writeDataRelocSection() {
897 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
898 // for descriptions of the reloc sections.
900 if (DataRelocations.empty())
903 SectionBookkeeping Section;
904 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
906 encodeULEB128(wasm::WASM_SEC_DATA, getStream());
907 encodeULEB128(DataRelocations.size(), getStream());
909 writeRelocations(DataRelocations);
914 void WasmObjectWriter::writeLinkingMetaDataSection(
915 ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
916 const SmallVector<std::pair<StringRef, uint32_t>, 4> &SymbolFlags,
917 const SmallVector<std::pair<uint16_t, uint32_t>, 2> &InitFuncs) {
918 SectionBookkeeping Section;
919 startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
920 SectionBookkeeping SubSection;
922 if (SymbolFlags.size() != 0) {
923 startSection(SubSection, wasm::WASM_SYMBOL_INFO);
924 encodeULEB128(SymbolFlags.size(), getStream());
925 for (auto Pair: SymbolFlags) {
926 writeString(Pair.first);
927 encodeULEB128(Pair.second, getStream());
929 endSection(SubSection);
933 startSection(SubSection, wasm::WASM_DATA_SIZE);
934 encodeULEB128(DataSize, getStream());
935 endSection(SubSection);
938 if (Segments.size()) {
939 startSection(SubSection, wasm::WASM_SEGMENT_INFO);
940 encodeULEB128(Segments.size(), getStream());
941 for (const WasmDataSegment &Segment : Segments) {
942 writeString(Segment.Name);
943 encodeULEB128(Segment.Alignment, getStream());
944 encodeULEB128(Segment.Flags, getStream());
946 endSection(SubSection);
949 if (!InitFuncs.empty()) {
950 startSection(SubSection, wasm::WASM_INIT_FUNCS);
951 encodeULEB128(InitFuncs.size(), getStream());
952 for (auto &StartFunc : InitFuncs) {
953 encodeULEB128(StartFunc.first, getStream()); // priority
954 encodeULEB128(StartFunc.second, getStream()); // function index
956 endSection(SubSection);
962 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
963 assert(Symbol.isFunction());
964 assert(TypeIndices.count(&Symbol));
965 return TypeIndices[&Symbol];
968 uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
969 assert(Symbol.isFunction());
972 const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
973 F.Returns = ResolvedSym->getReturns();
974 F.Params = ResolvedSym->getParams();
977 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
979 FunctionTypes.push_back(F);
980 TypeIndices[&Symbol] = Pair.first->second;
982 DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
983 DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
984 return Pair.first->second;
987 void WasmObjectWriter::writeObject(MCAssembler &Asm,
988 const MCAsmLayout &Layout) {
989 DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
990 MCContext &Ctx = Asm.getContext();
991 wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32;
993 // Collect information from the available symbols.
994 SmallVector<WasmFunction, 4> Functions;
995 SmallVector<uint32_t, 4> TableElems;
996 SmallVector<WasmImport, 4> Imports;
997 SmallVector<WasmExport, 4> Exports;
998 SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags;
999 SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
1000 unsigned NumFuncImports = 0;
1001 SmallVector<WasmDataSegment, 4> DataSegments;
1002 uint32_t DataSize = 0;
1004 // In the special .global_variables section, we've encoded global
1005 // variables used by the function. Translate them into the Globals
1007 MCSectionWasm *GlobalVars =
1008 Ctx.getWasmSection(".global_variables", SectionKind::getMetadata());
1009 if (!GlobalVars->getFragmentList().empty()) {
1010 if (GlobalVars->getFragmentList().size() != 1)
1011 report_fatal_error("only one .global_variables fragment supported");
1012 const MCFragment &Frag = *GlobalVars->begin();
1013 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1014 report_fatal_error("only data supported in .global_variables");
1015 const auto &DataFrag = cast<MCDataFragment>(Frag);
1016 if (!DataFrag.getFixups().empty())
1017 report_fatal_error("fixups not supported in .global_variables");
1018 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1019 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1020 *end = (const uint8_t *)Contents.data() + Contents.size();
1024 report_fatal_error("truncated global variable encoding");
1025 G.Type = wasm::ValType(int8_t(*p++));
1026 G.IsMutable = bool(*p++);
1027 G.HasImport = bool(*p++);
1032 Import.ModuleName = (const char *)p;
1033 const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p);
1035 report_fatal_error("global module name must be nul-terminated");
1037 nul = (const uint8_t *)memchr(p, '\0', end - p);
1039 report_fatal_error("global base name must be nul-terminated");
1040 Import.FieldName = (const char *)p;
1043 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1044 Import.Type = int32_t(G.Type);
1046 G.ImportIndex = NumGlobalImports;
1049 Imports.push_back(Import);
1052 G.InitialValue = decodeSLEB128(p, &n);
1054 if ((ptrdiff_t)n > end - p)
1055 report_fatal_error("global initial value must be valid SLEB128");
1058 Globals.push_back(G);
1062 // For now, always emit the memory import, since loads and stores are not
1063 // valid without it. In the future, we could perhaps be more clever and omit
1064 // it if there are no loads or stores.
1065 MCSymbolWasm *MemorySym =
1066 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
1067 WasmImport MemImport;
1068 MemImport.ModuleName = MemorySym->getModuleName();
1069 MemImport.FieldName = MemorySym->getName();
1070 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
1071 Imports.push_back(MemImport);
1073 // For now, always emit the table section, since indirect calls are not
1074 // valid without it. In the future, we could perhaps be more clever and omit
1075 // it if there are no indirect calls.
1076 MCSymbolWasm *TableSym =
1077 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
1078 WasmImport TableImport;
1079 TableImport.ModuleName = TableSym->getModuleName();
1080 TableImport.FieldName = TableSym->getName();
1081 TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
1082 TableImport.Type = wasm::WASM_TYPE_ANYFUNC;
1083 Imports.push_back(TableImport);
1085 // Populate FunctionTypeIndices and Imports.
1086 for (const MCSymbol &S : Asm.symbols()) {
1087 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1089 // Register types for all functions, including those with private linkage
1091 // because wasm always needs a type signature.
1092 if (WS.isFunction())
1093 registerFunctionType(WS);
1095 if (WS.isTemporary())
1098 // If the symbol is not defined in this translation unit, import it.
1099 if (!WS.isDefined(/*SetUsed=*/false) || WS.isVariable()) {
1101 Import.ModuleName = WS.getModuleName();
1102 Import.FieldName = WS.getName();
1104 if (WS.isFunction()) {
1105 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1106 Import.Type = getFunctionType(WS);
1107 SymbolIndices[&WS] = NumFuncImports;
1110 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1111 Import.Type = int32_t(PtrType);
1112 Import.IsMutable = false;
1113 SymbolIndices[&WS] = NumGlobalImports;
1115 // If this global is the stack pointer, make it mutable.
1116 if (WS.getName() == "__stack_pointer")
1117 Import.IsMutable = true;
1122 Imports.push_back(Import);
1126 for (MCSection &Sec : Asm) {
1127 auto &Section = static_cast<MCSectionWasm &>(Sec);
1128 if (!Section.isWasmData())
1131 // .init_array sections are handled specially elsewhere.
1132 if (cast<MCSectionWasm>(Sec).getSectionName().startswith(".init_array"))
1135 DataSize = alignTo(DataSize, Section.getAlignment());
1136 DataSegments.emplace_back();
1137 WasmDataSegment &Segment = DataSegments.back();
1138 Segment.Name = Section.getSectionName();
1139 Segment.Offset = DataSize;
1140 Segment.Section = &Section;
1141 addData(Segment.Data, Section);
1142 Segment.Alignment = Section.getAlignment();
1144 DataSize += Segment.Data.size();
1145 Section.setMemoryOffset(Segment.Offset);
1148 // Handle regular defined and undefined symbols.
1149 for (const MCSymbol &S : Asm.symbols()) {
1150 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1151 // or used in relocations.
1152 if (S.isTemporary() && S.getName().empty())
1155 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1156 DEBUG(dbgs() << "MCSymbol: '" << S << "'"
1157 << " isDefined=" << S.isDefined() << " isExternal="
1158 << S.isExternal() << " isTemporary=" << S.isTemporary()
1159 << " isFunction=" << WS.isFunction()
1160 << " isWeak=" << WS.isWeak()
1161 << " isHidden=" << WS.isHidden()
1162 << " isVariable=" << WS.isVariable() << "\n");
1164 if (WS.isWeak() || WS.isHidden()) {
1165 uint32_t Flags = (WS.isWeak() ? wasm::WASM_SYMBOL_BINDING_WEAK : 0) |
1166 (WS.isHidden() ? wasm::WASM_SYMBOL_VISIBILITY_HIDDEN : 0);
1167 SymbolFlags.emplace_back(WS.getName(), Flags);
1170 if (WS.isVariable())
1175 if (WS.isFunction()) {
1176 if (WS.isDefined(/*SetUsed=*/false)) {
1177 if (WS.getOffset() != 0)
1179 "function sections must contain one function each");
1181 if (WS.getSize() == 0)
1183 "function symbols must have a size set with .size");
1185 // A definition. Take the next available index.
1186 Index = NumFuncImports + Functions.size();
1188 // Prepare the function.
1190 Func.Type = getFunctionType(WS);
1192 SymbolIndices[&WS] = Index;
1193 Functions.push_back(Func);
1195 // An import; the index was assigned above.
1196 Index = SymbolIndices.find(&WS)->second;
1199 DEBUG(dbgs() << " -> function index: " << Index << "\n");
1201 if (WS.isTemporary() && !WS.getSize())
1204 if (!WS.isDefined(/*SetUsed=*/false))
1208 report_fatal_error("data symbols must have a size set with .size: " +
1212 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1213 report_fatal_error(".size expression must be evaluatable");
1215 // For each global, prepare a corresponding wasm global holding its
1216 // address. For externals these will also be named exports.
1217 Index = NumGlobalImports + Globals.size();
1218 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1221 Global.Type = PtrType;
1222 Global.IsMutable = false;
1223 Global.HasImport = false;
1224 Global.InitialValue = DataSection.getMemoryOffset() + Layout.getSymbolOffset(WS);
1225 Global.ImportIndex = 0;
1226 SymbolIndices[&WS] = Index;
1227 DEBUG(dbgs() << " -> global index: " << Index << "\n");
1228 Globals.push_back(Global);
1231 // If the symbol is visible outside this translation unit, export it.
1232 if (WS.isDefined(/*SetUsed=*/false)) {
1234 Export.FieldName = WS.getName();
1235 Export.Index = Index;
1236 if (WS.isFunction())
1237 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1239 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1240 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
1241 Exports.push_back(Export);
1242 if (!WS.isExternal())
1243 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
1247 // Handle weak aliases. We need to process these in a separate pass because
1248 // we need to have processed the target of the alias before the alias itself
1249 // and the symbols are not necessarily ordered in this way.
1250 for (const MCSymbol &S : Asm.symbols()) {
1251 if (!S.isVariable())
1254 assert(S.isDefined(/*SetUsed=*/false));
1256 // Find the target symbol of this weak alias and export that index
1257 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1258 const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
1259 DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
1260 assert(SymbolIndices.count(ResolvedSym) > 0);
1261 uint32_t Index = SymbolIndices.find(ResolvedSym)->second;
1262 DEBUG(dbgs() << " -> index:" << Index << "\n");
1265 Export.FieldName = WS.getName();
1266 Export.Index = Index;
1267 if (WS.isFunction())
1268 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1270 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1271 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
1272 Exports.push_back(Export);
1274 if (!WS.isExternal())
1275 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
1279 auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
1280 // Functions referenced by a relocation need to prepared to be called
1282 const MCSymbolWasm& WS = *Rel.Symbol;
1283 if (WS.isFunction() && IndirectSymbolIndices.count(&WS) == 0) {
1285 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
1286 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
1287 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
1288 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
1289 uint32_t Index = SymbolIndices.find(&WS)->second;
1290 IndirectSymbolIndices[&WS] = TableElems.size();
1291 DEBUG(dbgs() << " -> adding to table: " << TableElems.size() << "\n");
1292 TableElems.push_back(Index);
1293 registerFunctionType(WS);
1302 for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1303 HandleReloc(RelEntry);
1304 for (const WasmRelocationEntry &RelEntry : DataRelocations)
1305 HandleReloc(RelEntry);
1308 // Translate .init_array section contents into start functions.
1309 for (const MCSection &S : Asm) {
1310 const auto &WS = static_cast<const MCSectionWasm &>(S);
1311 if (WS.getSectionName().startswith(".fini_array"))
1312 report_fatal_error(".fini_array sections are unsupported");
1313 if (!WS.getSectionName().startswith(".init_array"))
1315 if (WS.getFragmentList().empty())
1317 if (WS.getFragmentList().size() != 2)
1318 report_fatal_error("only one .init_array section fragment supported");
1319 const MCFragment &AlignFrag = *WS.begin();
1320 if (AlignFrag.getKind() != MCFragment::FT_Align)
1321 report_fatal_error(".init_array section should be aligned");
1322 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
1323 report_fatal_error(".init_array section should be aligned for pointers");
1324 const MCFragment &Frag = *std::next(WS.begin());
1325 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1326 report_fatal_error("only data supported in .init_array section");
1327 uint16_t Priority = UINT16_MAX;
1328 if (WS.getSectionName().size() != 11) {
1329 if (WS.getSectionName()[11] != '.')
1330 report_fatal_error(".init_array section priority should start with '.'");
1331 if (WS.getSectionName().substr(12).getAsInteger(10, Priority))
1332 report_fatal_error("invalid .init_array section priority");
1334 const auto &DataFrag = cast<MCDataFragment>(Frag);
1335 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1336 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1337 *end = (const uint8_t *)Contents.data() + Contents.size();
1340 report_fatal_error("non-symbolic data in .init_array section");
1342 for (const MCFixup &Fixup : DataFrag.getFixups()) {
1343 assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1344 const MCExpr *Expr = Fixup.getValue();
1345 auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr);
1347 report_fatal_error("fixups in .init_array should be symbol references");
1348 if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
1349 report_fatal_error("symbols in .init_array should be for functions");
1350 auto I = SymbolIndices.find(cast<MCSymbolWasm>(&Sym->getSymbol()));
1351 if (I == SymbolIndices.end())
1352 report_fatal_error("symbols in .init_array should be defined");
1353 uint32_t Index = I->second;
1354 InitFuncs.push_back(std::make_pair(Priority, Index));
1358 // Write out the Wasm header.
1361 writeTypeSection(FunctionTypes);
1362 writeImportSection(Imports, DataSize, TableElems.size());
1363 writeFunctionSection(Functions);
1364 // Skip the "table" section; we import the table instead.
1365 // Skip the "memory" section; we import the memory instead.
1366 writeGlobalSection();
1367 writeExportSection(Exports);
1368 writeElemSection(TableElems);
1369 writeCodeSection(Asm, Layout, Functions);
1370 writeDataSection(DataSegments);
1371 writeNameSection(Functions, Imports, NumFuncImports);
1372 writeCodeRelocSection();
1373 writeDataRelocSection();
1374 writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
1377 // TODO: Translate the .comment section to the output.
1378 // TODO: Translate debug sections to the output.
1381 std::unique_ptr<MCObjectWriter>
1382 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1383 raw_pwrite_stream &OS) {
1384 // FIXME: Can't use make_unique<WasmObjectWriter>(...) as WasmObjectWriter's
1385 // destructor is private. Is that necessary?
1386 return std::unique_ptr<MCObjectWriter>(
1387 new WasmObjectWriter(std::move(MOTW), OS));