#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include "llvm/ExecutionEngine/RuntimeDyldChecker.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/SwapByteOrder.h"
#include <deque>
#include <map>
#include <system_error>
#include <unordered_map>
using namespace llvm;
using namespace llvm::object;
namespace llvm {
#define UNIMPLEMENTED_RELOC(RelType) \
case RelType: \
return make_error<RuntimeDyldError>("Unimplemented relocation: " #RelType)
class SectionEntry {
std::string Name;
uint8_t *Address;
size_t Size;
uint64_t LoadAddress;
uintptr_t StubOffset;
size_t AllocationSize;
uintptr_t ObjAddress;
public:
SectionEntry(StringRef name, uint8_t *address, size_t size,
size_t allocationSize, uintptr_t objAddress)
: Name(std::string(name)), Address(address), Size(size),
LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
AllocationSize(allocationSize), ObjAddress(objAddress) {
(void)AllocationSize;
}
StringRef getName() const { return Name; }
uint8_t *getAddress() const { return Address; }
uint8_t *getAddressWithOffset(unsigned OffsetBytes) const {
assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
return Address + OffsetBytes;
}
size_t getSize() const { return Size; }
uint64_t getLoadAddress() const { return LoadAddress; }
void setLoadAddress(uint64_t LA) { LoadAddress = LA; }
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const {
assert(OffsetBytes <= AllocationSize && "Offset out of bounds!");
return LoadAddress + OffsetBytes;
}
uintptr_t getStubOffset() const { return StubOffset; }
void advanceStubOffset(unsigned StubSize) {
StubOffset += StubSize;
assert(StubOffset <= AllocationSize && "Not enough space allocated!");
}
uintptr_t getObjAddress() const { return ObjAddress; }
};
class RelocationEntry {
public:
unsigned SectionID;
uint64_t Offset;
uint32_t RelType;
int64_t Addend;
struct SectionPair {
uint32_t SectionA;
uint32_t SectionB;
};
union {
uint64_t SymOffset;
SectionPair Sections;
};
bool IsPCRel;
unsigned Size;
bool IsTargetThumbFunc = false;
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
: SectionID(id), Offset(offset), RelType(type), Addend(addend),
SymOffset(0), IsPCRel(false), Size(0), IsTargetThumbFunc(false) {}
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
uint64_t symoffset)
: SectionID(id), Offset(offset), RelType(type), Addend(addend),
SymOffset(symoffset), IsPCRel(false), Size(0),
IsTargetThumbFunc(false) {}
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
bool IsPCRel, unsigned Size)
: SectionID(id), Offset(offset), RelType(type), Addend(addend),
SymOffset(0), IsPCRel(IsPCRel), Size(Size), IsTargetThumbFunc(false) {}
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
uint64_t SectionBOffset, bool IsPCRel, unsigned Size)
: SectionID(id), Offset(offset), RelType(type),
Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
Size(Size), IsTargetThumbFunc(false) {
Sections.SectionA = SectionA;
Sections.SectionB = SectionB;
}
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend,
unsigned SectionA, uint64_t SectionAOffset, unsigned SectionB,
uint64_t SectionBOffset, bool IsPCRel, unsigned Size,
bool IsTargetThumbFunc)
: SectionID(id), Offset(offset), RelType(type),
Addend(SectionAOffset - SectionBOffset + addend), IsPCRel(IsPCRel),
Size(Size), IsTargetThumbFunc(IsTargetThumbFunc) {
Sections.SectionA = SectionA;
Sections.SectionB = SectionB;
}
};
class RelocationValueRef {
public:
unsigned SectionID = 0;
uint64_t Offset = 0;
int64_t Addend = 0;
const char *SymbolName = nullptr;
bool IsStubThumb = false;
inline bool operator==(const RelocationValueRef &Other) const {
return SectionID == Other.SectionID && Offset == Other.Offset &&
Addend == Other.Addend && SymbolName == Other.SymbolName &&
IsStubThumb == Other.IsStubThumb;
}
inline bool operator<(const RelocationValueRef &Other) const {
if (SectionID != Other.SectionID)
return SectionID < Other.SectionID;
if (Offset != Other.Offset)
return Offset < Other.Offset;
if (Addend != Other.Addend)
return Addend < Other.Addend;
if (IsStubThumb != Other.IsStubThumb)
return IsStubThumb < Other.IsStubThumb;
return SymbolName < Other.SymbolName;
}
};
class SymbolTableEntry {
public:
SymbolTableEntry() = default;
SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
: Offset(Offset), SectionID(SectionID), Flags(Flags) {}
unsigned getSectionID() const { return SectionID; }
uint64_t getOffset() const { return Offset; }
void setOffset(uint64_t NewOffset) { Offset = NewOffset; }
JITSymbolFlags getFlags() const { return Flags; }
private:
uint64_t Offset = 0;
unsigned SectionID = 0;
JITSymbolFlags Flags = JITSymbolFlags::None;
};
typedef StringMap<SymbolTableEntry> RTDyldSymbolTable;
class RuntimeDyldImpl {
friend class RuntimeDyld::LoadedObjectInfo;
protected:
static const unsigned AbsoluteSymbolSection = ~0U;
RuntimeDyld::MemoryManager &MemMgr;
JITSymbolResolver &Resolver;
typedef std::deque<SectionEntry> SectionList;
SectionList Sections;
typedef unsigned SID; #define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1))
typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
RTDyldSymbolTable GlobalSymbolTable;
typedef std::vector<SymbolRef> CommonSymbolList;
typedef SmallVector<RelocationEntry, 64> RelocationList;
std::unordered_map<unsigned, RelocationList> Relocations;
StringMap<RelocationList> ExternalSymbolRelocations;
typedef std::map<RelocationValueRef, uintptr_t> StubMap;
Triple::ArchType Arch;
bool IsTargetLittleEndian;
bool IsMipsO32ABI;
bool IsMipsN32ABI;
bool IsMipsN64ABI;
bool ProcessAllSections;
sys::Mutex lock;
using NotifyStubEmittedFunction =
RuntimeDyld::NotifyStubEmittedFunction;
NotifyStubEmittedFunction NotifyStubEmitted;
virtual unsigned getMaxStubSize() const = 0;
virtual unsigned getStubAlignment() = 0;
bool HasError;
std::string ErrorStr;
void writeInt16BE(uint8_t *Addr, uint16_t Value) {
llvm::support::endian::write<uint16_t, llvm::support::unaligned>(
Addr, Value, IsTargetLittleEndian ? support::little : support::big);
}
void writeInt32BE(uint8_t *Addr, uint32_t Value) {
llvm::support::endian::write<uint32_t, llvm::support::unaligned>(
Addr, Value, IsTargetLittleEndian ? support::little : support::big);
}
void writeInt64BE(uint8_t *Addr, uint64_t Value) {
llvm::support::endian::write<uint64_t, llvm::support::unaligned>(
Addr, Value, IsTargetLittleEndian ? support::little : support::big);
}
virtual void setMipsABI(const ObjectFile &Obj) {
IsMipsO32ABI = false;
IsMipsN32ABI = false;
IsMipsN64ABI = false;
}
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const;
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const;
virtual Expected<JITSymbolFlags> getJITSymbolFlags(const SymbolRef &Sym);
virtual uint64_t modifyAddressBasedOnFlags(uint64_t Addr,
JITSymbolFlags Flags) const {
return Addr;
}
Error emitCommonSymbols(const ObjectFile &Obj,
CommonSymbolList &CommonSymbols, uint64_t CommonSize,
uint32_t CommonAlign);
Expected<unsigned> emitSection(const ObjectFile &Obj,
const SectionRef &Section,
bool IsCode);
Expected<unsigned> findOrEmitSection(const ObjectFile &Obj,
const SectionRef &Section, bool IsCode,
ObjSectionToIDMap &LocalSections);
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID);
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName);
uint8_t *createStubFunction(uint8_t *Addr, unsigned AbiVariant = 0);
void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
virtual Expected<relocation_iterator>
processRelocationRef(unsigned SectionID, relocation_iterator RelI,
const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID,
StubMap &Stubs) = 0;
void applyExternalSymbolRelocations(
const StringMap<JITEvaluatedSymbol> ExternalSymbolMap);
Error resolveExternalSymbols();
Error computeTotalAllocSize(const ObjectFile &Obj,
uint64_t &CodeSize, uint32_t &CodeAlign,
uint64_t &RODataSize, uint32_t &RODataAlign,
uint64_t &RWDataSize, uint32_t &RWDataAlign);
unsigned computeGOTSize(const ObjectFile &Obj);
unsigned computeSectionStubBufSize(const ObjectFile &Obj,
const SectionRef &Section);
Expected<ObjSectionToIDMap> loadObjectImpl(const object::ObjectFile &Obj);
virtual size_t getGOTEntrySize() { return 0; }
virtual bool relocationNeedsGot(const RelocationRef &R) const {
return false;
}
virtual bool relocationNeedsStub(const RelocationRef &R) const {
return true; }
public:
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
JITSymbolResolver &Resolver)
: MemMgr(MemMgr), Resolver(Resolver),
ProcessAllSections(false), HasError(false) {
}
virtual ~RuntimeDyldImpl();
void setProcessAllSections(bool ProcessAllSections) {
this->ProcessAllSections = ProcessAllSections;
}
virtual std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const object::ObjectFile &Obj) = 0;
uint64_t getSectionLoadAddress(unsigned SectionID) const {
if (SectionID == AbsoluteSymbolSection)
return 0;
else
return Sections[SectionID].getLoadAddress();
}
uint8_t *getSectionAddress(unsigned SectionID) const {
if (SectionID == AbsoluteSymbolSection)
return nullptr;
else
return Sections[SectionID].getAddress();
}
StringRef getSectionContent(unsigned SectionID) const {
if (SectionID == AbsoluteSymbolSection)
return {};
else
return StringRef(
reinterpret_cast<char *>(Sections[SectionID].getAddress()),
Sections[SectionID].getStubOffset() + getMaxStubSize());
}
uint8_t* getSymbolLocalAddress(StringRef Name) const {
RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
if (pos == GlobalSymbolTable.end())
return nullptr;
const auto &SymInfo = pos->second;
if (SymInfo.getSectionID() == AbsoluteSymbolSection)
return nullptr;
return getSectionAddress(SymInfo.getSectionID()) + SymInfo.getOffset();
}
unsigned getSymbolSectionID(StringRef Name) const {
auto GSTItr = GlobalSymbolTable.find(Name);
if (GSTItr == GlobalSymbolTable.end())
return ~0U;
return GSTItr->second.getSectionID();
}
JITEvaluatedSymbol getSymbol(StringRef Name) const {
RTDyldSymbolTable::const_iterator pos = GlobalSymbolTable.find(Name);
if (pos == GlobalSymbolTable.end())
return nullptr;
const auto &SymEntry = pos->second;
uint64_t SectionAddr = 0;
if (SymEntry.getSectionID() != AbsoluteSymbolSection)
SectionAddr = getSectionLoadAddress(SymEntry.getSectionID());
uint64_t TargetAddr = SectionAddr + SymEntry.getOffset();
TargetAddr = modifyAddressBasedOnFlags(TargetAddr, SymEntry.getFlags());
return JITEvaluatedSymbol(TargetAddr, SymEntry.getFlags());
}
std::map<StringRef, JITEvaluatedSymbol> getSymbolTable() const {
std::map<StringRef, JITEvaluatedSymbol> Result;
for (auto &KV : GlobalSymbolTable) {
auto SectionID = KV.second.getSectionID();
uint64_t SectionAddr = getSectionLoadAddress(SectionID);
Result[KV.first()] =
JITEvaluatedSymbol(SectionAddr + KV.second.getOffset(), KV.second.getFlags());
}
return Result;
}
void resolveRelocations();
void resolveLocalRelocations();
static void finalizeAsync(
std::unique_ptr<RuntimeDyldImpl> This,
unique_function<void(object::OwningBinary<object::ObjectFile>,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo>,
Error)>
OnEmitted,
object::OwningBinary<object::ObjectFile> O,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info);
void reassignSectionAddress(unsigned SectionID, uint64_t Addr);
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress);
bool hasError() { return HasError; }
void clearError() { HasError = false; }
StringRef getErrorString() { return ErrorStr; }
virtual bool isCompatibleFile(const ObjectFile &Obj) const = 0;
void setNotifyStubEmitted(NotifyStubEmittedFunction NotifyStubEmitted) {
this->NotifyStubEmitted = std::move(NotifyStubEmitted);
}
virtual void registerEHFrames();
void deregisterEHFrames();
virtual Error finalizeLoad(const ObjectFile &ObjImg,
ObjSectionToIDMap &SectionMap) {
return Error::success();
}
};
}
#endif