#ifndef LLVM_RUNTIME_DYLD_IMPL_H
#define LLVM_RUNTIME_DYLD_IMPL_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Triple.h"
#include "llvm/ExecutionEngine/ObjectImage.h"
#include "llvm/ExecutionEngine/RuntimeDyld.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 "llvm/Support/system_error.h"
#include <map>
using namespace llvm;
using namespace llvm::object;
namespace llvm {
class ObjectBuffer;
class Twine;
class SectionEntry {
public:
StringRef Name;
uint8_t *Address;
size_t Size;
uint64_t LoadAddress;
uintptr_t StubOffset;
uintptr_t ObjAddress;
SectionEntry(StringRef name, uint8_t *address, size_t size,
uintptr_t objAddress)
: Name(name), Address(address), Size(size), LoadAddress((uintptr_t)address),
StubOffset(size), ObjAddress(objAddress) {}
};
class RelocationEntry {
public:
struct SectionPair {
uint32_t SectionA;
uint32_t SectionB;
};
unsigned SectionID;
uint64_t Offset;
uint32_t RelType;
int64_t Addend;
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(0) {}
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 RuntimeDyldImpl {
protected:
RTDyldMemoryManager *MemMgr;
typedef SmallVector<SectionEntry, 64> SectionList;
SectionList Sections;
typedef unsigned SID; #define RTDYLD_INVALID_SECTION_ID ((SID)(-1))
typedef std::map<SectionRef, unsigned> ObjSectionToIDMap;
typedef std::pair<unsigned, uintptr_t> SymbolLoc;
typedef StringMap<SymbolLoc> SymbolTableMap;
SymbolTableMap GlobalSymbolTable;
typedef std::pair<unsigned, unsigned> CommonSymbolInfo;
typedef std::map<SymbolRef, CommonSymbolInfo> CommonSymbolMap;
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 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) {
return Sections[SectionID].LoadAddress;
}
uint8_t *getSectionAddress(unsigned SectionID) {
return (uint8_t*)Sections[SectionID].Address;
}
void writeInt16BE(uint8_t *Addr, uint16_t Value) {
if (IsTargetLittleEndian)
Value = sys::SwapByteOrder(Value);
*Addr = (Value >> 8) & 0xFF;
*(Addr+1) = Value & 0xFF;
}
void writeInt32BE(uint8_t *Addr, uint32_t Value) {
if (IsTargetLittleEndian)
Value = 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)
Value = 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;
}
void emitCommonSymbols(ObjectImage &Obj,
const CommonSymbolMap &CommonSymbols,
uint64_t TotalSize,
SymbolTableMap &SymbolTable);
unsigned emitSection(ObjectImage &Obj,
const SectionRef &Section,
bool IsCode);
unsigned findOrEmitSection(ObjectImage &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);
void resolveRelocationList(const RelocationList &Relocs, uint64_t Value);
virtual void resolveRelocation(const RelocationEntry &RE, uint64_t Value) = 0;
virtual relocation_iterator
processRelocationRef(unsigned SectionID, const section_iterator &SI,
relocation_iterator RelI, ObjectImage &Obj,
ObjSectionToIDMap &ObjSectionToID,
const SymbolTableMap &Symbols, StubMap &Stubs) = 0;
void resolveExternalSymbols();
virtual void updateGOTEntries(StringRef Name, uint64_t Addr) {}
virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
virtual ObjectImage *createObjectImageFromFile(object::ObjectFile *InputObject);
void computeTotalAllocSize(ObjectImage &Obj,
uint64_t& CodeSize,
uint64_t& DataSizeRO,
uint64_t& DataSizeRW);
unsigned computeSectionStubBufSize(ObjectImage &Obj, const SectionRef &Section);
ObjectImage *loadObject(ObjectImage *InputObject);
public:
RuntimeDyldImpl(RTDyldMemoryManager *mm)
: MemMgr(mm), ProcessAllSections(false), HasError(false) {}
virtual ~RuntimeDyldImpl();
void setProcessAllSections(bool ProcessAllSections) {
this->ProcessAllSections = ProcessAllSections;
}
ObjectImage *loadObject(ObjectBuffer *InputBuffer);
ObjectImage *loadObject(object::ObjectFile *InputObject);
void *getSymbolAddress(StringRef Name) {
SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name);
if (pos == GlobalSymbolTable.end())
return 0;
SymbolLoc Loc = pos->second;
return getSectionAddress(Loc.first) + Loc.second;
}
uint64_t getSymbolLoadAddress(StringRef Name) {
SymbolTableMap::const_iterator pos = GlobalSymbolTable.find(Name);
if (pos == GlobalSymbolTable.end())
return 0;
SymbolLoc Loc = pos->second;
return getSectionLoadAddress(Loc.first) + Loc.second;
}
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 isCompatibleFormat(const ObjectBuffer *Buffer) const = 0;
virtual bool isCompatibleFile(const ObjectFile *Obj) const = 0;
virtual void registerEHFrames();
virtual void deregisterEHFrames();
virtual void finalizeLoad(ObjectImage &ObjImg, ObjSectionToIDMap &SectionMap) {}
};
}
#endif