2017-02-22 09:23:18 +08:00
|
|
|
//===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Wasm object file writer information.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Wasm.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.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/MCObjectFileInfo.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"
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2017-02-25 07:18:00 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
|
|
|
#define DEBUG_TYPE "reloc-info"
|
|
|
|
|
|
|
|
namespace {
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// For patching purposes, we need to remember where each section starts, both
|
|
|
|
// for patching up the section size field, and for patching up references to
|
|
|
|
// locations within the section.
|
|
|
|
struct SectionBookkeeping {
|
|
|
|
// Where the size of the section is written.
|
|
|
|
uint64_t SizeOffset;
|
|
|
|
// Where the contents of the section starts (after the header).
|
|
|
|
uint64_t ContentsOffset;
|
|
|
|
};
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
// The signature of a wasm function, in a struct capable of being used as a
|
|
|
|
// DenseMap key.
|
|
|
|
struct WasmFunctionType {
|
|
|
|
// Support empty and tombstone instances, needed by DenseMap.
|
|
|
|
enum { Plain, Empty, Tombstone } State;
|
|
|
|
|
|
|
|
// The return types of the function.
|
|
|
|
SmallVector<wasm::ValType, 1> Returns;
|
|
|
|
|
|
|
|
// The parameter types of the function.
|
|
|
|
SmallVector<wasm::ValType, 4> Params;
|
|
|
|
|
|
|
|
WasmFunctionType() : State(Plain) {}
|
|
|
|
|
|
|
|
bool operator==(const WasmFunctionType &Other) const {
|
|
|
|
return State == Other.State && Returns == Other.Returns &&
|
|
|
|
Params == Other.Params;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Traits for using WasmFunctionType in a DenseMap.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A wasm import to be written into the import section.
|
|
|
|
struct WasmImport {
|
|
|
|
StringRef ModuleName;
|
|
|
|
StringRef FieldName;
|
|
|
|
unsigned Kind;
|
|
|
|
int32_t Type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A wasm function to be written into the function section.
|
|
|
|
struct WasmFunction {
|
|
|
|
int32_t Type;
|
|
|
|
const MCSymbolWasm *Sym;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A wasm export to be written into the export section.
|
|
|
|
struct WasmExport {
|
|
|
|
StringRef FieldName;
|
|
|
|
unsigned Kind;
|
|
|
|
uint32_t Index;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A wasm global to be written into the global section.
|
|
|
|
struct WasmGlobal {
|
|
|
|
wasm::ValType Type;
|
|
|
|
bool IsMutable;
|
|
|
|
bool HasImport;
|
|
|
|
uint64_t InitialValue;
|
|
|
|
uint32_t ImportIndex;
|
|
|
|
};
|
|
|
|
|
2017-06-07 00:38:59 +08:00
|
|
|
// Information about a single relocation.
|
|
|
|
struct WasmRelocationEntry {
|
|
|
|
uint64_t Offset; // Where is the relocation.
|
|
|
|
const MCSymbolWasm *Symbol; // The symbol to relocate with.
|
|
|
|
int64_t Addend; // A value to add to the symbol.
|
|
|
|
unsigned Type; // The type of the relocation.
|
|
|
|
MCSectionWasm *FixupSection;// The section the relocation is targeting.
|
|
|
|
|
|
|
|
WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
|
|
|
|
int64_t Addend, unsigned Type,
|
|
|
|
MCSectionWasm *FixupSection)
|
|
|
|
: Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
|
|
|
|
FixupSection(FixupSection) {}
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
bool hasAddend() const {
|
|
|
|
switch (Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:38:59 +08:00
|
|
|
void print(raw_ostream &Out) const {
|
|
|
|
Out << "Off=" << Offset << ", Sym=" << Symbol << ", Addend=" << Addend
|
|
|
|
<< ", Type=" << Type << ", FixupSection=" << FixupSection;
|
|
|
|
}
|
2017-06-20 12:04:59 +08:00
|
|
|
|
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
|
|
|
|
#endif
|
2017-06-07 00:38:59 +08:00
|
|
|
};
|
|
|
|
|
2017-06-20 12:47:58 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
|
2017-06-20 12:04:59 +08:00
|
|
|
Rel.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
class WasmObjectWriter : public MCObjectWriter {
|
|
|
|
/// Helper struct for containing some precomputed information on symbols.
|
|
|
|
struct WasmSymbolData {
|
|
|
|
const MCSymbolWasm *Symbol;
|
|
|
|
StringRef Name;
|
|
|
|
|
|
|
|
// Support lexicographic sorting.
|
|
|
|
bool operator<(const WasmSymbolData &RHS) const { return Name < RHS.Name; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The target specific Wasm writer instance.
|
|
|
|
std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// Relocations for fixing up references in the code section.
|
|
|
|
std::vector<WasmRelocationEntry> CodeRelocations;
|
|
|
|
|
|
|
|
// Relocations for fixing up references in the data section.
|
|
|
|
std::vector<WasmRelocationEntry> DataRelocations;
|
|
|
|
|
|
|
|
// Index values to use for fixing up call_indirect type indices.
|
2017-06-07 03:15:05 +08:00
|
|
|
// Maps function symbols to the index of the type of the function
|
|
|
|
DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
|
2017-06-13 07:52:44 +08:00
|
|
|
// Maps function symbols to the table element index space. Used
|
|
|
|
// for TABLE_INDEX relocation types (i.e. address taken functions).
|
|
|
|
DenseMap<const MCSymbolWasm *, uint32_t> IndirectSymbolIndices;
|
|
|
|
// Maps function/global symbols to the function/global index space.
|
2017-06-07 03:15:05 +08:00
|
|
|
DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
|
|
|
|
|
|
|
|
DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
|
|
|
|
FunctionTypeIndices;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
// TargetObjectWriter wrappers.
|
|
|
|
bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
|
2017-06-14 02:51:50 +08:00
|
|
|
unsigned getRelocType(const MCValue &Target, const MCFixup &Fixup) const {
|
|
|
|
return TargetObjectWriter->getRelocType(Target, Fixup);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
void startSection(SectionBookkeeping &Section, unsigned SectionId,
|
|
|
|
const char *Name = nullptr);
|
|
|
|
void endSection(SectionBookkeeping &Section);
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
public:
|
|
|
|
WasmObjectWriter(MCWasmObjectTargetWriter *MOTW, raw_pwrite_stream &OS)
|
|
|
|
: MCObjectWriter(OS, /*IsLittleEndian=*/true), TargetObjectWriter(MOTW) {}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
private:
|
2017-02-22 09:23:18 +08:00
|
|
|
~WasmObjectWriter() override;
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
void reset() override {
|
|
|
|
CodeRelocations.clear();
|
|
|
|
DataRelocations.clear();
|
|
|
|
TypeIndices.clear();
|
|
|
|
SymbolIndices.clear();
|
2017-06-13 07:52:44 +08:00
|
|
|
IndirectSymbolIndices.clear();
|
2017-06-07 03:15:05 +08:00
|
|
|
FunctionTypeIndices.clear();
|
|
|
|
MCObjectWriter::reset();
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
void writeHeader(const MCAssembler &Asm);
|
|
|
|
|
|
|
|
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment, const MCFixup &Fixup,
|
|
|
|
MCValue Target, bool &IsPCRel,
|
|
|
|
uint64_t &FixedValue) override;
|
|
|
|
|
|
|
|
void executePostLayoutBinding(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) override;
|
|
|
|
|
|
|
|
void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
void writeString(const StringRef Str) {
|
|
|
|
encodeULEB128(Str.size(), getStream());
|
|
|
|
writeBytes(Str);
|
|
|
|
}
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
void writeValueType(wasm::ValType Ty) {
|
|
|
|
encodeSLEB128(int32_t(Ty), getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeTypeSection(const SmallVector<WasmFunctionType, 4> &FunctionTypes);
|
|
|
|
void writeImportSection(const SmallVector<WasmImport, 4> &Imports);
|
|
|
|
void writeFunctionSection(const SmallVector<WasmFunction, 4> &Functions);
|
2017-06-13 07:52:44 +08:00
|
|
|
void writeTableSection(uint32_t NumElements);
|
2017-06-03 10:01:24 +08:00
|
|
|
void writeMemorySection(const SmallVector<char, 0> &DataBytes);
|
|
|
|
void writeGlobalSection(const SmallVector<WasmGlobal, 4> &Globals);
|
|
|
|
void writeExportSection(const SmallVector<WasmExport, 4> &Exports);
|
|
|
|
void writeElemSection(const SmallVector<uint32_t, 4> &TableElems);
|
|
|
|
void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const SmallVector<WasmFunction, 4> &Functions);
|
|
|
|
uint64_t
|
2017-06-07 03:15:05 +08:00
|
|
|
writeDataSection(const SmallVector<char, 0> &DataBytes);
|
2017-06-03 10:01:24 +08:00
|
|
|
void writeNameSection(const SmallVector<WasmFunction, 4> &Functions,
|
|
|
|
const SmallVector<WasmImport, 4> &Imports,
|
|
|
|
uint32_t NumFuncImports);
|
2017-06-07 03:15:05 +08:00
|
|
|
void writeCodeRelocSection();
|
|
|
|
void writeDataRelocSection(uint64_t DataSectionHeaderSize);
|
2017-06-20 12:04:59 +08:00
|
|
|
void writeLinkingMetaDataSection(ArrayRef<StringRef> WeakSymbols,
|
|
|
|
bool HasStackPointer,
|
2017-06-03 10:01:24 +08:00
|
|
|
uint32_t StackPointerGlobal);
|
2017-06-07 03:15:05 +08:00
|
|
|
|
|
|
|
void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
|
|
|
|
uint64_t ContentsOffset);
|
|
|
|
|
|
|
|
void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations,
|
|
|
|
uint64_t HeaderSize);
|
|
|
|
uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
|
2017-02-22 09:23:18 +08:00
|
|
|
};
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
WasmObjectWriter::~WasmObjectWriter() {}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// Return the padding size to write a 32-bit value into a 5-byte ULEB128.
|
|
|
|
static unsigned PaddingFor5ByteULEB128(uint32_t X) {
|
|
|
|
return X == 0 ? 4 : (4u - (31u - countLeadingZeros(X)) / 7u);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the padding size to write a 32-bit value into a 5-byte SLEB128.
|
|
|
|
static unsigned PaddingFor5ByteSLEB128(int32_t X) {
|
|
|
|
return 5 - getSLEB128Size(X);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out a section header and a patchable section size field.
|
|
|
|
void WasmObjectWriter::startSection(SectionBookkeeping &Section,
|
|
|
|
unsigned SectionId,
|
|
|
|
const char *Name) {
|
|
|
|
assert((Name != nullptr) == (SectionId == wasm::WASM_SEC_CUSTOM) &&
|
|
|
|
"Only custom sections can have names");
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
DEBUG(dbgs() << "startSection " << SectionId << ": " << Name << "\n");
|
2017-03-15 04:23:22 +08:00
|
|
|
encodeULEB128(SectionId, getStream());
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
Section.SizeOffset = getStream().tell();
|
|
|
|
|
|
|
|
// The section size. We don't know the size yet, so reserve enough space
|
|
|
|
// for any 32-bit value; we'll patch it later.
|
|
|
|
encodeULEB128(UINT32_MAX, getStream());
|
|
|
|
|
|
|
|
// The position where the section starts, for measuring its size.
|
|
|
|
Section.ContentsOffset = getStream().tell();
|
|
|
|
|
|
|
|
// Custom sections in wasm also have a string identifier.
|
|
|
|
if (SectionId == wasm::WASM_SEC_CUSTOM) {
|
2017-06-20 12:04:59 +08:00
|
|
|
assert(Name);
|
|
|
|
writeString(StringRef(Name));
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the section is complete and we know how big it is, patch up the
|
|
|
|
// section size field at the start of the section.
|
|
|
|
void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
|
|
|
|
uint64_t Size = getStream().tell() - Section.ContentsOffset;
|
|
|
|
if (uint32_t(Size) != Size)
|
|
|
|
report_fatal_error("section size does not fit in a uint32_t");
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
DEBUG(dbgs() << "endSection size=" << Size << "\n");
|
2017-02-25 07:18:00 +08:00
|
|
|
unsigned Padding = PaddingFor5ByteULEB128(Size);
|
|
|
|
|
|
|
|
// Write the final section size to the payload_len field, which follows
|
|
|
|
// the section id byte.
|
|
|
|
uint8_t Buffer[16];
|
|
|
|
unsigned SizeLen = encodeULEB128(Size, Buffer, Padding);
|
|
|
|
assert(SizeLen == 5);
|
|
|
|
getStream().pwrite((char *)Buffer, SizeLen, Section.SizeOffset);
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
// Emit the Wasm header.
|
|
|
|
void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
|
2017-02-23 02:50:20 +08:00
|
|
|
writeBytes(StringRef(wasm::WasmMagic, sizeof(wasm::WasmMagic)));
|
|
|
|
writeLE32(wasm::WasmVersion);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
const MCFragment *Fragment,
|
|
|
|
const MCFixup &Fixup, MCValue Target,
|
|
|
|
bool &IsPCRel, uint64_t &FixedValue) {
|
2017-02-25 07:18:00 +08:00
|
|
|
MCSectionWasm &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
|
|
|
|
uint64_t C = Target.getConstant();
|
|
|
|
uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
|
|
|
|
MCContext &Ctx = Asm.getContext();
|
|
|
|
|
|
|
|
if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
|
|
|
|
assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
|
|
|
|
"Should not have constructed this");
|
|
|
|
|
|
|
|
// Let A, B and C being the components of Target and R be the location of
|
|
|
|
// the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
|
|
|
|
// If it is pcrel, we want to compute (A - B + C - R).
|
|
|
|
|
|
|
|
// In general, Wasm has no relocations for -B. It can only represent (A + C)
|
|
|
|
// or (A + C - R). If B = R + K and the relocation is not pcrel, we can
|
|
|
|
// replace B to implement it: (A - R - K + C)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We either rejected the fixup or folded B into C at this point.
|
|
|
|
const MCSymbolRefExpr *RefA = Target.getSymA();
|
|
|
|
const auto *SymA = RefA ? cast<MCSymbolWasm>(&RefA->getSymbol()) : nullptr;
|
|
|
|
|
|
|
|
bool ViaWeakRef = false;
|
|
|
|
if (SymA && SymA->isVariable()) {
|
|
|
|
const MCExpr *Expr = SymA->getVariableValue();
|
|
|
|
if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
|
|
|
|
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
|
|
|
|
SymA = cast<MCSymbolWasm>(&Inner->getSymbol());
|
|
|
|
ViaWeakRef = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put any constant offset in an addend. Offsets can be negative, and
|
|
|
|
// LLVM expects wrapping, in contrast to wasm's immediates which can't
|
|
|
|
// be negative and don't wrap.
|
|
|
|
FixedValue = 0;
|
|
|
|
|
|
|
|
if (SymA) {
|
|
|
|
if (ViaWeakRef)
|
|
|
|
llvm_unreachable("weakref used in reloc not yet implemented");
|
|
|
|
else
|
|
|
|
SymA->setUsedInReloc();
|
|
|
|
}
|
|
|
|
|
2017-06-14 02:51:50 +08:00
|
|
|
assert(!IsPCRel);
|
2017-06-17 07:59:10 +08:00
|
|
|
assert(SymA);
|
|
|
|
|
2017-06-14 02:51:50 +08:00
|
|
|
unsigned Type = getRelocType(Target, Fixup);
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
|
2017-06-20 12:04:59 +08:00
|
|
|
DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
if (FixupSection.hasInstructions())
|
|
|
|
CodeRelocations.push_back(Rec);
|
|
|
|
else
|
|
|
|
DataRelocations.push_back(Rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write X as an (unsigned) LEB value at offset Offset in Stream, padded
|
|
|
|
// to allow patching.
|
|
|
|
static void
|
|
|
|
WritePatchableLEB(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
|
|
|
|
uint8_t Buffer[5];
|
|
|
|
unsigned Padding = PaddingFor5ByteULEB128(X);
|
|
|
|
unsigned SizeLen = encodeULEB128(X, Buffer, Padding);
|
|
|
|
assert(SizeLen == 5);
|
|
|
|
Stream.pwrite((char *)Buffer, SizeLen, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write X as an signed LEB value at offset Offset in Stream, padded
|
|
|
|
// to allow patching.
|
|
|
|
static void
|
|
|
|
WritePatchableSLEB(raw_pwrite_stream &Stream, int32_t X, uint64_t Offset) {
|
|
|
|
uint8_t Buffer[5];
|
|
|
|
unsigned Padding = PaddingFor5ByteSLEB128(X);
|
|
|
|
unsigned SizeLen = encodeSLEB128(X, Buffer, Padding);
|
|
|
|
assert(SizeLen == 5);
|
|
|
|
Stream.pwrite((char *)Buffer, SizeLen, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write X as a plain integer value at offset Offset in Stream.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute a value to write into the code at the location covered
|
|
|
|
// by RelEntry. This value isn't used by the static linker, since
|
|
|
|
// we have addends; it just serves to make the code more readable
|
|
|
|
// and to make standalone wasm modules directly usable.
|
|
|
|
static uint32_t ProvisionalValue(const WasmRelocationEntry &RelEntry) {
|
|
|
|
const MCSymbolWasm *Sym = RelEntry.Symbol;
|
|
|
|
|
|
|
|
// For undefined symbols, use a hopefully invalid value.
|
2017-06-20 12:04:59 +08:00
|
|
|
if (!Sym->isDefined(/*SetUsed=*/false))
|
2017-02-25 07:18:00 +08:00
|
|
|
return UINT32_MAX;
|
|
|
|
|
|
|
|
MCSectionWasm &Section =
|
|
|
|
cast<MCSectionWasm>(RelEntry.Symbol->getSection(false));
|
|
|
|
uint64_t Address = Section.getSectionOffset() + RelEntry.Addend;
|
|
|
|
|
|
|
|
// Ignore overflow. LLVM allows address arithmetic to silently wrap.
|
|
|
|
uint32_t Value = Address;
|
|
|
|
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
uint32_t WasmObjectWriter::getRelocationIndexValue(
|
|
|
|
const WasmRelocationEntry &RelEntry) {
|
|
|
|
switch (RelEntry.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
|
2017-06-20 12:04:59 +08:00
|
|
|
if (!IndirectSymbolIndices.count(RelEntry.Symbol))
|
|
|
|
report_fatal_error("symbol not found table index space:" +
|
|
|
|
RelEntry.Symbol->getName());
|
2017-06-13 07:52:44 +08:00
|
|
|
return IndirectSymbolIndices[RelEntry.Symbol];
|
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
2017-06-17 07:59:10 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
2017-06-07 03:15:05 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
|
2017-06-20 12:04:59 +08:00
|
|
|
if (!SymbolIndices.count(RelEntry.Symbol))
|
|
|
|
report_fatal_error("symbol not found function/global index space:" +
|
|
|
|
RelEntry.Symbol->getName());
|
2017-06-07 03:15:05 +08:00
|
|
|
return SymbolIndices[RelEntry.Symbol];
|
|
|
|
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
2017-06-20 12:04:59 +08:00
|
|
|
if (!TypeIndices.count(RelEntry.Symbol))
|
|
|
|
report_fatal_error("symbol not found in type index space:" +
|
|
|
|
RelEntry.Symbol->getName());
|
2017-06-07 03:15:05 +08:00
|
|
|
return TypeIndices[RelEntry.Symbol];
|
|
|
|
default:
|
|
|
|
llvm_unreachable("invalid relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// Apply the portions of the relocation records that we can handle ourselves
|
|
|
|
// directly.
|
2017-06-07 03:15:05 +08:00
|
|
|
void WasmObjectWriter::applyRelocations(
|
|
|
|
ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset) {
|
|
|
|
raw_pwrite_stream &Stream = getStream();
|
2017-02-25 07:18:00 +08:00
|
|
|
for (const WasmRelocationEntry &RelEntry : Relocations) {
|
|
|
|
uint64_t Offset = ContentsOffset +
|
|
|
|
RelEntry.FixupSection->getSectionOffset() +
|
|
|
|
RelEntry.Offset;
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
|
2017-06-07 03:15:05 +08:00
|
|
|
switch (RelEntry.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
2017-06-17 07:59:10 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
|
2017-06-07 03:15:05 +08:00
|
|
|
uint32_t Index = getRelocationIndexValue(RelEntry);
|
|
|
|
WritePatchableSLEB(Stream, Index, Offset);
|
2017-02-25 07:18:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-06-07 03:15:05 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
|
|
|
|
uint32_t Index = getRelocationIndexValue(RelEntry);
|
|
|
|
WriteI32(Stream, Index, Offset);
|
2017-02-25 07:18:00 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB: {
|
|
|
|
uint32_t Value = ProvisionalValue(RelEntry);
|
|
|
|
WritePatchableSLEB(Stream, Value, Offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB: {
|
|
|
|
uint32_t Value = ProvisionalValue(RelEntry);
|
|
|
|
WritePatchableLEB(Stream, Value, Offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32: {
|
|
|
|
uint32_t Value = ProvisionalValue(RelEntry);
|
|
|
|
WriteI32(Stream, Value, Offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2017-06-17 07:59:10 +08:00
|
|
|
llvm_unreachable("invalid relocation type");
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the portions of the relocation records that the linker will
|
|
|
|
// need to handle.
|
2017-06-07 03:15:05 +08:00
|
|
|
void WasmObjectWriter::writeRelocations(
|
|
|
|
ArrayRef<WasmRelocationEntry> Relocations, uint64_t HeaderSize) {
|
|
|
|
raw_pwrite_stream &Stream = getStream();
|
|
|
|
for (const WasmRelocationEntry& RelEntry : Relocations) {
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
uint64_t Offset = RelEntry.Offset +
|
2017-04-29 05:22:38 +08:00
|
|
|
RelEntry.FixupSection->getSectionOffset() + HeaderSize;
|
2017-06-07 03:15:05 +08:00
|
|
|
uint32_t Index = getRelocationIndexValue(RelEntry);
|
2017-02-22 09:23:18 +08:00
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
encodeULEB128(RelEntry.Type, Stream);
|
2017-03-31 07:58:19 +08:00
|
|
|
encodeULEB128(Offset, Stream);
|
2017-06-07 03:15:05 +08:00
|
|
|
encodeULEB128(Index, Stream);
|
|
|
|
if (RelEntry.hasAddend())
|
|
|
|
encodeSLEB128(RelEntry.Addend, Stream);
|
2017-03-31 07:58:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
void WasmObjectWriter::writeTypeSection(
|
|
|
|
const SmallVector<WasmFunctionType, 4> &FunctionTypes) {
|
|
|
|
if (FunctionTypes.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_TYPE);
|
|
|
|
|
|
|
|
encodeULEB128(FunctionTypes.size(), getStream());
|
|
|
|
|
|
|
|
for (const WasmFunctionType &FuncTy : FunctionTypes) {
|
|
|
|
encodeSLEB128(wasm::WASM_TYPE_FUNC, getStream());
|
|
|
|
encodeULEB128(FuncTy.Params.size(), getStream());
|
|
|
|
for (wasm::ValType Ty : FuncTy.Params)
|
|
|
|
writeValueType(Ty);
|
|
|
|
encodeULEB128(FuncTy.Returns.size(), getStream());
|
|
|
|
for (wasm::ValType Ty : FuncTy.Returns)
|
|
|
|
writeValueType(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
void WasmObjectWriter::writeImportSection(
|
|
|
|
const SmallVector<WasmImport, 4> &Imports) {
|
|
|
|
if (Imports.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_IMPORT);
|
|
|
|
|
|
|
|
encodeULEB128(Imports.size(), getStream());
|
|
|
|
for (const WasmImport &Import : Imports) {
|
2017-06-20 12:04:59 +08:00
|
|
|
writeString(Import.ModuleName);
|
|
|
|
writeString(Import.FieldName);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
encodeULEB128(Import.Kind, getStream());
|
|
|
|
|
|
|
|
switch (Import.Kind) {
|
|
|
|
case wasm::WASM_EXTERNAL_FUNCTION:
|
|
|
|
encodeULEB128(Import.Type, getStream());
|
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_GLOBAL:
|
|
|
|
encodeSLEB128(int32_t(Import.Type), getStream());
|
|
|
|
encodeULEB128(0, getStream()); // mutability
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported import kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeFunctionSection(
|
|
|
|
const SmallVector<WasmFunction, 4> &Functions) {
|
|
|
|
if (Functions.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_FUNCTION);
|
|
|
|
|
|
|
|
encodeULEB128(Functions.size(), getStream());
|
|
|
|
for (const WasmFunction &Func : Functions)
|
|
|
|
encodeULEB128(Func.Type, getStream());
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-06-13 07:52:44 +08:00
|
|
|
void WasmObjectWriter::writeTableSection(uint32_t NumElements) {
|
2017-06-03 10:01:24 +08:00
|
|
|
// For now, always emit the table section, since indirect calls are not
|
|
|
|
// valid without it. In the future, we could perhaps be more clever and omit
|
|
|
|
// it if there are no indirect calls.
|
2017-06-13 07:52:44 +08:00
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_TABLE);
|
|
|
|
|
2017-06-13 07:52:44 +08:00
|
|
|
encodeULEB128(1, getStream()); // The number of tables.
|
|
|
|
// Fixed to 1 for now.
|
|
|
|
encodeSLEB128(wasm::WASM_TYPE_ANYFUNC, getStream()); // Type of table
|
|
|
|
encodeULEB128(0, getStream()); // flags
|
|
|
|
encodeULEB128(NumElements, getStream()); // initial
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeMemorySection(
|
|
|
|
const SmallVector<char, 0> &DataBytes) {
|
|
|
|
// For now, always emit the memory section, since loads and stores are not
|
|
|
|
// valid without it. In the future, we could perhaps be more clever and omit
|
|
|
|
// it if there are no loads or stores.
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
uint32_t NumPages =
|
|
|
|
(DataBytes.size() + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
|
|
|
|
|
|
|
|
startSection(Section, wasm::WASM_SEC_MEMORY);
|
|
|
|
encodeULEB128(1, getStream()); // number of memory spaces
|
|
|
|
|
|
|
|
encodeULEB128(0, getStream()); // flags
|
|
|
|
encodeULEB128(NumPages, getStream()); // initial
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeGlobalSection(
|
|
|
|
const SmallVector<WasmGlobal, 4> &Globals) {
|
|
|
|
if (Globals.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_GLOBAL);
|
|
|
|
|
|
|
|
encodeULEB128(Globals.size(), getStream());
|
|
|
|
for (const WasmGlobal &Global : Globals) {
|
|
|
|
writeValueType(Global.Type);
|
|
|
|
write8(Global.IsMutable);
|
|
|
|
|
|
|
|
if (Global.HasImport) {
|
|
|
|
assert(Global.InitialValue == 0);
|
|
|
|
write8(wasm::WASM_OPCODE_GET_GLOBAL);
|
|
|
|
encodeULEB128(Global.ImportIndex, getStream());
|
|
|
|
} else {
|
|
|
|
assert(Global.ImportIndex == 0);
|
|
|
|
write8(wasm::WASM_OPCODE_I32_CONST);
|
|
|
|
encodeSLEB128(Global.InitialValue, getStream()); // offset
|
|
|
|
}
|
|
|
|
write8(wasm::WASM_OPCODE_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeExportSection(
|
|
|
|
const SmallVector<WasmExport, 4> &Exports) {
|
|
|
|
if (Exports.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_EXPORT);
|
|
|
|
|
|
|
|
encodeULEB128(Exports.size(), getStream());
|
|
|
|
for (const WasmExport &Export : Exports) {
|
2017-06-20 12:04:59 +08:00
|
|
|
writeString(Export.FieldName);
|
2017-06-03 10:01:24 +08:00
|
|
|
encodeSLEB128(Export.Kind, getStream());
|
|
|
|
encodeULEB128(Export.Index, getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeElemSection(
|
|
|
|
const SmallVector<uint32_t, 4> &TableElems) {
|
|
|
|
if (TableElems.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_ELEM);
|
|
|
|
|
|
|
|
encodeULEB128(1, getStream()); // number of "segments"
|
|
|
|
encodeULEB128(0, getStream()); // the table index
|
|
|
|
|
|
|
|
// init expr for starting offset
|
|
|
|
write8(wasm::WASM_OPCODE_I32_CONST);
|
|
|
|
encodeSLEB128(0, getStream());
|
|
|
|
write8(wasm::WASM_OPCODE_END);
|
|
|
|
|
|
|
|
encodeULEB128(TableElems.size(), getStream());
|
|
|
|
for (uint32_t Elem : TableElems)
|
|
|
|
encodeULEB128(Elem, getStream());
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeCodeSection(
|
|
|
|
const MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const SmallVector<WasmFunction, 4> &Functions) {
|
|
|
|
if (Functions.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_CODE);
|
|
|
|
|
|
|
|
encodeULEB128(Functions.size(), getStream());
|
|
|
|
|
|
|
|
for (const WasmFunction &Func : Functions) {
|
|
|
|
MCSectionWasm &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, getStream());
|
|
|
|
|
|
|
|
FuncSection.setSectionOffset(getStream().tell() -
|
|
|
|
Section.ContentsOffset);
|
|
|
|
|
|
|
|
Asm.writeSectionData(&FuncSection, Layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply fixups.
|
2017-06-07 03:15:05 +08:00
|
|
|
applyRelocations(CodeRelocations, Section.ContentsOffset);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t WasmObjectWriter::writeDataSection(
|
2017-06-07 03:15:05 +08:00
|
|
|
const SmallVector<char, 0> &DataBytes) {
|
2017-06-03 10:01:24 +08:00
|
|
|
if (DataBytes.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_DATA);
|
|
|
|
|
|
|
|
encodeULEB128(1, getStream()); // count
|
|
|
|
encodeULEB128(0, getStream()); // memory index
|
|
|
|
write8(wasm::WASM_OPCODE_I32_CONST);
|
|
|
|
encodeSLEB128(0, getStream()); // offset
|
|
|
|
write8(wasm::WASM_OPCODE_END);
|
|
|
|
encodeULEB128(DataBytes.size(), getStream()); // size
|
|
|
|
uint32_t HeaderSize = getStream().tell() - Section.ContentsOffset;
|
|
|
|
writeBytes(DataBytes); // data
|
|
|
|
|
|
|
|
// Apply fixups.
|
2017-06-07 03:15:05 +08:00
|
|
|
applyRelocations(DataRelocations, Section.ContentsOffset + HeaderSize);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
return HeaderSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeNameSection(
|
|
|
|
const SmallVector<WasmFunction, 4> &Functions,
|
|
|
|
const SmallVector<WasmImport, 4> &Imports,
|
|
|
|
unsigned NumFuncImports) {
|
|
|
|
uint32_t TotalFunctions = NumFuncImports + Functions.size();
|
|
|
|
if (TotalFunctions == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_CUSTOM, "name");
|
|
|
|
SectionBookkeeping SubSection;
|
|
|
|
startSection(SubSection, wasm::WASM_NAMES_FUNCTION);
|
|
|
|
|
|
|
|
encodeULEB128(TotalFunctions, getStream());
|
|
|
|
uint32_t Index = 0;
|
|
|
|
for (const WasmImport &Import : Imports) {
|
|
|
|
if (Import.Kind == wasm::WASM_EXTERNAL_FUNCTION) {
|
|
|
|
encodeULEB128(Index, getStream());
|
2017-06-20 12:04:59 +08:00
|
|
|
writeString(Import.FieldName);
|
2017-06-03 10:01:24 +08:00
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const WasmFunction &Func : Functions) {
|
|
|
|
encodeULEB128(Index, getStream());
|
2017-06-20 12:04:59 +08:00
|
|
|
writeString(Func.Sym->getName());
|
2017-06-03 10:01:24 +08:00
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(SubSection);
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
void WasmObjectWriter::writeCodeRelocSection() {
|
2017-06-03 10:01:24 +08:00
|
|
|
// See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
|
|
|
|
// for descriptions of the reloc sections.
|
|
|
|
|
|
|
|
if (CodeRelocations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.CODE");
|
|
|
|
|
|
|
|
encodeULEB128(wasm::WASM_SEC_CODE, getStream());
|
2017-06-07 03:15:05 +08:00
|
|
|
encodeULEB128(CodeRelocations.size(), getStream());
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
writeRelocations(CodeRelocations, 0);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
void WasmObjectWriter::writeDataRelocSection(uint64_t DataSectionHeaderSize) {
|
2017-06-03 10:01:24 +08:00
|
|
|
// See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
|
|
|
|
// for descriptions of the reloc sections.
|
|
|
|
|
|
|
|
if (DataRelocations.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_CUSTOM, "reloc.DATA");
|
|
|
|
|
|
|
|
encodeULEB128(wasm::WASM_SEC_DATA, getStream());
|
|
|
|
encodeULEB128(DataRelocations.size(), getStream());
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
writeRelocations(DataRelocations, DataSectionHeaderSize);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeLinkingMetaDataSection(
|
2017-06-20 12:04:59 +08:00
|
|
|
ArrayRef<StringRef> WeakSymbols, bool HasStackPointer,
|
|
|
|
uint32_t StackPointerGlobal) {
|
|
|
|
if (!HasStackPointer && WeakSymbols.empty())
|
2017-06-03 10:01:24 +08:00
|
|
|
return;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
|
2017-06-20 12:04:59 +08:00
|
|
|
SectionBookkeeping SubSection;
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
if (HasStackPointer) {
|
|
|
|
startSection(SubSection, wasm::WASM_STACK_POINTER);
|
|
|
|
encodeULEB128(StackPointerGlobal, getStream()); // id
|
|
|
|
endSection(SubSection);
|
|
|
|
}
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
if (WeakSymbols.size() != 0) {
|
|
|
|
startSection(SubSection, wasm::WASM_SYMBOL_INFO);
|
|
|
|
encodeULEB128(WeakSymbols.size(), getStream());
|
|
|
|
for (const StringRef Export: WeakSymbols) {
|
|
|
|
writeString(Export);
|
|
|
|
encodeULEB128(wasm::WASM_SYMBOL_FLAG_WEAK, getStream());
|
|
|
|
}
|
|
|
|
endSection(SubSection);
|
|
|
|
}
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
void WasmObjectWriter::writeObject(MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout) {
|
2017-06-20 12:04:59 +08:00
|
|
|
DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
|
2017-02-25 07:46:05 +08:00
|
|
|
MCContext &Ctx = Asm.getContext();
|
2017-03-17 04:49:48 +08:00
|
|
|
wasm::ValType PtrType = is64Bit() ? wasm::ValType::I64 : wasm::ValType::I32;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
// Collect information from the available symbols.
|
|
|
|
SmallVector<WasmFunctionType, 4> FunctionTypes;
|
|
|
|
SmallVector<WasmFunction, 4> Functions;
|
|
|
|
SmallVector<uint32_t, 4> TableElems;
|
|
|
|
SmallVector<WasmGlobal, 4> Globals;
|
|
|
|
SmallVector<WasmImport, 4> Imports;
|
|
|
|
SmallVector<WasmExport, 4> Exports;
|
2017-06-20 12:04:59 +08:00
|
|
|
SmallVector<StringRef, 4> WeakSymbols;
|
2017-02-25 07:18:00 +08:00
|
|
|
SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
|
|
|
|
unsigned NumFuncImports = 0;
|
|
|
|
unsigned NumGlobalImports = 0;
|
|
|
|
SmallVector<char, 0> DataBytes;
|
2017-03-31 07:58:19 +08:00
|
|
|
uint32_t StackPointerGlobal = 0;
|
|
|
|
bool HasStackPointer = false;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
// Populate the IsAddressTaken set.
|
2017-06-20 12:04:59 +08:00
|
|
|
for (const WasmRelocationEntry &RelEntry : CodeRelocations) {
|
2017-02-25 07:18:00 +08:00
|
|
|
switch (RelEntry.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
|
|
|
|
IsAddressTaken.insert(RelEntry.Symbol);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 12:04:59 +08:00
|
|
|
for (const WasmRelocationEntry &RelEntry : DataRelocations) {
|
2017-02-25 07:18:00 +08:00
|
|
|
switch (RelEntry.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
|
|
|
|
IsAddressTaken.insert(RelEntry.Symbol);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the Imports set.
|
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
const auto &WS = static_cast<const MCSymbolWasm &>(S);
|
2017-03-17 04:49:48 +08:00
|
|
|
int32_t Type;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
if (WS.isFunction()) {
|
|
|
|
// Prepare the function's type, if we haven't seen it yet.
|
|
|
|
WasmFunctionType F;
|
|
|
|
F.Returns = WS.getReturns();
|
|
|
|
F.Params = WS.getParams();
|
|
|
|
auto Pair =
|
|
|
|
FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
|
|
|
|
if (Pair.second)
|
|
|
|
FunctionTypes.push_back(F);
|
|
|
|
|
|
|
|
Type = Pair.first->second;
|
|
|
|
} else {
|
2017-03-17 04:49:48 +08:00
|
|
|
Type = int32_t(PtrType);
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the symbol is not defined in this translation unit, import it.
|
|
|
|
if (!WS.isTemporary() && !WS.isDefined(/*SetUsed=*/false)) {
|
|
|
|
WasmImport Import;
|
|
|
|
Import.ModuleName = WS.getModuleName();
|
|
|
|
Import.FieldName = WS.getName();
|
|
|
|
|
|
|
|
if (WS.isFunction()) {
|
|
|
|
Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
|
|
|
|
Import.Type = Type;
|
|
|
|
SymbolIndices[&WS] = NumFuncImports;
|
|
|
|
++NumFuncImports;
|
|
|
|
} else {
|
|
|
|
Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
|
|
|
|
Import.Type = Type;
|
|
|
|
SymbolIndices[&WS] = NumGlobalImports;
|
|
|
|
++NumGlobalImports;
|
|
|
|
}
|
|
|
|
|
|
|
|
Imports.push_back(Import);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-25 07:46:05 +08:00
|
|
|
// In the special .global_variables section, we've encoded global
|
|
|
|
// variables used by the function. Translate them into the Globals
|
|
|
|
// list.
|
|
|
|
MCSectionWasm *GlobalVars = Ctx.getWasmSection(".global_variables", 0, 0);
|
|
|
|
if (!GlobalVars->getFragmentList().empty()) {
|
|
|
|
if (GlobalVars->getFragmentList().size() != 1)
|
|
|
|
report_fatal_error("only one .global_variables fragment supported");
|
|
|
|
const MCFragment &Frag = *GlobalVars->begin();
|
|
|
|
if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
|
|
|
|
report_fatal_error("only data supported in .global_variables");
|
|
|
|
const MCDataFragment &DataFrag = cast<MCDataFragment>(Frag);
|
|
|
|
if (!DataFrag.getFixups().empty())
|
|
|
|
report_fatal_error("fixups not supported in .global_variables");
|
|
|
|
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
|
2017-03-31 07:58:19 +08:00
|
|
|
for (const uint8_t *p = (const uint8_t *)Contents.data(),
|
|
|
|
*end = (const uint8_t *)Contents.data() + Contents.size();
|
|
|
|
p != end; ) {
|
2017-02-25 07:46:05 +08:00
|
|
|
WasmGlobal G;
|
2017-03-31 07:58:19 +08:00
|
|
|
if (end - p < 3)
|
|
|
|
report_fatal_error("truncated global variable encoding");
|
|
|
|
G.Type = wasm::ValType(int8_t(*p++));
|
|
|
|
G.IsMutable = bool(*p++);
|
|
|
|
G.HasImport = bool(*p++);
|
|
|
|
if (G.HasImport) {
|
|
|
|
G.InitialValue = 0;
|
|
|
|
|
|
|
|
WasmImport Import;
|
|
|
|
Import.ModuleName = (const char *)p;
|
|
|
|
const uint8_t *nul = (const uint8_t *)memchr(p, '\0', end - p);
|
|
|
|
if (!nul)
|
|
|
|
report_fatal_error("global module name must be nul-terminated");
|
|
|
|
p = nul + 1;
|
|
|
|
nul = (const uint8_t *)memchr(p, '\0', end - p);
|
|
|
|
if (!nul)
|
|
|
|
report_fatal_error("global base name must be nul-terminated");
|
|
|
|
Import.FieldName = (const char *)p;
|
|
|
|
p = nul + 1;
|
|
|
|
|
|
|
|
Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
|
|
|
|
Import.Type = int32_t(G.Type);
|
|
|
|
|
|
|
|
G.ImportIndex = NumGlobalImports;
|
|
|
|
++NumGlobalImports;
|
|
|
|
|
|
|
|
Imports.push_back(Import);
|
|
|
|
} else {
|
|
|
|
unsigned n;
|
|
|
|
G.InitialValue = decodeSLEB128(p, &n);
|
|
|
|
G.ImportIndex = 0;
|
2017-03-31 18:45:35 +08:00
|
|
|
if ((ptrdiff_t)n > end - p)
|
2017-03-31 07:58:19 +08:00
|
|
|
report_fatal_error("global initial value must be valid SLEB128");
|
|
|
|
p += n;
|
|
|
|
}
|
2017-02-25 07:46:05 +08:00
|
|
|
Globals.push_back(G);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 07:58:19 +08:00
|
|
|
// In the special .stack_pointer section, we've encoded the stack pointer
|
|
|
|
// index.
|
|
|
|
MCSectionWasm *StackPtr = Ctx.getWasmSection(".stack_pointer", 0, 0);
|
|
|
|
if (!StackPtr->getFragmentList().empty()) {
|
|
|
|
if (StackPtr->getFragmentList().size() != 1)
|
|
|
|
report_fatal_error("only one .stack_pointer fragment supported");
|
|
|
|
const MCFragment &Frag = *StackPtr->begin();
|
|
|
|
if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
|
|
|
|
report_fatal_error("only data supported in .stack_pointer");
|
|
|
|
const MCDataFragment &DataFrag = cast<MCDataFragment>(Frag);
|
|
|
|
if (!DataFrag.getFixups().empty())
|
|
|
|
report_fatal_error("fixups not supported in .stack_pointer");
|
|
|
|
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
|
|
|
|
if (Contents.size() != 4)
|
|
|
|
report_fatal_error("only one entry supported in .stack_pointer");
|
|
|
|
HasStackPointer = true;
|
|
|
|
StackPointerGlobal = NumGlobalImports + *(const int32_t *)Contents.data();
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
// Handle regular defined and undefined symbols.
|
2017-02-25 07:18:00 +08:00
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
// Ignore unnamed temporary symbols, which aren't ever exported, imported,
|
|
|
|
// or used in relocations.
|
|
|
|
if (S.isTemporary() && S.getName().empty())
|
|
|
|
continue;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
|
|
|
// Variable references (weak references) are handled in a second pass
|
|
|
|
if (S.isVariable())
|
|
|
|
continue;
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
const auto &WS = static_cast<const MCSymbolWasm &>(S);
|
2017-06-20 12:04:59 +08:00
|
|
|
DEBUG(dbgs() << "MCSymbol: '" << S << "'"
|
|
|
|
<< " isDefined=" << S.isDefined() << " isExternal="
|
|
|
|
<< S.isExternal() << " isTemporary=" << S.isTemporary()
|
|
|
|
<< " isFunction=" << WS.isFunction()
|
|
|
|
<< " isWeak=" << WS.isWeak()
|
|
|
|
<< " isVariable=" << WS.isVariable() << "\n");
|
|
|
|
|
|
|
|
if (WS.isWeak())
|
|
|
|
WeakSymbols.push_back(WS.getName());
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
unsigned Index;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
|
|
|
//<< " function=" << S.isFunction()
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
if (WS.isFunction()) {
|
|
|
|
// Prepare the function's type, if we haven't seen it yet.
|
|
|
|
WasmFunctionType F;
|
|
|
|
F.Returns = WS.getReturns();
|
|
|
|
F.Params = WS.getParams();
|
|
|
|
auto Pair =
|
|
|
|
FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
|
|
|
|
if (Pair.second)
|
|
|
|
FunctionTypes.push_back(F);
|
|
|
|
|
2017-03-17 04:49:48 +08:00
|
|
|
int32_t Type = Pair.first->second;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
if (WS.isDefined(/*SetUsed=*/false)) {
|
2017-06-20 12:04:59 +08:00
|
|
|
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");
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// A definition. Take the next available index.
|
|
|
|
Index = NumFuncImports + Functions.size();
|
|
|
|
|
|
|
|
// Prepare the function.
|
|
|
|
WasmFunction Func;
|
|
|
|
Func.Type = Type;
|
|
|
|
Func.Sym = &WS;
|
|
|
|
SymbolIndices[&WS] = Index;
|
|
|
|
Functions.push_back(Func);
|
|
|
|
} else {
|
2017-06-20 12:04:59 +08:00
|
|
|
// Should be no such thing as weak undefined symbol
|
|
|
|
assert(!WS.isVariable());
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// An import; the index was assigned above.
|
|
|
|
Index = SymbolIndices.find(&WS)->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If needed, prepare the function to be called indirectly.
|
2017-06-13 07:52:44 +08:00
|
|
|
if (IsAddressTaken.count(&WS)) {
|
|
|
|
IndirectSymbolIndices[&WS] = TableElems.size();
|
2017-02-25 07:18:00 +08:00
|
|
|
TableElems.push_back(Index);
|
2017-06-13 07:52:44 +08:00
|
|
|
}
|
2017-02-25 07:18:00 +08:00
|
|
|
} else {
|
2017-06-02 09:05:24 +08:00
|
|
|
if (WS.isTemporary() && !WS.getSize())
|
|
|
|
continue;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
if (WS.isDefined(/*SetUsed=*/false)) {
|
2017-06-02 09:05:24 +08:00
|
|
|
if (WS.getOffset() != 0)
|
|
|
|
report_fatal_error("data sections must contain one variable each: " +
|
|
|
|
WS.getName());
|
|
|
|
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");
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
MCSectionWasm &DataSection =
|
|
|
|
static_cast<MCSectionWasm &>(WS.getSection());
|
|
|
|
|
|
|
|
if (uint64_t(Size) != Layout.getSectionFileSize(&DataSection))
|
|
|
|
report_fatal_error("data sections must contain at most one variable");
|
|
|
|
|
|
|
|
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
|
|
|
|
|
|
|
|
DataSection.setSectionOffset(DataBytes.size());
|
|
|
|
|
|
|
|
for (MCSection::iterator I = DataSection.begin(), E = DataSection.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const MCFragment &Frag = *I;
|
|
|
|
if (Frag.hasInstructions())
|
|
|
|
report_fatal_error("only data supported in data sections");
|
|
|
|
|
|
|
|
if (const MCAlignFragment *Align = dyn_cast<MCAlignFragment>(&Frag)) {
|
|
|
|
if (Align->getValueSize() != 1)
|
|
|
|
report_fatal_error("only byte values supported for alignment");
|
|
|
|
// If nops are requested, use zeros, as this is the data section.
|
|
|
|
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 (const MCFillFragment *Fill =
|
|
|
|
dyn_cast<MCFillFragment>(&Frag)) {
|
|
|
|
DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
|
|
|
|
} else {
|
|
|
|
const MCDataFragment &DataFrag = cast<MCDataFragment>(Frag);
|
|
|
|
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
|
|
|
|
|
|
|
|
DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 05:08:07 +08:00
|
|
|
// For each global, prepare a corresponding wasm global holding its
|
|
|
|
// address. For externals these will also be named exports.
|
|
|
|
Index = NumGlobalImports + Globals.size();
|
|
|
|
|
|
|
|
WasmGlobal Global;
|
|
|
|
Global.Type = PtrType;
|
|
|
|
Global.IsMutable = false;
|
|
|
|
Global.HasImport = false;
|
|
|
|
Global.InitialValue = DataSection.getSectionOffset();
|
|
|
|
Global.ImportIndex = 0;
|
|
|
|
SymbolIndices[&WS] = Index;
|
|
|
|
Globals.push_back(Global);
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the symbol is visible outside this translation unit, export it.
|
2017-06-20 12:04:59 +08:00
|
|
|
if (WS.isExternal() && WS.isDefined(/*SetUsed=*/false)) {
|
2017-02-25 07:18:00 +08:00
|
|
|
WasmExport Export;
|
|
|
|
Export.FieldName = WS.getName();
|
|
|
|
Export.Index = Index;
|
|
|
|
if (WS.isFunction())
|
|
|
|
Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
|
|
|
|
else
|
|
|
|
Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
|
|
|
|
Exports.push_back(Export);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:04:59 +08:00
|
|
|
// Handle weak aliases
|
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
if (!S.isVariable())
|
|
|
|
continue;
|
|
|
|
assert(S.isExternal());
|
|
|
|
assert(S.isDefined(/*SetUsed=*/false));
|
|
|
|
|
|
|
|
const auto &WS = static_cast<const MCSymbolWasm &>(S);
|
|
|
|
|
|
|
|
// Find the target symbol of this weak alias
|
|
|
|
const MCExpr *Expr = WS.getVariableValue();
|
|
|
|
auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
|
|
|
|
const MCSymbolWasm *ResolvedSym = cast<MCSymbolWasm>(&Inner->getSymbol());
|
|
|
|
uint32_t Index = SymbolIndices.find(ResolvedSym)->second;
|
|
|
|
DEBUG(dbgs() << "Weak alias: '" << WS << "' -> '" << ResolvedSym << "' = " << Index << "\n");
|
|
|
|
SymbolIndices[&WS] = Index;
|
|
|
|
|
|
|
|
WasmExport Export;
|
|
|
|
Export.FieldName = WS.getName();
|
|
|
|
Export.Index = Index;
|
|
|
|
if (WS.isFunction())
|
|
|
|
Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
|
|
|
|
else
|
|
|
|
Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
|
|
|
|
WeakSymbols.push_back(Export.FieldName);
|
|
|
|
Exports.push_back(Export);
|
|
|
|
}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// Add types for indirect function calls.
|
2017-06-07 03:15:05 +08:00
|
|
|
for (const WasmRelocationEntry &Fixup : CodeRelocations) {
|
|
|
|
if (Fixup.Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
|
|
|
|
continue;
|
2017-03-31 07:58:19 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
WasmFunctionType F;
|
|
|
|
F.Returns = Fixup.Symbol->getReturns();
|
|
|
|
F.Params = Fixup.Symbol->getParams();
|
|
|
|
auto Pair =
|
|
|
|
FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
|
|
|
|
if (Pair.second)
|
|
|
|
FunctionTypes.push_back(F);
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
TypeIndices[Fixup.Symbol] = Pair.first->second;
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
// Write out the Wasm header.
|
|
|
|
writeHeader(Asm);
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
writeTypeSection(FunctionTypes);
|
|
|
|
writeImportSection(Imports);
|
|
|
|
writeFunctionSection(Functions);
|
2017-06-13 07:52:44 +08:00
|
|
|
writeTableSection(TableElems.size());
|
2017-06-03 10:01:24 +08:00
|
|
|
writeMemorySection(DataBytes);
|
|
|
|
writeGlobalSection(Globals);
|
|
|
|
writeExportSection(Exports);
|
|
|
|
// TODO: Start Section
|
|
|
|
writeElemSection(TableElems);
|
2017-06-07 03:15:05 +08:00
|
|
|
writeCodeSection(Asm, Layout, Functions);
|
|
|
|
uint64_t DataSectionHeaderSize = writeDataSection(DataBytes);
|
2017-06-03 10:01:24 +08:00
|
|
|
writeNameSection(Functions, Imports, NumFuncImports);
|
2017-06-07 03:15:05 +08:00
|
|
|
writeCodeRelocSection();
|
|
|
|
writeDataRelocSection(DataSectionHeaderSize);
|
2017-06-20 12:04:59 +08:00
|
|
|
writeLinkingMetaDataSection(WeakSymbols, HasStackPointer, StackPointerGlobal);
|
2017-03-31 07:58:19 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// TODO: Translate the .comment section to the output.
|
|
|
|
// TODO: Translate debug sections to the output.
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MCObjectWriter *llvm::createWasmObjectWriter(MCWasmObjectTargetWriter *MOTW,
|
|
|
|
raw_pwrite_stream &OS) {
|
|
|
|
return new WasmObjectWriter(MOTW, OS);
|
|
|
|
}
|