#include "llvm/ADT/STLExtras.h"
#include "llvm/BinaryFormat/Wasm.h"
#include "llvm/BinaryFormat/WasmTraits.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionWasm.h"
#include "llvm/MC/MCSymbolWasm.h"
#include "llvm/MC/MCValue.h"
#include "llvm/MC/MCWasmObjectWriter.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/LEB128.h"
#include <vector>
usingnamespacellvm;
#define DEBUG_TYPE …
namespace {
static const uint32_t InitialTableOffset = …;
struct SectionBookkeeping { … };
struct WasmDataSegment { … };
struct WasmFunction { … };
struct WasmGlobal { … };
struct WasmComdatEntry { … };
struct WasmRelocationEntry { … };
static const uint32_t InvalidIndex = …;
struct WasmCustomSection { … };
#if !defined(NDEBUG)
raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
Rel.print(OS);
return OS;
}
#endif
template <typename T, int W>
void writePatchableULEB(raw_pwrite_stream &Stream, T Value, uint64_t Offset) { … }
template <typename T, int W>
void writePatchableSLEB(raw_pwrite_stream &Stream, T Value, uint64_t Offset) { … }
static void writePatchableU32(raw_pwrite_stream &Stream, uint32_t Value,
uint64_t Offset) { … }
static void writePatchableS32(raw_pwrite_stream &Stream, int32_t Value,
uint64_t Offset) { … }
static void writePatchableU64(raw_pwrite_stream &Stream, uint64_t Value,
uint64_t Offset) { … }
static void writePatchableS64(raw_pwrite_stream &Stream, int64_t Value,
uint64_t Offset) { … }
static void patchI32(raw_pwrite_stream &Stream, uint32_t Value,
uint64_t Offset) { … }
static void patchI64(raw_pwrite_stream &Stream, uint64_t Value,
uint64_t Offset) { … }
bool isDwoSection(const MCSection &Sec) { … }
class WasmObjectWriter : public MCObjectWriter { … };
}
void WasmObjectWriter::startSection(SectionBookkeeping &Section,
unsigned SectionId) { … }
void WasmObjectWriter::writeStringWithAlignment(const StringRef Str,
unsigned Alignment) { … }
void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
StringRef Name) { … }
void WasmObjectWriter::endSection(SectionBookkeeping &Section) { … }
void WasmObjectWriter::writeHeader(const MCAssembler &Asm) { … }
void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm) { … }
void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
const MCFragment *Fragment,
const MCFixup &Fixup, MCValue Target,
uint64_t &FixedValue) { … }
uint64_t
WasmObjectWriter::getProvisionalValue(const MCAssembler &Asm,
const WasmRelocationEntry &RelEntry) { … }
static void addData(SmallVectorImpl<char> &DataBytes,
MCSectionWasm &DataSection) { … }
uint32_t
WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) { … }
void WasmObjectWriter::applyRelocations(
ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset,
const MCAssembler &Asm) { … }
void WasmObjectWriter::writeTypeSection(
ArrayRef<wasm::WasmSignature> Signatures) { … }
void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
uint64_t DataSize,
uint32_t NumElements) { … }
void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) { … }
void WasmObjectWriter::writeTagSection(ArrayRef<uint32_t> TagTypes) { … }
void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) { … }
void WasmObjectWriter::writeTableSection(ArrayRef<wasm::WasmTable> Tables) { … }
void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) { … }
void WasmObjectWriter::writeElemSection(
const MCSymbolWasm *IndirectFunctionTable, ArrayRef<uint32_t> TableElems) { … }
void WasmObjectWriter::writeDataCountSection() { … }
uint32_t WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
ArrayRef<WasmFunction> Functions) { … }
uint32_t WasmObjectWriter::writeDataSection(const MCAssembler &Asm) { … }
void WasmObjectWriter::writeRelocSection(
uint32_t SectionIndex, StringRef Name,
std::vector<WasmRelocationEntry> &Relocs) { … }
void WasmObjectWriter::writeCustomRelocSections() { … }
void WasmObjectWriter::writeLinkingMetaDataSection(
ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) { … }
void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
const MCAssembler &Asm) { … }
uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) { … }
uint32_t WasmObjectWriter::getTagType(const MCSymbolWasm &Symbol) { … }
void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) { … }
void WasmObjectWriter::registerTagType(const MCSymbolWasm &Symbol) { … }
static bool isInSymtab(const MCSymbolWasm &Sym) { … }
void WasmObjectWriter::prepareImports(
SmallVectorImpl<wasm::WasmImport> &Imports, MCAssembler &Asm) { … }
uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm) { … }
uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
DwoMode Mode) { … }
std::unique_ptr<MCObjectWriter>
llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
raw_pwrite_stream &OS) { … }
std::unique_ptr<MCObjectWriter>
llvm::createWasmDwoObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
raw_pwrite_stream &OS,
raw_pwrite_stream &DwoOS) { … }