#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDIMPL_H
#include "llvm/ADT/DenseMap.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 "llvm/Support/raw_ostream.h"
#include <map>
#include <system_error>
using namespace llvm;
using namespace llvm::object;
namespace llvm {
inline std::error_code Check(std::error_code Err) {
if (Err) {
report_fatal_error(Err.message());
}
return Err;
}
class Twine;
class SectionEntry {
std::string Name;
uint8_t *Address;
size_t Size;
uint64_t LoadAddress;
uintptr_t StubOffset;
uintptr_t ObjAddress;
public:
SectionEntry(StringRef name, uint8_t *address, size_t size,
uintptr_t objAddress)
: Name(name), Address(address), Size(size),
LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
ObjAddress(objAddress) {}
StringRef getName() const { return Name; }
uint8_t *getAddress() const { return Address; }
uint8_t *getAddressWithOffset(unsigned OffsetBytes) const {
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 {
return LoadAddress + OffsetBytes;
}
uintptr_t getStubOffset() const { return StubOffset; }
void advanceStubOffset(unsigned StubSize) { StubOffset += StubSize; }
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;
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) {}
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) {}
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) {}
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) {
Sections.SectionA = SectionA;
Sections.SectionB = SectionB;
}
};
class RelocationValueRef {
public:
unsigned SectionID;
uint64_t Offset;
int64_t Addend;
const char *SymbolName;
RelocationValueRef() : SectionID(0), Offset(0), Addend(0),
SymbolName(nullptr) {}
inline bool operator==(const RelocationValueRef &Other) const {
return SectionID == Other.SectionID && Offset == Other.Offset &&
Addend == Other.Addend && SymbolName == Other.SymbolName;
}
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;
return SymbolName < Other.SymbolName;
}
};
class SymbolTableEntry : public JITSymbolBase {
public:
SymbolTableEntry()
: JITSymbolBase(JITSymbolFlags::None), Offset(0), SectionID(0) {}
SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
: JITSymbolBase(Flags), Offset(Offset), SectionID(SectionID) {}
unsigned getSectionID() const { return SectionID; }
uint64_t getOffset() const { return Offset; }
private:
uint64_t Offset;
unsigned SectionID;
};
typedef StringMap<SymbolTableEntry> RTDyldSymbolTable;
class RuntimeDyldImpl {
friend class RuntimeDyld::LoadedObjectInfo;
friend class RuntimeDyldCheckerImpl;
protected:
static const unsigned AbsoluteSymbolSection = ~0U;
RuntimeDyld::MemoryManager &MemMgr;
RuntimeDyld::SymbolResolver &Resolver;
RuntimeDyldCheckerImpl *Checker;
typedef SmallVector<SectionEntry, 64> 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;
DenseMap<unsigned, RelocationList> Relocations;
StringMap<RelocationList> ExternalSymbolRelocations;
typedef std::map<RelocationValueRef, uintptr_t> StubMap;
Triple::ArchType Arch;
bool IsTargetLittleEndian;
bool IsMipsO32ABI;
bool IsMipsN64ABI;
bool ProcessAllSections;
sys::Mutex lock;
virtual unsigned getMaxStubSize() = 0;
virtual unsigned getStubAlignment() = 0;
bool HasError;
std::string ErrorStr;
bool Error(const Twine &Msg) {
ErrorStr = Msg.str();
HasError = true;
return true;
}
uint64_t getSectionLoadAddress(unsigned SectionID) const {
return Sections[SectionID].getLoadAddress();
}
uint8_t *getSectionAddress(unsigned SectionID) const {
return Sections[SectionID].getAddress();
}
void writeInt16BE(uint8_t *Addr, uint16_t Value) {
if (IsTargetLittleEndian)
sys::swapByteOrder(Value);
*Addr = (Value >> 8) & 0xFF;
*(Addr + 1) = Value & 0xFF;
}
void writeInt32BE(uint8_t *Addr, uint32_t Value) {
if (IsTargetLittleEndian)
sys::swapByteOrder(Value);
*Addr = (Value >> 24) & 0xFF;
*(Addr + 1) = (Value >> 16) & 0xFF;
*(Addr + 2) = (Value >> 8) & 0xFF;
*(Addr + 3) = Value & 0xFF;
}
void writeInt64BE(uint8_t *Addr, uint64_t Value) {
if (IsTargetLittleEndian)
sys::swapByteOrder(Value);
*Addr = (Value >> 56) & 0xFF;
*(Addr + 1) = (Value >> 48) & 0xFF;
*(Addr + 2) = (Value >> 40) & 0xFF;
*(Addr + 3) = (Value >> 32) & 0xFF;
*(Addr + 4) = (Value >> 24) & 0xFF;
*(Addr + 5) = (Value >> 16) & 0xFF;
*(Addr + 6) = (Value >> 8) & 0xFF;
*(Addr + 7) = Value & 0xFF;
}
virtual void setMipsABI(const ObjectFile &Obj) {
IsMipsO32ABI = false;
IsMipsN64ABI = false;
}
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const;
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const;
void emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols);
unsigned emitSection(const ObjectFile &Obj, const SectionRef &Section,
bool IsCode);
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 relocation_iterator
processRelocationRef(unsigned SectionID, relocation_iterator RelI,
const ObjectFile &Obj, ObjSectionToIDMap &ObjSectionToID,
StubMap &Stubs) = 0;
void resolveExternalSymbols();
void computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize,
uint64_t &DataSizeRO, uint64_t &DataSizeRW);
unsigned computeSectionStubBufSize(const ObjectFile &Obj,
const SectionRef &Section);
ObjSectionToIDMap loadObjectImpl(const object::ObjectFile &Obj);
public:
RuntimeDyldImpl(RuntimeDyld::MemoryManager &MemMgr,
RuntimeDyld::SymbolResolver &Resolver)
: MemMgr(MemMgr), Resolver(Resolver), Checker(nullptr),
ProcessAllSections(false), HasError(false) {
}
virtual ~RuntimeDyldImpl();
void setProcessAllSections(bool ProcessAllSections) {
this->ProcessAllSections = ProcessAllSections;
}
void setRuntimeDyldChecker(RuntimeDyldCheckerImpl *Checker) {
this->Checker = Checker;
}
virtual std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const object::ObjectFile &Obj) = 0;
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();
}
RuntimeDyld::SymbolInfo 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();
return RuntimeDyld::SymbolInfo(TargetAddr, SymEntry.getFlags());
}
void resolveRelocations();
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;
virtual void registerEHFrames();
virtual void deregisterEHFrames();
virtual void finalizeLoad(const ObjectFile &ObjImg,
ObjSectionToIDMap &SectionMap) {}
};
}
#endif