#ifndef LLD_WASM_SYMBOLS_H
#define LLD_WASM_SYMBOLS_H
#include "Config.h"
#include "lld/Common/LLVM.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/Wasm.h"
#include <optional>
namespace lld {
namespace wasm {
extern const char *defaultModule;
extern const char *functionTableName;
extern const char *memoryName;
using llvm::wasm::WasmSymbolType;
class InputFile;
class InputChunk;
class InputSegment;
class InputFunction;
class InputGlobal;
class InputTag;
class InputSection;
class InputTable;
class OutputSection;
#define INVALID_INDEX UINT32_MAX
class Symbol {
public:
enum Kind : uint8_t {
DefinedFunctionKind,
DefinedDataKind,
DefinedGlobalKind,
DefinedTagKind,
DefinedTableKind,
SectionKind,
OutputSectionKind,
UndefinedFunctionKind,
UndefinedDataKind,
UndefinedGlobalKind,
UndefinedTableKind,
UndefinedTagKind,
LazyKind,
SharedFunctionKind,
SharedDataKind,
};
Kind kind() const { return symbolKind; }
bool isDefined() const { return !isLazy() && !isUndefined(); }
bool isUndefined() const {
return symbolKind == UndefinedFunctionKind ||
symbolKind == UndefinedDataKind ||
symbolKind == UndefinedGlobalKind ||
symbolKind == UndefinedTableKind || symbolKind == UndefinedTagKind;
}
bool isLazy() const { return symbolKind == LazyKind; }
bool isShared() const {
return symbolKind == SharedFunctionKind || symbolKind == SharedDataKind;
}
bool isLocal() const;
bool isWeak() const;
bool isHidden() const;
bool isTLS() const;
bool isDiscarded() const;
bool isUndefWeak() const {
return isWeak() && (isUndefined() || isLazy());
}
StringRef getName() const { return name; }
InputFile *getFile() const { return file; }
InputChunk *getChunk() const;
bool isLive() const;
void markLive();
void setHidden(bool isHidden);
uint32_t getOutputSymbolIndex() const;
void setOutputSymbolIndex(uint32_t index);
WasmSymbolType getWasmType() const;
bool isImported() const;
bool isExported() const;
bool isExportedExplicit() const;
bool isNoStrip() const;
const WasmSignature* getSignature() const;
uint32_t getGOTIndex() const {
assert(gotIndex != INVALID_INDEX);
return gotIndex;
}
void setGOTIndex(uint32_t index);
bool hasGOTIndex() const { return gotIndex != INVALID_INDEX; }
protected:
Symbol(StringRef name, Kind k, uint32_t flags, InputFile *f)
: name(name), file(f), symbolKind(k), referenced(!config->gcSections),
requiresGOT(false), isUsedInRegularObj(false), forceExport(false),
forceImport(false), canInline(false), traced(false), isStub(false),
flags(flags) {}
StringRef name;
InputFile *file;
uint32_t outputSymbolIndex = INVALID_INDEX;
uint32_t gotIndex = INVALID_INDEX;
Kind symbolKind;
public:
bool referenced : 1;
bool requiresGOT : 1;
bool isUsedInRegularObj : 1;
bool forceExport : 1;
bool forceImport : 1;
bool canInline : 1;
bool traced : 1;
bool isStub : 1;
uint32_t flags;
std::optional<StringRef> importName;
std::optional<StringRef> importModule;
};
class FunctionSymbol : public Symbol {
public:
static bool classof(const Symbol *s) {
return s->kind() == DefinedFunctionKind ||
s->kind() == SharedFunctionKind ||
s->kind() == UndefinedFunctionKind;
}
void setTableIndex(uint32_t index);
uint32_t getTableIndex() const;
bool hasTableIndex() const;
uint32_t getFunctionIndex() const;
void setFunctionIndex(uint32_t index);
bool hasFunctionIndex() const;
const WasmSignature *signature;
protected:
FunctionSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
const WasmSignature *sig)
: Symbol(name, k, flags, f), signature(sig) {}
uint32_t tableIndex = INVALID_INDEX;
uint32_t functionIndex = INVALID_INDEX;
};
class DefinedFunction : public FunctionSymbol {
public:
DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
InputFunction *function);
static bool classof(const Symbol *s) {
return s->kind() == DefinedFunctionKind;
}
uint32_t getExportedFunctionIndex() const;
InputFunction *function;
};
class UndefinedFunction : public FunctionSymbol {
public:
UndefinedFunction(StringRef name, std::optional<StringRef> importName,
std::optional<StringRef> importModule, uint32_t flags,
InputFile *file = nullptr,
const WasmSignature *type = nullptr,
bool isCalledDirectly = true)
: FunctionSymbol(name, UndefinedFunctionKind, flags, file, type),
isCalledDirectly(isCalledDirectly) {
this->importName = importName;
this->importModule = importModule;
}
static bool classof(const Symbol *s) {
return s->kind() == UndefinedFunctionKind;
}
DefinedFunction *stubFunction = nullptr;
bool isCalledDirectly;
};
class OutputSectionSymbol : public Symbol {
public:
OutputSectionSymbol(const OutputSection *s)
: Symbol("", OutputSectionKind, llvm::wasm::WASM_SYMBOL_BINDING_LOCAL,
nullptr),
section(s) {}
static bool classof(const Symbol *s) {
return s->kind() == OutputSectionKind;
}
const OutputSection *section;
};
class SectionSymbol : public Symbol {
public:
SectionSymbol(uint32_t flags, const InputChunk *s, InputFile *f = nullptr)
: Symbol("", SectionKind, flags, f), section(s) {}
static bool classof(const Symbol *s) { return s->kind() == SectionKind; }
const OutputSectionSymbol *getOutputSectionSymbol() const;
const InputChunk *section;
};
class DataSymbol : public Symbol {
public:
static bool classof(const Symbol *s) {
return s->kind() == DefinedDataKind || s->kind() == UndefinedDataKind ||
s->kind() == SharedDataKind;
}
protected:
DataSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f)
: Symbol(name, k, flags, f) {}
};
class DefinedData : public DataSymbol {
public:
DefinedData(StringRef name, uint32_t flags, InputFile *f, InputChunk *segment,
uint64_t value, uint64_t size)
: DataSymbol(name, DefinedDataKind, flags, f), segment(segment),
value(value), size(size) {}
DefinedData(StringRef name, uint32_t flags)
: DataSymbol(name, DefinedDataKind, flags, nullptr) {}
static bool classof(const Symbol *s) { return s->kind() == DefinedDataKind; }
uint64_t getVA() const;
void setVA(uint64_t va);
uint64_t getOutputSegmentOffset() const;
uint64_t getOutputSegmentIndex() const;
uint64_t getSize() const { return size; }
InputChunk *segment = nullptr;
uint64_t value = 0;
protected:
uint64_t size = 0;
};
class SharedData : public DataSymbol {
public:
SharedData(StringRef name, uint32_t flags, InputFile *f)
: DataSymbol(name, SharedDataKind, flags, f) {}
};
class UndefinedData : public DataSymbol {
public:
UndefinedData(StringRef name, uint32_t flags, InputFile *file = nullptr)
: DataSymbol(name, UndefinedDataKind, flags, file) {}
static bool classof(const Symbol *s) {
return s->kind() == UndefinedDataKind;
}
};
class GlobalSymbol : public Symbol {
public:
static bool classof(const Symbol *s) {
return s->kind() == DefinedGlobalKind || s->kind() == UndefinedGlobalKind;
}
const WasmGlobalType *getGlobalType() const { return globalType; }
uint32_t getGlobalIndex() const;
void setGlobalIndex(uint32_t index);
bool hasGlobalIndex() const;
protected:
GlobalSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
const WasmGlobalType *globalType)
: Symbol(name, k, flags, f), globalType(globalType) {}
const WasmGlobalType *globalType;
uint32_t globalIndex = INVALID_INDEX;
};
class DefinedGlobal : public GlobalSymbol {
public:
DefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
InputGlobal *global);
static bool classof(const Symbol *s) {
return s->kind() == DefinedGlobalKind;
}
InputGlobal *global;
};
class UndefinedGlobal : public GlobalSymbol {
public:
UndefinedGlobal(StringRef name, std::optional<StringRef> importName,
std::optional<StringRef> importModule, uint32_t flags,
InputFile *file = nullptr,
const WasmGlobalType *type = nullptr)
: GlobalSymbol(name, UndefinedGlobalKind, flags, file, type) {
this->importName = importName;
this->importModule = importModule;
}
static bool classof(const Symbol *s) {
return s->kind() == UndefinedGlobalKind;
}
};
class TableSymbol : public Symbol {
public:
static bool classof(const Symbol *s) {
return s->kind() == DefinedTableKind || s->kind() == UndefinedTableKind;
}
const WasmTableType *getTableType() const { return tableType; }
void setLimits(const WasmLimits &limits);
uint32_t getTableNumber() const;
void setTableNumber(uint32_t number);
bool hasTableNumber() const;
protected:
TableSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
const WasmTableType *type)
: Symbol(name, k, flags, f), tableType(type) {}
const WasmTableType *tableType;
uint32_t tableNumber = INVALID_INDEX;
};
class DefinedTable : public TableSymbol {
public:
DefinedTable(StringRef name, uint32_t flags, InputFile *file,
InputTable *table);
static bool classof(const Symbol *s) { return s->kind() == DefinedTableKind; }
InputTable *table;
};
class UndefinedTable : public TableSymbol {
public:
UndefinedTable(StringRef name, std::optional<StringRef> importName,
std::optional<StringRef> importModule, uint32_t flags,
InputFile *file, const WasmTableType *type)
: TableSymbol(name, UndefinedTableKind, flags, file, type) {
this->importName = importName;
this->importModule = importModule;
}
static bool classof(const Symbol *s) {
return s->kind() == UndefinedTableKind;
}
};
class TagSymbol : public Symbol {
public:
static bool classof(const Symbol *s) {
return s->kind() == DefinedTagKind || s->kind() == UndefinedTagKind;
}
uint32_t getTagIndex() const;
void setTagIndex(uint32_t index);
bool hasTagIndex() const;
const WasmSignature *signature;
protected:
TagSymbol(StringRef name, Kind k, uint32_t flags, InputFile *f,
const WasmSignature *sig)
: Symbol(name, k, flags, f), signature(sig) {}
uint32_t tagIndex = INVALID_INDEX;
};
class DefinedTag : public TagSymbol {
public:
DefinedTag(StringRef name, uint32_t flags, InputFile *file, InputTag *tag);
static bool classof(const Symbol *s) { return s->kind() == DefinedTagKind; }
InputTag *tag;
};
class UndefinedTag : public TagSymbol {
public:
UndefinedTag(StringRef name, std::optional<StringRef> importName,
std::optional<StringRef> importModule, uint32_t flags,
InputFile *file = nullptr, const WasmSignature *sig = nullptr)
: TagSymbol(name, UndefinedTagKind, flags, file, sig) {
this->importName = importName;
this->importModule = importModule;
}
static bool classof(const Symbol *s) { return s->kind() == UndefinedTagKind; }
};
class SharedFunctionSymbol : public FunctionSymbol {
public:
SharedFunctionSymbol(StringRef name, uint32_t flags, InputFile *file,
const WasmSignature *sig)
: FunctionSymbol(name, SharedFunctionKind, flags, file, sig) {}
static bool classof(const Symbol *s) {
return s->kind() == SharedFunctionKind;
}
};
class LazySymbol : public Symbol {
public:
LazySymbol(StringRef name, uint32_t flags, InputFile *file)
: Symbol(name, LazyKind, flags, file) {}
static bool classof(const Symbol *s) { return s->kind() == LazyKind; }
void extract();
void setWeak();
const WasmSignature *signature = nullptr;
};
struct WasmSym {
static DefinedData *globalBase;
static GlobalSymbol *stackPointer;
static DefinedData *stackLow;
static DefinedData *stackHigh;
static GlobalSymbol *tlsBase;
static GlobalSymbol *tlsSize;
static GlobalSymbol *tlsAlign;
static DefinedData *dataEnd;
static DefinedData *heapBase;
static DefinedData *heapEnd;
static DefinedData *initMemoryFlag;
static DefinedFunction *initMemory;
static DefinedFunction *callCtors;
static DefinedFunction *callDtors;
static DefinedFunction *applyDataRelocs;
static DefinedFunction *applyGlobalRelocs;
static DefinedFunction *applyTLSRelocs;
static DefinedFunction *applyGlobalTLSRelocs;
static DefinedFunction *initTLS;
static DefinedFunction *startFunction;
static DefinedData *dsoHandle;
static UndefinedGlobal *tableBase;
static DefinedData *definedTableBase;
static UndefinedGlobal *memoryBase;
static DefinedData *definedMemoryBase;
static TableSymbol *indirectFunctionTable;
};
union SymbolUnion {
alignas(DefinedFunction) char a[sizeof(DefinedFunction)];
alignas(DefinedData) char b[sizeof(DefinedData)];
alignas(DefinedGlobal) char c[sizeof(DefinedGlobal)];
alignas(DefinedTag) char d[sizeof(DefinedTag)];
alignas(DefinedTable) char e[sizeof(DefinedTable)];
alignas(LazySymbol) char f[sizeof(LazySymbol)];
alignas(UndefinedFunction) char g[sizeof(UndefinedFunction)];
alignas(UndefinedData) char h[sizeof(UndefinedData)];
alignas(UndefinedGlobal) char i[sizeof(UndefinedGlobal)];
alignas(UndefinedTable) char j[sizeof(UndefinedTable)];
alignas(SectionSymbol) char k[sizeof(SectionSymbol)];
alignas(SharedFunctionSymbol) char l[sizeof(SharedFunctionSymbol)];
};
static_assert(sizeof(SymbolUnion) <= 120, "SymbolUnion too large");
void printTraceSymbol(Symbol *sym);
void printTraceSymbolUndefined(StringRef name, const InputFile* file);
template <typename T, typename... ArgT>
T *replaceSymbol(Symbol *s, ArgT &&... arg) {
static_assert(std::is_trivially_destructible<T>(),
"Symbol types must be trivially destructible");
static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
static_assert(alignof(T) <= alignof(SymbolUnion),
"SymbolUnion not aligned enough");
assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
"Not a Symbol");
Symbol symCopy = *s;
T *s2 = new (s) T(std::forward<ArgT>(arg)...);
s2->isUsedInRegularObj = symCopy.isUsedInRegularObj;
s2->forceExport = symCopy.forceExport;
s2->forceImport = symCopy.forceImport;
s2->canInline = symCopy.canInline;
s2->traced = symCopy.traced;
s2->referenced = symCopy.referenced;
if (s2->traced)
printTraceSymbol(s2);
return s2;
}
}
std::string toString(const wasm::Symbol &sym);
std::string toString(wasm::Symbol::Kind kind);
std::string maybeDemangleSymbol(StringRef name);
}
#endif