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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "dyld"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "RuntimeDyldMachO.h"
|
2011-07-13 07:57:58 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2011-07-13 07:57:58 +00:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2013-05-05 20:43:10 +00:00
|
|
|
static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText, intptr_t DeltaForEH) {
|
|
|
|
uint32_t Length = *((uint32_t*)P);
|
|
|
|
P += 4;
|
|
|
|
unsigned char *Ret = P + Length;
|
|
|
|
uint32_t Offset = *((uint32_t*)P);
|
|
|
|
if (Offset == 0) // is a CIE
|
|
|
|
return Ret;
|
|
|
|
|
|
|
|
P += 4;
|
|
|
|
intptr_t FDELocation = *((intptr_t*)P);
|
|
|
|
intptr_t NewLocation = FDELocation - DeltaForText;
|
|
|
|
*((intptr_t*)P) = NewLocation;
|
|
|
|
P += sizeof(intptr_t);
|
|
|
|
|
|
|
|
// Skip the FDE address range
|
|
|
|
P += sizeof(intptr_t);
|
|
|
|
|
|
|
|
uint8_t Augmentationsize = *P;
|
|
|
|
P += 1;
|
|
|
|
if (Augmentationsize != 0) {
|
|
|
|
intptr_t LSDA = *((intptr_t*)P);
|
|
|
|
intptr_t NewLSDA = LSDA - DeltaForEH;
|
|
|
|
*((intptr_t*)P) = NewLSDA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
|
|
|
|
intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
|
|
|
|
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];
|
|
|
|
SectionEntry *ExceptTab = NULL;
|
|
|
|
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;
|
|
|
|
do {
|
|
|
|
P = processFDE(P, DeltaForText, DeltaForEH);
|
|
|
|
} while(P != End);
|
2013-05-05 20:43:10 +00:00
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
MemMgr->registerEHFrames(EHFrame->Address,
|
|
|
|
EHFrame->LoadAddress,
|
|
|
|
EHFrame->Size);
|
|
|
|
}
|
|
|
|
UnregisteredEHFrameSections.clear();
|
|
|
|
}
|
2013-05-05 20:43:10 +00:00
|
|
|
|
2013-10-11 21:25:48 +00:00
|
|
|
void RuntimeDyldMachO::finalizeLoad(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;
|
|
|
|
}
|
|
|
|
UnregisteredEHFrameSections.push_back(EHFrameRelatedSections(EHFrameSID,
|
|
|
|
TextSID,
|
|
|
|
ExceptTabSID));
|
2013-05-05 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 19:38:06 +00:00
|
|
|
// The target location for the relocation is described by RE.SectionID and
|
|
|
|
// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
|
|
|
|
// SectionEntry has three members describing its location.
|
|
|
|
// SectionEntry::Address is the address at which the section has been loaded
|
|
|
|
// into memory in the current (host) process. SectionEntry::LoadAddress is the
|
|
|
|
// address that the section will have in the target process.
|
|
|
|
// SectionEntry::ObjAddress is the address of the bits for this section in the
|
|
|
|
// original emitted object image (also in the current address space).
|
|
|
|
//
|
|
|
|
// Relocations will be applied as if the section were loaded at
|
|
|
|
// SectionEntry::LoadAddress, but they will be applied at an address based
|
|
|
|
// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to
|
|
|
|
// Target memory contents if they are required for value calculations.
|
|
|
|
//
|
|
|
|
// The Value parameter here is the load address of the symbol for the
|
|
|
|
// relocation to be applied. For relocations which refer to symbols in the
|
|
|
|
// current object Value will be the LoadAddress of the section in which
|
|
|
|
// the symbol resides (RE.Addend provides additional information about the
|
|
|
|
// symbol location). For external symbols, Value will be the address of the
|
|
|
|
// symbol in the target address space.
|
2013-04-29 17:24:34 +00:00
|
|
|
void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
|
|
|
|
uint64_t Value) {
|
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
|
|
|
return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
|
|
|
|
RE.IsPCRel, RE.Size);
|
|
|
|
}
|
|
|
|
|
2012-11-02 19:45:23 +00:00
|
|
|
void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
|
|
|
|
uint64_t Offset,
|
2012-03-30 16:45:19 +00:00
|
|
|
uint64_t Value,
|
|
|
|
uint32_t Type,
|
2013-04-29 17:24:34 +00:00
|
|
|
int64_t Addend,
|
|
|
|
bool isPCRel,
|
|
|
|
unsigned LogSize) {
|
2012-11-02 19:45:23 +00:00
|
|
|
uint8_t *LocalAddress = Section.Address + Offset;
|
|
|
|
uint64_t FinalAddress = Section.LoadAddress + Offset;
|
2013-04-29 17:24:34 +00:00
|
|
|
unsigned MachoType = Type;
|
|
|
|
unsigned Size = 1 << LogSize;
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2012-04-30 10:06:27 +00:00
|
|
|
DEBUG(dbgs() << "resolveRelocation LocalAddress: "
|
|
|
|
<< format("%p", LocalAddress)
|
2012-03-30 16:45:19 +00:00
|
|
|
<< " FinalAddress: " << format("%p", FinalAddress)
|
|
|
|
<< " Value: " << format("%p", Value)
|
|
|
|
<< " Addend: " << Addend
|
|
|
|
<< " isPCRel: " << isPCRel
|
|
|
|
<< " MachoType: " << MachoType
|
|
|
|
<< " Size: " << Size
|
|
|
|
<< "\n");
|
|
|
|
|
2011-07-13 07:57:58 +00:00
|
|
|
// This just dispatches to the proper target specific routine.
|
2012-03-30 16:45:19 +00:00
|
|
|
switch (Arch) {
|
2012-02-07 05:05:23 +00:00
|
|
|
default: llvm_unreachable("Unsupported CPU type!");
|
2012-03-30 16:45:19 +00:00
|
|
|
case Triple::x86_64:
|
|
|
|
resolveX86_64Relocation(LocalAddress,
|
|
|
|
FinalAddress,
|
|
|
|
(uintptr_t)Value,
|
|
|
|
isPCRel,
|
|
|
|
MachoType,
|
|
|
|
Size,
|
|
|
|
Addend);
|
|
|
|
break;
|
|
|
|
case Triple::x86:
|
|
|
|
resolveI386Relocation(LocalAddress,
|
2012-04-30 10:06:27 +00:00
|
|
|
FinalAddress,
|
|
|
|
(uintptr_t)Value,
|
|
|
|
isPCRel,
|
2012-09-13 01:24:32 +00:00
|
|
|
MachoType,
|
2012-04-30 10:06:27 +00:00
|
|
|
Size,
|
|
|
|
Addend);
|
2012-03-30 16:45:19 +00:00
|
|
|
break;
|
|
|
|
case Triple::arm: // Fall through.
|
|
|
|
case Triple::thumb:
|
|
|
|
resolveARMRelocation(LocalAddress,
|
|
|
|
FinalAddress,
|
|
|
|
(uintptr_t)Value,
|
|
|
|
isPCRel,
|
|
|
|
MachoType,
|
|
|
|
Size,
|
|
|
|
Addend);
|
|
|
|
break;
|
2011-07-13 07:57:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 10:06:27 +00:00
|
|
|
bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
|
|
|
|
uint64_t FinalAddress,
|
|
|
|
uint64_t Value,
|
|
|
|
bool isPCRel,
|
|
|
|
unsigned Type,
|
|
|
|
unsigned Size,
|
|
|
|
int64_t Addend) {
|
2012-03-26 20:45:52 +00:00
|
|
|
if (isPCRel)
|
|
|
|
Value -= FinalAddress + 4; // see resolveX86_64Relocation
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid relocation type!");
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::GENERIC_RELOC_VANILLA: {
|
2012-03-26 20:45:52 +00:00
|
|
|
uint8_t *p = LocalAddress;
|
|
|
|
uint64_t ValueToWrite = Value + Addend;
|
|
|
|
for (unsigned i = 0; i < Size; ++i) {
|
|
|
|
*p++ = (uint8_t)(ValueToWrite & 0xff);
|
|
|
|
ValueToWrite >>= 8;
|
|
|
|
}
|
2013-01-31 19:46:28 +00:00
|
|
|
return false;
|
2012-03-26 20:45:52 +00:00
|
|
|
}
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::GENERIC_RELOC_SECTDIFF:
|
|
|
|
case MachO::GENERIC_RELOC_LOCAL_SECTDIFF:
|
|
|
|
case MachO::GENERIC_RELOC_PB_LA_PTR:
|
2012-03-26 20:45:52 +00:00
|
|
|
return Error("Relocation type not implemented yet!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 10:06:27 +00:00
|
|
|
bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
|
|
|
|
uint64_t FinalAddress,
|
|
|
|
uint64_t Value,
|
|
|
|
bool isPCRel,
|
|
|
|
unsigned Type,
|
|
|
|
unsigned Size,
|
|
|
|
int64_t Addend) {
|
2011-07-13 07:57:58 +00:00
|
|
|
// If the relocation is PC-relative, the value to be encoded is the
|
|
|
|
// pointer difference.
|
|
|
|
if (isPCRel)
|
|
|
|
// FIXME: It seems this value needs to be adjusted by 4 for an effective PC
|
|
|
|
// address. Is that expected? Only for branches, perhaps?
|
2012-03-07 23:05:25 +00:00
|
|
|
Value -= FinalAddress + 4;
|
2011-07-13 07:57:58 +00:00
|
|
|
|
|
|
|
switch(Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid relocation type!");
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::X86_64_RELOC_SIGNED_1:
|
|
|
|
case MachO::X86_64_RELOC_SIGNED_2:
|
|
|
|
case MachO::X86_64_RELOC_SIGNED_4:
|
|
|
|
case MachO::X86_64_RELOC_SIGNED:
|
|
|
|
case MachO::X86_64_RELOC_UNSIGNED:
|
|
|
|
case MachO::X86_64_RELOC_BRANCH: {
|
2012-01-16 23:50:49 +00:00
|
|
|
Value += Addend;
|
2011-07-13 07:57:58 +00:00
|
|
|
// Mask in the target value a byte at a time (we don't have an alignment
|
|
|
|
// guarantee for the target address, so this is safest).
|
2012-03-07 23:05:25 +00:00
|
|
|
uint8_t *p = (uint8_t*)LocalAddress;
|
2011-07-13 07:57:58 +00:00
|
|
|
for (unsigned i = 0; i < Size; ++i) {
|
|
|
|
*p++ = (uint8_t)Value;
|
|
|
|
Value >>= 8;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::X86_64_RELOC_GOT_LOAD:
|
|
|
|
case MachO::X86_64_RELOC_GOT:
|
|
|
|
case MachO::X86_64_RELOC_SUBTRACTOR:
|
|
|
|
case MachO::X86_64_RELOC_TLV:
|
2011-07-13 07:57:58 +00:00
|
|
|
return Error("Relocation type not implemented yet!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 10:06:27 +00:00
|
|
|
bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
|
|
|
|
uint64_t FinalAddress,
|
|
|
|
uint64_t Value,
|
|
|
|
bool isPCRel,
|
|
|
|
unsigned Type,
|
|
|
|
unsigned Size,
|
|
|
|
int64_t Addend) {
|
2011-07-13 07:57:58 +00:00
|
|
|
// If the relocation is PC-relative, the value to be encoded is the
|
|
|
|
// pointer difference.
|
|
|
|
if (isPCRel) {
|
2012-03-07 23:05:25 +00:00
|
|
|
Value -= FinalAddress;
|
2011-07-13 07:57:58 +00:00
|
|
|
// ARM PCRel relocations have an effective-PC offset of two instructions
|
|
|
|
// (four bytes in Thumb mode, 8 bytes in ARM mode).
|
|
|
|
// FIXME: For now, assume ARM mode.
|
|
|
|
Value -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(Type) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid relocation type!");
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::ARM_RELOC_VANILLA: {
|
2011-07-13 07:57:58 +00:00
|
|
|
// Mask in the target value a byte at a time (we don't have an alignment
|
|
|
|
// guarantee for the target address, so this is safest).
|
2012-03-07 23:05:25 +00:00
|
|
|
uint8_t *p = (uint8_t*)LocalAddress;
|
2011-07-13 07:57:58 +00:00
|
|
|
for (unsigned i = 0; i < Size; ++i) {
|
2013-08-27 05:38:30 +00:00
|
|
|
*p++ = (uint8_t)Value;
|
2011-07-13 07:57:58 +00:00
|
|
|
Value >>= 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::ARM_RELOC_BR24: {
|
2011-07-13 07:57:58 +00:00
|
|
|
// Mask the value into the target address. We know instructions are
|
|
|
|
// 32-bit aligned, so we can do it all at once.
|
2012-03-07 23:05:25 +00:00
|
|
|
uint32_t *p = (uint32_t*)LocalAddress;
|
2011-07-13 07:57:58 +00:00
|
|
|
// The low two bits of the value are not encoded.
|
|
|
|
Value >>= 2;
|
|
|
|
// Mask the value to 24 bits.
|
|
|
|
Value &= 0xffffff;
|
|
|
|
// FIXME: If the destination is a Thumb function (and the instruction
|
|
|
|
// is a non-predicated BL instruction), we need to change it to a BLX
|
|
|
|
// instruction instead.
|
|
|
|
|
|
|
|
// Insert the value into the instruction.
|
|
|
|
*p = (*p & ~0xffffff) | Value;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-01 04:28:48 +00:00
|
|
|
case MachO::ARM_THUMB_RELOC_BR22:
|
|
|
|
case MachO::ARM_THUMB_32BIT_BRANCH:
|
|
|
|
case MachO::ARM_RELOC_HALF:
|
|
|
|
case MachO::ARM_RELOC_HALF_SECTDIFF:
|
|
|
|
case MachO::ARM_RELOC_PAIR:
|
|
|
|
case MachO::ARM_RELOC_SECTDIFF:
|
|
|
|
case MachO::ARM_RELOC_LOCAL_SECTDIFF:
|
|
|
|
case MachO::ARM_RELOC_PB_LA_PTR:
|
2011-07-13 07:57:58 +00:00
|
|
|
return Error("Relocation type not implemented yet!");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-29 14:44:23 +00:00
|
|
|
void RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
|
2013-04-29 19:03:21 +00:00
|
|
|
RelocationRef RelI,
|
2012-04-16 22:12:58 +00:00
|
|
|
ObjectImage &Obj,
|
2012-03-30 16:45:19 +00:00
|
|
|
ObjSectionToIDMap &ObjSectionToID,
|
2012-05-01 06:58:59 +00:00
|
|
|
const SymbolTableMap &Symbols,
|
2012-03-30 16:45:19 +00:00
|
|
|
StubMap &Stubs) {
|
2013-04-29 14:44:23 +00:00
|
|
|
const ObjectFile *OF = Obj.getObjectFile();
|
|
|
|
const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
|
2013-09-01 04:28:48 +00:00
|
|
|
MachO::any_relocation_info RE= MachO->getRelocation(RelI.getRawDataRefImpl());
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2013-04-29 14:44:23 +00:00
|
|
|
uint32_t RelType = MachO->getAnyRelocationType(RE);
|
2013-08-09 00:57:01 +00:00
|
|
|
|
|
|
|
// FIXME: Properly handle scattered relocations.
|
|
|
|
// For now, optimistically skip these: they can often be ignored, as
|
|
|
|
// the static linker will already have applied the relocation, and it
|
|
|
|
// only needs to be reapplied if symbols move relative to one another.
|
|
|
|
// Note: This will fail horribly where the relocations *do* need to be
|
|
|
|
// applied, but that was already the case.
|
|
|
|
if (MachO->isRelocationScattered(RE))
|
|
|
|
return;
|
|
|
|
|
2012-03-30 16:45:19 +00:00
|
|
|
RelocationValueRef Value;
|
2013-04-29 14:44:23 +00:00
|
|
|
SectionEntry &Section = Sections[SectionID];
|
2012-03-30 16:45:19 +00:00
|
|
|
|
2013-04-29 14:44:23 +00:00
|
|
|
bool isExtern = MachO->getPlainRelocationExternal(RE);
|
2013-04-29 17:24:34 +00:00
|
|
|
bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
|
|
|
|
unsigned Size = MachO->getAnyRelocationLength(RE);
|
2013-04-30 01:29:57 +00:00
|
|
|
uint64_t Offset;
|
|
|
|
RelI.getOffset(Offset);
|
2013-04-30 15:40:54 +00:00
|
|
|
uint8_t *LocalAddress = Section.Address + Offset;
|
|
|
|
unsigned NumBytes = 1 << Size;
|
|
|
|
uint64_t Addend = 0;
|
|
|
|
memcpy(&Addend, LocalAddress, NumBytes);
|
2013-04-30 01:29:57 +00:00
|
|
|
|
2013-04-30 15:40:54 +00:00
|
|
|
if (isExtern) {
|
2012-05-01 10:41:12 +00:00
|
|
|
// Obtain the symbol name which is referenced in the relocation
|
2013-06-05 01:33:53 +00:00
|
|
|
symbol_iterator Symbol = RelI.getSymbol();
|
2012-03-30 16:45:19 +00:00
|
|
|
StringRef TargetName;
|
2013-06-05 01:33:53 +00:00
|
|
|
Symbol->getName(TargetName);
|
2012-05-01 10:41:12 +00:00
|
|
|
// First search for the symbol in the local symbol table
|
2012-05-01 06:58:59 +00:00
|
|
|
SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
|
2012-03-30 16:45:19 +00:00
|
|
|
if (lsi != Symbols.end()) {
|
|
|
|
Value.SectionID = lsi->second.first;
|
2013-04-30 01:29:57 +00:00
|
|
|
Value.Addend = lsi->second.second + Addend;
|
2012-03-30 16:45:19 +00:00
|
|
|
} else {
|
2012-05-01 10:41:12 +00:00
|
|
|
// Search for the symbol in the global symbol table
|
2012-05-01 06:58:59 +00:00
|
|
|
SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
|
|
|
|
if (gsi != GlobalSymbolTable.end()) {
|
2012-03-30 16:45:19 +00:00
|
|
|
Value.SectionID = gsi->second.first;
|
2013-04-30 01:29:57 +00:00
|
|
|
Value.Addend = gsi->second.second + Addend;
|
|
|
|
} else {
|
2012-03-30 16:45:19 +00:00
|
|
|
Value.SymbolName = TargetName.data();
|
2013-04-30 01:29:57 +00:00
|
|
|
Value.Addend = Addend;
|
|
|
|
}
|
2011-07-13 07:57:58 +00:00
|
|
|
}
|
2012-03-30 16:45:19 +00:00
|
|
|
} else {
|
2013-04-30 15:40:54 +00:00
|
|
|
SectionRef Sec = MachO->getRelocationSection(RE);
|
|
|
|
Value.SectionID = findOrEmitSection(Obj, Sec, true, ObjSectionToID);
|
|
|
|
uint64_t Addr;
|
|
|
|
Sec.getAddress(Addr);
|
|
|
|
Value.Addend = Addend - Addr;
|
2012-03-29 23:23:59 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 04:28:48 +00:00
|
|
|
if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
|
|
|
|
RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
|
2013-05-05 20:43:10 +00:00
|
|
|
assert(IsPCRel);
|
|
|
|
assert(Size == 2);
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
uint8_t *Addr;
|
|
|
|
if (i != Stubs.end()) {
|
|
|
|
Addr = Section.Address + i->second;
|
|
|
|
} else {
|
|
|
|
Stubs[Value] = Section.StubOffset;
|
|
|
|
uint8_t *GOTEntry = Section.Address + Section.StubOffset;
|
|
|
|
RelocationEntry RE(SectionID, Section.StubOffset,
|
2013-09-01 04:28:48 +00:00
|
|
|
MachO::X86_64_RELOC_UNSIGNED, 0, false, 3);
|
2013-05-05 20:43:10 +00:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
Section.StubOffset += 8;
|
|
|
|
Addr = GOTEntry;
|
|
|
|
}
|
|
|
|
resolveRelocation(Section, Offset, (uint64_t)Addr,
|
2013-09-01 04:28:48 +00:00
|
|
|
MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2);
|
2013-05-05 20:43:10 +00:00
|
|
|
} else if (Arch == Triple::arm &&
|
2013-09-01 04:28:48 +00:00
|
|
|
(RelType & 0xf) == MachO::ARM_RELOC_BR24) {
|
2012-03-30 16:45:19 +00:00
|
|
|
// This is an ARM branch relocation, need to use a stub function.
|
|
|
|
|
|
|
|
// Look up for existing stub.
|
|
|
|
StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
if (i != Stubs.end())
|
2013-04-29 14:44:23 +00:00
|
|
|
resolveRelocation(Section, Offset,
|
2012-03-30 16:45:19 +00:00
|
|
|
(uint64_t)Section.Address + i->second,
|
2013-04-29 17:24:34 +00:00
|
|
|
RelType, 0, IsPCRel, Size);
|
2012-03-30 16:45:19 +00:00
|
|
|
else {
|
|
|
|
// Create a new stub function.
|
|
|
|
Stubs[Value] = Section.StubOffset;
|
|
|
|
uint8_t *StubTargetAddr = createStubFunction(Section.Address +
|
|
|
|
Section.StubOffset);
|
2013-04-29 14:44:23 +00:00
|
|
|
RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
|
2013-09-01 04:28:48 +00:00
|
|
|
MachO::GENERIC_RELOC_VANILLA, Value.Addend);
|
2012-05-01 10:41:12 +00:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
2013-04-29 14:44:23 +00:00
|
|
|
resolveRelocation(Section, Offset,
|
2012-03-30 16:45:19 +00:00
|
|
|
(uint64_t)Section.Address + Section.StubOffset,
|
2013-04-29 17:24:34 +00:00
|
|
|
RelType, 0, IsPCRel, Size);
|
2012-03-30 16:45:19 +00:00
|
|
|
Section.StubOffset += getMaxStubSize();
|
|
|
|
}
|
2012-05-01 10:41:12 +00:00
|
|
|
} else {
|
2013-04-29 17:24:34 +00:00
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Value.Addend,
|
|
|
|
IsPCRel, Size);
|
2012-05-01 10:41:12 +00:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
}
|
2011-07-13 07:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-30 10:06:27 +00:00
|
|
|
bool RuntimeDyldMachO::isCompatibleFormat(
|
2012-10-02 21:18:39 +00:00
|
|
|
const ObjectBuffer *InputBuffer) const {
|
|
|
|
if (InputBuffer->getBufferSize() < 4)
|
|
|
|
return false;
|
|
|
|
StringRef Magic(InputBuffer->getBufferStart(), 4);
|
2011-07-13 07:57:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace llvm
|