#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/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 stubOffset, uintptr_t objAddress)
: Name(name), Address(address), Size(size), LoadAddress((uintptr_t)address),
StubOffset(stubOffset), ObjAddress(objAddress) {}
};
class RelocationEntry {
public:
unsigned SectionID;
uintptr_t Offset;
uint32_t RelType;
intptr_t Addend;
RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend)
: SectionID(id), Offset(offset), RelType(type), Addend(addend) {}
};
class ObjRelocationInfo {
public:
unsigned SectionID;
uint64_t Offset;
SymbolRef Symbol;
uint64_t Type;
int64_t AdditionalInfo;
};
class RelocationValueRef {
public:
unsigned SectionID;
intptr_t Addend;
const char *SymbolName;
RelocationValueRef(): SectionID(0), Addend(0), SymbolName(0) {}
inline bool operator==(const RelocationValueRef &Other) const {
return std::memcmp(this, &Other, sizeof(RelocationValueRef)) == 0;
}
inline bool operator <(const RelocationValueRef &Other) const {
return std::memcmp(this, &Other, sizeof(RelocationValueRef)) < 0;
}
};
class RuntimeDyldImpl {
protected:
RTDyldMemoryManager *MemMgr;
typedef SmallVector<SectionEntry, 64> SectionList;
SectionList Sections;
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;
inline unsigned getMaxStubSize() {
if (Arch == Triple::arm || Arch == Triple::thumb)
return 8; else if (Arch == Triple::mipsel || Arch == Triple::mips)
return 16;
else if (Arch == Triple::ppc64)
return 44;
else
return 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;
}
virtual unsigned getCommonSymbolAlignment(const SymbolRef &Sym) {
return 0;
}
void writeInt16BE(uint8_t *Addr, uint16_t Value) {
if (sys::isLittleEndianHost())
Value = sys::SwapByteOrder(Value);
*Addr = (Value >> 8) & 0xFF;
*(Addr+1) = Value & 0xFF;
}
void writeInt32BE(uint8_t *Addr, uint32_t Value) {
if (sys::isLittleEndianHost())
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 (sys::isLittleEndianHost())
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);
void resolveRelocationEntry(const RelocationEntry &RE, uint64_t Value);
virtual void resolveRelocation(const SectionEntry &Section,
uint64_t Offset,
uint64_t Value,
uint32_t Type,
int64_t Addend) = 0;
virtual void processRelocationRef(const ObjRelocationInfo &Rel,
ObjectImage &Obj,
ObjSectionToIDMap &ObjSectionToID,
const SymbolTableMap &Symbols,
StubMap &Stubs) = 0;
void resolveExternalSymbols();
virtual ObjectImage *createObjectImage(ObjectBuffer *InputBuffer);
public:
RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
virtual ~RuntimeDyldImpl();
ObjectImage *loadObject(ObjectBuffer *InputBuffer);
void *getSymbolAddress(StringRef Name) {
if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end())
return 0;
SymbolLoc Loc = GlobalSymbolTable.lookup(Name);
return getSectionAddress(Loc.first) + Loc.second;
}
uint64_t getSymbolLoadAddress(StringRef Name) {
if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end())
return 0;
SymbolLoc Loc = GlobalSymbolTable.lookup(Name);
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;
};
}
#endif