2012-01-16 23:50:58 +00:00
|
|
|
//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
|
2011-07-13 07:57:58 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of the MC-JIT runtime dynamic linker.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "RuntimeDyldMachO.h"
|
2011-07-13 07:57:58 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2014-07-17 18:54:50 +00:00
|
|
|
|
|
|
|
#include "Targets/RuntimeDyldMachOARM.h"
|
|
|
|
#include "Targets/RuntimeDyldMachOAArch64.h"
|
|
|
|
#include "Targets/RuntimeDyldMachOI386.h"
|
|
|
|
#include "Targets/RuntimeDyldMachOX86_64.h"
|
|
|
|
|
2011-07-13 07:57:58 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2014-04-22 03:04:17 +00:00
|
|
|
#define DEBUG_TYPE "dyld"
|
|
|
|
|
2011-07-13 07:57:58 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2014-08-08 23:12:22 +00:00
|
|
|
int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
|
|
|
|
unsigned NumBytes = 1 << RE.Size;
|
2014-08-29 23:17:47 +00:00
|
|
|
uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset;
|
2014-08-27 17:41:06 +00:00
|
|
|
|
2014-08-29 23:17:47 +00:00
|
|
|
return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
|
2014-07-17 18:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
|
|
|
|
ObjectImage &ObjImg, const relocation_iterator &RI,
|
|
|
|
const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
|
|
|
|
const SymbolTableMap &Symbols) {
|
|
|
|
|
|
|
|
const MachOObjectFile &Obj =
|
|
|
|
static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
|
|
|
|
MachO::any_relocation_info RelInfo =
|
|
|
|
Obj.getRelocation(RI->getRawDataRefImpl());
|
|
|
|
RelocationValueRef Value;
|
|
|
|
|
|
|
|
bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
|
|
|
|
if (IsExternal) {
|
|
|
|
symbol_iterator Symbol = RI->getSymbol();
|
|
|
|
StringRef TargetName;
|
|
|
|
Symbol->getName(TargetName);
|
|
|
|
SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
|
|
|
|
if (SI != Symbols.end()) {
|
|
|
|
Value.SectionID = SI->second.first;
|
2014-09-07 04:03:32 +00:00
|
|
|
Value.Offset = SI->second.second + RE.Addend;
|
2014-07-17 18:54:50 +00:00
|
|
|
} else {
|
|
|
|
SI = GlobalSymbolTable.find(TargetName.data());
|
|
|
|
if (SI != GlobalSymbolTable.end()) {
|
|
|
|
Value.SectionID = SI->second.first;
|
2014-09-07 04:03:32 +00:00
|
|
|
Value.Offset = SI->second.second + RE.Addend;
|
2014-07-17 18:54:50 +00:00
|
|
|
} else {
|
|
|
|
Value.SymbolName = TargetName.data();
|
2014-09-07 04:03:32 +00:00
|
|
|
Value.Offset = RE.Addend;
|
2014-07-17 18:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SectionRef Sec = Obj.getRelocationSection(RelInfo);
|
|
|
|
bool IsCode = false;
|
|
|
|
Sec.isText(IsCode);
|
|
|
|
Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
|
|
|
|
uint64_t Addr;
|
|
|
|
Sec.getAddress(Addr);
|
2014-09-07 04:03:32 +00:00
|
|
|
Value.Offset = RE.Addend - Addr;
|
2014-07-17 18:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
|
|
|
|
ObjectImage &ObjImg,
|
2014-07-30 03:35:05 +00:00
|
|
|
const relocation_iterator &RI,
|
|
|
|
unsigned OffsetToNextPC) {
|
2014-07-17 18:54:50 +00:00
|
|
|
const MachOObjectFile &Obj =
|
|
|
|
static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
|
|
|
|
MachO::any_relocation_info RelInfo =
|
|
|
|
Obj.getRelocation(RI->getRawDataRefImpl());
|
|
|
|
|
|
|
|
bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
|
|
|
|
if (IsPCRel) {
|
|
|
|
uint64_t RelocAddr = 0;
|
|
|
|
RI->getAddress(RelocAddr);
|
2014-09-07 04:03:32 +00:00
|
|
|
Value.Offset += RelocAddr + OffsetToNextPC;
|
2014-07-17 18:54:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
|
|
|
|
uint64_t Value) const {
|
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
|
|
|
uint8_t *LocalAddress = Section.Address + RE.Offset;
|
|
|
|
uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
|
|
|
|
|
|
|
|
dbgs() << "resolveRelocation Section: " << RE.SectionID
|
|
|
|
<< " LocalAddress: " << format("%p", LocalAddress)
|
2014-08-28 04:25:17 +00:00
|
|
|
<< " FinalAddress: " << format("0x%016" PRIx64, FinalAddress)
|
|
|
|
<< " Value: " << format("0x%016" PRIx64, Value) << " Addend: " << RE.Addend
|
2014-07-17 18:54:50 +00:00
|
|
|
<< " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
|
|
|
|
<< " Size: " << (1 << RE.Size) << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
|
|
|
|
if (InputBuffer->getBufferSize() < 4)
|
|
|
|
return false;
|
|
|
|
StringRef Magic(InputBuffer->getBufferStart(), 4);
|
|
|
|
if (Magic == "\xFE\xED\xFA\xCE")
|
|
|
|
return true;
|
|
|
|
if (Magic == "\xCE\xFA\xED\xFE")
|
|
|
|
return true;
|
|
|
|
if (Magic == "\xFE\xED\xFA\xCF")
|
|
|
|
return true;
|
|
|
|
if (Magic == "\xCF\xFA\xED\xFE")
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
|
|
|
|
return Obj->isMachO();
|
|
|
|
}
|
|
|
|
|
2014-09-04 04:53:03 +00:00
|
|
|
template <typename Impl>
|
|
|
|
void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(ObjectImage &ObjImg,
|
|
|
|
ObjSectionToIDMap &SectionMap) {
|
|
|
|
unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
|
|
|
|
unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
|
|
|
|
unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
|
|
|
|
ObjSectionToIDMap::iterator i, e;
|
|
|
|
|
|
|
|
for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
|
|
|
|
const SectionRef &Section = i->first;
|
|
|
|
StringRef Name;
|
|
|
|
Section.getName(Name);
|
|
|
|
if (Name == "__eh_frame")
|
|
|
|
EHFrameSID = i->second;
|
|
|
|
else if (Name == "__text")
|
|
|
|
TextSID = i->second;
|
|
|
|
else if (Name == "__gcc_except_tab")
|
|
|
|
ExceptTabSID = i->second;
|
|
|
|
else
|
|
|
|
impl().finalizeSection(ObjImg, i->second, Section);
|
|
|
|
}
|
|
|
|
UnregisteredEHFrameSections.push_back(
|
|
|
|
EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
|
|
|
|
int64_t DeltaForText,
|
|
|
|
int64_t DeltaForEH) {
|
|
|
|
typedef typename Impl::TargetPtrT TargetPtrT;
|
|
|
|
|
2014-05-12 21:39:59 +00:00
|
|
|
DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
|
|
|
|
<< ", Delta for EH: " << DeltaForEH << "\n");
|
2014-03-21 20:28:42 +00:00
|
|
|
uint32_t Length = *((uint32_t *)P);
|
2013-05-05 20:43:10 +00:00
|
|
|
P += 4;
|
|
|
|
unsigned char *Ret = P + Length;
|
2014-03-21 20:28:42 +00:00
|
|
|
uint32_t Offset = *((uint32_t *)P);
|
2013-05-05 20:43:10 +00:00
|
|
|
if (Offset == 0) // is a CIE
|
|
|
|
return Ret;
|
|
|
|
|
|
|
|
P += 4;
|
2014-09-04 04:53:03 +00:00
|
|
|
TargetPtrT FDELocation = *((TargetPtrT*)P);
|
|
|
|
TargetPtrT NewLocation = FDELocation - DeltaForText;
|
|
|
|
*((TargetPtrT*)P) = NewLocation;
|
|
|
|
P += sizeof(TargetPtrT);
|
2013-05-05 20:43:10 +00:00
|
|
|
|
|
|
|
// Skip the FDE address range
|
2014-09-04 04:53:03 +00:00
|
|
|
P += sizeof(TargetPtrT);
|
2013-05-05 20:43:10 +00:00
|
|
|
|
|
|
|
uint8_t Augmentationsize = *P;
|
|
|
|
P += 1;
|
|
|
|
if (Augmentationsize != 0) {
|
2014-09-04 04:53:03 +00:00
|
|
|
TargetPtrT LSDA = *((TargetPtrT *)P);
|
|
|
|
TargetPtrT NewLSDA = LSDA - DeltaForEH;
|
|
|
|
*((TargetPtrT *)P) = NewLSDA;
|
2013-05-05 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2014-09-04 04:53:03 +00:00
|
|
|
static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
|
|
|
|
int64_t ObjDistance = A->ObjAddress - B->ObjAddress;
|
|
|
|
int64_t MemDistance = A->LoadAddress - B->LoadAddress;
|
2013-05-05 20:43:10 +00:00
|
|
|
return ObjDistance - MemDistance;
|
|
|
|
}
|
|
|
|
|
2014-09-04 04:53:03 +00:00
|
|
|
template <typename Impl>
|
|
|
|
void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
|
2013-10-11 21:25:48 +00:00
|
|
|
|
|
|
|
if (!MemMgr)
|
|
|
|
return;
|
|
|
|
for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
|
|
|
|
EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
|
|
|
|
if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
|
|
|
|
SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
|
|
|
|
continue;
|
|
|
|
SectionEntry *Text = &Sections[SectionInfo.TextSID];
|
|
|
|
SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
|
2014-04-24 06:44:33 +00:00
|
|
|
SectionEntry *ExceptTab = nullptr;
|
2013-10-11 21:25:48 +00:00
|
|
|
if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
|
|
|
|
ExceptTab = &Sections[SectionInfo.ExceptTabSID];
|
|
|
|
|
2014-09-04 04:53:03 +00:00
|
|
|
int64_t DeltaForText = computeDelta(Text, EHFrame);
|
|
|
|
int64_t DeltaForEH = 0;
|
2013-10-11 21:25:48 +00:00
|
|
|
if (ExceptTab)
|
|
|
|
DeltaForEH = computeDelta(ExceptTab, EHFrame);
|
2013-05-05 20:43:10 +00:00
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
unsigned char *P = EHFrame->Address;
|
|
|
|
unsigned char *End = P + EHFrame->Size;
|
2014-03-21 20:28:42 +00:00
|
|
|
do {
|
2013-10-11 21:25:48 +00:00
|
|
|
P = processFDE(P, DeltaForText, DeltaForEH);
|
2014-03-21 20:28:42 +00:00
|
|
|
} while (P != End);
|
2013-05-05 20:43:10 +00:00
|
|
|
|
2014-03-21 20:28:42 +00:00
|
|
|
MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
|
2013-10-11 21:25:48 +00:00
|
|
|
EHFrame->Size);
|
|
|
|
}
|
|
|
|
UnregisteredEHFrameSections.clear();
|
|
|
|
}
|
2013-05-05 20:43:10 +00:00
|
|
|
|
2014-07-17 18:54:50 +00:00
|
|
|
std::unique_ptr<RuntimeDyldMachO>
|
|
|
|
llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
|
2012-03-30 16:45:19 +00:00
|
|
|
switch (Arch) {
|
2014-03-21 20:28:42 +00:00
|
|
|
default:
|
2014-07-17 18:54:50 +00:00
|
|
|
llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
|
2014-07-11 23:52:07 +00:00
|
|
|
break;
|
2014-07-17 18:54:50 +00:00
|
|
|
case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
|
2014-07-23 12:32:47 +00:00
|
|
|
case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
|
2014-07-17 18:54:50 +00:00
|
|
|
case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
|
|
|
|
case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
|
2014-07-11 23:52:07 +00:00
|
|
|
}
|
2014-01-08 04:09:09 +00:00
|
|
|
}
|
|
|
|
|
2011-07-13 07:57:58 +00:00
|
|
|
} // end namespace llvm
|