#define DEBUG_TYPE "dyld"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include "llvm/Object/MachOObject.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.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"
using namespace llvm;
using namespace llvm::object;
RTDyldMemoryManager::~RTDyldMemoryManager() {}
namespace llvm {
class RuntimeDyldImpl {
unsigned CPUType;
unsigned CPUSubtype;
RTDyldMemoryManager *MemMgr;
StringMap<sys::MemoryBlock> Functions;
StringMap<uint8_t*> SymbolTable;
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 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);
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:
RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
bool loadObject(MemoryBuffer *InputBuffer);
void *getSymbolAddress(StringRef Name) {
return SymbolTable.lookup(Name);
}
void resolveRelocations();
void reassignSymbolAddress(StringRef Name, uint8_t *Addr);
bool hasError() { return HasError; }
void clearError() { HasError = false; }
StringRef getErrorString() { return ErrorStr; }
};
void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
uint8_t *EndAddress) {
uintptr_t Size = EndAddress - StartAddress + 1;
uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), Size);
assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) &&
"Memory manager failed to allocate enough memory!");
memcpy(Mem, StartAddress, EndAddress - StartAddress + 1);
MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
Functions[Name] = sys::MemoryBlock(Mem, Size);
SymbolTable[Name] = Mem;
DEBUG(dbgs() << " allocated to " << Mem << "\n");
}
bool RuntimeDyldImpl::
resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel,
unsigned Type, unsigned Size) {
switch (CPUType) {
default: assert(0 && "Unsupported CPU type!");
case mach::CTM_x86_64:
return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value,
isPCRel, Type, Size);
case mach::CTM_ARM:
return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value,
isPCRel, Type, Size);
}
llvm_unreachable("");
}
bool RuntimeDyldImpl::
resolveX86_64Relocation(uintptr_t Address, uintptr_t Value,
bool isPCRel, unsigned Type,
unsigned Size) {
if (isPCRel)
Value -= Address + 4;
switch(Type) {
default:
llvm_unreachable("Invalid relocation type!");
case macho::RIT_X86_64_Unsigned:
case macho::RIT_X86_64_Branch: {
uint8_t *p = (uint8_t*)Address;
for (unsigned i = 0; i < Size; ++i) {
*p++ = (uint8_t)Value;
Value >>= 8;
}
return false;
}
case macho::RIT_X86_64_Signed:
case macho::RIT_X86_64_GOTLoad:
case macho::RIT_X86_64_GOT:
case macho::RIT_X86_64_Subtractor:
case macho::RIT_X86_64_Signed1:
case macho::RIT_X86_64_Signed2:
case macho::RIT_X86_64_Signed4:
case macho::RIT_X86_64_TLV:
return Error("Relocation type not implemented yet!");
}
return false;
}
bool RuntimeDyldImpl::resolveARMRelocation(uintptr_t Address, uintptr_t Value,
bool isPCRel, unsigned Type,
unsigned Size) {
if (isPCRel) {
Value -= Address;
Value -= 8;
}
switch(Type) {
default:
llvm_unreachable("Invalid relocation type!");
case macho::RIT_Vanilla: {
llvm_unreachable("Invalid relocation type!");
uint8_t *p = (uint8_t*)Address;
for (unsigned i = 0; i < Size; ++i) {
*p++ = (uint8_t)Value;
Value >>= 8;
}
break;
}
case macho::RIT_ARM_Branch24Bit: {
uint32_t *p = (uint32_t*)Address;
Value >>= 2;
Value &= 0xffffff;
*p = (*p & ~0xffffff) | Value;
break;
}
case macho::RIT_ARM_ThumbBranch22Bit:
case macho::RIT_ARM_ThumbBranch32Bit:
case macho::RIT_ARM_Half:
case macho::RIT_ARM_HalfDifference:
case macho::RIT_Pair:
case macho::RIT_Difference:
case macho::RIT_ARM_LocalDifference:
case macho::RIT_ARM_PreboundLazyPointer:
return Error("Relocation type not implemented yet!");
}
return false;
}
bool RuntimeDyldImpl::
loadSegment32(const MachOObject *Obj,
const MachOObject::LoadCommandInfo *SegmentLCI,
const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
if (!SegmentLC)
return Error("unable to load segment load command");
for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
InMemoryStruct<macho::Section> Sect;
Obj->ReadSection(*SegmentLCI, SectNum, Sect);
if (!Sect)
return Error("unable to load section: '" + Twine(SectNum) + "'");
if (Sect->Flags != 0x80000400)
return Error("unsupported section type!");
typedef std::pair<uint64_t, StringRef> SymbolEntry;
SmallVector<SymbolEntry, 64> Symbols;
SmallVector<StringRef, 64> SymbolNames;
for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
InMemoryStruct<macho::SymbolTableEntry> STE;
Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
if (!STE)
return Error("unable to read symbol: '" + Twine(i) + "'");
if (STE->SectionIndex > SegmentLC->NumSections)
return Error("invalid section index for symbol: '" + Twine(i) + "'");
StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
SymbolNames.push_back(Name);
if ((unsigned)STE->SectionIndex - 1 != SectNum)
continue;
if (STE->Type != 0xF) return Error("unexpected symbol type!");
if (STE->Flags != 0x0 && STE->Flags != 0x8)
return Error("unexpected symbol type!");
Symbols.push_back(SymbolEntry(STE->Value, Name));
DEBUG(dbgs() << "Function sym: '" << Name << "' @ " <<
(Sect->Address + STE->Value) << "\n");
}
array_pod_sort(Symbols.begin(), Symbols.end());
uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
SegmentLC->FileSize).data();
for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
uint64_t StartOffset = Sect->Address + Symbols[i].first;
uint64_t EndOffset = Symbols[i + 1].first - 1;
DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
<< " from [" << StartOffset << ", " << EndOffset << "]\n");
extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
}
uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
uint64_t EndOffset = Sect->Size - 1;
DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
<< " from [" << StartOffset << ", " << EndOffset << "]\n");
extractFunction(Symbols[Symbols.size()-1].second,
Base + StartOffset, Base + EndOffset);
for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
InMemoryStruct<macho::RelocationEntry> RE;
Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
if (RE->Word0 & macho::RF_Scattered)
return Error("NOT YET IMPLEMENTED: scattered relocations.");
uint32_t Offset = RE->Word0;
unsigned SymbolNum;
unsigned NumSymbols = Symbols.size();
assert(NumSymbols > 0 && Symbols[0].first <= Offset &&
"No symbol containing relocation!");
for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum)
if (Symbols[SymbolNum + 1].first > Offset)
break;
Offset -= Symbols[SymbolNum].first;
StringRef TargetName = SymbolNames[SymbolNum];
bool isExtern = (RE->Word1 >> 27) & 1;
if (!isExtern)
return Error("Internal relocations not supported.");
uint32_t SourceNum = RE->Word1 & 0xffffff; StringRef SourceName = SymbolNames[SourceNum];
Relocations[SourceName].push_back(RelocationEntry(TargetName,
Offset,
RE->Word1,
0 ));
DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset
<< " from '" << SourceName << "(Word1: "
<< format("0x%x", RE->Word1) << ")\n");
}
}
return false;
}
bool RuntimeDyldImpl::
loadSegment64(const MachOObject *Obj,
const MachOObject::LoadCommandInfo *SegmentLCI,
const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
if (!Segment64LC)
return Error("unable to load segment load command");
for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
InMemoryStruct<macho::Section64> Sect;
Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
if (!Sect)
return Error("unable to load section: '" + Twine(SectNum) + "'");
if (Sect->Flags != 0x80000400)
return Error("unsupported section type!");
typedef std::pair<uint64_t, StringRef> SymbolEntry;
SmallVector<SymbolEntry, 64> Symbols;
SmallVector<StringRef, 64> SymbolNames;
for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
InMemoryStruct<macho::Symbol64TableEntry> STE;
Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
if (!STE)
return Error("unable to read symbol: '" + Twine(i) + "'");
if (STE->SectionIndex > Segment64LC->NumSections)
return Error("invalid section index for symbol: '" + Twine(i) + "'");
StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
SymbolNames.push_back(Name);
if ((unsigned)STE->SectionIndex - 1 != SectNum)
continue;
if (STE->Type != 0xF) return Error("unexpected symbol type!");
if (STE->Flags != 0x0)
return Error("unexpected symbol type!");
Symbols.push_back(SymbolEntry(STE->Value, Name));
DEBUG(dbgs() << "Function sym: '" << Name << "' @ " <<
(Sect->Address + STE->Value) << "\n");
}
array_pod_sort(Symbols.begin(), Symbols.end());
uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
Segment64LC->FileSize).data();
for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
uint64_t StartOffset = Sect->Address + Symbols[i].first;
uint64_t EndOffset = Symbols[i + 1].first - 1;
DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
<< " from [" << StartOffset << ", " << EndOffset << "]\n");
extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
}
uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
uint64_t EndOffset = Sect->Size - 1;
DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
<< " from [" << StartOffset << ", " << EndOffset << "]\n");
extractFunction(Symbols[Symbols.size()-1].second,
Base + StartOffset, Base + EndOffset);
for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
InMemoryStruct<macho::RelocationEntry> RE;
Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
if (RE->Word0 & macho::RF_Scattered)
return Error("NOT YET IMPLEMENTED: scattered relocations.");
uint32_t Offset = RE->Word0;
unsigned SymbolNum;
unsigned NumSymbols = Symbols.size();
assert(NumSymbols > 0 && Symbols[0].first <= Offset &&
"No symbol containing relocation!");
for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum)
if (Symbols[SymbolNum + 1].first > Offset)
break;
Offset -= Symbols[SymbolNum].first;
StringRef TargetName = SymbolNames[SymbolNum];
bool isExtern = (RE->Word1 >> 27) & 1;
if (!isExtern)
return Error("Internal relocations not supported.");
uint32_t SourceNum = RE->Word1 & 0xffffff; StringRef SourceName = SymbolNames[SourceNum];
Relocations[SourceName].push_back(RelocationEntry(TargetName,
Offset,
RE->Word1,
0 ));
DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset
<< " from '" << SourceName << "(Word1: "
<< format("0x%x", RE->Word1) << ")\n");
}
}
return false;
}
bool RuntimeDyldImpl::loadObject(MemoryBuffer *InputBuffer) {
if (hasError())
return true;
std::string ErrorStr;
OwningPtr<MachOObject> Obj(
MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
if (!Obj)
return Error("unable to load object: '" + ErrorStr + "'");
const macho::Header &Header = Obj->getHeader();
CPUType = Header.CPUType;
CPUSubtype = Header.CPUSubtype;
const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
*DysymtabLCI = 0;
for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
switch (LCI.Command.Type) {
case macho::LCT_Segment:
case macho::LCT_Segment64:
if (SegmentLCI)
return Error("unexpected input object (multiple segments)");
SegmentLCI = &LCI;
break;
case macho::LCT_Symtab:
if (SymtabLCI)
return Error("unexpected input object (multiple symbol tables)");
SymtabLCI = &LCI;
break;
case macho::LCT_Dysymtab:
if (DysymtabLCI)
return Error("unexpected input object (multiple symbol tables)");
DysymtabLCI = &LCI;
break;
default:
return Error("unexpected input object (unexpected load command");
}
}
if (!SymtabLCI)
return Error("no symbol table found in object");
if (!SegmentLCI)
return Error("no symbol table found in object");
InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
if (!SymtabLC)
return Error("unable to load symbol table load command");
Obj->RegisterStringTable(*SymtabLC);
if (DysymtabLCI) {
InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
if (!DysymtabLC)
return Error("unable to load dynamic link-exit load command");
}
if (SegmentLCI->Command.Type == macho::LCT_Segment) {
if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
return true;
} else {
if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
return true;
}
return false;
}
void RuntimeDyldImpl::resolveRelocations() {
StringMap<uint8_t*>::iterator i = SymbolTable.begin();
StringMap<uint8_t*>::iterator e = SymbolTable.end();
for (;i != e; ++i)
reassignSymbolAddress(i->getKey(), i->getValue());
}
void RuntimeDyldImpl::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
SymbolTable[Name] = Addr;
RelocationList &Relocs = Relocations[Name];
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
RelocationEntry &RE = Relocs[i];
uint8_t *Target = SymbolTable[RE.Target] + RE.Offset;
bool isPCRel = (RE.Data >> 24) & 1;
unsigned Type = (RE.Data >> 28) & 0xf;
unsigned Size = 1 << ((RE.Data >> 25) & 3);
DEBUG(dbgs() << "Resolving relocation at '" << RE.Target
<< "' + " << RE.Offset << " (" << format("%p", Target) << ")"
<< " from '" << Name << " (" << format("%p", Addr) << ")"
<< "(" << (isPCRel ? "pcrel" : "absolute")
<< ", type: " << Type << ", Size: " << Size << ").\n");
resolveRelocation(Target, Addr, isPCRel, Type, Size);
RE.isResolved = true;
}
}
RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) {
Dyld = new RuntimeDyldImpl(MM);
}
RuntimeDyld::~RuntimeDyld() {
delete Dyld;
}
bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
return Dyld->loadObject(InputBuffer);
}
void *RuntimeDyld::getSymbolAddress(StringRef Name) {
return Dyld->getSymbolAddress(Name);
}
void RuntimeDyld::resolveRelocations() {
Dyld->resolveRelocations();
}
void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) {
Dyld->reassignSymbolAddress(Name, Addr);
}
StringRef RuntimeDyld::getErrorString() {
return Dyld->getErrorString();
}
}