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 {
|
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
|
|
|
uint8_t *LocalAddress = Section.Address + RE.Offset;
|
|
|
|
unsigned NumBytes = 1 << RE.Size;
|
2014-07-22 21:42:46 +00:00
|
|
|
int64_t Addend = 0;
|
2014-07-17 18:54:50 +00:00
|
|
|
memcpy(&Addend, LocalAddress, NumBytes);
|
2014-07-19 00:19:17 +00:00
|
|
|
return Addend;
|
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;
|
|
|
|
Value.Addend = SI->second.second + RE.Addend;
|
|
|
|
} else {
|
|
|
|
SI = GlobalSymbolTable.find(TargetName.data());
|
|
|
|
if (SI != GlobalSymbolTable.end()) {
|
|
|
|
Value.SectionID = SI->second.first;
|
|
|
|
Value.Addend = SI->second.second + RE.Addend;
|
|
|
|
} else {
|
|
|
|
Value.SymbolName = TargetName.data();
|
|
|
|
Value.Addend = RE.Addend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
Value.Addend = RE.Addend - Addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-30 03:35:05 +00:00
|
|
|
Value.Addend += 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-25 18:37:38 +00:00
|
|
|
<< " FinalAddress: " << format("0x%x", FinalAddress)
|
|
|
|
<< " Value: " << format("0x%x", Value) << " Addend: " << RE.Addend
|
2014-07-17 18:54:50 +00:00
|
|
|
<< " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
|
|
|
|
<< " Size: " << (1 << RE.Size) << "\n";
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:43:16 +00:00
|
|
|
bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Dst, uint64_t Value,
|
2014-07-17 18:54:50 +00:00
|
|
|
unsigned Size) {
|
2014-08-18 21:43:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
// If host and target endianness match use memcpy, otherwise copy in reverse
|
|
|
|
// order.
|
|
|
|
if (IsTargetLittleEndian == sys::IsLittleEndianHost)
|
|
|
|
memcpy(Dst, &Value, Size);
|
|
|
|
else {
|
|
|
|
uint8_t *Src = reinterpret_cast<uint8_t*>(&Value) + Size - 1;
|
|
|
|
for (unsigned i = 0; i < Size; ++i)
|
|
|
|
*Dst++ = *Src--;
|
2014-07-17 18:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-21 20:28:42 +00:00
|
|
|
static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
|
|
|
|
intptr_t DeltaForEH) {
|
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-03-21 20:28:42 +00:00
|
|
|
intptr_t FDELocation = *((intptr_t *)P);
|
2013-05-05 20:43:10 +00:00
|
|
|
intptr_t NewLocation = FDELocation - DeltaForText;
|
2014-03-21 20:28:42 +00:00
|
|
|
*((intptr_t *)P) = NewLocation;
|
2013-05-05 20:43:10 +00:00
|
|
|
P += sizeof(intptr_t);
|
|
|
|
|
|
|
|
// Skip the FDE address range
|
|
|
|
P += sizeof(intptr_t);
|
|
|
|
|
|
|
|
uint8_t Augmentationsize = *P;
|
|
|
|
P += 1;
|
|
|
|
if (Augmentationsize != 0) {
|
2014-03-21 20:28:42 +00:00
|
|
|
intptr_t LSDA = *((intptr_t *)P);
|
2013-05-05 20:43:10 +00:00
|
|
|
intptr_t NewLSDA = LSDA - DeltaForEH;
|
2014-03-21 20:28:42 +00:00
|
|
|
*((intptr_t *)P) = NewLSDA;
|
2013-05-05 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
|
2014-03-21 20:28:42 +00:00
|
|
|
intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
|
2013-05-05 20:43:10 +00:00
|
|
|
intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
|
|
|
|
return ObjDistance - MemDistance;
|
|
|
|
}
|
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
void RuntimeDyldMachO::registerEHFrames() {
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
intptr_t DeltaForText = computeDelta(Text, EHFrame);
|
|
|
|
intptr_t DeltaForEH = 0;
|
|
|
|
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
|