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/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"
|
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;
|
|
|
|
|
2017-07-07 10:01:29 +08:00
|
|
|
#define DEBUG_TYPE "mc"
|
2017-02-22 09:23:18 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2018-01-20 02:57:01 +08:00
|
|
|
// Went we ceate the indirect function table we start at 1, so that there is
|
|
|
|
// and emtpy slot at 0 and therefore calling a null function pointer will trap.
|
|
|
|
static const uint32_t kInitialTableOffset = 1;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-15 07:07:53 +08:00
|
|
|
// A wasm data segment. A wasm binary contains only a single data section
|
|
|
|
// but that can contain many segments, each with their own virtual location
|
|
|
|
// in memory. Each MCSection data created by llvm is modeled as its own
|
|
|
|
// wasm data segment.
|
|
|
|
struct WasmDataSegment {
|
|
|
|
MCSectionWasm *Section;
|
2017-09-21 03:03:35 +08:00
|
|
|
StringRef Name;
|
2017-09-15 07:07:53 +08:00
|
|
|
uint32_t Offset;
|
2017-09-30 00:50:08 +08:00
|
|
|
uint32_t Alignment;
|
|
|
|
uint32_t Flags;
|
2017-09-15 07:07:53 +08:00
|
|
|
SmallVector<char, 4> Data;
|
|
|
|
};
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
// A wasm function to be written into the function section.
|
|
|
|
struct WasmFunction {
|
|
|
|
int32_t Type;
|
|
|
|
const MCSymbolWasm *Sym;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A wasm global to be written into the global section.
|
|
|
|
struct WasmGlobal {
|
2018-02-01 03:50:14 +08:00
|
|
|
wasm::WasmGlobalType Type;
|
2017-06-03 10:01:24 +08:00
|
|
|
uint64_t InitialValue;
|
|
|
|
};
|
|
|
|
|
2018-01-10 07:43:14 +08:00
|
|
|
// Information about a single item which is part of a COMDAT. For each data
|
|
|
|
// segment or function which is in the COMDAT, there is a corresponding
|
|
|
|
// WasmComdatEntry.
|
|
|
|
struct WasmComdatEntry {
|
|
|
|
unsigned Kind;
|
|
|
|
uint32_t Index;
|
|
|
|
};
|
|
|
|
|
2017-06-07 00:38:59 +08:00
|
|
|
// Information about a single relocation.
|
|
|
|
struct WasmRelocationEntry {
|
2017-06-22 07:46:41 +08:00
|
|
|
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.
|
|
|
|
const MCSectionWasm *FixupSection;// The section the relocation is targeting.
|
2017-06-07 00:38:59 +08:00
|
|
|
|
|
|
|
WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
|
|
|
|
int64_t Addend, unsigned Type,
|
2017-06-22 07:46:41 +08:00
|
|
|
const MCSectionWasm *FixupSection)
|
2017-06-07 00:38:59 +08:00
|
|
|
: Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
|
|
|
|
FixupSection(FixupSection) {}
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
bool hasAddend() const {
|
|
|
|
switch (Type) {
|
2017-09-02 01:32:01 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
2017-06-07 03:15:05 +08:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:38:59 +08:00
|
|
|
void print(raw_ostream &Out) const {
|
2017-07-06 04:25:08 +08:00
|
|
|
Out << "Off=" << Offset << ", Sym=" << *Symbol << ", Addend=" << Addend
|
2017-09-16 04:54:59 +08:00
|
|
|
<< ", Type=" << Type
|
|
|
|
<< ", FixupSection=" << FixupSection->getSectionName();
|
2017-06-07 00:38:59 +08:00
|
|
|
}
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-10-15 22:32:27 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-06-20 12:04:59 +08:00
|
|
|
LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
|
|
|
|
#endif
|
2017-06-07 00:38:59 +08:00
|
|
|
};
|
|
|
|
|
2017-06-20 13:05:10 +08:00
|
|
|
#if !defined(NDEBUG)
|
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-06-20 13:05:10 +08:00
|
|
|
#endif
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-02-22 09:23:18 +08:00
|
|
|
class WasmObjectWriter : public MCObjectWriter {
|
|
|
|
/// 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).
|
2018-02-01 03:28:47 +08:00
|
|
|
DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
|
2018-02-23 13:08:34 +08:00
|
|
|
// Maps function/global symbols to the (shared) Symbol index space.
|
2017-06-07 03:15:05 +08:00
|
|
|
DenseMap<const MCSymbolWasm *, uint32_t> SymbolIndices;
|
2018-02-23 13:08:34 +08:00
|
|
|
// Maps function/global symbols to the function/global Wasm index space.
|
|
|
|
DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
|
|
|
|
// Maps data symbols to the Wasm segment and offset/size with the segment.
|
|
|
|
DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
|
2017-06-07 03:15:05 +08:00
|
|
|
|
|
|
|
DenseMap<WasmFunctionType, int32_t, WasmFunctionTypeDenseMapInfo>
|
|
|
|
FunctionTypeIndices;
|
2017-07-07 10:01:29 +08:00
|
|
|
SmallVector<WasmFunctionType, 4> FunctionTypes;
|
2017-09-15 07:07:53 +08:00
|
|
|
SmallVector<WasmGlobal, 4> Globals;
|
2018-02-23 13:08:34 +08:00
|
|
|
SmallVector<WasmDataSegment, 4> DataSegments;
|
2018-01-18 03:28:43 +08:00
|
|
|
unsigned NumFunctionImports = 0;
|
2017-09-15 07:07:53 +08:00
|
|
|
unsigned NumGlobalImports = 0;
|
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:
|
2017-10-10 09:15:10 +08:00
|
|
|
WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS)
|
|
|
|
: MCObjectWriter(OS, /*IsLittleEndian=*/true),
|
|
|
|
TargetObjectWriter(std::move(MOTW)) {}
|
2017-02-22 09:23:18 +08:00
|
|
|
|
|
|
|
~WasmObjectWriter() override;
|
|
|
|
|
2018-01-16 01:06:23 +08:00
|
|
|
private:
|
2017-06-07 03:15:05 +08:00
|
|
|
void reset() override {
|
|
|
|
CodeRelocations.clear();
|
|
|
|
DataRelocations.clear();
|
|
|
|
TypeIndices.clear();
|
|
|
|
SymbolIndices.clear();
|
2018-02-23 13:08:34 +08:00
|
|
|
WasmIndices.clear();
|
2018-02-01 03:28:47 +08:00
|
|
|
TableIndices.clear();
|
2018-02-23 13:08:34 +08:00
|
|
|
DataLocations.clear();
|
2017-06-07 03:15:05 +08:00
|
|
|
FunctionTypeIndices.clear();
|
2017-07-07 10:01:29 +08:00
|
|
|
FunctionTypes.clear();
|
2017-09-15 07:07:53 +08:00
|
|
|
Globals.clear();
|
2018-02-23 13:08:34 +08:00
|
|
|
DataSegments.clear();
|
2017-06-07 03:15:05 +08:00
|
|
|
MCObjectWriter::reset();
|
2018-01-18 03:28:43 +08:00
|
|
|
NumFunctionImports = 0;
|
2017-09-15 07:07:53 +08:00
|
|
|
NumGlobalImports = 0;
|
2017-06-07 03:15:05 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2017-07-12 07:56:10 +08:00
|
|
|
MCValue Target, uint64_t &FixedValue) override;
|
2017-02-22 09:23:18 +08:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2017-09-16 03:50:44 +08:00
|
|
|
void writeTypeSection(ArrayRef<WasmFunctionType> FunctionTypes);
|
2018-02-13 06:41:29 +08:00
|
|
|
void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint32_t DataSize,
|
2017-12-12 07:03:38 +08:00
|
|
|
uint32_t NumElements);
|
2017-09-16 03:50:44 +08:00
|
|
|
void writeFunctionSection(ArrayRef<WasmFunction> Functions);
|
2017-09-15 07:07:53 +08:00
|
|
|
void writeGlobalSection();
|
2018-02-13 06:41:29 +08:00
|
|
|
void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
|
2017-09-16 03:50:44 +08:00
|
|
|
void writeElemSection(ArrayRef<uint32_t> TableElems);
|
2017-06-03 10:01:24 +08:00
|
|
|
void writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
|
2017-09-16 03:50:44 +08:00
|
|
|
ArrayRef<WasmFunction> Functions);
|
2018-02-23 13:08:34 +08:00
|
|
|
void writeDataSection();
|
2017-06-07 03:15:05 +08:00
|
|
|
void writeCodeRelocSection();
|
2017-09-15 07:07:53 +08:00
|
|
|
void writeDataRelocSection();
|
2017-09-21 05:17:04 +08:00
|
|
|
void writeLinkingMetaDataSection(
|
2018-02-28 07:57:37 +08:00
|
|
|
ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
|
2018-01-10 07:43:14 +08:00
|
|
|
ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
|
2018-02-23 13:08:34 +08:00
|
|
|
const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
|
2017-06-07 03:15:05 +08:00
|
|
|
|
2017-09-15 07:07:53 +08:00
|
|
|
uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
|
2017-06-07 03:15:05 +08:00
|
|
|
void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
|
|
|
|
uint64_t ContentsOffset);
|
|
|
|
|
2017-09-15 07:07:53 +08:00
|
|
|
void writeRelocations(ArrayRef<WasmRelocationEntry> Relocations);
|
2017-06-07 03:15:05 +08:00
|
|
|
uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
|
2017-07-07 10:01:29 +08:00
|
|
|
uint32_t getFunctionType(const MCSymbolWasm& Symbol);
|
|
|
|
uint32_t registerFunctionType(const MCSymbolWasm& Symbol);
|
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
|
|
|
// 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
|
|
|
|
|
|
|
// Write the final section size to the payload_len field, which follows
|
|
|
|
// the section id byte.
|
|
|
|
uint8_t Buffer[16];
|
2017-09-16 04:34:47 +08:00
|
|
|
unsigned SizeLen = encodeULEB128(Size, Buffer, 5);
|
2017-02-25 07:18:00 +08:00
|
|
|
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,
|
2017-07-12 07:56:10 +08:00
|
|
|
uint64_t &FixedValue) {
|
|
|
|
MCAsmBackend &Backend = Asm.getBackend();
|
|
|
|
bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
|
|
|
|
MCFixupKindInfo::FKF_IsPCRel;
|
2017-06-22 07:46:41 +08:00
|
|
|
const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
|
2017-02-25 07:18:00 +08:00
|
|
|
uint64_t C = Target.getConstant();
|
|
|
|
uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
|
|
|
|
MCContext &Ctx = Asm.getContext();
|
|
|
|
|
2017-12-15 08:17:10 +08:00
|
|
|
// The .init_array isn't translated as data, so don't do relocations in it.
|
|
|
|
if (FixupSection.getSectionName().startswith(".init_array"))
|
|
|
|
return;
|
|
|
|
|
2018-02-17 02:06:05 +08:00
|
|
|
// TODO(sbc): Add support for debug sections.
|
|
|
|
if (FixupSection.getKind().isMetadata())
|
|
|
|
return;
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (SymA && SymA->isVariable()) {
|
|
|
|
const MCExpr *Expr = SymA->getVariableValue();
|
2017-07-11 10:21:57 +08:00
|
|
|
const auto *Inner = cast<MCSymbolRefExpr>(Expr);
|
|
|
|
if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
|
|
|
|
llvm_unreachable("weakref used in reloc not yet implemented");
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2017-07-11 10:21:57 +08:00
|
|
|
if (SymA)
|
|
|
|
SymA->setUsedInReloc();
|
2017-02-25 07:18:00 +08:00
|
|
|
|
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
|
|
|
|
2018-02-17 02:06:05 +08:00
|
|
|
// Relocation other than R_WEBASSEMBLY_TYPE_INDEX_LEB are currently required
|
|
|
|
// to be against a named symbol.
|
|
|
|
// TODO(sbc): Add support for relocations against unnamed temporaries such
|
|
|
|
// as those generated by llvm's `blockaddress`.
|
|
|
|
// See: test/MC/WebAssembly/blockaddress.ll
|
|
|
|
if (SymA->getName().empty() && Type != wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB)
|
|
|
|
report_fatal_error("relocations against un-named temporaries are not yet "
|
|
|
|
"supported by wasm");
|
|
|
|
|
2017-10-21 05:28:38 +08:00
|
|
|
if (FixupSection.isWasmData())
|
2017-02-25 07:18:00 +08:00
|
|
|
DataRelocations.push_back(Rec);
|
2017-10-21 05:28:38 +08:00
|
|
|
else if (FixupSection.getKind().isText())
|
|
|
|
CodeRelocations.push_back(Rec);
|
2018-02-17 02:06:05 +08:00
|
|
|
else
|
2017-10-21 05:28:38 +08:00
|
|
|
llvm_unreachable("unexpected section type");
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
2017-09-16 04:34:47 +08:00
|
|
|
unsigned SizeLen = encodeULEB128(X, Buffer, 5);
|
2017-02-25 07:18:00 +08:00
|
|
|
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];
|
2017-09-16 04:34:47 +08:00
|
|
|
unsigned SizeLen = encodeSLEB128(X, Buffer, 5);
|
2017-02-25 07:18:00 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-16 03:22:01 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
// Compute a value to write into the code at the location covered
|
2018-01-23 09:23:17 +08:00
|
|
|
// by RelEntry. This value isn't used by the static linker; it just serves
|
|
|
|
// to make the object format more readable and more likely to be directly
|
|
|
|
// useable.
|
2017-09-15 07:07:53 +08:00
|
|
|
uint32_t
|
|
|
|
WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry) {
|
2018-01-23 09:23:17 +08:00
|
|
|
switch (RelEntry.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
2018-02-01 03:28:47 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: {
|
|
|
|
// Provisional value is table address of the resolved symbol itself
|
|
|
|
const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
|
|
|
|
assert(Sym->isFunction());
|
|
|
|
return TableIndices[Sym];
|
|
|
|
}
|
2018-01-23 09:23:17 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
2018-02-23 13:08:34 +08:00
|
|
|
// Provisional value is same as the index
|
2018-01-23 09:23:17 +08:00
|
|
|
return getRelocationIndexValue(RelEntry);
|
2018-02-23 13:08:34 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
|
|
|
// Provisional value is function/global Wasm index
|
|
|
|
if (!WasmIndices.count(RelEntry.Symbol))
|
|
|
|
report_fatal_error("symbol not found in wasm index space: " +
|
|
|
|
RelEntry.Symbol->getName());
|
|
|
|
return WasmIndices[RelEntry.Symbol];
|
2018-01-23 09:23:17 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB: {
|
2018-02-01 03:28:47 +08:00
|
|
|
// Provisional value is address of the global
|
2018-01-23 09:23:17 +08:00
|
|
|
const MCSymbolWasm *Sym = ResolveSymbol(*RelEntry.Symbol);
|
|
|
|
// For undefined symbols, use zero
|
|
|
|
if (!Sym->isDefined())
|
|
|
|
return 0;
|
2018-02-23 13:08:34 +08:00
|
|
|
const wasm::WasmDataReference &Ref = DataLocations[Sym];
|
|
|
|
const WasmDataSegment &Segment = DataSegments[Ref.Segment];
|
2018-01-23 09:23:17 +08:00
|
|
|
// Ignore overflow. LLVM allows address arithmetic to silently wrap.
|
2018-02-23 13:08:34 +08:00
|
|
|
return Segment.Offset + Ref.Offset + RelEntry.Addend;
|
2018-01-23 09:23:17 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("invalid relocation type");
|
|
|
|
}
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
2017-09-16 04:54:59 +08:00
|
|
|
static void addData(SmallVectorImpl<char> &DataBytes,
|
2017-09-30 00:50:08 +08:00
|
|
|
MCSectionWasm &DataSection) {
|
2017-09-16 04:54:59 +08:00
|
|
|
DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
|
|
|
|
|
2017-09-30 00:50:08 +08:00
|
|
|
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
|
|
|
|
|
2017-09-16 04:54:59 +08:00
|
|
|
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");
|
|
|
|
// 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 (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
|
2018-01-10 06:48:37 +08:00
|
|
|
int64_t Size;
|
|
|
|
if (!Fill->getSize().evaluateAsAbsolute(Size))
|
|
|
|
llvm_unreachable("The fill should be an assembler constant");
|
|
|
|
DataBytes.insert(DataBytes.end(), Size, Fill->getValue());
|
2017-09-16 04:54:59 +08:00
|
|
|
} else {
|
|
|
|
const auto &DataFrag = cast<MCDataFragment>(Frag);
|
|
|
|
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
|
|
|
|
|
|
|
|
DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
|
|
|
|
}
|
|
|
|
|
2018-01-23 09:23:17 +08:00
|
|
|
uint32_t
|
|
|
|
WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
|
|
|
|
if (RelEntry.Type == wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB) {
|
2017-06-20 12:04:59 +08:00
|
|
|
if (!TypeIndices.count(RelEntry.Symbol))
|
2017-07-07 10:01:29 +08:00
|
|
|
report_fatal_error("symbol not found in type index space: " +
|
2017-06-20 12:04:59 +08:00
|
|
|
RelEntry.Symbol->getName());
|
2017-06-07 03:15:05 +08:00
|
|
|
return TypeIndices[RelEntry.Symbol];
|
|
|
|
}
|
2018-01-23 09:23:17 +08:00
|
|
|
|
|
|
|
if (!SymbolIndices.count(RelEntry.Symbol))
|
2018-02-23 13:08:34 +08:00
|
|
|
report_fatal_error("symbol not found in symbol index space: " +
|
2018-01-23 09:23:17 +08:00
|
|
|
RelEntry.Symbol->getName());
|
|
|
|
return SymbolIndices[RelEntry.Symbol];
|
2017-06-07 03:15:05 +08:00
|
|
|
}
|
|
|
|
|
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");
|
2018-01-23 09:23:17 +08:00
|
|
|
uint32_t Value = getProvisionalValue(RelEntry);
|
|
|
|
|
2017-06-07 03:15:05 +08:00
|
|
|
switch (RelEntry.Type) {
|
|
|
|
case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
2017-06-17 07:59:10 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
2018-01-23 09:23:17 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
2017-02-25 07:18:00 +08:00
|
|
|
WritePatchableLEB(Stream, Value, Offset);
|
|
|
|
break;
|
2018-01-23 09:23:17 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
2017-02-25 07:18:00 +08:00
|
|
|
WriteI32(Stream, Value, Offset);
|
|
|
|
break;
|
2018-01-23 09:23:17 +08:00
|
|
|
case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
|
|
|
|
WritePatchableSLEB(Stream, Value, Offset);
|
|
|
|
break;
|
2017-02-25 07:18:00 +08:00
|
|
|
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(
|
2017-09-15 07:07:53 +08:00
|
|
|
ArrayRef<WasmRelocationEntry> Relocations) {
|
2017-06-07 03:15:05 +08:00
|
|
|
raw_pwrite_stream &Stream = getStream();
|
|
|
|
for (const WasmRelocationEntry& RelEntry : Relocations) {
|
2017-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
uint64_t Offset = RelEntry.Offset +
|
2017-09-15 07:07:53 +08:00
|
|
|
RelEntry.FixupSection->getSectionOffset();
|
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(
|
2017-09-16 03:50:44 +08:00
|
|
|
ArrayRef<WasmFunctionType> FunctionTypes) {
|
2017-06-03 10:01:24 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-13 06:41:29 +08:00
|
|
|
void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
|
2017-12-12 07:03:38 +08:00
|
|
|
uint32_t DataSize,
|
|
|
|
uint32_t NumElements) {
|
2017-06-03 10:01:24 +08:00
|
|
|
if (Imports.empty())
|
|
|
|
return;
|
|
|
|
|
2017-12-12 07:03:38 +08:00
|
|
|
uint32_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_IMPORT);
|
|
|
|
|
|
|
|
encodeULEB128(Imports.size(), getStream());
|
2018-02-13 06:41:29 +08:00
|
|
|
for (const wasm::WasmImport &Import : Imports) {
|
|
|
|
writeString(Import.Module);
|
|
|
|
writeString(Import.Field);
|
2017-06-03 10:01:24 +08:00
|
|
|
encodeULEB128(Import.Kind, getStream());
|
|
|
|
|
|
|
|
switch (Import.Kind) {
|
|
|
|
case wasm::WASM_EXTERNAL_FUNCTION:
|
2018-02-13 06:41:29 +08:00
|
|
|
encodeULEB128(Import.SigIndex, getStream());
|
2017-06-03 10:01:24 +08:00
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_GLOBAL:
|
2018-02-13 06:41:29 +08:00
|
|
|
encodeSLEB128(Import.Global.Type, getStream());
|
|
|
|
encodeULEB128(uint32_t(Import.Global.Mutable), getStream());
|
2017-06-03 10:01:24 +08:00
|
|
|
break;
|
2017-12-12 07:03:38 +08:00
|
|
|
case wasm::WASM_EXTERNAL_MEMORY:
|
|
|
|
encodeULEB128(0, getStream()); // flags
|
|
|
|
encodeULEB128(NumPages, getStream()); // initial
|
|
|
|
break;
|
|
|
|
case wasm::WASM_EXTERNAL_TABLE:
|
2018-02-13 06:41:29 +08:00
|
|
|
encodeSLEB128(Import.Table.ElemType, getStream());
|
2017-12-12 07:03:38 +08:00
|
|
|
encodeULEB128(0, getStream()); // flags
|
|
|
|
encodeULEB128(NumElements, getStream()); // initial
|
|
|
|
break;
|
2017-06-03 10:01:24 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unsupported import kind");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-09-16 03:50:44 +08:00
|
|
|
void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
|
2017-06-03 10:01:24 +08:00
|
|
|
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-09-15 07:07:53 +08:00
|
|
|
void WasmObjectWriter::writeGlobalSection() {
|
2017-06-03 10:01:24 +08:00
|
|
|
if (Globals.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_GLOBAL);
|
|
|
|
|
|
|
|
encodeULEB128(Globals.size(), getStream());
|
|
|
|
for (const WasmGlobal &Global : Globals) {
|
2018-02-01 03:50:14 +08:00
|
|
|
writeValueType(static_cast<wasm::ValType>(Global.Type.Type));
|
|
|
|
write8(Global.Type.Mutable);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2018-02-01 03:50:14 +08:00
|
|
|
write8(wasm::WASM_OPCODE_I32_CONST);
|
|
|
|
encodeSLEB128(Global.InitialValue, getStream());
|
2017-06-03 10:01:24 +08:00
|
|
|
write8(wasm::WASM_OPCODE_END);
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2018-02-13 06:41:29 +08:00
|
|
|
void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
|
2017-06-03 10:01:24 +08:00
|
|
|
if (Exports.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_EXPORT);
|
|
|
|
|
|
|
|
encodeULEB128(Exports.size(), getStream());
|
2018-02-13 06:41:29 +08:00
|
|
|
for (const wasm::WasmExport &Export : Exports) {
|
|
|
|
writeString(Export.Name);
|
2017-06-03 10:01:24 +08:00
|
|
|
encodeSLEB128(Export.Kind, getStream());
|
|
|
|
encodeULEB128(Export.Index, getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-09-16 03:50:44 +08:00
|
|
|
void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
|
2017-06-03 10:01:24 +08:00
|
|
|
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);
|
2018-01-20 02:57:01 +08:00
|
|
|
encodeSLEB128(kInitialTableOffset, getStream());
|
2017-06-03 10:01:24 +08:00
|
|
|
write8(wasm::WASM_OPCODE_END);
|
|
|
|
|
|
|
|
encodeULEB128(TableElems.size(), getStream());
|
|
|
|
for (uint32_t Elem : TableElems)
|
|
|
|
encodeULEB128(Elem, getStream());
|
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-09-16 03:50:44 +08:00
|
|
|
void WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
|
|
|
|
const MCAsmLayout &Layout,
|
|
|
|
ArrayRef<WasmFunction> Functions) {
|
2017-06-03 10:01:24 +08:00
|
|
|
if (Functions.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_CODE);
|
|
|
|
|
|
|
|
encodeULEB128(Functions.size(), getStream());
|
|
|
|
|
|
|
|
for (const WasmFunction &Func : Functions) {
|
2017-06-22 07:46:41 +08:00
|
|
|
auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
int64_t Size = 0;
|
|
|
|
if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
|
|
|
|
report_fatal_error(".size expression must be evaluatable");
|
|
|
|
|
|
|
|
encodeULEB128(Size, getStream());
|
2017-06-22 07:46:41 +08:00
|
|
|
FuncSection.setSectionOffset(getStream().tell() - Section.ContentsOffset);
|
2017-06-03 10:01:24 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
void WasmObjectWriter::writeDataSection() {
|
|
|
|
if (DataSegments.empty())
|
2017-09-15 07:07:53 +08:00
|
|
|
return;
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
SectionBookkeeping Section;
|
|
|
|
startSection(Section, wasm::WASM_SEC_DATA);
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
encodeULEB128(DataSegments.size(), getStream()); // count
|
2017-09-15 07:07:53 +08:00
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
for (const WasmDataSegment &Segment : DataSegments) {
|
2017-09-15 07:07:53 +08:00
|
|
|
encodeULEB128(0, getStream()); // memory index
|
|
|
|
write8(wasm::WASM_OPCODE_I32_CONST);
|
|
|
|
encodeSLEB128(Segment.Offset, getStream()); // offset
|
|
|
|
write8(wasm::WASM_OPCODE_END);
|
|
|
|
encodeULEB128(Segment.Data.size(), getStream()); // size
|
|
|
|
Segment.Section->setSectionOffset(getStream().tell() - Section.ContentsOffset);
|
|
|
|
writeBytes(Segment.Data); // data
|
|
|
|
}
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
// Apply fixups.
|
2017-09-15 07:07:53 +08:00
|
|
|
applyRelocations(DataRelocations, Section.ContentsOffset);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
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-09-15 07:07:53 +08:00
|
|
|
writeRelocations(CodeRelocations);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-09-15 07:07:53 +08:00
|
|
|
void WasmObjectWriter::writeDataRelocSection() {
|
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-09-15 07:07:53 +08:00
|
|
|
writeRelocations(DataRelocations);
|
2017-06-03 10:01:24 +08:00
|
|
|
|
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmObjectWriter::writeLinkingMetaDataSection(
|
2018-02-28 07:57:37 +08:00
|
|
|
ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
|
2018-01-10 07:43:14 +08:00
|
|
|
ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
|
2018-02-23 13:08:34 +08:00
|
|
|
const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
|
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
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
if (SymbolInfos.size() != 0) {
|
|
|
|
startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
|
|
|
|
encodeULEB128(SymbolInfos.size(), getStream());
|
|
|
|
for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
|
|
|
|
encodeULEB128(Sym.Kind, getStream());
|
|
|
|
encodeULEB128(Sym.Flags, getStream());
|
|
|
|
switch (Sym.Kind) {
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
|
|
|
|
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
encodeULEB128(Sym.ElementIndex, getStream());
|
|
|
|
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, getStream());
|
|
|
|
encodeULEB128(Sym.DataRef.Offset, getStream());
|
|
|
|
encodeULEB128(Sym.DataRef.Size, getStream());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unexpected kind");
|
|
|
|
}
|
2017-06-20 12:04:59 +08:00
|
|
|
}
|
|
|
|
endSection(SubSection);
|
|
|
|
}
|
2017-06-03 10:01:24 +08:00
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
if (DataSegments.size()) {
|
2017-09-30 00:50:08 +08:00
|
|
|
startSection(SubSection, wasm::WASM_SEGMENT_INFO);
|
2018-02-23 13:08:34 +08:00
|
|
|
encodeULEB128(DataSegments.size(), getStream());
|
|
|
|
for (const WasmDataSegment &Segment : DataSegments) {
|
2017-09-21 03:03:35 +08:00
|
|
|
writeString(Segment.Name);
|
2017-09-30 00:50:08 +08:00
|
|
|
encodeULEB128(Segment.Alignment, getStream());
|
|
|
|
encodeULEB128(Segment.Flags, getStream());
|
|
|
|
}
|
2017-09-21 03:03:35 +08:00
|
|
|
endSection(SubSection);
|
|
|
|
}
|
|
|
|
|
2017-12-15 08:17:10 +08:00
|
|
|
if (!InitFuncs.empty()) {
|
|
|
|
startSection(SubSection, wasm::WASM_INIT_FUNCS);
|
|
|
|
encodeULEB128(InitFuncs.size(), getStream());
|
|
|
|
for (auto &StartFunc : InitFuncs) {
|
|
|
|
encodeULEB128(StartFunc.first, getStream()); // priority
|
|
|
|
encodeULEB128(StartFunc.second, getStream()); // function index
|
|
|
|
}
|
|
|
|
endSection(SubSection);
|
|
|
|
}
|
|
|
|
|
2018-01-10 07:43:14 +08:00
|
|
|
if (Comdats.size()) {
|
|
|
|
startSection(SubSection, wasm::WASM_COMDAT_INFO);
|
|
|
|
encodeULEB128(Comdats.size(), getStream());
|
|
|
|
for (const auto &C : Comdats) {
|
|
|
|
writeString(C.first);
|
|
|
|
encodeULEB128(0, getStream()); // flags for future use
|
|
|
|
encodeULEB128(C.second.size(), getStream());
|
|
|
|
for (const WasmComdatEntry &Entry : C.second) {
|
|
|
|
encodeULEB128(Entry.Kind, getStream());
|
|
|
|
encodeULEB128(Entry.Index, getStream());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endSection(SubSection);
|
|
|
|
}
|
|
|
|
|
2017-06-03 10:01:24 +08:00
|
|
|
endSection(Section);
|
|
|
|
}
|
|
|
|
|
2017-07-07 10:01:29 +08:00
|
|
|
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;
|
2017-09-16 03:22:01 +08:00
|
|
|
const MCSymbolWasm* ResolvedSym = ResolveSymbol(Symbol);
|
|
|
|
F.Returns = ResolvedSym->getReturns();
|
|
|
|
F.Params = ResolvedSym->getParams();
|
2017-07-07 10:01:29 +08:00
|
|
|
|
|
|
|
auto Pair =
|
|
|
|
FunctionTypeIndices.insert(std::make_pair(F, FunctionTypes.size()));
|
|
|
|
if (Pair.second)
|
|
|
|
FunctionTypes.push_back(F);
|
|
|
|
TypeIndices[&Symbol] = Pair.first->second;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "registerFunctionType: " << Symbol << " new:" << Pair.second << "\n");
|
|
|
|
DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
|
|
|
|
return Pair.first->second;
|
|
|
|
}
|
|
|
|
|
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-02-25 07:18:00 +08:00
|
|
|
|
|
|
|
// Collect information from the available symbols.
|
|
|
|
SmallVector<WasmFunction, 4> Functions;
|
|
|
|
SmallVector<uint32_t, 4> TableElems;
|
2018-02-13 06:41:29 +08:00
|
|
|
SmallVector<wasm::WasmImport, 4> Imports;
|
|
|
|
SmallVector<wasm::WasmExport, 4> Exports;
|
2018-02-23 13:08:34 +08:00
|
|
|
SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
|
2017-12-15 08:17:10 +08:00
|
|
|
SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
|
2018-01-10 07:43:14 +08:00
|
|
|
std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
|
2017-09-15 07:07:53 +08:00
|
|
|
uint32_t DataSize = 0;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
2017-12-12 07:03:38 +08:00
|
|
|
// For now, always emit the memory import, 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.
|
|
|
|
MCSymbolWasm *MemorySym =
|
|
|
|
cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__linear_memory"));
|
2018-02-13 06:41:29 +08:00
|
|
|
wasm::WasmImport MemImport;
|
|
|
|
MemImport.Module = MemorySym->getModuleName();
|
|
|
|
MemImport.Field = MemorySym->getName();
|
2017-12-12 07:03:38 +08:00
|
|
|
MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
|
|
|
|
Imports.push_back(MemImport);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
MCSymbolWasm *TableSym =
|
|
|
|
cast<MCSymbolWasm>(Ctx.getOrCreateSymbol("__indirect_function_table"));
|
2018-02-13 06:41:29 +08:00
|
|
|
wasm::WasmImport TableImport;
|
|
|
|
TableImport.Module = TableSym->getModuleName();
|
|
|
|
TableImport.Field = TableSym->getName();
|
2017-12-12 07:03:38 +08:00
|
|
|
TableImport.Kind = wasm::WASM_EXTERNAL_TABLE;
|
2018-02-13 06:41:29 +08:00
|
|
|
TableImport.Table.ElemType = wasm::WASM_TYPE_ANYFUNC;
|
2017-12-12 07:03:38 +08:00
|
|
|
Imports.push_back(TableImport);
|
|
|
|
|
2018-03-01 01:19:48 +08:00
|
|
|
// Populate FunctionTypeIndices, and Imports and WasmIndices for undefined
|
|
|
|
// symbols. This must be done before populating WasmIndices for defined
|
|
|
|
// symbols.
|
2017-12-06 02:29:48 +08:00
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
const auto &WS = static_cast<const MCSymbolWasm &>(S);
|
|
|
|
|
|
|
|
// Register types for all functions, including those with private linkage
|
2018-01-18 03:28:43 +08:00
|
|
|
// (because wasm always needs a type signature).
|
2017-12-06 02:29:48 +08:00
|
|
|
if (WS.isFunction())
|
|
|
|
registerFunctionType(WS);
|
|
|
|
|
|
|
|
if (WS.isTemporary())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the symbol is not defined in this translation unit, import it.
|
2018-02-23 13:08:34 +08:00
|
|
|
if (!WS.isDefined() && !WS.isComdat()) {
|
2017-12-06 02:29:48 +08:00
|
|
|
if (WS.isFunction()) {
|
2018-02-23 13:08:34 +08:00
|
|
|
wasm::WasmImport Import;
|
|
|
|
Import.Module = WS.getModuleName();
|
|
|
|
Import.Field = WS.getName();
|
2017-12-06 02:29:48 +08:00
|
|
|
Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
|
2018-02-13 06:41:29 +08:00
|
|
|
Import.SigIndex = getFunctionType(WS);
|
2018-02-23 13:08:34 +08:00
|
|
|
Imports.push_back(Import);
|
|
|
|
WasmIndices[&WS] = NumFunctionImports++;
|
|
|
|
} else if (WS.isGlobal()) {
|
|
|
|
wasm::WasmImport Import;
|
|
|
|
Import.Module = WS.getModuleName();
|
|
|
|
Import.Field = WS.getName();
|
2017-12-06 02:29:48 +08:00
|
|
|
Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
|
2018-02-23 13:08:34 +08:00
|
|
|
Import.Global = WS.getGlobalType();
|
|
|
|
Imports.push_back(Import);
|
|
|
|
WasmIndices[&WS] = NumGlobalImports++;
|
2017-12-06 02:29:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:19:48 +08:00
|
|
|
// Populate DataSegments, which must be done before populating DataLocations.
|
2017-09-16 04:54:59 +08:00
|
|
|
for (MCSection &Sec : Asm) {
|
|
|
|
auto &Section = static_cast<MCSectionWasm &>(Sec);
|
2017-10-21 05:28:38 +08:00
|
|
|
if (!Section.isWasmData())
|
2017-09-16 04:54:59 +08:00
|
|
|
continue;
|
|
|
|
|
2017-12-15 08:17:10 +08:00
|
|
|
// .init_array sections are handled specially elsewhere.
|
|
|
|
if (cast<MCSectionWasm>(Sec).getSectionName().startswith(".init_array"))
|
|
|
|
continue;
|
|
|
|
|
2018-01-31 12:21:44 +08:00
|
|
|
uint32_t SegmentIndex = DataSegments.size();
|
2017-09-16 04:54:59 +08:00
|
|
|
DataSize = alignTo(DataSize, Section.getAlignment());
|
|
|
|
DataSegments.emplace_back();
|
|
|
|
WasmDataSegment &Segment = DataSegments.back();
|
2017-09-21 03:03:35 +08:00
|
|
|
Segment.Name = Section.getSectionName();
|
2017-09-16 04:54:59 +08:00
|
|
|
Segment.Offset = DataSize;
|
|
|
|
Segment.Section = &Section;
|
2017-09-30 00:50:08 +08:00
|
|
|
addData(Segment.Data, Section);
|
|
|
|
Segment.Alignment = Section.getAlignment();
|
|
|
|
Segment.Flags = 0;
|
2017-09-16 04:54:59 +08:00
|
|
|
DataSize += Segment.Data.size();
|
2018-02-23 13:08:34 +08:00
|
|
|
Section.setSegmentIndex(SegmentIndex);
|
2018-01-10 07:43:14 +08:00
|
|
|
|
|
|
|
if (const MCSymbolWasm *C = Section.getGroup()) {
|
|
|
|
Comdats[C->getName()].emplace_back(
|
2018-01-31 12:21:44 +08:00
|
|
|
WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
|
2018-01-10 07:43:14 +08:00
|
|
|
}
|
2017-09-16 04:54:59 +08:00
|
|
|
}
|
|
|
|
|
2018-03-01 01:19:48 +08:00
|
|
|
// Populate WasmIndices and DataLocations for defined 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
|
|
|
|
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 << "'"
|
2018-01-31 12:21:44 +08:00
|
|
|
<< " isDefined=" << S.isDefined()
|
|
|
|
<< " isExternal=" << S.isExternal()
|
|
|
|
<< " isTemporary=" << S.isTemporary()
|
2017-06-20 12:04:59 +08:00
|
|
|
<< " isFunction=" << WS.isFunction()
|
|
|
|
<< " isWeak=" << WS.isWeak()
|
2017-12-03 09:19:23 +08:00
|
|
|
<< " isHidden=" << WS.isHidden()
|
2017-06-20 12:04:59 +08:00
|
|
|
<< " isVariable=" << WS.isVariable() << "\n");
|
|
|
|
|
2017-07-07 10:01:29 +08:00
|
|
|
if (WS.isVariable())
|
|
|
|
continue;
|
2018-02-23 13:08:34 +08:00
|
|
|
if (WS.isComdat() && !WS.isDefined())
|
|
|
|
continue;
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-02-25 07:18:00 +08:00
|
|
|
if (WS.isFunction()) {
|
2018-02-23 13:08:34 +08:00
|
|
|
unsigned Index;
|
2018-01-12 07:59:16 +08:00
|
|
|
if (WS.isDefined()) {
|
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");
|
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
// A definition. Write out the function body.
|
2018-01-18 03:28:43 +08:00
|
|
|
Index = NumFunctionImports + Functions.size();
|
2017-02-25 07:18:00 +08:00
|
|
|
WasmFunction Func;
|
2017-07-07 10:01:29 +08:00
|
|
|
Func.Type = getFunctionType(WS);
|
2017-02-25 07:18:00 +08:00
|
|
|
Func.Sym = &WS;
|
2018-02-23 13:08:34 +08:00
|
|
|
WasmIndices[&WS] = Index;
|
2017-02-25 07:18:00 +08:00
|
|
|
Functions.push_back(Func);
|
2018-02-23 13:08:34 +08:00
|
|
|
|
|
|
|
auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
|
|
|
|
if (const MCSymbolWasm *C = Section.getGroup()) {
|
|
|
|
Comdats[C->getName()].emplace_back(
|
|
|
|
WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
|
|
|
|
}
|
2017-02-25 07:18:00 +08:00
|
|
|
} else {
|
|
|
|
// An import; the index was assigned above.
|
2018-02-23 13:08:34 +08:00
|
|
|
Index = WasmIndices.find(&WS)->second;
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:01:29 +08:00
|
|
|
DEBUG(dbgs() << " -> function index: " << Index << "\n");
|
2018-02-23 13:08:34 +08:00
|
|
|
} else if (WS.isData()) {
|
2017-06-02 09:05:24 +08:00
|
|
|
if (WS.isTemporary() && !WS.getSize())
|
|
|
|
continue;
|
2017-02-25 07:18:00 +08:00
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
if (!WS.isDefined()) {
|
|
|
|
DEBUG(dbgs() << " -> segment index: -1");
|
2017-06-22 07:46:41 +08:00
|
|
|
continue;
|
2018-02-23 13:08:34 +08:00
|
|
|
}
|
2017-05-26 05:08:07 +08:00
|
|
|
|
2017-06-22 07:46:41 +08:00
|
|
|
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-09-16 04:54:59 +08:00
|
|
|
auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
|
2018-01-10 07:43:14 +08:00
|
|
|
assert(DataSection.isWasmData());
|
2017-09-15 07:07:53 +08:00
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
// For each data symbol, export it in the symtab as a reference to the
|
|
|
|
// corresponding Wasm data segment.
|
|
|
|
wasm::WasmDataReference Ref = wasm::WasmDataReference{
|
|
|
|
DataSection.getSegmentIndex(),
|
|
|
|
static_cast<uint32_t>(Layout.getSymbolOffset(WS)),
|
|
|
|
static_cast<uint32_t>(Size)};
|
|
|
|
DataLocations[&WS] = Ref;
|
|
|
|
DEBUG(dbgs() << " -> segment index: " << Ref.Segment);
|
|
|
|
} else {
|
|
|
|
// A "true" Wasm global (currently just __stack_pointer)
|
2018-02-24 05:14:47 +08:00
|
|
|
if (WS.isDefined())
|
2018-02-23 13:08:34 +08:00
|
|
|
report_fatal_error("don't yet support defined globals");
|
|
|
|
|
2018-02-24 05:14:47 +08:00
|
|
|
// An import; the index was assigned above
|
|
|
|
DEBUG(dbgs() << " -> global index: " << WasmIndices.find(&WS)->second
|
|
|
|
<< "\n");
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:19:48 +08:00
|
|
|
// Populate WasmIndices and DataLocations for aliased symbols. We need to
|
|
|
|
// process these in a separate pass because we need to have processed the
|
|
|
|
// target of the alias before the alias itself and the symbols are not
|
|
|
|
// necessarily ordered in this way.
|
2017-06-20 12:04:59 +08:00
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
if (!S.isVariable())
|
|
|
|
continue;
|
2017-09-21 05:17:04 +08:00
|
|
|
|
2018-01-12 07:59:16 +08:00
|
|
|
assert(S.isDefined());
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2017-07-07 10:01:29 +08:00
|
|
|
// Find the target symbol of this weak alias and export that index
|
2017-09-16 03:22:01 +08:00
|
|
|
const auto &WS = static_cast<const MCSymbolWasm &>(S);
|
|
|
|
const MCSymbolWasm *ResolvedSym = ResolveSymbol(WS);
|
2017-07-07 10:01:29 +08:00
|
|
|
DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *ResolvedSym << "'\n");
|
2017-06-20 12:04:59 +08:00
|
|
|
|
2018-02-23 13:08:34 +08:00
|
|
|
if (WS.isFunction()) {
|
|
|
|
assert(WasmIndices.count(ResolvedSym) > 0);
|
|
|
|
uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
|
|
|
|
WasmIndices[&WS] = WasmIndex;
|
|
|
|
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;
|
|
|
|
DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
|
|
|
|
} else {
|
|
|
|
report_fatal_error("don't yet support global aliases");
|
|
|
|
}
|
2018-03-01 01:19:48 +08:00
|
|
|
}
|
2018-02-23 13:08:34 +08:00
|
|
|
|
2018-03-01 01:19:48 +08:00
|
|
|
// Finally, populate the symbol table itself, in its "natural" order.
|
|
|
|
for (const MCSymbol &S : Asm.symbols()) {
|
|
|
|
const auto &WS = static_cast<const MCSymbolWasm &>(S);
|
|
|
|
if (WS.isTemporary() && WS.getName().empty())
|
|
|
|
continue;
|
|
|
|
if (WS.isComdat() && !WS.isDefined())
|
|
|
|
continue;
|
|
|
|
if (WS.isTemporary() && WS.isData() && !WS.getSize())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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())
|
|
|
|
Info.ElementIndex = WasmIndices.find(&WS)->second;
|
|
|
|
else if (WS.isDefined())
|
|
|
|
Info.DataRef = DataLocations.find(&WS)->second;
|
|
|
|
SymbolIndices[&WS] = SymbolInfos.size();
|
|
|
|
SymbolInfos.emplace_back(Info);
|
2017-06-20 12:04:59 +08:00
|
|
|
}
|
|
|
|
|
2017-12-23 04:31:39 +08:00
|
|
|
{
|
|
|
|
auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
|
2018-02-01 03:28:47 +08:00
|
|
|
// Functions referenced by a relocation need to put in the table. This is
|
|
|
|
// purely to make the object file's provisional values readable, and is
|
|
|
|
// ignored by the linker, which re-calculates the relocations itself.
|
|
|
|
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);
|
2018-02-23 13:08:34 +08:00
|
|
|
uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
|
2018-02-01 03:28:47 +08:00
|
|
|
uint32_t TableIndex = TableElems.size() + kInitialTableOffset;
|
|
|
|
if (TableIndices.try_emplace(&WS, TableIndex).second) {
|
|
|
|
DEBUG(dbgs() << " -> adding " << WS.getName()
|
|
|
|
<< " to table: " << TableIndex << "\n");
|
2018-02-23 13:08:34 +08:00
|
|
|
TableElems.push_back(FunctionIndex);
|
2018-02-01 03:28:47 +08:00
|
|
|
registerFunctionType(WS);
|
2017-12-23 04:31:39 +08:00
|
|
|
}
|
|
|
|
};
|
2017-03-31 07:58:19 +08:00
|
|
|
|
2017-12-23 04:31:39 +08:00
|
|
|
for (const WasmRelocationEntry &RelEntry : CodeRelocations)
|
|
|
|
HandleReloc(RelEntry);
|
|
|
|
for (const WasmRelocationEntry &RelEntry : DataRelocations)
|
|
|
|
HandleReloc(RelEntry);
|
2017-02-25 07:18:00 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 08:17:10 +08:00
|
|
|
// Translate .init_array section contents into start functions.
|
|
|
|
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() != 2)
|
|
|
|
report_fatal_error("only one .init_array section fragment supported");
|
|
|
|
const MCFragment &AlignFrag = *WS.begin();
|
|
|
|
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(WS.begin());
|
|
|
|
if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
|
|
|
|
report_fatal_error("only data supported in .init_array section");
|
|
|
|
uint16_t Priority = UINT16_MAX;
|
|
|
|
if (WS.getSectionName().size() != 11) {
|
|
|
|
if (WS.getSectionName()[11] != '.')
|
|
|
|
report_fatal_error(".init_array section priority should start with '.'");
|
|
|
|
if (WS.getSectionName().substr(12).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");
|
|
|
|
auto I = SymbolIndices.find(cast<MCSymbolWasm>(&Sym->getSymbol()));
|
|
|
|
if (I == SymbolIndices.end())
|
|
|
|
report_fatal_error("symbols in .init_array should be defined");
|
|
|
|
uint32_t Index = I->second;
|
|
|
|
InitFuncs.push_back(std::make_pair(Priority, Index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2017-12-12 07:03:38 +08:00
|
|
|
writeImportSection(Imports, DataSize, TableElems.size());
|
2017-06-03 10:01:24 +08:00
|
|
|
writeFunctionSection(Functions);
|
2017-12-12 07:03:38 +08:00
|
|
|
// Skip the "table" section; we import the table instead.
|
|
|
|
// Skip the "memory" section; we import the memory instead.
|
2017-09-15 07:07:53 +08:00
|
|
|
writeGlobalSection();
|
2017-06-03 10:01:24 +08:00
|
|
|
writeExportSection(Exports);
|
|
|
|
writeElemSection(TableElems);
|
2017-06-07 03:15:05 +08:00
|
|
|
writeCodeSection(Asm, Layout, Functions);
|
2018-02-23 13:08:34 +08:00
|
|
|
writeDataSection();
|
2017-06-07 03:15:05 +08:00
|
|
|
writeCodeRelocSection();
|
2017-09-15 07:07:53 +08:00
|
|
|
writeDataRelocSection();
|
2018-02-28 07:57:37 +08:00
|
|
|
writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
|
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
|
|
|
}
|
|
|
|
|
2017-10-11 00:28:07 +08:00
|
|
|
std::unique_ptr<MCObjectWriter>
|
2017-10-10 09:15:10 +08:00
|
|
|
llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
|
|
|
|
raw_pwrite_stream &OS) {
|
2018-01-16 01:06:23 +08:00
|
|
|
return llvm::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|