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 // Went we ceate the indirect function table we start at 1, so that there is
42 // and emtpy slot at 0 and therefore calling a null function pointer will trap.
43 static const uint32_t kInitialTableOffset = 1;
45 // For patching purposes, we need to remember where each section starts, both
46 // for patching up the section size field, and for patching up references to
47 // locations within the section.
48 struct SectionBookkeeping {
49 // Where the size of the section is written.
51 // Where the contents of the section starts (after the header).
52 uint64_t ContentsOffset;
55 // The signature of a wasm function, in a struct capable of being used as a
57 struct WasmFunctionType {
58 // Support empty and tombstone instances, needed by DenseMap.
59 enum { Plain, Empty, Tombstone } State;
61 // The return types of the function.
62 SmallVector<wasm::ValType, 1> Returns;
64 // The parameter types of the function.
65 SmallVector<wasm::ValType, 4> Params;
67 WasmFunctionType() : State(Plain) {}
69 bool operator==(const WasmFunctionType &Other) const {
70 return State == Other.State && Returns == Other.Returns &&
71 Params == Other.Params;
75 // Traits for using WasmFunctionType in a DenseMap.
76 struct WasmFunctionTypeDenseMapInfo {
77 static WasmFunctionType getEmptyKey() {
78 WasmFunctionType FuncTy;
79 FuncTy.State = WasmFunctionType::Empty;
82 static WasmFunctionType getTombstoneKey() {
83 WasmFunctionType FuncTy;
84 FuncTy.State = WasmFunctionType::Tombstone;
87 static unsigned getHashValue(const WasmFunctionType &FuncTy) {
88 uintptr_t Value = FuncTy.State;
89 for (wasm::ValType Ret : FuncTy.Returns)
90 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
91 for (wasm::ValType Param : FuncTy.Params)
92 Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
95 static bool isEqual(const WasmFunctionType &LHS,
96 const WasmFunctionType &RHS) {
101 // A wasm data segment. A wasm binary contains only a single data section
102 // but that can contain many segments, each with their own virtual location
103 // in memory. Each MCSection data created by llvm is modeled as its own
104 // wasm data segment.
105 struct WasmDataSegment {
106 MCSectionWasm *Section;
111 SmallVector<char, 4> Data;
114 // A wasm import to be written into the import section.
116 StringRef ModuleName;
123 // A wasm function to be written into the function section.
124 struct WasmFunction {
126 const MCSymbolWasm *Sym;
129 // A wasm export to be written into the export section.
136 // A wasm global to be written into the global section.
138 wasm::WasmGlobalType Type;
139 uint64_t InitialValue;
142 // Information about a single item which is part of a COMDAT. For each data
143 // segment or function which is in the COMDAT, there is a corresponding
145 struct WasmComdatEntry {
150 // Information about a single relocation.
151 struct WasmRelocationEntry {
152 uint64_t Offset; // Where is the relocation.
153 const MCSymbolWasm *Symbol; // The symbol to relocate with.
154 int64_t Addend; // A value to add to the symbol.
155 unsigned Type; // The type of the relocation.
156 const MCSectionWasm *FixupSection;// The section the relocation is targeting.
158 WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
159 int64_t Addend, unsigned Type,
160 const MCSectionWasm *FixupSection)
161 : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
162 FixupSection(FixupSection) {}
164 bool hasAddend() const {
166 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
167 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
168 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
175 void print(raw_ostream &Out) const {
176 Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
178 << ", FixupSection=" << FixupSection->getSectionName();
181 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
182 LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
187 raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
193 class WasmObjectWriter : public MCObjectWriter {
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> TableIndices;
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 NumFunctionImports = 0;
217 unsigned NumGlobalImports = 0;
219 // TargetObjectWriter wrappers.
220 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
221 unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
222 return TargetObjectWriter->getRelocType(Target, Fixup);
225 void startSection(SectionBookkeeping &Section, unsigned SectionId,
226 const char *Name = nullptr);
227 void endSection(SectionBookkeeping &Section);
230 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
231 raw_pwrite_stream &OS)
232 : MCObjectWriter(OS, /*IsLittleEndian=*/true),
233 TargetObjectWriter(std::move(MOTW)) {}
235 ~WasmObjectWriter() override;
238 void reset() override {
239 CodeRelocations.clear();
240 DataRelocations.clear();
242 SymbolIndices.clear();
243 TableIndices.clear();
244 FunctionTypeIndices.clear();
245 FunctionTypes.clear();
247 MCObjectWriter::reset();
248 NumFunctionImports = 0;
249 NumGlobalImports = 0;
252 void writeHeader(const MCAssembler &Asm);
254 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
255 const MCFragment *Fragment, const MCFixup &Fixup,
256 MCValue Target, uint64_t &FixedValue) override;
258 void executePostLayoutBinding(MCAssembler &Asm,
259 const MCAsmLayout &Layout) override;
261 void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
263 void writeString(const StringRef Str) {
264 encodeULEB128(Str.size(), getStream());
268 void writeValueType(wasm::ValType Ty) {
269 encodeSLEB128(int32_t(Ty), getStream());
272 void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
273 void writeImportSection(ArrayRef<WasmImport> Imports, uint32_t DataSize,
274 uint32_t NumElements);
275 void writeFunctionSection(ArrayRef<WasmFunction> Functions);
276 void writeGlobalSection();
277 void writeExportSection(ArrayRef<WasmExport> Exports);
278 void writeElemSection(ArrayRef<uint32_t> TableElems);
279 void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
280 ArrayRef<WasmFunction> Functions);
281 void writeDataSection(ArrayRef<WasmDataSegment> Segments);
282 void writeCodeRelocSection();
283 void writeDataRelocSection();
284 void writeLinkingMetaDataSection(
285 ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
286 ArrayRef<std::pair<StringRef, uint32_t>> SymbolFlags,
287 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
288 const std::map<StringRef, std::vector<WasmComdatEntry>>& Comdats);
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; it just serves
489 // to make the object format more readable and more likely to be directly
492 WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
493 switch (RelEntry.Type) {
494 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
495 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
496 // Provisional value is table address of the resolved symbol itself
497 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
498 assert(Sym->isFunction());
499 return TableIndices[Sym];
501 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
502 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
503 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
504 // Provisional value is function/type/global index itself
505 return getRelocationIndexValue(RelEntry);
506 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
507 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
508 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
509 // Provisional value is address of the global
510 const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
511 // For undefined symbols, use zero
512 if (!Sym->isDefined())
515 uint32_t GlobalIndex = SymbolIndices[Sym];
516 const WasmGlobal& Global = Globals[GlobalIndex - NumGlobalImports];
517 uint64_t Address = Global.InitialValue + RelEntry.Addend;
519 // Ignore overflow. LLVM allows address arithmetic to silently wrap.
523 llvm_unreachable("invalid relocation type");
527 static void addData(SmallVectorImpl<char> &DataBytes,
528 MCSectionWasm &DataSection) {
529 DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
531 DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
533 size_t LastFragmentSize = 0;
534 for (const MCFragment &Frag : DataSection) {
535 if (Frag.hasInstructions())
536 report_fatal_error("only data supported in data sections");
538 if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
539 if (Align->getValueSize() != 1)
540 report_fatal_error("only byte values supported for alignment");
541 // If nops are requested, use zeros, as this is the data section.
542 uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
543 uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
544 Align->getAlignment()),
546 Align->getMaxBytesToEmit());
547 DataBytes.resize(Size, Value);
548 } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
550 if (!Fill->getSize().evaluateAsAbsolute(Size))
551 llvm_unreachable("The fill should be an assembler constant");
552 DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
554 const auto &DataFrag = cast<MCDataFragment>(Frag);
555 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
557 DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
558 LastFragmentSize = Contents.size();
562 // Don't allow empty segments, or segments that end with zero-sized
563 // fragment, otherwise the linker cannot map symbols to a unique
564 // data segment. This can be triggered by zero-sized structs
565 // See: test/MC/WebAssembly/bss.ll
566 if (LastFragmentSize == 0)
567 DataBytes.resize(DataBytes.size() + 1);
568 DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
572 WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
573 if (RelEntry.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
574 if (!TypeIndices.count(RelEntry.Symbol))
575 report_fatal_error("symbol not found in type index space: " +
576 RelEntry.Symbol->getName());
577 return TypeIndices[RelEntry.Symbol];
580 if (!SymbolIndices.count(RelEntry.Symbol))
581 report_fatal_error("symbol not found in function/global index space: " +
582 RelEntry.Symbol->getName());
583 return SymbolIndices[RelEntry.Symbol];
586 // Apply the portions of the relocation records that we can handle ourselves
588 void WasmObjectWriter::applyRelocations(
589 ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
590 raw_pwrite_stream &Stream = getStream();
591 for (const WasmRelocationEntry &RelEntry : Relocations) {
592 uint64_t Offset = ContentsOffset +
593 RelEntry.FixupSection->getSectionOffset() +
596 DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
597 uint32_t Value = getProvisionalValue(RelEntry);
599 switch (RelEntry.Type) {
600 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
601 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
602 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
603 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
604 WritePatchableLEB(Stream, Value, Offset);
606 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
607 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
608 WriteI32(Stream, Value, Offset);
610 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
611 case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
612 WritePatchableSLEB(Stream, Value, Offset);
615 llvm_unreachable("invalid relocation type");
620 // Write out the portions of the relocation records that the linker will
622 void WasmObjectWriter::writeRelocations(
623 ArrayRef<WasmRelocationEntry> Relocations) {
624 raw_pwrite_stream &Stream = getStream();
625 for (const WasmRelocationEntry& RelEntry : Relocations) {
627 uint64_t Offset = RelEntry.Offset +
628 RelEntry.FixupSection->getSectionOffset();
629 uint32_t Index = getRelocationIndexValue(RelEntry);
631 encodeULEB128(RelEntry.Type, Stream);
632 encodeULEB128(Offset, Stream);
633 encodeULEB128(Index, Stream);
634 if (RelEntry.hasAddend())
635 encodeSLEB128(RelEntry.Addend, Stream);
639 void WasmObjectWriter::writeTypeSection(
640 ArrayRef<WasmFunctionType> FunctionTypes) {
641 if (FunctionTypes.empty())
644 SectionBookkeeping Section;
645 startSection(Section, wasm::WASM_SEC_TYPE);
647 encodeULEB128(FunctionTypes.size(), getStream());
649 for (const WasmFunctionType &FuncTy : FunctionTypes) {
650 encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
651 encodeULEB128(FuncTy.Params.size(), getStream());
652 for (wasm::ValType Ty : FuncTy.Params)
654 encodeULEB128(FuncTy.Returns.size(), getStream());
655 for (wasm::ValType Ty : FuncTy.Returns)
662 void WasmObjectWriter::writeImportSection(ArrayRef<WasmImport> Imports,
664 uint32_t NumElements) {
668 uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
670 SectionBookkeeping Section;
671 startSection(Section, wasm::WASM_SEC_IMPORT);
673 encodeULEB128(Imports.size(), getStream());
674 for (const WasmImport &Import : Imports) {
675 writeString(Import.ModuleName);
676 writeString(Import.FieldName);
678 encodeULEB128(Import.Kind, getStream());
680 switch (Import.Kind) {
681 case wasm::WASM_EXTERNAL_FUNCTION:
682 encodeULEB128(Import.Type, getStream());
684 case wasm::WASM_EXTERNAL_GLOBAL:
685 encodeSLEB128(int32_t(Import.Type), getStream());
686 encodeULEB128(int32_t(Import.IsMutable), getStream());
688 case wasm::WASM_EXTERNAL_MEMORY:
689 encodeULEB128(0, getStream()); // flags
690 encodeULEB128(NumPages, getStream()); // initial
692 case wasm::WASM_EXTERNAL_TABLE:
693 encodeSLEB128(int32_t(Import.Type), getStream());
694 encodeULEB128(0, getStream()); // flags
695 encodeULEB128(NumElements, getStream()); // initial
698 llvm_unreachable("unsupported import kind");
705 void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
706 if (Functions.empty())
709 SectionBookkeeping Section;
710 startSection(Section, wasm::WASM_SEC_FUNCTION);
712 encodeULEB128(Functions.size(), getStream());
713 for (const WasmFunction &Func : Functions)
714 encodeULEB128(Func.Type, getStream());
719 void WasmObjectWriter::writeGlobalSection() {
723 SectionBookkeeping Section;
724 startSection(Section, wasm::WASM_SEC_GLOBAL);
726 encodeULEB128(Globals.size(), getStream());
727 for (const WasmGlobal &Global : Globals) {
728 writeValueType(static_cast<wasm::ValType>(Global.Type.Type));
729 write8(Global.Type.Mutable);
731 write8(wasm::WASM_OPCODE_I32_CONST);
732 encodeSLEB128(Global.InitialValue, getStream());
733 write8(wasm::WASM_OPCODE_END);
739 void WasmObjectWriter::writeExportSection(ArrayRef<WasmExport> Exports) {
743 SectionBookkeeping Section;
744 startSection(Section, wasm::WASM_SEC_EXPORT);
746 encodeULEB128(Exports.size(), getStream());
747 for (const WasmExport &Export : Exports) {
748 writeString(Export.FieldName);
749 encodeSLEB128(Export.Kind, getStream());
750 encodeULEB128(Export.Index, getStream());
756 void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
757 if (TableElems.empty())
760 SectionBookkeeping Section;
761 startSection(Section, wasm::WASM_SEC_ELEM);
763 encodeULEB128(1, getStream()); // number of "segments"
764 encodeULEB128(0, getStream()); // the table index
766 // init expr for starting offset
767 write8(wasm::WASM_OPCODE_I32_CONST);
768 encodeSLEB128(kInitialTableOffset, getStream());
769 write8(wasm::WASM_OPCODE_END);
771 encodeULEB128(TableElems.size(), getStream());
772 for (uint32_t Elem : TableElems)
773 encodeULEB128(Elem, getStream());
778 void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
779 const MCAsmLayout &Layout,
780 ArrayRef<WasmFunction> Functions) {
781 if (Functions.empty())
784 SectionBookkeeping Section;
785 startSection(Section, wasm::WASM_SEC_CODE);
787 encodeULEB128(Functions.size(), getStream());
789 for (const WasmFunction &Func : Functions) {
790 auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
793 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
794 report_fatal_error(".size expression must be evaluatable");
796 encodeULEB128(Size, getStream());
797 FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
798 Asm.writeSectionData(&FuncSection, Layout);
802 applyRelocations(CodeRelocations, Section.ContentsOffset);
807 void WasmObjectWriter::writeDataSection(ArrayRef<WasmDataSegment> Segments) {
808 if (Segments.empty())
811 SectionBookkeeping Section;
812 startSection(Section, wasm::WASM_SEC_DATA);
814 encodeULEB128(Segments.size(), getStream()); // count
816 for (const WasmDataSegment & Segment : Segments) {
817 encodeULEB128(0, getStream()); // memory index
818 write8(wasm::WASM_OPCODE_I32_CONST);
819 encodeSLEB128(Segment.Offset, getStream()); // offset
820 write8(wasm::WASM_OPCODE_END);
821 encodeULEB128(Segment.Data.size(), getStream()); // size
822 Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
823 writeBytes(Segment.Data); // data
827 applyRelocations(DataRelocations, Section.ContentsOffset);
832 void WasmObjectWriter::writeCodeRelocSection() {
833 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
834 // for descriptions of the reloc sections.
836 if (CodeRelocations.empty())
839 SectionBookkeeping Section;
840 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
842 encodeULEB128(wasm::WASM_SEC_CODE, getStream());
843 encodeULEB128(CodeRelocations.size(), getStream());
845 writeRelocations(CodeRelocations);
850 void WasmObjectWriter::writeDataRelocSection() {
851 // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
852 // for descriptions of the reloc sections.
854 if (DataRelocations.empty())
857 SectionBookkeeping Section;
858 startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
860 encodeULEB128(wasm::WASM_SEC_DATA, getStream());
861 encodeULEB128(DataRelocations.size(), getStream());
863 writeRelocations(DataRelocations);
868 void WasmObjectWriter::writeLinkingMetaDataSection(
869 ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
870 ArrayRef<std::pair<StringRef, uint32_t>> SymbolFlags,
871 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
872 const std::map<StringRef, std::vector<WasmComdatEntry>>& Comdats) {
873 SectionBookkeeping Section;
874 startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
875 SectionBookkeeping SubSection;
877 if (SymbolFlags.size() != 0) {
878 startSection(SubSection, wasm::WASM_SYMBOL_INFO);
879 encodeULEB128(SymbolFlags.size(), getStream());
880 for (auto Pair: SymbolFlags) {
881 writeString(Pair.first);
882 encodeULEB128(Pair.second, getStream());
884 endSection(SubSection);
888 startSection(SubSection, wasm::WASM_DATA_SIZE);
889 encodeULEB128(DataSize, getStream());
890 endSection(SubSection);
893 if (Segments.size()) {
894 startSection(SubSection, wasm::WASM_SEGMENT_INFO);
895 encodeULEB128(Segments.size(), getStream());
896 for (const WasmDataSegment &Segment : Segments) {
897 writeString(Segment.Name);
898 encodeULEB128(Segment.Alignment, getStream());
899 encodeULEB128(Segment.Flags, getStream());
901 endSection(SubSection);
904 if (!InitFuncs.empty()) {
905 startSection(SubSection, wasm::WASM_INIT_FUNCS);
906 encodeULEB128(InitFuncs.size(), getStream());
907 for (auto &StartFunc : InitFuncs) {
908 encodeULEB128(StartFunc.first, getStream()); // priority
909 encodeULEB128(StartFunc.second, getStream()); // function index
911 endSection(SubSection);
914 if (Comdats.size()) {
915 startSection(SubSection, wasm::WASM_COMDAT_INFO);
916 encodeULEB128(Comdats.size(), getStream());
917 for (const auto &C : Comdats) {
918 writeString(C.first);
919 encodeULEB128(0, getStream()); // flags for future use
920 encodeULEB128(C.second.size(), getStream());
921 for (const WasmComdatEntry &Entry : C.second) {
922 encodeULEB128(Entry.Kind, getStream());
923 encodeULEB128(Entry.Index, getStream());
926 endSection(SubSection);
932 uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
933 assert(Symbol.isFunction());
934 assert(TypeIndices.count(&Symbol));
935 return TypeIndices[&Symbol];
938 uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
939 assert(Symbol.isFunction());
942 const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
943 F.Returns = ResolvedSym->getReturns();
944 F.Params = ResolvedSym->getParams();
947 FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
949 FunctionTypes.push_back(F);
950 TypeIndices[&Symbol] = Pair.first->second;
952 DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
953 DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
954 return Pair.first->second;
957 void WasmObjectWriter::writeObject(MCAssembler &Asm,
958 const MCAsmLayout &Layout) {
959 DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
960 MCContext &Ctx = Asm.getContext();
961 int32_t PtrType = is64Bit() ? wasm::WASM_TYPE_I64 : wasm::WASM_TYPE_I32;
963 // Collect information from the available symbols.
964 SmallVector<WasmFunction, 4> Functions;
965 SmallVector<uint32_t, 4> TableElems;
966 SmallVector<WasmImport, 4> Imports;
967 SmallVector<WasmExport, 4> Exports;
968 SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags;
969 SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
970 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
971 SmallVector<WasmDataSegment, 4> DataSegments;
972 uint32_t DataSize = 0;
974 // For now, always emit the memory import, since loads and stores are not
975 // valid without it. In the future, we could perhaps be more clever and omit
976 // it if there are no loads or stores.
977 MCSymbolWasm *MemorySym =
978 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
979 WasmImport MemImport;
980 MemImport.ModuleName = MemorySym->getModuleName();
981 MemImport.FieldName = MemorySym->getName();
982 MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
983 Imports.push_back(MemImport);
985 // For now, always emit the table section, since indirect calls are not
986 // valid without it. In the future, we could perhaps be more clever and omit
987 // it if there are no indirect calls.
988 MCSymbolWasm *TableSym =
989 cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
990 WasmImport TableImport;
991 TableImport.ModuleName = TableSym->getModuleName();
992 TableImport.FieldName = TableSym->getName();
993 TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
994 TableImport.Type = wasm::WASM_TYPE_ANYFUNC;
995 Imports.push_back(TableImport);
997 // Populate FunctionTypeIndices and Imports.
998 for (const MCSymbol &S : Asm.symbols()) {
999 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1001 // Register types for all functions, including those with private linkage
1002 // (because wasm always needs a type signature).
1003 if (WS.isFunction())
1004 registerFunctionType(WS);
1006 if (WS.isTemporary())
1009 // If the symbol is not defined in this translation unit, import it.
1010 if ((!WS.isDefined() && !WS.isComdat()) ||
1013 Import.ModuleName = WS.getModuleName();
1014 Import.FieldName = WS.getName();
1016 if (WS.isFunction()) {
1017 Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1018 Import.Type = getFunctionType(WS);
1019 SymbolIndices[&WS] = NumFunctionImports;
1020 ++NumFunctionImports;
1022 Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1023 Import.Type = PtrType;
1024 Import.IsMutable = false;
1025 SymbolIndices[&WS] = NumGlobalImports;
1027 // If this global is the stack pointer, make it mutable.
1028 if (WS.getName() == "__stack_pointer")
1029 Import.IsMutable = true;
1034 Imports.push_back(Import);
1038 for (MCSection &Sec : Asm) {
1039 auto &Section = static_cast<MCSectionWasm &>(Sec);
1040 if (!Section.isWasmData())
1043 // .init_array sections are handled specially elsewhere.
1044 if (cast<MCSectionWasm>(Sec).getSectionName().startswith(".init_array"))
1047 uint32_t SegmentIndex = DataSegments.size();
1048 DataSize = alignTo(DataSize, Section.getAlignment());
1049 DataSegments.emplace_back();
1050 WasmDataSegment &Segment = DataSegments.back();
1051 Segment.Name = Section.getSectionName();
1052 Segment.Offset = DataSize;
1053 Segment.Section = &Section;
1054 addData(Segment.Data, Section);
1055 Segment.Alignment = Section.getAlignment();
1057 DataSize += Segment.Data.size();
1058 Section.setMemoryOffset(Segment.Offset);
1060 if (const MCSymbolWasm *C = Section.getGroup()) {
1061 Comdats[C->getName()].emplace_back(
1062 WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
1066 // Handle regular defined and undefined symbols.
1067 for (const MCSymbol &S : Asm.symbols()) {
1068 // Ignore unnamed temporary symbols, which aren't ever exported, imported,
1069 // or used in relocations.
1070 if (S.isTemporary() && S.getName().empty())
1073 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1074 DEBUG(dbgs() << "MCSymbol: '" << S << "'"
1075 << " isDefined=" << S.isDefined()
1076 << " isExternal=" << S.isExternal()
1077 << " isTemporary=" << S.isTemporary()
1078 << " isFunction=" << WS.isFunction()
1079 << " isWeak=" << WS.isWeak()
1080 << " isHidden=" << WS.isHidden()
1081 << " isVariable=" << WS.isVariable() << "\n");
1083 if (WS.isWeak() || WS.isHidden()) {
1084 uint32_t Flags = (WS.isWeak() ? wasm::WASM_SYMBOL_BINDING_WEAK : 0) |
1085 (WS.isHidden() ? wasm::WASM_SYMBOL_VISIBILITY_HIDDEN : 0);
1086 SymbolFlags.emplace_back(WS.getName(), Flags);
1089 if (WS.isVariable())
1094 if (WS.isFunction()) {
1095 if (WS.isDefined()) {
1096 if (WS.getOffset() != 0)
1098 "function sections must contain one function each");
1100 if (WS.getSize() == 0)
1102 "function symbols must have a size set with .size");
1104 // A definition. Take the next available index.
1105 Index = NumFunctionImports + Functions.size();
1107 // Prepare the function.
1109 Func.Type = getFunctionType(WS);
1111 SymbolIndices[&WS] = Index;
1112 Functions.push_back(Func);
1114 // An import; the index was assigned above.
1115 Index = SymbolIndices.find(&WS)->second;
1118 DEBUG(dbgs() << " -> function index: " << Index << "\n");
1120 if (WS.isTemporary() && !WS.getSize())
1123 if (!WS.isDefined())
1127 report_fatal_error("data symbols must have a size set with .size: " +
1131 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1132 report_fatal_error(".size expression must be evaluatable");
1134 // For each global, prepare a corresponding wasm global holding its
1135 // address. For externals these will also be named exports.
1136 Index = NumGlobalImports + Globals.size();
1137 auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
1138 assert(DataSection.isWasmData());
1141 Global.Type.Type = PtrType;
1142 Global.Type.Mutable = false;
1143 Global.InitialValue = DataSection.getMemoryOffset() + Layout.getSymbolOffset(WS);
1144 SymbolIndices[&WS] = Index;
1145 DEBUG(dbgs() << " -> global index: " << Index << "\n");
1146 Globals.push_back(Global);
1149 // If the symbol is visible outside this translation unit, export it.
1150 if (WS.isDefined()) {
1152 Export.FieldName = WS.getName();
1153 Export.Index = Index;
1154 if (WS.isFunction())
1155 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1157 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1158 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
1159 Exports.push_back(Export);
1161 if (!WS.isExternal())
1162 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
1164 if (WS.isFunction()) {
1165 auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
1166 if (const MCSymbolWasm *C = Section.getGroup())
1167 Comdats[C->getName()].emplace_back(
1168 WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
1173 // Handle weak aliases. We need to process these in a separate pass because
1174 // we need to have processed the target of the alias before the alias itself
1175 // and the symbols are not necessarily ordered in this way.
1176 for (const MCSymbol &S : Asm.symbols()) {
1177 if (!S.isVariable())
1180 assert(S.isDefined());
1182 // Find the target symbol of this weak alias and export that index
1183 const auto &WS = static_cast<const MCSymbolWasm &>(S);
1184 const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
1185 DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
1186 assert(SymbolIndices.count(ResolvedSym) > 0);
1187 uint32_t Index = SymbolIndices.find(ResolvedSym)->second;
1188 DEBUG(dbgs() << " -> index:" << Index << "\n");
1191 Export.FieldName = WS.getName();
1192 Export.Index = Index;
1193 if (WS.isFunction())
1194 Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
1196 Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
1197 DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
1198 Exports.push_back(Export);
1200 if (!WS.isExternal())
1201 SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
1205 auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
1206 // Functions referenced by a relocation need to put in the table. This is
1207 // purely to make the object file's provisional values readable, and is
1208 // ignored by the linker, which re-calculates the relocations itself.
1209 if (Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 &&
1210 Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB)
1212 assert(Rel.Symbol->isFunction());
1213 const MCSymbolWasm &WS = *ResolveSymbol(*Rel.Symbol);
1214 uint32_t SymbolIndex = SymbolIndices.find(&WS)->second;
1215 uint32_t TableIndex = TableElems.size() + kInitialTableOffset;
1216 if (TableIndices.try_emplace(&WS, TableIndex).second) {
1217 DEBUG(dbgs() << " -> adding " << WS.getName()
1218 << " to table: " << TableIndex << "\n");
1219 TableElems.push_back(SymbolIndex);
1220 registerFunctionType(WS);
1224 for (const WasmRelocationEntry &RelEntry : CodeRelocations)
1225 HandleReloc(RelEntry);
1226 for (const WasmRelocationEntry &RelEntry : DataRelocations)
1227 HandleReloc(RelEntry);
1230 // Translate .init_array section contents into start functions.
1231 for (const MCSection &S : Asm) {
1232 const auto &WS = static_cast<const MCSectionWasm &>(S);
1233 if (WS.getSectionName().startswith(".fini_array"))
1234 report_fatal_error(".fini_array sections are unsupported");
1235 if (!WS.getSectionName().startswith(".init_array"))
1237 if (WS.getFragmentList().empty())
1239 if (WS.getFragmentList().size() != 2)
1240 report_fatal_error("only one .init_array section fragment supported");
1241 const MCFragment &AlignFrag = *WS.begin();
1242 if (AlignFrag.getKind() != MCFragment::FT_Align)
1243 report_fatal_error(".init_array section should be aligned");
1244 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
1245 report_fatal_error(".init_array section should be aligned for pointers");
1246 const MCFragment &Frag = *std::next(WS.begin());
1247 if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
1248 report_fatal_error("only data supported in .init_array section");
1249 uint16_t Priority = UINT16_MAX;
1250 if (WS.getSectionName().size() != 11) {
1251 if (WS.getSectionName()[11] != '.')
1252 report_fatal_error(".init_array section priority should start with '.'");
1253 if (WS.getSectionName().substr(12).getAsInteger(10, Priority))
1254 report_fatal_error("invalid .init_array section priority");
1256 const auto &DataFrag = cast<MCDataFragment>(Frag);
1257 const SmallVectorImpl<char> &Contents = DataFrag.getContents();
1258 for (const uint8_t *p = (const uint8_t *)Contents.data(),
1259 *end = (const uint8_t *)Contents.data() + Contents.size();
1262 report_fatal_error("non-symbolic data in .init_array section");
1264 for (const MCFixup &Fixup : DataFrag.getFixups()) {
1265 assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
1266 const MCExpr *Expr = Fixup.getValue();
1267 auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr);
1269 report_fatal_error("fixups in .init_array should be symbol references");
1270 if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
1271 report_fatal_error("symbols in .init_array should be for functions");
1272 auto I = SymbolIndices.find(cast<MCSymbolWasm>(&Sym->getSymbol()));
1273 if (I == SymbolIndices.end())
1274 report_fatal_error("symbols in .init_array should be defined");
1275 uint32_t Index = I->second;
1276 InitFuncs.push_back(std::make_pair(Priority, Index));
1280 // Write out the Wasm header.
1283 writeTypeSection(FunctionTypes);
1284 writeImportSection(Imports, DataSize, TableElems.size());
1285 writeFunctionSection(Functions);
1286 // Skip the "table" section; we import the table instead.
1287 // Skip the "memory" section; we import the memory instead.
1288 writeGlobalSection();
1289 writeExportSection(Exports);
1290 writeElemSection(TableElems);
1291 writeCodeSection(Asm, Layout, Functions);
1292 writeDataSection(DataSegments);
1293 writeCodeRelocSection();
1294 writeDataRelocSection();
1295 writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
1296 InitFuncs, Comdats);
1298 // TODO: Translate the .comment section to the output.
1299 // TODO: Translate debug sections to the output.
1302 std::unique_ptr<MCObjectWriter>
1303 llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
1304 raw_pwrite_stream &OS) {
1305 return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS);