#ifndef LLVM_RUNTIME_DYLD_IMPL_H
#define LLVM_RUNTIME_DYLD_IMPL_H
#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include "llvm/Object/MachOObject.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/system_error.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
using namespace llvm::object;
namespace llvm {
class RuntimeDyldImpl {
protected:
unsigned CPUType;
unsigned CPUSubtype;
RTDyldMemoryManager *MemMgr;
StringMap<sys::MemoryBlock> Functions;
StringMap<uint8_t*> SymbolTable;
bool HasError;
std::string ErrorStr;
bool Error(const Twine &Msg) {
ErrorStr = Msg.str();
HasError = true;
return true;
}
void extractFunction(StringRef Name, uint8_t *StartAddress,
uint8_t *EndAddress);
public:
RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
virtual ~RuntimeDyldImpl();
virtual bool loadObject(MemoryBuffer *InputBuffer) = 0;
void *getSymbolAddress(StringRef Name) {
return SymbolTable.lookup(Name);
}
void resolveRelocations();
virtual void reassignSymbolAddress(StringRef Name, uint8_t *Addr) = 0;
bool hasError() { return HasError; }
void clearError() { HasError = false; }
StringRef getErrorString() { return ErrorStr; }
virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0;
};
class RuntimeDyldMachO : public RuntimeDyldImpl {
struct RelocationEntry {
std::string Target; uint64_t Offset; uint32_t Data; int64_t Addend; bool isResolved;
RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend)
: Target(t), Offset(offset), Data(data), Addend(addend),
isResolved(false) {}
};
typedef SmallVector<RelocationEntry, 4> RelocationList;
StringMap<RelocationList> Relocations;
bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
unsigned Type, unsigned Size);
bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
unsigned Type, unsigned Size);
bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
unsigned Type, unsigned Size);
bool loadSegment32(const MachOObject *Obj,
const MachOObject::LoadCommandInfo *SegmentLCI,
const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
bool loadSegment64(const MachOObject *Obj,
const MachOObject::LoadCommandInfo *SegmentLCI,
const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
public:
RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {}
bool loadObject(MemoryBuffer *InputBuffer);
void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
static bool isKnownFormat(const MemoryBuffer *InputBuffer);
bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const {
return isKnownFormat(InputBuffer);
}
};
}
#endif