#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionWasm.h"
#include "llvm/MC/MCSymbolWasm.h"
#include "llvm/MC/MCValue.h"
#include "llvm/MC/MCWasmObjectWriter.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/StringSaver.h"
#include <vector>
using namespace llvm;
#define DEBUG_TYPE "mc"
namespace {
static const uint32_t kInitialTableOffset = 1;
struct SectionBookkeeping {
uint64_t SizeOffset;
uint64_t PayloadOffset;
uint64_t ContentsOffset;
uint32_t Index;
};
struct WasmFunctionType {
enum { Plain, Empty, Tombstone } State;
SmallVector<wasm::ValType, 1> Returns;
SmallVector<wasm::ValType, 4> Params;
WasmFunctionType() : State(Plain) {}
bool operator==(const WasmFunctionType &Other) const {
return State == Other.State && Returns == Other.Returns &&
Params == Other.Params;
}
};
struct WasmFunctionTypeDenseMapInfo {
static WasmFunctionType getEmptyKey() {
WasmFunctionType FuncTy;
FuncTy.State = WasmFunctionType::Empty;
return FuncTy;
}
static WasmFunctionType getTombstoneKey() {
WasmFunctionType FuncTy;
FuncTy.State = WasmFunctionType::Tombstone;
return FuncTy;
}
static unsigned getHashValue(const WasmFunctionType &FuncTy) {
uintptr_t Value = FuncTy.State;
for (wasm::ValType Ret : FuncTy.Returns)
Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Ret));
for (wasm::ValType Param : FuncTy.Params)
Value += DenseMapInfo<int32_t>::getHashValue(int32_t(Param));
return Value;
}
static bool isEqual(const WasmFunctionType &LHS,
const WasmFunctionType &RHS) {
return LHS == RHS;
}
};
struct WasmDataSegment {
MCSectionWasm *Section;
StringRef Name;
uint32_t Offset;
uint32_t Alignment;
uint32_t Flags;
SmallVector<char, 4> Data;
};
struct WasmFunction {
int32_t Type;
const MCSymbolWasm *Sym;
};
struct WasmGlobal {
wasm::WasmGlobalType Type;
uint64_t InitialValue;
};
struct WasmComdatEntry {
unsigned Kind;
uint32_t Index;
};
struct WasmRelocationEntry {
uint64_t Offset;
const MCSymbolWasm *Symbol;
int64_t Addend;
unsigned Type;
const MCSectionWasm *FixupSection;
WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
int64_t Addend, unsigned Type,
const MCSectionWasm *FixupSection)
: Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
FixupSection(FixupSection) {}
bool hasAddend() const {
switch (Type) {
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
return true;
default:
return false;
}
}
void print(raw_ostream &Out) const {
Out << wasm::relocTypetoString(Type)
<< " Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
<< ", FixupSection=" << FixupSection->getSectionName();
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
#endif
};
static const uint32_t INVALID_INDEX = -1;
struct WasmCustomSection {
StringRef Name;
MCSectionWasm *Section;
uint32_t OutputContentsOffset;
uint32_t OutputIndex;
WasmCustomSection(StringRef Name, MCSectionWasm *Section)
: Name(Name), Section(Section), OutputContentsOffset(0),
OutputIndex(INVALID_INDEX) {}
};
#if !defined(NDEBUG)
raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
Rel.print(OS);
return OS;
}
#endif
class WasmObjectWriter : public MCObjectWriter {
support::endian::Writer W;
std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
std::vector<WasmRelocationEntry> CodeRelocations;
uint32_t CodeSectionIndex;
std::vector<WasmRelocationEntry> DataRelocations;
uint32_t DataSectionIndex;
DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
std::vector<WasmCustomSection> CustomSections;
DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
CustomSectionsRelocations;
DenseMap<const MCSection *, const MCSymbol *> SectionFunctions;
DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
FunctionTypeIndices;
SmallVector<WasmFunctionType, 4> FunctionTypes;
SmallVector<WasmGlobal, 4> Globals;
SmallVector<WasmDataSegment, 4> DataSegments;
unsigned NumFunctionImports = 0;
unsigned NumGlobalImports = 0;
uint32_t SectionCount = 0;
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
return TargetObjectWriter->getRelocType(Target, Fixup);
}
void startSection(SectionBookkeeping &Section, unsigned SectionId);
void startCustomSection(SectionBookkeeping &Section, StringRef Name);
void endSection(SectionBookkeeping &Section);
public:
WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
raw_pwrite_stream &OS)
: W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {}
~WasmObjectWriter() override;
private:
void reset() override {
CodeRelocations.clear();
DataRelocations.clear();
TypeIndices.clear();
WasmIndices.clear();
TableIndices.clear();
DataLocations.clear();
CustomSectionsRelocations.clear();
FunctionTypeIndices.clear();
FunctionTypes.clear();
Globals.clear();
DataSegments.clear();
SectionFunctions.clear();
NumFunctionImports = 0;
NumGlobalImports = 0;
MCObjectWriter::reset();
}
void writeHeader(const MCAssembler &Asm);
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
const MCFragment *Fragment, const MCFixup &Fixup,
MCValue Target, uint64_t &FixedValue) override;
void executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) override;
uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
void writeString(const StringRef Str) {
encodeULEB128(Str.size(), W.OS);
W.OS << Str;
}
void writeValueType(wasm::ValType Ty) {
W.OS << static_cast<char>(Ty);
}
void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize,
uint32_t NumElements);
void writeFunctionSection(ArrayRef<WasmFunction> Functions);
void writeGlobalSection();
void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
void writeElemSection(ArrayRef<uint32_t> TableElems);
void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
ArrayRef<WasmFunction> Functions);
void writeDataSection();
void writeRelocSection(uint32_t SectionIndex, StringRef Name,
ArrayRef<WasmRelocationEntry> Relocations);
void writeLinkingMetaDataSection(
ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
void writeCustomSections(const MCAssembler &Asm, const MCAsmLayout &Layout);
void writeCustomRelocSections();
void
updateCustomSectionRelocations(const SmallVector<WasmFunction, 4> &Functions,
const MCAsmLayout &Layout);
uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
uint64_t ContentsOffset);
uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
uint32_t getFunctionType(const MCSymbolWasm &Symbol);
uint32_t registerFunctionType(const MCSymbolWasm &Symbol);
};
}
WasmObjectWriter::~WasmObjectWriter() {}
void WasmObjectWriter::startSection(SectionBookkeeping &Section,
unsigned SectionId) {
LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n");
W.OS << char(SectionId);
Section.SizeOffset = W.OS.tell();
encodeULEB128(UINT32_MAX, W.OS);
Section.ContentsOffset = W.OS.tell();
Section.PayloadOffset = W.OS.tell();
Section.Index = SectionCount++;
}
void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
StringRef Name) {
LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n");
startSection(Section, wasm::WASM_SEC_CUSTOM);
Section.PayloadOffset = W.OS.tell();
writeString(Name);
Section.ContentsOffset = W.OS.tell();
}
void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
uint64_t Size = W.OS.tell() - Section.PayloadOffset;
if (uint32_t(Size) != Size)
report_fatal_error("section size does not fit in a uint32_t");
LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n");
uint8_t Buffer[16];
unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
assert(SizeLen == 5);
static_cast<raw_pwrite_stream &>(W.OS).pwrite((char *)Buffer, SizeLen,
Section.SizeOffset);
}
void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
W.OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
W.write<uint32_t>(wasm::WasmVersion);
}
void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
const MCAsmLayout &Layout) {
for (const MCSymbol &S : Asm.symbols()) {
const auto &WS = static_cast<const MCSymbolWasm &>(S);
if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());
auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
if (!Pair.second)
report_fatal_error("section already has a defining function: " +
Sec.getSectionName());
}
}
}
void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
const MCAsmLayout &Layout,
const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue) {
MCAsmBackend &Backend = Asm.getBackend();
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
MCFixupKindInfo::FKF_IsPCRel;
const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
uint64_t C = Target.getConstant();
uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
MCContext &Ctx = Asm.getContext();
if (FixupSection.getSectionName().startswith(".init_array"))
return;
if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
"Should not have constructed this");
if (IsPCRel) {
Ctx.reportError(
Fixup.getLoc(),
"No relocation available to represent this relative expression");
return;
}
const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
if (SymB.isUndefined()) {
Ctx.reportError(Fixup.getLoc(),
Twine("symbol '") + SymB.getName() +
"' can not be undefined in a subtraction expression");
return;
}
assert(!SymB.isAbsolute() && "Should have been folded");
const MCSection &SecB = SymB.getSection();
if (&SecB != &FixupSection) {
Ctx.reportError(Fixup.getLoc(),
"Cannot represent a difference across sections");
return;
}
uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
uint64_t K = SymBOffset - FixupOffset;
IsPCRel = true;
C -= K;
}
const MCSymbolRefExpr *RefA = Target.getSymA();
const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
if (SymA && SymA->isVariable()) {
const MCExpr *Expr = SymA->getVariableValue();
const auto *Inner = cast<MCSymbolRefExpr>(Expr);
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
llvm_unreachable("weakref used in reloc not yet implemented");
}
FixedValue = 0;
unsigned Type = getRelocType(Target, Fixup);
assert(!IsPCRel);
assert(SymA);
if (Type == wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32 ||
Type == wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32) {
if (!FixupSection.getKind().isMetadata())
report_fatal_error("relocations for function or section offsets are "
"only supported in metadata sections");
const MCSymbol *SectionSymbol = nullptr;
const MCSection &SecA = SymA->getSection();
if (SecA.getKind().isText())
SectionSymbol = SectionFunctions.find(&SecA)->second;
else
SectionSymbol = SecA.getBeginSymbol();
if (!SectionSymbol)
report_fatal_error("section symbol is required for relocation");
C += Layout.getSymbolOffset(*SymA);
SymA = cast<MCSymbolWasm>(SectionSymbol);
}
if (Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
if (SymA->getName().empty())
report_fatal_error("relocations against un-named temporaries are not yet "
"supported by wasm");
SymA->setUsedInReloc();
}
WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
if (FixupSection.isWasmData()) {
DataRelocations.push_back(Rec);
} else if (FixupSection.getKind().isText()) {
CodeRelocations.push_back(Rec);
} else if (FixupSection.getKind().isMetadata()) {
CustomSectionsRelocations[&FixupSection].push_back(Rec);
} else {
llvm_unreachable("unexpected section type");
}
}
static void
WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
uint8_t Buffer[5];
unsigned SizeLen = encodeULEB128(X, Buffer, 5);
assert(SizeLen == 5);
Stream.pwrite((char *)Buffer, SizeLen, Offset);
}
static void
WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
uint8_t Buffer[5];
unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
assert(SizeLen == 5);
Stream.pwrite((char *)Buffer, SizeLen, Offset);
}
static void WriteI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
uint8_t Buffer[4];
support::endian::write32le(Buffer, X);
Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
}
static const MCSymbolWasm* ResolveSymbol(const MCSymbolWasm& Symbol) {
if (Symbol.isVariable()) {
const MCExpr *Expr = Symbol.getVariableValue();
auto *Inner = cast<MCSymbolRefExpr>(Expr);
return cast<MCSymbolWasm>(&Inner->getSymbol());
}
return &Symbol;
}
uint32_t
WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
switch (RelEntry.Type) {
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
assert(Sym->isFunction());
return TableIndices[Sym];
}
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
return getRelocationIndexValue(RelEntry);
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
if (!WasmIndices.count(RelEntry.Symbol))
report_fatal_error("symbol not found in wasm index space: " +
RelEntry.Symbol->getName());
return WasmIndices[RelEntry.Symbol];
case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32: {
const auto &Section =
static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
return Section.getSectionOffset() + RelEntry.Addend;
}
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
if (!Sym->isDefined())
return 0;
const wasm::WasmDataReference &Ref = DataLocations[Sym];
const WasmDataSegment &Segment = DataSegments[Ref.Segment];
return Segment.Offset + Ref.Offset + RelEntry.Addend;
}
default:
llvm_unreachable("invalid relocation type");
}
}
static void addData(SmallVectorImpl<char> &DataBytes,
MCSectionWasm &DataSection) {
LLVM_DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
for (const MCFragment &Frag : DataSection) {
if (Frag.hasInstructions())
report_fatal_error("only data supported in data sections");
if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
if (Align->getValueSize() != 1)
report_fatal_error("only byte values supported for alignment");
uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
uint64_t Size = std::min<uint64_t>(alignTo(DataBytes.size(),
Align->getAlignment()),
DataBytes.size() +
Align->getMaxBytesToEmit());
DataBytes.resize(Size, Value);
} else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
int64_t NumValues;
if (!Fill->getNumValues().evaluateAsAbsolute(NumValues))
llvm_unreachable("The fill should be an assembler constant");
DataBytes.insert(DataBytes.end(), Fill->getValueSize() * NumValues,
Fill->getValue());
} else {
const auto &DataFrag = cast<MCDataFragment>(Frag);
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
}
}
LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
}
uint32_t
WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
if (RelEntry.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
if (!TypeIndices.count(RelEntry.Symbol))
report_fatal_error("symbol not found in type index space: " +
RelEntry.Symbol->getName());
return TypeIndices[RelEntry.Symbol];
}
return RelEntry.Symbol->getIndex();
}
void WasmObjectWriter::applyRelocations(
ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
for (const WasmRelocationEntry &RelEntry : Relocations) {
uint64_t Offset = ContentsOffset +
RelEntry.FixupSection->getSectionOffset() +
RelEntry.Offset;
LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
uint32_t Value = getProvisionalValue(RelEntry);
switch (RelEntry.Type) {
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
WritePatchableLEB(Stream, Value, Offset);
break;
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
case wasm::R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
case wasm::R_WEBASSEMBLY_SECTION_OFFSET_I32:
WriteI32(Stream, Value, Offset);
break;
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
WritePatchableSLEB(Stream, Value, Offset);
break;
default:
llvm_unreachable("invalid relocation type");
}
}
}
void WasmObjectWriter::writeTypeSection(
ArrayRef<WasmFunctionType> FunctionTypes) {
if (FunctionTypes.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_TYPE);
encodeULEB128(FunctionTypes.size(), W.OS);
for (const WasmFunctionType &FuncTy : FunctionTypes) {
W.OS << char(wasm::WASM_TYPE_FUNC);
encodeULEB128(FuncTy.Params.size(), W.OS);
for (wasm::ValType Ty : FuncTy.Params)
writeValueType(Ty);
encodeULEB128(FuncTy.Returns.size(), W.OS);
for (wasm::ValType Ty : FuncTy.Returns)
writeValueType(Ty);
}
endSection(Section);
}
void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
uint32_t DataSize,
uint32_t NumElements) {
if (Imports.empty())
return;
uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_IMPORT);
encodeULEB128(Imports.size(), W.OS);
for (const wasm::WasmImport &Import : Imports) {
writeString(Import.Module);
writeString(Import.Field);
W.OS << char(Import.Kind);
switch (Import.Kind) {
case wasm::WASM_EXTERNAL_FUNCTION:
encodeULEB128(Import.SigIndex, W.OS);
break;
case wasm::WASM_EXTERNAL_GLOBAL:
W.OS << char(Import.Global.Type);
W.OS << char(Import.Global.Mutable ? 1 : 0);
break;
case wasm::WASM_EXTERNAL_MEMORY:
encodeULEB128(0, W.OS);
encodeULEB128(NumPages, W.OS);
break;
case wasm::WASM_EXTERNAL_TABLE:
W.OS << char(Import.Table.ElemType);
encodeULEB128(0, W.OS);
encodeULEB128(NumElements, W.OS);
break;
default:
llvm_unreachable("unsupported import kind");
}
}
endSection(Section);
}
void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
if (Functions.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_FUNCTION);
encodeULEB128(Functions.size(), W.OS);
for (const WasmFunction &Func : Functions)
encodeULEB128(Func.Type, W.OS);
endSection(Section);
}
void WasmObjectWriter::writeGlobalSection() {
if (Globals.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_GLOBAL);
encodeULEB128(Globals.size(), W.OS);
for (const WasmGlobal &Global : Globals) {
writeValueType(static_cast<wasm::ValType>(Global.Type.Type));
W.OS << char(Global.Type.Mutable);
W.OS << char(wasm::WASM_OPCODE_I32_CONST);
encodeSLEB128(Global.InitialValue, W.OS);
W.OS << char(wasm::WASM_OPCODE_END);
}
endSection(Section);
}
void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
if (Exports.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_EXPORT);
encodeULEB128(Exports.size(), W.OS);
for (const wasm::WasmExport &Export : Exports) {
writeString(Export.Name);
W.OS << char(Export.Kind);
encodeULEB128(Export.Index, W.OS);
}
endSection(Section);
}
void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
if (TableElems.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_ELEM);
encodeULEB128(1, W.OS);
encodeULEB128(0, W.OS);
W.OS << char(wasm::WASM_OPCODE_I32_CONST);
encodeSLEB128(kInitialTableOffset, W.OS);
W.OS << char(wasm::WASM_OPCODE_END);
encodeULEB128(TableElems.size(), W.OS);
for (uint32_t Elem : TableElems)
encodeULEB128(Elem, W.OS);
endSection(Section);
}
void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
const MCAsmLayout &Layout,
ArrayRef<WasmFunction> Functions) {
if (Functions.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_CODE);
CodeSectionIndex = Section.Index;
encodeULEB128(Functions.size(), W.OS);
for (const WasmFunction &Func : Functions) {
auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
int64_t Size = 0;
if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
report_fatal_error(".size expression must be evaluatable");
encodeULEB128(Size, W.OS);
FuncSection.setSectionOffset(W.OS.tell() - Section.ContentsOffset);
Asm.writeSectionData(W.OS, &FuncSection, Layout);
}
applyRelocations(CodeRelocations, Section.ContentsOffset);
endSection(Section);
}
void WasmObjectWriter::writeDataSection() {
if (DataSegments.empty())
return;
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_DATA);
DataSectionIndex = Section.Index;
encodeULEB128(DataSegments.size(), W.OS);
for (const WasmDataSegment &Segment : DataSegments) {
encodeULEB128(0, W.OS);
W.OS << char(wasm::WASM_OPCODE_I32_CONST);
encodeSLEB128(Segment.Offset, W.OS);
W.OS << char(wasm::WASM_OPCODE_END);
encodeULEB128(Segment.Data.size(), W.OS);
Segment.Section->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
W.OS << Segment.Data;
}
applyRelocations(DataRelocations, Section.ContentsOffset);
endSection(Section);
}
void WasmObjectWriter::writeRelocSection(
uint32_t SectionIndex, StringRef Name,
ArrayRef<WasmRelocationEntry> Relocations) {
if (Relocations.empty())
return;
SectionBookkeeping Section;
startCustomSection(Section, std::string("reloc.") + Name.str());
encodeULEB128(SectionIndex, W.OS);
encodeULEB128(Relocations.size(), W.OS);
for (const WasmRelocationEntry& RelEntry : Relocations) {
uint64_t Offset = RelEntry.Offset +
RelEntry.FixupSection->getSectionOffset();
uint32_t Index = getRelocationIndexValue(RelEntry);
W.OS << char(RelEntry.Type);
encodeULEB128(Offset, W.OS);
encodeULEB128(Index, W.OS);
if (RelEntry.hasAddend())
encodeSLEB128(RelEntry.Addend, W.OS);
}
endSection(Section);
}
void WasmObjectWriter::writeCustomRelocSections() {
for (const auto &Sec : CustomSections) {
auto &Relocations = CustomSectionsRelocations[Sec.Section];
writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
}
}
void WasmObjectWriter::writeLinkingMetaDataSection(
ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
SectionBookkeeping Section;
startCustomSection(Section, "linking");
encodeULEB128(wasm::WasmMetadataVersion, W.OS);
SectionBookkeeping SubSection;
if (SymbolInfos.size() != 0) {
startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
encodeULEB128(SymbolInfos.size(), W.OS);
for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
encodeULEB128(Sym.Kind, W.OS);
encodeULEB128(Sym.Flags, W.OS);
switch (Sym.Kind) {
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
encodeULEB128(Sym.ElementIndex, W.OS);
if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0)
writeString(Sym.Name);
break;
case wasm::WASM_SYMBOL_TYPE_DATA:
writeString(Sym.Name);
if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
encodeULEB128(Sym.DataRef.Segment, W.OS);
encodeULEB128(Sym.DataRef.Offset, W.OS);
encodeULEB128(Sym.DataRef.Size, W.OS);
}
break;
case wasm::WASM_SYMBOL_TYPE_SECTION: {
const uint32_t SectionIndex =
CustomSections[Sym.ElementIndex].OutputIndex;
encodeULEB128(SectionIndex, W.OS);
break;
}
default:
llvm_unreachable("unexpected kind");
}
}
endSection(SubSection);
}
if (DataSegments.size()) {
startSection(SubSection, wasm::WASM_SEGMENT_INFO);
encodeULEB128(DataSegments.size(), W.OS);
for (const WasmDataSegment &Segment : DataSegments) {
writeString(Segment.Name);
encodeULEB128(Segment.Alignment, W.OS);
encodeULEB128(Segment.Flags, W.OS);
}
endSection(SubSection);
}
if (!InitFuncs.empty()) {
startSection(SubSection, wasm::WASM_INIT_FUNCS);
encodeULEB128(InitFuncs.size(), W.OS);
for (auto &StartFunc : InitFuncs) {
encodeULEB128(StartFunc.first, W.OS);
encodeULEB128(StartFunc.second, W.OS);
}
endSection(SubSection);
}
if (Comdats.size()) {
startSection(SubSection, wasm::WASM_COMDAT_INFO);
encodeULEB128(Comdats.size(), W.OS);
for (const auto &C : Comdats) {
writeString(C.first);
encodeULEB128(0, W.OS);
encodeULEB128(C.second.size(), W.OS);
for (const WasmComdatEntry &Entry : C.second) {
encodeULEB128(Entry.Kind, W.OS);
encodeULEB128(Entry.Index, W.OS);
}
}
endSection(SubSection);
}
endSection(Section);
}
void WasmObjectWriter::writeCustomSections(const MCAssembler &Asm,
const MCAsmLayout &Layout) {
for (auto &CustomSection : CustomSections) {
SectionBookkeeping Section;
auto *Sec = CustomSection.Section;
startCustomSection(Section, CustomSection.Name);
Sec->setSectionOffset(W.OS.tell() - Section.ContentsOffset);
Asm.writeSectionData(W.OS, Sec, Layout);
CustomSection.OutputContentsOffset = Section.ContentsOffset;
CustomSection.OutputIndex = Section.Index;
endSection(Section);
auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
applyRelocations(Relocations, CustomSection.OutputContentsOffset);
}
}
uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm& Symbol) {
assert(Symbol.isFunction());
assert(TypeIndices.count(&Symbol));
return TypeIndices[&Symbol];
}
uint32_t WasmObjectWriter::registerFunctionType(const MCSymbolWasm& Symbol) {
assert(Symbol.isFunction());
WasmFunctionType F;
const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
F.Returns = ResolvedSym->getReturns();
F.Params = ResolvedSym->getParams();
auto Pair =
FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
if (Pair.second)
FunctionTypes.push_back(F);
TypeIndices[&Symbol] = Pair.first->second;
LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol
<< " new:" << Pair.second << "\n");
LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
return Pair.first->second;
}
static bool isInSymtab(const MCSymbolWasm &Sym) {
if (Sym.isUsedInReloc())
return true;
if (Sym.isComdat() && !Sym.isDefined())
return false;
if (Sym.isTemporary() && Sym.getName().empty())
return false;
if (Sym.isTemporary() && Sym.isData() && !Sym.getSize())
return false;
if (Sym.isSection())
return false;
return true;
}
uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
uint64_t StartOffset = W.OS.tell();
LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
MCContext &Ctx = Asm.getContext();
SmallVector<WasmFunction, 4> Functions;
SmallVector<uint32_t, 4> TableElems;
SmallVector<wasm::WasmImport, 4> Imports;
SmallVector<wasm::WasmExport, 4> Exports;
SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
uint32_t DataSize = 0;
MCSymbolWasm *MemorySym =
cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
wasm::WasmImport MemImport;
MemImport.Module = MemorySym->getModuleName();
MemImport.Field = MemorySym->getName();
MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
Imports.push_back(MemImport);
MCSymbolWasm *TableSym =
cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
wasm::WasmImport TableImport;
TableImport.Module = TableSym->getModuleName();
TableImport.Field = TableSym->getName();
TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
TableImport.Table.ElemType = wasm::WASM_TYPE_ANYFUNC;
Imports.push_back(TableImport);
for (const MCSymbol &S : Asm.symbols()) {
const auto &WS = static_cast<const MCSymbolWasm &>(S);
if (WS.isFunction())
registerFunctionType(WS);
if (WS.isTemporary())
continue;
if (!WS.isDefined() && !WS.isComdat()) {
if (WS.isFunction()) {
wasm::WasmImport Import;
Import.Module = WS.getModuleName();
Import.Field = WS.getName();
Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
Import.SigIndex = getFunctionType(WS);
Imports.push_back(Import);
WasmIndices[&WS] = NumFunctionImports++;
} else if (WS.isGlobal()) {
if (WS.isWeak())
report_fatal_error("undefined global symbol cannot be weak");
wasm::WasmImport Import;
Import.Module = WS.getModuleName();
Import.Field = WS.getName();
Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
Import.Global = WS.getGlobalType();
Imports.push_back(Import);
WasmIndices[&WS] = NumGlobalImports++;
}
}
}
for (MCSection &Sec : Asm) {
auto &Section = static_cast<MCSectionWasm &>(Sec);
StringRef SectionName = Section.getSectionName();
if (SectionName.startswith(".init_array"))
continue;
if (Section.getKind().isText())
continue;
if (Section.isWasmData()) {
uint32_t SegmentIndex = DataSegments.size();
DataSize = alignTo(DataSize, Section.getAlignment());
DataSegments.emplace_back();
WasmDataSegment &Segment = DataSegments.back();
Segment.Name = SectionName;
Segment.Offset = DataSize;
Segment.Section = &Section;
addData(Segment.Data, Section);
Segment.Alignment = Section.getAlignment();
Segment.Flags = 0;
DataSize += Segment.Data.size();
Section.setSegmentIndex(SegmentIndex);
if (const MCSymbolWasm *C = Section.getGroup()) {
Comdats[C->getName()].emplace_back(
WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
}
} else {
assert(Sec.getKind().isMetadata());
StringRef Name = SectionName;
if (Name.startswith(".custom_section."))
Name = Name.substr(strlen(".custom_section."));
MCSymbol* Begin = Sec.getBeginSymbol();
if (Begin) {
WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size();
if (SectionName != Begin->getName())
report_fatal_error("section name and begin symbol should match: " +
Twine(SectionName));
}
CustomSections.emplace_back(Name, &Section);
}
}
for (const MCSymbol &S : Asm.symbols()) {
if (S.isTemporary() && S.getName().empty())
continue;
const auto &WS = static_cast<const MCSymbolWasm &>(S);
LLVM_DEBUG(
dbgs() << "MCSymbol: " << toString(WS.getType()) << " '" << S << "'"
<< " isDefined=" << S.isDefined() << " isExternal="
<< S.isExternal() << " isTemporary=" << S.isTemporary()
<< " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden()
<< " isVariable=" << WS.isVariable() << "\n");
if (WS.isVariable())
continue;
if (WS.isComdat() && !WS.isDefined())
continue;
if (WS.isFunction()) {
unsigned Index;
if (WS.isDefined()) {
if (WS.getOffset() != 0)
report_fatal_error(
"function sections must contain one function each");
if (WS.getSize() == 0)
report_fatal_error(
"function symbols must have a size set with .size");
Index = NumFunctionImports + Functions.size();
WasmFunction Func;
Func.Type = getFunctionType(WS);
Func.Sym = &WS;
WasmIndices[&WS] = Index;
Functions.push_back(Func);
auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
if (const MCSymbolWasm *C = Section.getGroup()) {
Comdats[C->getName()].emplace_back(
WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
}
} else {
Index = WasmIndices.find(&WS)->second;
}
LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n");
} else if (WS.isData()) {
if (WS.isTemporary() && !WS.getSize())
continue;
if (!WS.isDefined()) {
LLVM_DEBUG(dbgs() << " -> segment index: -1"
<< "\n");
continue;
}
if (!WS.getSize())
report_fatal_error("data symbols must have a size set with .size: " +
WS.getName());
int64_t Size = 0;
if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
report_fatal_error(".size expression must be evaluatable");
auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
assert(DataSection.isWasmData());
wasm::WasmDataReference Ref = wasm::WasmDataReference{
DataSection.getSegmentIndex(),
static_cast<uint32_t>(Layout.getSymbolOffset(WS)),
static_cast<uint32_t>(Size)};
DataLocations[&WS] = Ref;
LLVM_DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n");
} else if (WS.isGlobal()) {
if (WS.isDefined())
report_fatal_error("don't yet support defined globals");
LLVM_DEBUG(dbgs() << " -> global index: "
<< WasmIndices.find(&WS)->second << "\n");
} else {
assert(WS.isSection());
}
}
for (const MCSymbol &S : Asm.symbols()) {
if (!S.isVariable())
continue;
assert(S.isDefined());
const auto &WS = static_cast<const MCSymbolWasm &>(S);
const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym
<< "'\n");
if (WS.isFunction()) {
assert(WasmIndices.count(ResolvedSym) > 0);
uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
WasmIndices[&WS] = WasmIndex;
LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
} else if (WS.isData()) {
assert(DataLocations.count(ResolvedSym) > 0);
const wasm::WasmDataReference &Ref =
DataLocations.find(ResolvedSym)->second;
DataLocations[&WS] = Ref;
LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
} else {
report_fatal_error("don't yet support global aliases");
}
}
for (const MCSymbol &S : Asm.symbols()) {
const auto &WS = static_cast<const MCSymbolWasm &>(S);
if (!isInSymtab(WS)) {
WS.setIndex(INVALID_INDEX);
continue;
}
LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
uint32_t Flags = 0;
if (WS.isWeak())
Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
if (WS.isHidden())
Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
if (!WS.isExternal() && WS.isDefined())
Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
if (WS.isUndefined())
Flags |= wasm::WASM_SYMBOL_UNDEFINED;
wasm::WasmSymbolInfo Info;
Info.Name = WS.getName();
Info.Kind = WS.getType();
Info.Flags = Flags;
if (!WS.isData()) {
assert(WasmIndices.count(&WS) > 0);
Info.ElementIndex = WasmIndices.find(&WS)->second;
} else if (WS.isDefined()) {
assert(DataLocations.count(&WS) > 0);
Info.DataRef = DataLocations.find(&WS)->second;
}
WS.setIndex(SymbolInfos.size());
SymbolInfos.emplace_back(Info);
}
{
auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
if (Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_I32 &&
Rel.Type != wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB)
return;
assert(Rel.Symbol->isFunction());
const MCSymbolWasm &WS = *ResolveSymbol(*Rel.Symbol);
uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
uint32_t TableIndex = TableElems.size() + kInitialTableOffset;
if (TableIndices.try_emplace(&WS, TableIndex).second) {
LLVM_DEBUG(dbgs() << " -> adding " << WS.getName()
<< " to table: " << TableIndex << "\n");
TableElems.push_back(FunctionIndex);
registerFunctionType(WS);
}
};
for (const WasmRelocationEntry &RelEntry : CodeRelocations)
HandleReloc(RelEntry);
for (const WasmRelocationEntry &RelEntry : DataRelocations)
HandleReloc(RelEntry);
}
for (const MCSection &S : Asm) {
const auto &WS = static_cast<const MCSectionWasm &>(S);
if (WS.getSectionName().startswith(".fini_array"))
report_fatal_error(".fini_array sections are unsupported");
if (!WS.getSectionName().startswith(".init_array"))
continue;
if (WS.getFragmentList().empty())
continue;
if (WS.getFragmentList().size() != 3)
report_fatal_error("only one .init_array section fragment supported");
auto IT = WS.begin();
const MCFragment &EmptyFrag = *IT;
if (EmptyFrag.getKind() != MCFragment::FT_Data)
report_fatal_error(".init_array section should be aligned");
IT = std::next(IT);
const MCFragment &AlignFrag = *IT;
if (AlignFrag.getKind() != MCFragment::FT_Align)
report_fatal_error(".init_array section should be aligned");
if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
report_fatal_error(".init_array section should be aligned for pointers");
const MCFragment &Frag = *std::next(IT);
if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
report_fatal_error("only data supported in .init_array section");
uint16_t Priority = UINT16_MAX;
unsigned PrefixLength = strlen(".init_array");
if (WS.getSectionName().size() > PrefixLength) {
if (WS.getSectionName()[PrefixLength] != '.')
report_fatal_error(".init_array section priority should start with '.'");
if (WS.getSectionName()
.substr(PrefixLength + 1)
.getAsInteger(10, Priority))
report_fatal_error("invalid .init_array section priority");
}
const auto &DataFrag = cast<MCDataFragment>(Frag);
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
for (const uint8_t *p = (const uint8_t *)Contents.data(),
*end = (const uint8_t *)Contents.data() + Contents.size();
p != end; ++p) {
if (*p != 0)
report_fatal_error("non-symbolic data in .init_array section");
}
for (const MCFixup &Fixup : DataFrag.getFixups()) {
assert(Fixup.getKind() == MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
const MCExpr *Expr = Fixup.getValue();
auto *Sym = dyn_cast<MCSymbolRefExpr>(Expr);
if (!Sym)
report_fatal_error("fixups in .init_array should be symbol references");
if (Sym->getKind() != MCSymbolRefExpr::VK_WebAssembly_FUNCTION)
report_fatal_error("symbols in .init_array should be for functions");
if (Sym->getSymbol().getIndex() == INVALID_INDEX)
report_fatal_error("symbols in .init_array should exist in symbtab");
InitFuncs.push_back(
std::make_pair(Priority, Sym->getSymbol().getIndex()));
}
}
writeHeader(Asm);
writeTypeSection(FunctionTypes);
writeImportSection(Imports, DataSize, TableElems.size());
writeFunctionSection(Functions);
writeGlobalSection();
writeExportSection(Exports);
writeElemSection(TableElems);
writeCodeSection(Asm, Layout, Functions);
writeDataSection();
writeCustomSections(Asm, Layout);
writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
writeCustomRelocSections();
return W.OS.tell() - StartOffset;
}
std::unique_ptr<MCObjectWriter>
llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
raw_pwrite_stream &OS) {
return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
}