1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2025-01-31 12:41:49 +01:00

[RuntimeDyld] Add accessors to SectionEntry; NFC

Summary:
Remove naked access to the data members in `SectionEntry` and route
accesses through accessor functions.  This makes it obvious how the
instances of the class are used, and will also facilitate adding bounds
checking to `advanceStubOffset` in a later change.

Reviewers: lhames, loladiro, andrew.w.kaylor

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D14674

llvm-svn: 253918
This commit is contained in:
Sanjoy Das 2015-11-23 21:47:41 +00:00
parent f0ca422f64
commit 1ad5258091
12 changed files with 266 additions and 223 deletions

View File

@ -41,20 +41,21 @@ void RuntimeDyldImpl::deregisterEHFrames() {}
#ifndef NDEBUG #ifndef NDEBUG
static void dumpSectionMemory(const SectionEntry &S, StringRef State) { static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
dbgs() << "----- Contents of section " << S.Name << " " << State << " -----"; dbgs() << "----- Contents of section " << S.getName() << " " << State
<< " -----";
if (S.Address == nullptr) { if (S.getAddress() == nullptr) {
dbgs() << "\n <section not emitted>\n"; dbgs() << "\n <section not emitted>\n";
return; return;
} }
const unsigned ColsPerRow = 16; const unsigned ColsPerRow = 16;
uint8_t *DataAddr = S.Address; uint8_t *DataAddr = S.getAddress();
uint64_t LoadAddr = S.LoadAddress; uint64_t LoadAddr = S.getLoadAddress();
unsigned StartPadding = LoadAddr & (ColsPerRow - 1); unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
unsigned BytesRemaining = S.Size; unsigned BytesRemaining = S.getSize();
if (StartPadding) { if (StartPadding) {
dbgs() << "\n" << format("0x%016" PRIx64, dbgs() << "\n" << format("0x%016" PRIx64,
@ -97,7 +98,7 @@ void RuntimeDyldImpl::resolveRelocations() {
// symbol for the relocation is located. The SectionID in the relocation // symbol for the relocation is located. The SectionID in the relocation
// entry provides the section to which the relocation will be applied. // entry provides the section to which the relocation will be applied.
int Idx = it->getFirst(); int Idx = it->getFirst();
uint64_t Addr = Sections[Idx].LoadAddress; uint64_t Addr = Sections[Idx].getLoadAddress();
DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t" DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
<< format("%p", (uintptr_t)Addr) << "\n"); << format("%p", (uintptr_t)Addr) << "\n");
resolveRelocationList(it->getSecond(), Addr); resolveRelocationList(it->getSecond(), Addr);
@ -116,7 +117,7 @@ void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
uint64_t TargetAddress) { uint64_t TargetAddress) {
MutexGuard locked(lock); MutexGuard locked(lock);
for (unsigned i = 0, e = Sections.size(); i != e; ++i) { for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
if (Sections[i].Address == LocalAddress) { if (Sections[i].getAddress() == LocalAddress) {
reassignSectionAddress(i, TargetAddress); reassignSectionAddress(i, TargetAddress);
return; return;
} }
@ -778,11 +779,11 @@ void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
// Addr is a uint64_t because we can't assume the pointer width // Addr is a uint64_t because we can't assume the pointer width
// of the target is the same as that of the host. Just use a generic // of the target is the same as that of the host. Just use a generic
// "big enough" type. // "big enough" type.
DEBUG(dbgs() << "Reassigning address for section " DEBUG(dbgs() << "Reassigning address for section " << SectionID << " ("
<< SectionID << " (" << Sections[SectionID].Name << "): " << Sections[SectionID].getName() << "): "
<< format("0x%016" PRIx64, Sections[SectionID].LoadAddress) << " -> " << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
<< format("0x%016" PRIx64, Addr) << "\n"); << " -> " << format("0x%016" PRIx64, Addr) << "\n");
Sections[SectionID].LoadAddress = Addr; Sections[SectionID].setLoadAddress(Addr);
} }
void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
@ -790,7 +791,7 @@ void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
const RelocationEntry &RE = Relocs[i]; const RelocationEntry &RE = Relocs[i];
// Ignore relocations for sections that were not loaded // Ignore relocations for sections that were not loaded
if (Sections[RE.SectionID].Address == nullptr) if (Sections[RE.SectionID].getAddress() == nullptr)
continue; continue;
resolveRelocation(RE, Value); resolveRelocation(RE, Value);
} }
@ -856,17 +857,9 @@ void RuntimeDyldImpl::resolveExternalSymbols() {
uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress( uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
const object::SectionRef &Sec) const { const object::SectionRef &Sec) const {
// llvm::dbgs() << "Searching for " << Sec.getRawDataRefImpl() << " in:\n";
// for (auto E : ObjSecToIDMap)
// llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
auto I = ObjSecToIDMap.find(Sec); auto I = ObjSecToIDMap.find(Sec);
if (I != ObjSecToIDMap.end()) { if (I != ObjSecToIDMap.end())
// llvm::dbgs() << "Found ID " << I->second << " for Sec: " << Sec.getRawDataRefImpl() << ", LoadAddress = " << RTDyld.Sections[I->second].LoadAddress << "\n"; return RTDyld.Sections[I->second].getLoadAddress();
return RTDyld.Sections[I->second].LoadAddress;
} else {
// llvm::dbgs() << "Not found.\n";
}
return 0; return 0;
} }

View File

@ -799,11 +799,10 @@ std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getSectionAddr(
unsigned SectionID = SectionInfo->SectionID; unsigned SectionID = SectionInfo->SectionID;
uint64_t Addr; uint64_t Addr;
if (IsInsideLoad) if (IsInsideLoad)
Addr = Addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(
static_cast<uint64_t>( getRTDyld().Sections[SectionID].getAddress()));
reinterpret_cast<uintptr_t>(getRTDyld().Sections[SectionID].Address));
else else
Addr = getRTDyld().Sections[SectionID].LoadAddress; Addr = getRTDyld().Sections[SectionID].getLoadAddress();
return std::make_pair(Addr, std::string("")); return std::make_pair(Addr, std::string(""));
} }
@ -835,11 +834,11 @@ std::pair<uint64_t, std::string> RuntimeDyldCheckerImpl::getStubAddrFor(
uint64_t Addr; uint64_t Addr;
if (IsInsideLoad) { if (IsInsideLoad) {
uintptr_t SectionBase = uintptr_t SectionBase = reinterpret_cast<uintptr_t>(
reinterpret_cast<uintptr_t>(getRTDyld().Sections[SectionID].Address); getRTDyld().Sections[SectionID].getAddress());
Addr = static_cast<uint64_t>(SectionBase) + StubOffset; Addr = static_cast<uint64_t>(SectionBase) + StubOffset;
} else { } else {
uint64_t SectionBase = getRTDyld().Sections[SectionID].LoadAddress; uint64_t SectionBase = getRTDyld().Sections[SectionID].getLoadAddress();
Addr = SectionBase + StubOffset; Addr = SectionBase + StubOffset;
} }
@ -855,16 +854,16 @@ RuntimeDyldCheckerImpl::getSubsectionStartingAt(StringRef Name) const {
const auto &SymInfo = pos->second; const auto &SymInfo = pos->second;
uint8_t *SectionAddr = getRTDyld().getSectionAddress(SymInfo.getSectionID()); uint8_t *SectionAddr = getRTDyld().getSectionAddress(SymInfo.getSectionID());
return StringRef(reinterpret_cast<const char *>(SectionAddr) + return StringRef(reinterpret_cast<const char *>(SectionAddr) +
SymInfo.getOffset(), SymInfo.getOffset(),
getRTDyld().Sections[SymInfo.getSectionID()].Size - getRTDyld().Sections[SymInfo.getSectionID()].getSize() -
SymInfo.getOffset()); SymInfo.getOffset());
} }
void RuntimeDyldCheckerImpl::registerSection( void RuntimeDyldCheckerImpl::registerSection(
StringRef FilePath, unsigned SectionID) { StringRef FilePath, unsigned SectionID) {
StringRef FileName = sys::path::filename(FilePath); StringRef FileName = sys::path::filename(FilePath);
const SectionEntry &Section = getRTDyld().Sections[SectionID]; const SectionEntry &Section = getRTDyld().Sections[SectionID];
StringRef SectionName = Section.Name; StringRef SectionName = Section.getName();
Stubs[FileName][SectionName].SectionID = SectionID; Stubs[FileName][SectionName].SectionID = SectionID;
} }
@ -874,7 +873,7 @@ void RuntimeDyldCheckerImpl::registerStubMap(
const RuntimeDyldImpl::StubMap &RTDyldStubs) { const RuntimeDyldImpl::StubMap &RTDyldStubs) {
StringRef FileName = sys::path::filename(FilePath); StringRef FileName = sys::path::filename(FilePath);
const SectionEntry &Section = getRTDyld().Sections[SectionID]; const SectionEntry &Section = getRTDyld().Sections[SectionID];
StringRef SectionName = Section.Name; StringRef SectionName = Section.getName();
Stubs[FileName][SectionName].SectionID = SectionID; Stubs[FileName][SectionName].SectionID = SectionID;

View File

@ -198,9 +198,9 @@ RuntimeDyldELF::~RuntimeDyldELF() {}
void RuntimeDyldELF::registerEHFrames() { void RuntimeDyldELF::registerEHFrames() {
for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
SID EHFrameSID = UnregisteredEHFrameSections[i]; SID EHFrameSID = UnregisteredEHFrameSections[i];
uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
size_t EHFrameSize = Sections[EHFrameSID].Size; size_t EHFrameSize = Sections[EHFrameSID].getSize();
MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
RegisteredEHFrameSections.push_back(EHFrameSID); RegisteredEHFrameSections.push_back(EHFrameSID);
} }
@ -210,9 +210,9 @@ void RuntimeDyldELF::registerEHFrames() {
void RuntimeDyldELF::deregisterEHFrames() { void RuntimeDyldELF::deregisterEHFrames() {
for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) { for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) {
SID EHFrameSID = RegisteredEHFrameSections[i]; SID EHFrameSID = RegisteredEHFrameSections[i];
uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
size_t EHFrameSize = Sections[EHFrameSID].Size; size_t EHFrameSize = Sections[EHFrameSID].getSize();
MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); MemMgr.deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
} }
RegisteredEHFrameSections.clear(); RegisteredEHFrameSections.clear();
@ -232,9 +232,10 @@ void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
llvm_unreachable("Relocation type not implemented yet!"); llvm_unreachable("Relocation type not implemented yet!");
break; break;
case ELF::R_X86_64_64: { case ELF::R_X86_64_64: {
support::ulittle64_t::ref(Section.Address + Offset) = Value + Addend; support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
Value + Addend;
DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at "
<< format("%p\n", Section.Address + Offset)); << format("%p\n", Section.getAddressWithOffset(Offset)));
break; break;
} }
case ELF::R_X86_64_32: case ELF::R_X86_64_32:
@ -244,31 +245,34 @@ void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
(Type == ELF::R_X86_64_32S && (Type == ELF::R_X86_64_32S &&
((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
support::ulittle32_t::ref(Section.Address + Offset) = TruncatedAddr; support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
TruncatedAddr;
DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at "
<< format("%p\n", Section.Address + Offset)); << format("%p\n", Section.getAddressWithOffset(Offset)));
break; break;
} }
case ELF::R_X86_64_PC8: { case ELF::R_X86_64_PC8: {
uint64_t FinalAddress = Section.LoadAddress + Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
int64_t RealOffset = Value + Addend - FinalAddress; int64_t RealOffset = Value + Addend - FinalAddress;
assert(isInt<8>(RealOffset)); assert(isInt<8>(RealOffset));
int8_t TruncOffset = (RealOffset & 0xFF); int8_t TruncOffset = (RealOffset & 0xFF);
Section.Address[Offset] = TruncOffset; Section.getAddress()[Offset] = TruncOffset;
break; break;
} }
case ELF::R_X86_64_PC32: { case ELF::R_X86_64_PC32: {
uint64_t FinalAddress = Section.LoadAddress + Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
int64_t RealOffset = Value + Addend - FinalAddress; int64_t RealOffset = Value + Addend - FinalAddress;
assert(isInt<32>(RealOffset)); assert(isInt<32>(RealOffset));
int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
support::ulittle32_t::ref(Section.Address + Offset) = TruncOffset; support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
TruncOffset;
break; break;
} }
case ELF::R_X86_64_PC64: { case ELF::R_X86_64_PC64: {
uint64_t FinalAddress = Section.LoadAddress + Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
int64_t RealOffset = Value + Addend - FinalAddress; int64_t RealOffset = Value + Addend - FinalAddress;
support::ulittle64_t::ref(Section.Address + Offset) = RealOffset; support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) =
RealOffset;
break; break;
} }
} }
@ -279,13 +283,16 @@ void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
uint32_t Type, int32_t Addend) { uint32_t Type, int32_t Addend) {
switch (Type) { switch (Type) {
case ELF::R_386_32: { case ELF::R_386_32: {
support::ulittle32_t::ref(Section.Address + Offset) = Value + Addend; support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
Value + Addend;
break; break;
} }
case ELF::R_386_PC32: { case ELF::R_386_PC32: {
uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); uint32_t FinalAddress =
Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
uint32_t RealOffset = Value + Addend - FinalAddress; uint32_t RealOffset = Value + Addend - FinalAddress;
support::ulittle32_t::ref(Section.Address + Offset) = RealOffset; support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) =
RealOffset;
break; break;
} }
default: default:
@ -299,11 +306,12 @@ void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
uint64_t Offset, uint64_t Value, uint64_t Offset, uint64_t Value,
uint32_t Type, int64_t Addend) { uint32_t Type, int64_t Addend) {
uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset); uint32_t *TargetPtr =
uint64_t FinalAddress = Section.LoadAddress + Offset; reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x"
<< format("%llx", Section.Address + Offset) << format("%llx", Section.getAddressWithOffset(Offset))
<< " FinalAddress: 0x" << format("%llx", FinalAddress) << " FinalAddress: 0x" << format("%llx", FinalAddress)
<< " Value: 0x" << format("%llx", Value) << " Type: 0x" << " Value: 0x" << format("%llx", Value) << " Type: 0x"
<< format("%x", Type) << " Addend: 0x" << format("%llx", Addend) << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
@ -315,7 +323,7 @@ void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section,
break; break;
case ELF::R_AARCH64_ABS64: { case ELF::R_AARCH64_ABS64: {
uint64_t *TargetPtr = uint64_t *TargetPtr =
reinterpret_cast<uint64_t *>(Section.Address + Offset); reinterpret_cast<uint64_t *>(Section.getAddressWithOffset(Offset));
*TargetPtr = Value + Addend; *TargetPtr = Value + Addend;
break; break;
} }
@ -438,12 +446,13 @@ void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
uint64_t Offset, uint32_t Value, uint64_t Offset, uint32_t Value,
uint32_t Type, int32_t Addend) { uint32_t Type, int32_t Addend) {
// TODO: Add Thumb relocations. // TODO: Add Thumb relocations.
uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset); uint32_t *TargetPtr =
uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); reinterpret_cast<uint32_t *>(Section.getAddressWithOffset(Offset));
uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF;
Value += Addend; Value += Addend;
DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
<< Section.Address + Offset << Section.getAddressWithOffset(Offset)
<< " FinalAddress: " << format("%p", FinalAddress) << " Value: " << " FinalAddress: " << format("%p", FinalAddress) << " Value: "
<< format("%x", Value) << " Type: " << format("%x", Type) << format("%x", Value) << " Type: " << format("%x", Type)
<< " Addend: " << format("%x", Addend) << "\n"); << " Addend: " << format("%x", Addend) << "\n");
@ -487,13 +496,14 @@ void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section, void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
uint64_t Offset, uint32_t Value, uint64_t Offset, uint32_t Value,
uint32_t Type, int32_t Addend) { uint32_t Type, int32_t Addend) {
uint8_t *TargetPtr = Section.Address + Offset; uint8_t *TargetPtr = Section.getAddressWithOffset(Offset);
Value += Addend; Value += Addend;
DEBUG(dbgs() << "resolveMIPSRelocation, LocalAddress: " DEBUG(dbgs() << "resolveMIPSRelocation, LocalAddress: "
<< Section.Address + Offset << " FinalAddress: " << Section.getAddressWithOffset(Offset) << " FinalAddress: "
<< format("%p", Section.LoadAddress + Offset) << " Value: " << format("%p", Section.getLoadAddressWithOffset(Offset))
<< format("%x", Value) << " Type: " << format("%x", Type) << " Value: " << format("%x", Value)
<< " Type: " << format("%x", Type)
<< " Addend: " << format("%x", Addend) << "\n"); << " Addend: " << format("%x", Addend) << "\n");
uint32_t Insn = readBytesUnaligned(TargetPtr, 4); uint32_t Insn = readBytesUnaligned(TargetPtr, 4);
@ -522,47 +532,47 @@ void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
break; break;
case ELF::R_MIPS_PC32: { case ELF::R_MIPS_PC32: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
writeBytesUnaligned(Value - FinalAddress, (uint8_t *)TargetPtr, 4); writeBytesUnaligned(Value - FinalAddress, (uint8_t *)TargetPtr, 4);
break; break;
} }
case ELF::R_MIPS_PC16: { case ELF::R_MIPS_PC16: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
Insn &= 0xffff0000; Insn &= 0xffff0000;
Insn |= ((Value - FinalAddress) >> 2) & 0xffff; Insn |= ((Value - FinalAddress) >> 2) & 0xffff;
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
break; break;
} }
case ELF::R_MIPS_PC19_S2: { case ELF::R_MIPS_PC19_S2: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
Insn &= 0xfff80000; Insn &= 0xfff80000;
Insn |= ((Value - (FinalAddress & ~0x3)) >> 2) & 0x7ffff; Insn |= ((Value - (FinalAddress & ~0x3)) >> 2) & 0x7ffff;
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
break; break;
} }
case ELF::R_MIPS_PC21_S2: { case ELF::R_MIPS_PC21_S2: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
Insn &= 0xffe00000; Insn &= 0xffe00000;
Insn |= ((Value - FinalAddress) >> 2) & 0x1fffff; Insn |= ((Value - FinalAddress) >> 2) & 0x1fffff;
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
break; break;
} }
case ELF::R_MIPS_PC26_S2: { case ELF::R_MIPS_PC26_S2: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
Insn &= 0xfc000000; Insn &= 0xfc000000;
Insn |= ((Value - FinalAddress) >> 2) & 0x3ffffff; Insn |= ((Value - FinalAddress) >> 2) & 0x3ffffff;
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
break; break;
} }
case ELF::R_MIPS_PCHI16: { case ELF::R_MIPS_PCHI16: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
Insn &= 0xffff0000; Insn &= 0xffff0000;
Insn |= ((Value - FinalAddress + 0x8000) >> 16) & 0xffff; Insn |= ((Value - FinalAddress + 0x8000) >> 16) & 0xffff;
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
break; break;
} }
case ELF::R_MIPS_PCLO16: { case ELF::R_MIPS_PCLO16: {
uint32_t FinalAddress = (Section.LoadAddress + Offset); uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
Insn &= 0xffff0000; Insn &= 0xffff0000;
Insn |= (Value - FinalAddress) & 0xffff; Insn |= (Value - FinalAddress) & 0xffff;
writeBytesUnaligned(Insn, TargetPtr, 4); writeBytesUnaligned(Insn, TargetPtr, 4);
@ -613,7 +623,8 @@ void RuntimeDyldELF::resolveMIPS64Relocation(const SectionEntry &Section,
CalculatedValue, SymOffset, CalculatedValue, SymOffset,
SectionID); SectionID);
} }
applyMIPS64Relocation(Section.Address + Offset, CalculatedValue, RelType); applyMIPS64Relocation(Section.getAddressWithOffset(Offset), CalculatedValue,
RelType);
} }
int64_t int64_t
@ -623,13 +634,12 @@ RuntimeDyldELF::evaluateMIPS64Relocation(const SectionEntry &Section,
uint64_t SymOffset, SID SectionID) { uint64_t SymOffset, SID SectionID) {
DEBUG(dbgs() << "evaluateMIPS64Relocation, LocalAddress: 0x" DEBUG(dbgs() << "evaluateMIPS64Relocation, LocalAddress: 0x"
<< format("%llx", Section.Address + Offset) << format("%llx", Section.getAddressWithOffset(Offset))
<< " FinalAddress: 0x" << " FinalAddress: 0x"
<< format("%llx", Section.LoadAddress + Offset) << format("%llx", Section.getLoadAddressWithOffset(Offset))
<< " Value: 0x" << format("%llx", Value) << " Type: 0x" << " Value: 0x" << format("%llx", Value) << " Type: 0x"
<< format("%x", Type) << " Addend: 0x" << format("%llx", Addend) << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
<< " SymOffset: " << format("%x", SymOffset) << " SymOffset: " << format("%x", SymOffset) << "\n");
<< "\n");
switch (Type) { switch (Type) {
default: default:
@ -682,35 +692,35 @@ RuntimeDyldELF::evaluateMIPS64Relocation(const SectionEntry &Section,
return Value + Addend - (GOTAddr + 0x7ff0); return Value + Addend - (GOTAddr + 0x7ff0);
} }
case ELF::R_MIPS_PC16: { case ELF::R_MIPS_PC16: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return ((Value + Addend - FinalAddress) >> 2) & 0xffff; return ((Value + Addend - FinalAddress) >> 2) & 0xffff;
} }
case ELF::R_MIPS_PC32: { case ELF::R_MIPS_PC32: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return Value + Addend - FinalAddress; return Value + Addend - FinalAddress;
} }
case ELF::R_MIPS_PC18_S3: { case ELF::R_MIPS_PC18_S3: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return ((Value + Addend - (FinalAddress & ~0x7)) >> 3) & 0x3ffff; return ((Value + Addend - (FinalAddress & ~0x7)) >> 3) & 0x3ffff;
} }
case ELF::R_MIPS_PC19_S2: { case ELF::R_MIPS_PC19_S2: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return ((Value + Addend - (FinalAddress & ~0x3)) >> 2) & 0x7ffff; return ((Value + Addend - (FinalAddress & ~0x3)) >> 2) & 0x7ffff;
} }
case ELF::R_MIPS_PC21_S2: { case ELF::R_MIPS_PC21_S2: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return ((Value + Addend - FinalAddress) >> 2) & 0x1fffff; return ((Value + Addend - FinalAddress) >> 2) & 0x1fffff;
} }
case ELF::R_MIPS_PC26_S2: { case ELF::R_MIPS_PC26_S2: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return ((Value + Addend - FinalAddress) >> 2) & 0x3ffffff; return ((Value + Addend - FinalAddress) >> 2) & 0x3ffffff;
} }
case ELF::R_MIPS_PCHI16: { case ELF::R_MIPS_PCHI16: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return ((Value + Addend - FinalAddress + 0x8000) >> 16) & 0xffff; return ((Value + Addend - FinalAddress + 0x8000) >> 16) & 0xffff;
} }
case ELF::R_MIPS_PCLO16: { case ELF::R_MIPS_PCLO16: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
return (Value + Addend - FinalAddress) & 0xffff; return (Value + Addend - FinalAddress) & 0xffff;
} }
} }
@ -898,7 +908,7 @@ static inline uint16_t applyPPChighesta (uint64_t value) {
void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section, void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
uint64_t Offset, uint64_t Value, uint64_t Offset, uint64_t Value,
uint32_t Type, int64_t Addend) { uint32_t Type, int64_t Addend) {
uint8_t *LocalAddress = Section.Address + Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
switch (Type) { switch (Type) {
default: default:
llvm_unreachable("Relocation type not implemented yet!"); llvm_unreachable("Relocation type not implemented yet!");
@ -918,7 +928,7 @@ void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section,
void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
uint64_t Offset, uint64_t Value, uint64_t Offset, uint64_t Value,
uint32_t Type, int64_t Addend) { uint32_t Type, int64_t Addend) {
uint8_t *LocalAddress = Section.Address + Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
switch (Type) { switch (Type) {
default: default:
llvm_unreachable("Relocation type not implemented yet!"); llvm_unreachable("Relocation type not implemented yet!");
@ -960,17 +970,17 @@ void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
} break; } break;
case ELF::R_PPC64_REL16_LO: { case ELF::R_PPC64_REL16_LO: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
uint64_t Delta = Value - FinalAddress + Addend; uint64_t Delta = Value - FinalAddress + Addend;
writeInt16BE(LocalAddress, applyPPClo(Delta)); writeInt16BE(LocalAddress, applyPPClo(Delta));
} break; } break;
case ELF::R_PPC64_REL16_HI: { case ELF::R_PPC64_REL16_HI: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
uint64_t Delta = Value - FinalAddress + Addend; uint64_t Delta = Value - FinalAddress + Addend;
writeInt16BE(LocalAddress, applyPPChi(Delta)); writeInt16BE(LocalAddress, applyPPChi(Delta));
} break; } break;
case ELF::R_PPC64_REL16_HA: { case ELF::R_PPC64_REL16_HA: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
uint64_t Delta = Value - FinalAddress + Addend; uint64_t Delta = Value - FinalAddress + Addend;
writeInt16BE(LocalAddress, applyPPCha(Delta)); writeInt16BE(LocalAddress, applyPPCha(Delta));
} break; } break;
@ -981,7 +991,7 @@ void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
writeInt32BE(LocalAddress, Result); writeInt32BE(LocalAddress, Result);
} break; } break;
case ELF::R_PPC64_REL24: { case ELF::R_PPC64_REL24: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
if (SignExtend32<26>(delta) != delta) if (SignExtend32<26>(delta) != delta)
llvm_unreachable("Relocation R_PPC64_REL24 overflow"); llvm_unreachable("Relocation R_PPC64_REL24 overflow");
@ -989,14 +999,14 @@ void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC)); writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
} break; } break;
case ELF::R_PPC64_REL32: { case ELF::R_PPC64_REL32: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
if (SignExtend32<32>(delta) != delta) if (SignExtend32<32>(delta) != delta)
llvm_unreachable("Relocation R_PPC64_REL32 overflow"); llvm_unreachable("Relocation R_PPC64_REL32 overflow");
writeInt32BE(LocalAddress, delta); writeInt32BE(LocalAddress, delta);
} break; } break;
case ELF::R_PPC64_REL64: { case ELF::R_PPC64_REL64: {
uint64_t FinalAddress = (Section.LoadAddress + Offset); uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset);
uint64_t Delta = Value - FinalAddress + Addend; uint64_t Delta = Value - FinalAddress + Addend;
writeInt64BE(LocalAddress, Delta); writeInt64BE(LocalAddress, Delta);
} break; } break;
@ -1009,27 +1019,27 @@ void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section,
uint64_t Offset, uint64_t Value, uint64_t Offset, uint64_t Value,
uint32_t Type, int64_t Addend) { uint32_t Type, int64_t Addend) {
uint8_t *LocalAddress = Section.Address + Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
switch (Type) { switch (Type) {
default: default:
llvm_unreachable("Relocation type not implemented yet!"); llvm_unreachable("Relocation type not implemented yet!");
break; break;
case ELF::R_390_PC16DBL: case ELF::R_390_PC16DBL:
case ELF::R_390_PLT16DBL: { case ELF::R_390_PLT16DBL: {
int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow");
writeInt16BE(LocalAddress, Delta / 2); writeInt16BE(LocalAddress, Delta / 2);
break; break;
} }
case ELF::R_390_PC32DBL: case ELF::R_390_PC32DBL:
case ELF::R_390_PLT32DBL: { case ELF::R_390_PLT32DBL: {
int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow");
writeInt32BE(LocalAddress, Delta / 2); writeInt32BE(LocalAddress, Delta / 2);
break; break;
} }
case ELF::R_390_PC32: { case ELF::R_390_PC32: {
int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset);
assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); assert(int32_t(Delta) == Delta && "R_390_PC32 overflow");
writeInt32BE(LocalAddress, Delta); writeInt32BE(LocalAddress, Delta);
break; break;
@ -1119,7 +1129,7 @@ void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
} }
void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const { void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const {
return (void*)(Sections[SectionID].ObjAddress + Offset); return (void *)(Sections[SectionID].getObjAddress() + Offset);
} }
void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) { void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) {
@ -1234,24 +1244,28 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
// Look for an existing stub. // Look for an existing stub.
StubMap::const_iterator i = Stubs.find(Value); StubMap::const_iterator i = Stubs.find(Value);
if (i != Stubs.end()) { if (i != Stubs.end()) {
resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, resolveRelocation(Section, Offset,
(uint64_t)Section.getAddressWithOffset(i->second),
RelType, 0); RelType, 0);
DEBUG(dbgs() << " Stub function found\n"); DEBUG(dbgs() << " Stub function found\n");
} else { } else {
// Create a new stub function. // Create a new stub function.
DEBUG(dbgs() << " Create a new stub function\n"); DEBUG(dbgs() << " Create a new stub function\n");
Stubs[Value] = Section.StubOffset; Stubs[Value] = Section.getStubOffset();
uint8_t *StubTargetAddr = uint8_t *StubTargetAddr = createStubFunction(
createStubFunction(Section.Address + Section.StubOffset); Section.getAddressWithOffset(Section.getStubOffset()));
RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address, RelocationEntry REmovz_g3(SectionID,
StubTargetAddr - Section.getAddress(),
ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend);
RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4, RelocationEntry REmovk_g2(SectionID, StubTargetAddr -
Section.getAddressWithOffset(4),
ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend);
RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8, RelocationEntry REmovk_g1(SectionID, StubTargetAddr -
Section.getAddressWithOffset(8),
ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend);
RelocationEntry REmovk_g0(SectionID, RelocationEntry REmovk_g0(SectionID, StubTargetAddr -
StubTargetAddr - Section.Address + 12, Section.getAddressWithOffset(12),
ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend);
if (Value.SymbolName) { if (Value.SymbolName) {
@ -1266,9 +1280,10 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
addRelocationForSection(REmovk_g0, Value.SectionID); addRelocationForSection(REmovk_g0, Value.SectionID);
} }
resolveRelocation(Section, Offset, resolveRelocation(Section, Offset,
(uint64_t)Section.Address + Section.StubOffset, RelType, reinterpret_cast<uint64_t>(Section.getAddressWithOffset(
0); Section.getStubOffset())),
Section.StubOffset += getMaxStubSize(); RelType, 0);
Section.advanceStubOffset(getMaxStubSize());
} }
} else if (Arch == Triple::arm) { } else if (Arch == Triple::arm) {
if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL || if (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL ||
@ -1280,26 +1295,29 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
// Look for an existing stub. // Look for an existing stub.
StubMap::const_iterator i = Stubs.find(Value); StubMap::const_iterator i = Stubs.find(Value);
if (i != Stubs.end()) { if (i != Stubs.end()) {
resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, resolveRelocation(
RelType, 0); Section, Offset,
reinterpret_cast<uint64_t>(Section.getAddressWithOffset(i->second)),
RelType, 0);
DEBUG(dbgs() << " Stub function found\n"); DEBUG(dbgs() << " Stub function found\n");
} else { } else {
// Create a new stub function. // Create a new stub function.
DEBUG(dbgs() << " Create a new stub function\n"); DEBUG(dbgs() << " Create a new stub function\n");
Stubs[Value] = Section.StubOffset; Stubs[Value] = Section.getStubOffset();
uint8_t *StubTargetAddr = uint8_t *StubTargetAddr = createStubFunction(
createStubFunction(Section.Address + Section.StubOffset); Section.getAddressWithOffset(Section.getStubOffset()));
RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
ELF::R_ARM_ABS32, Value.Addend); ELF::R_ARM_ABS32, Value.Addend);
if (Value.SymbolName) if (Value.SymbolName)
addRelocationForSymbol(RE, Value.SymbolName); addRelocationForSymbol(RE, Value.SymbolName);
else else
addRelocationForSection(RE, Value.SectionID); addRelocationForSection(RE, Value.SectionID);
resolveRelocation(Section, Offset, resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
(uint64_t)Section.Address + Section.StubOffset, RelType, Section.getAddressWithOffset(
0); Section.getStubOffset())),
Section.StubOffset += getMaxStubSize(); RelType, 0);
Section.advanceStubOffset(getMaxStubSize());
} }
} else { } else {
uint32_t *Placeholder = uint32_t *Placeholder =
@ -1338,15 +1356,16 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
} else { } else {
// Create a new stub function. // Create a new stub function.
DEBUG(dbgs() << " Create a new stub function\n"); DEBUG(dbgs() << " Create a new stub function\n");
Stubs[Value] = Section.StubOffset; Stubs[Value] = Section.getStubOffset();
uint8_t *StubTargetAddr = uint8_t *StubTargetAddr = createStubFunction(
createStubFunction(Section.Address + Section.StubOffset); Section.getAddressWithOffset(Section.getStubOffset()));
// Creating Hi and Lo relocations for the filled stub instructions. // Creating Hi and Lo relocations for the filled stub instructions.
RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address, RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
ELF::R_MIPS_HI16, Value.Addend); ELF::R_MIPS_HI16, Value.Addend);
RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4, RelocationEntry RELo(SectionID,
ELF::R_MIPS_LO16, Value.Addend); StubTargetAddr - Section.getAddressWithOffset(4),
ELF::R_MIPS_LO16, Value.Addend);
if (Value.SymbolName) { if (Value.SymbolName) {
addRelocationForSymbol(REHi, Value.SymbolName); addRelocationForSymbol(REHi, Value.SymbolName);
@ -1357,9 +1376,9 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
addRelocationForSection(RELo, Value.SectionID); addRelocationForSection(RELo, Value.SectionID);
} }
RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset); RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
addRelocationForSection(RE, SectionID); addRelocationForSection(RE, SectionID);
Section.StubOffset += getMaxStubSize(); Section.advanceStubOffset(getMaxStubSize());
} }
} else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) { } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) {
int64_t Addend = (Opcode & 0x0000ffff) << 16; int64_t Addend = (Opcode & 0x0000ffff) << 16;
@ -1427,7 +1446,7 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
// an external symbol (Symbol::ST_Unknown) or if the target address // an external symbol (Symbol::ST_Unknown) or if the target address
// is not within the signed 24-bits branch address. // is not within the signed 24-bits branch address.
SectionEntry &Section = Sections[SectionID]; SectionEntry &Section = Sections[SectionID];
uint8_t *Target = Section.Address + Offset; uint8_t *Target = Section.getAddressWithOffset(Offset);
bool RangeOverflow = false; bool RangeOverflow = false;
if (SymType != SymbolRef::ST_Unknown) { if (SymType != SymbolRef::ST_Unknown) {
if (AbiVariant != 2) { if (AbiVariant != 2) {
@ -1441,7 +1460,8 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
uint8_t SymOther = Symbol->getOther(); uint8_t SymOther = Symbol->getOther();
Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther); Value.Addend += ELF::decodePPC64LocalEntryOffset(SymOther);
} }
uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend; uint8_t *RelocTarget =
Sections[Value.SectionID].getAddressWithOffset(Value.Addend);
int32_t delta = static_cast<int32_t>(Target - RelocTarget); int32_t delta = static_cast<int32_t>(Target - RelocTarget);
// If it is within 26-bits branch range, just set the branch target // If it is within 26-bits branch range, just set the branch target
if (SignExtend32<26>(delta) == delta) { if (SignExtend32<26>(delta) == delta) {
@ -1461,23 +1481,25 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
if (i != Stubs.end()) { if (i != Stubs.end()) {
// Symbol function stub already created, just relocate to it // Symbol function stub already created, just relocate to it
resolveRelocation(Section, Offset, resolveRelocation(Section, Offset,
(uint64_t)Section.Address + i->second, RelType, 0); reinterpret_cast<uint64_t>(
Section.getAddressWithOffset(i->second)),
RelType, 0);
DEBUG(dbgs() << " Stub function found\n"); DEBUG(dbgs() << " Stub function found\n");
} else { } else {
// Create a new stub function. // Create a new stub function.
DEBUG(dbgs() << " Create a new stub function\n"); DEBUG(dbgs() << " Create a new stub function\n");
Stubs[Value] = Section.StubOffset; Stubs[Value] = Section.getStubOffset();
uint8_t *StubTargetAddr = uint8_t *StubTargetAddr = createStubFunction(
createStubFunction(Section.Address + Section.StubOffset, Section.getAddressWithOffset(Section.getStubOffset()),
AbiVariant); AbiVariant);
RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(),
ELF::R_PPC64_ADDR64, Value.Addend); ELF::R_PPC64_ADDR64, Value.Addend);
// Generates the 64-bits address loads as exemplified in section // Generates the 64-bits address loads as exemplified in section
// 4.5.1 in PPC64 ELF ABI. Note that the relocations need to // 4.5.1 in PPC64 ELF ABI. Note that the relocations need to
// apply to the low part of the instructions, so we have to update // apply to the low part of the instructions, so we have to update
// the offset according to the target endianness. // the offset according to the target endianness.
uint64_t StubRelocOffset = StubTargetAddr - Section.Address; uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress();
if (!IsTargetLittleEndian) if (!IsTargetLittleEndian)
StubRelocOffset += 2; StubRelocOffset += 2;
@ -1502,10 +1524,11 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
addRelocationForSection(REl, Value.SectionID); addRelocationForSection(REl, Value.SectionID);
} }
resolveRelocation(Section, Offset, resolveRelocation(Section, Offset, reinterpret_cast<uint64_t>(
(uint64_t)Section.Address + Section.StubOffset, Section.getAddressWithOffset(
Section.getStubOffset())),
RelType, 0); RelType, 0);
Section.StubOffset += getMaxStubSize(); Section.advanceStubOffset(getMaxStubSize());
} }
if (SymType == SymbolRef::ST_Unknown) { if (SymType == SymbolRef::ST_Unknown) {
// Restore the TOC for external calls // Restore the TOC for external calls
@ -1585,16 +1608,17 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
StubMap::const_iterator i = Stubs.find(Value); StubMap::const_iterator i = Stubs.find(Value);
uintptr_t StubAddress; uintptr_t StubAddress;
if (i != Stubs.end()) { if (i != Stubs.end()) {
StubAddress = uintptr_t(Section.Address) + i->second; StubAddress = uintptr_t(Section.getAddressWithOffset(i->second));
DEBUG(dbgs() << " Stub function found\n"); DEBUG(dbgs() << " Stub function found\n");
} else { } else {
// Create a new stub function. // Create a new stub function.
DEBUG(dbgs() << " Create a new stub function\n"); DEBUG(dbgs() << " Create a new stub function\n");
uintptr_t BaseAddress = uintptr_t(Section.Address); uintptr_t BaseAddress = uintptr_t(Section.getAddress());
uintptr_t StubAlignment = getStubAlignment(); uintptr_t StubAlignment = getStubAlignment();
StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) & StubAddress =
-StubAlignment; (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
-StubAlignment;
unsigned StubOffset = StubAddress - BaseAddress; unsigned StubOffset = StubAddress - BaseAddress;
Stubs[Value] = StubOffset; Stubs[Value] = StubOffset;
@ -1605,7 +1629,7 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
addRelocationForSymbol(RE, Value.SymbolName); addRelocationForSymbol(RE, Value.SymbolName);
else else
addRelocationForSection(RE, Value.SectionID); addRelocationForSection(RE, Value.SectionID);
Section.StubOffset = StubOffset + getMaxStubSize(); Section.advanceStubOffset(getMaxStubSize());
} }
if (RelType == ELF::R_390_GOTENT) if (RelType == ELF::R_390_GOTENT)
@ -1638,22 +1662,23 @@ relocation_iterator RuntimeDyldELF::processRelocationRef(
StubMap::const_iterator i = Stubs.find(Value); StubMap::const_iterator i = Stubs.find(Value);
uintptr_t StubAddress; uintptr_t StubAddress;
if (i != Stubs.end()) { if (i != Stubs.end()) {
StubAddress = uintptr_t(Section.Address) + i->second; StubAddress = uintptr_t(Section.getAddress()) + i->second;
DEBUG(dbgs() << " Stub function found\n"); DEBUG(dbgs() << " Stub function found\n");
} else { } else {
// Create a new stub function (equivalent to a PLT entry). // Create a new stub function (equivalent to a PLT entry).
DEBUG(dbgs() << " Create a new stub function\n"); DEBUG(dbgs() << " Create a new stub function\n");
uintptr_t BaseAddress = uintptr_t(Section.Address); uintptr_t BaseAddress = uintptr_t(Section.getAddress());
uintptr_t StubAlignment = getStubAlignment(); uintptr_t StubAlignment = getStubAlignment();
StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) & StubAddress =
-StubAlignment; (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
-StubAlignment;
unsigned StubOffset = StubAddress - BaseAddress; unsigned StubOffset = StubAddress - BaseAddress;
Stubs[Value] = StubOffset; Stubs[Value] = StubOffset;
createStubFunction((uint8_t *)StubAddress); createStubFunction((uint8_t *)StubAddress);
// Bump our stub offset counter // Bump our stub offset counter
Section.StubOffset = StubOffset + getMaxStubSize(); Section.advanceStubOffset(getMaxStubSize());
// Allocate a GOT Entry // Allocate a GOT Entry
uint64_t GOTOffset = allocateGOTEntries(SectionID, 1); uint64_t GOTOffset = allocateGOTEntries(SectionID, 1);

View File

@ -50,7 +50,6 @@ class Twine;
/// SectionEntry - represents a section emitted into memory by the dynamic /// SectionEntry - represents a section emitted into memory by the dynamic
/// linker. /// linker.
class SectionEntry { class SectionEntry {
public:
/// Name - section name. /// Name - section name.
std::string Name; std::string Name;
@ -74,11 +73,37 @@ public:
/// for calculating relocations in some object formats (like MachO). /// for calculating relocations in some object formats (like MachO).
uintptr_t ObjAddress; uintptr_t ObjAddress;
public:
SectionEntry(StringRef name, uint8_t *address, size_t size, SectionEntry(StringRef name, uint8_t *address, size_t size,
uintptr_t objAddress) uintptr_t objAddress)
: Name(name), Address(address), Size(size), : Name(name), Address(address), Size(size),
LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size), LoadAddress(reinterpret_cast<uintptr_t>(address)), StubOffset(size),
ObjAddress(objAddress) {} ObjAddress(objAddress) {}
StringRef getName() const { return Name; }
uint8_t *getAddress() const { return Address; }
/// \brief Return the address of this section with an offset.
uint8_t *getAddressWithOffset(unsigned OffsetBytes) const {
return Address + OffsetBytes;
}
size_t getSize() const { return Size; }
uint64_t getLoadAddress() const { return LoadAddress; }
void setLoadAddress(uint64_t LA) { LoadAddress = LA; }
/// \brief Return the load address of this section with an offset.
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const {
return LoadAddress + OffsetBytes;
}
uintptr_t getStubOffset() const { return StubOffset; }
void advanceStubOffset(unsigned StubSize) { StubOffset += StubSize; }
uintptr_t getObjAddress() const { return ObjAddress; }
}; };
/// RelocationEntry - used to represent relocations internally in the dynamic /// RelocationEntry - used to represent relocations internally in the dynamic
@ -271,11 +296,11 @@ protected:
} }
uint64_t getSectionLoadAddress(unsigned SectionID) const { uint64_t getSectionLoadAddress(unsigned SectionID) const {
return Sections[SectionID].LoadAddress; return Sections[SectionID].getLoadAddress();
} }
uint8_t *getSectionAddress(unsigned SectionID) const { uint8_t *getSectionAddress(unsigned SectionID) const {
return (uint8_t *)Sections[SectionID].Address; return Sections[SectionID].getAddress();
} }
void writeInt16BE(uint8_t *Addr, uint16_t Value) { void writeInt16BE(uint8_t *Addr, uint16_t Value) {

View File

@ -45,7 +45,7 @@ namespace llvm {
int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const { int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
unsigned NumBytes = 1 << RE.Size; unsigned NumBytes = 1 << RE.Size;
uint8_t *Src = Sections[RE.SectionID].Address + RE.Offset; uint8_t *Src = Sections[RE.SectionID].getAddress() + RE.Offset;
return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes)); return static_cast<int64_t>(readBytesUnaligned(Src, NumBytes));
} }
@ -64,7 +64,7 @@ relocation_iterator RuntimeDyldMachO::processScatteredVANILLA(
bool IsPCRel = Obj.getAnyRelocationPCRel(RE); bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
unsigned Size = Obj.getAnyRelocationLength(RE); unsigned Size = Obj.getAnyRelocationLength(RE);
uint64_t Offset = RelI->getOffset(); uint64_t Offset = RelI->getOffset();
uint8_t *LocalAddress = Section.Address + Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
unsigned NumBytes = 1 << Size; unsigned NumBytes = 1 << Size;
int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes); int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);
@ -135,8 +135,8 @@ void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE, void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
uint64_t Value) const { uint64_t Value) const {
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddress() + RE.Offset;
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddress() + RE.Offset;
dbgs() << "resolveRelocation Section: " << RE.SectionID dbgs() << "resolveRelocation Section: " << RE.SectionID
<< " LocalAddress: " << format("%p", LocalAddress) << " LocalAddress: " << format("%p", LocalAddress)
@ -183,10 +183,9 @@ void RuntimeDyldMachO::populateIndirectSymbolPointersSection(
"Pointers section does not contain a whole number of stubs?"); "Pointers section does not contain a whole number of stubs?");
DEBUG(dbgs() << "Populating pointer table section " DEBUG(dbgs() << "Populating pointer table section "
<< Sections[PTSectionID].Name << Sections[PTSectionID].getName() << ", Section ID "
<< ", Section ID " << PTSectionID << ", " << PTSectionID << ", " << NumPTEntries << " entries, "
<< NumPTEntries << " entries, " << PTEntrySize << PTEntrySize << " bytes each:\n");
<< " bytes each:\n");
for (unsigned i = 0; i < NumPTEntries; ++i) { for (unsigned i = 0; i < NumPTEntries; ++i) {
unsigned SymbolIndex = unsigned SymbolIndex =
@ -240,7 +239,7 @@ void RuntimeDyldMachOCRTPBase<Impl>::finalizeLoad(const ObjectFile &Obj,
} }
template <typename Impl> template <typename Impl>
unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P, unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(uint8_t *P,
int64_t DeltaForText, int64_t DeltaForText,
int64_t DeltaForEH) { int64_t DeltaForEH) {
typedef typename Impl::TargetPtrT TargetPtrT; typedef typename Impl::TargetPtrT TargetPtrT;
@ -249,7 +248,7 @@ unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
<< ", Delta for EH: " << DeltaForEH << "\n"); << ", Delta for EH: " << DeltaForEH << "\n");
uint32_t Length = readBytesUnaligned(P, 4); uint32_t Length = readBytesUnaligned(P, 4);
P += 4; P += 4;
unsigned char *Ret = P + Length; uint8_t *Ret = P + Length;
uint32_t Offset = readBytesUnaligned(P, 4); uint32_t Offset = readBytesUnaligned(P, 4);
if (Offset == 0) // is a CIE if (Offset == 0) // is a CIE
return Ret; return Ret;
@ -276,9 +275,9 @@ unsigned char *RuntimeDyldMachOCRTPBase<Impl>::processFDE(unsigned char *P,
} }
static int64_t computeDelta(SectionEntry *A, SectionEntry *B) { static int64_t computeDelta(SectionEntry *A, SectionEntry *B) {
int64_t ObjDistance = int64_t ObjDistance = static_cast<int64_t>(A->getObjAddress()) -
static_cast<int64_t>(A->ObjAddress) - static_cast<int64_t>(B->ObjAddress); static_cast<int64_t>(B->getObjAddress());
int64_t MemDistance = A->LoadAddress - B->LoadAddress; int64_t MemDistance = A->getLoadAddress() - B->getLoadAddress();
return ObjDistance - MemDistance; return ObjDistance - MemDistance;
} }
@ -301,14 +300,14 @@ void RuntimeDyldMachOCRTPBase<Impl>::registerEHFrames() {
if (ExceptTab) if (ExceptTab)
DeltaForEH = computeDelta(ExceptTab, EHFrame); DeltaForEH = computeDelta(ExceptTab, EHFrame);
unsigned char *P = EHFrame->Address; uint8_t *P = EHFrame->getAddress();
unsigned char *End = P + EHFrame->Size; uint8_t *End = P + EHFrame->getSize();
do { do {
P = processFDE(P, DeltaForText, DeltaForEH); P = processFDE(P, DeltaForText, DeltaForEH);
} while (P != End); } while (P != End);
MemMgr.registerEHFrames(EHFrame->Address, EHFrame->LoadAddress, MemMgr.registerEHFrames(EHFrame->getAddress(), EHFrame->getLoadAddress(),
EHFrame->Size); EHFrame->getSize());
} }
UnregisteredEHFrameSections.clear(); UnregisteredEHFrameSections.clear();
} }

View File

@ -146,7 +146,7 @@ private:
Impl &impl() { return static_cast<Impl &>(*this); } Impl &impl() { return static_cast<Impl &>(*this); }
const Impl &impl() const { return static_cast<const Impl &>(*this); } const Impl &impl() const { return static_cast<const Impl &>(*this); }
unsigned char *processFDE(unsigned char *P, int64_t DeltaForText, unsigned char *processFDE(uint8_t *P, int64_t DeltaForText,
int64_t DeltaForEH); int64_t DeltaForEH);
public: public:

View File

@ -105,7 +105,7 @@ public:
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
const auto Section = Sections[RE.SectionID]; const auto Section = Sections[RE.SectionID];
uint8_t *Target = Section.Address + RE.Offset; uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
switch (RE.RelType) { switch (RE.RelType) {
case COFF::IMAGE_REL_I386_ABSOLUTE: case COFF::IMAGE_REL_I386_ABSOLUTE:
@ -116,7 +116,8 @@ public:
uint64_t Result = uint64_t Result =
RE.Sections.SectionA == static_cast<uint32_t>(-1) RE.Sections.SectionA == static_cast<uint32_t>(-1)
? Value ? Value
: Sections[RE.Sections.SectionA].LoadAddress + RE.Addend; : Sections[RE.Sections.SectionA].getLoadAddressWithOffset(
RE.Addend);
assert(static_cast<int32_t>(Result) <= INT32_MAX && assert(static_cast<int32_t>(Result) <= INT32_MAX &&
"relocation overflow"); "relocation overflow");
assert(static_cast<int32_t>(Result) >= INT32_MIN && assert(static_cast<int32_t>(Result) >= INT32_MIN &&
@ -130,9 +131,10 @@ public:
} }
case COFF::IMAGE_REL_I386_DIR32NB: { case COFF::IMAGE_REL_I386_DIR32NB: {
// The target's 32-bit RVA. // The target's 32-bit RVA.
// NOTE: use Section[0].LoadAddress as an approximation of ImageBase // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase
uint64_t Result = Sections[RE.Sections.SectionA].LoadAddress + RE.Addend - uint64_t Result =
Sections[0].LoadAddress; Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend) -
Sections[0].getLoadAddress();
assert(static_cast<int32_t>(Result) <= INT32_MAX && assert(static_cast<int32_t>(Result) <= INT32_MAX &&
"relocation overflow"); "relocation overflow");
assert(static_cast<int32_t>(Result) >= INT32_MIN && assert(static_cast<int32_t>(Result) >= INT32_MIN &&
@ -146,8 +148,8 @@ public:
} }
case COFF::IMAGE_REL_I386_REL32: { case COFF::IMAGE_REL_I386_REL32: {
// 32-bit relative displacement to the target. // 32-bit relative displacement to the target.
uint64_t Result = Sections[RE.Sections.SectionA].LoadAddress - uint64_t Result = Sections[RE.Sections.SectionA].getLoadAddress() -
Section.LoadAddress + RE.Addend - 4 - RE.Offset; Section.getLoadAddress() + RE.Addend - 4 - RE.Offset;
assert(static_cast<int32_t>(Result) <= INT32_MAX && assert(static_cast<int32_t>(Result) <= INT32_MAX &&
"relocation overflow"); "relocation overflow");
assert(static_cast<int32_t>(Result) >= INT32_MIN && assert(static_cast<int32_t>(Result) >= INT32_MIN &&

View File

@ -62,7 +62,7 @@ public:
// symbol in the target address space. // symbol in the target address space.
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *Target = Section.Address + RE.Offset; uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
switch (RE.RelType) { switch (RE.RelType) {
@ -72,7 +72,7 @@ public:
case COFF::IMAGE_REL_AMD64_REL32_3: case COFF::IMAGE_REL_AMD64_REL32_3:
case COFF::IMAGE_REL_AMD64_REL32_4: case COFF::IMAGE_REL_AMD64_REL32_4:
case COFF::IMAGE_REL_AMD64_REL32_5: { case COFF::IMAGE_REL_AMD64_REL32_5: {
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
// Delta is the distance from the start of the reloc to the end of the // Delta is the distance from the start of the reloc to the end of the
// instruction with the reloc. // instruction with the reloc.
uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32); uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32);
@ -125,7 +125,7 @@ public:
uint64_t Offset = RelI->getOffset(); uint64_t Offset = RelI->getOffset();
uint64_t Addend = 0; uint64_t Addend = 0;
SectionEntry &Section = Sections[SectionID]; SectionEntry &Section = Sections[SectionID];
uintptr_t ObjTarget = Section.ObjAddress + Offset; uintptr_t ObjTarget = Section.getObjAddress() + Offset;
switch (RelType) { switch (RelType) {
@ -178,9 +178,9 @@ public:
unsigned getStubAlignment() override { return 1; } unsigned getStubAlignment() override { return 1; }
void registerEHFrames() override { void registerEHFrames() override {
for (auto const &EHFrameSID : UnregisteredEHFrameSections) { for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
size_t EHFrameSize = Sections[EHFrameSID].Size; size_t EHFrameSize = Sections[EHFrameSID].getSize();
MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
RegisteredEHFrameSections.push_back(EHFrameSID); RegisteredEHFrameSections.push_back(EHFrameSID);
} }

View File

@ -34,7 +34,7 @@ public:
/// Extract the addend encoded in the instruction / memory location. /// Extract the addend encoded in the instruction / memory location.
int64_t decodeAddend(const RelocationEntry &RE) const { int64_t decodeAddend(const RelocationEntry &RE) const {
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
unsigned NumBytes = 1 << RE.Size; unsigned NumBytes = 1 << RE.Size;
int64_t Addend = 0; int64_t Addend = 0;
// Verify that the relocation has the correct size and alignment. // Verify that the relocation has the correct size and alignment.
@ -304,7 +304,7 @@ public:
DEBUG(dumpRelocationToResolve(RE, Value)); DEBUG(dumpRelocationToResolve(RE, Value));
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
MachO::RelocationInfoType RelType = MachO::RelocationInfoType RelType =
static_cast<MachO::RelocationInfoType>(RE.RelType); static_cast<MachO::RelocationInfoType>(RE.RelType);
@ -324,7 +324,7 @@ public:
case MachO::ARM64_RELOC_BRANCH26: { case MachO::ARM64_RELOC_BRANCH26: {
assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported"); assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported");
// Check if branch is in range. // Check if branch is in range.
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
int64_t PCRelVal = Value - FinalAddress + RE.Addend; int64_t PCRelVal = Value - FinalAddress + RE.Addend;
encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
break; break;
@ -333,7 +333,7 @@ public:
case MachO::ARM64_RELOC_PAGE21: { case MachO::ARM64_RELOC_PAGE21: {
assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported"); assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported");
// Adjust for PC-relative relocation and offset. // Adjust for PC-relative relocation and offset.
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
int64_t PCRelVal = int64_t PCRelVal =
((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096)); ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096));
encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
@ -375,10 +375,10 @@ private:
else { else {
// FIXME: There must be a better way to do this then to check and fix the // FIXME: There must be a better way to do this then to check and fix the
// alignment every time!!! // alignment every time!!!
uintptr_t BaseAddress = uintptr_t(Section.Address); uintptr_t BaseAddress = uintptr_t(Section.getAddress());
uintptr_t StubAlignment = getStubAlignment(); uintptr_t StubAlignment = getStubAlignment();
uintptr_t StubAddress = uintptr_t StubAddress =
(BaseAddress + Section.StubOffset + StubAlignment - 1) & (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
-StubAlignment; -StubAlignment;
unsigned StubOffset = StubAddress - BaseAddress; unsigned StubOffset = StubAddress - BaseAddress;
Stubs[Value] = StubOffset; Stubs[Value] = StubOffset;
@ -391,7 +391,7 @@ private:
addRelocationForSymbol(GOTRE, Value.SymbolName); addRelocationForSymbol(GOTRE, Value.SymbolName);
else else
addRelocationForSection(GOTRE, Value.SectionID); addRelocationForSection(GOTRE, Value.SectionID);
Section.StubOffset = StubOffset + getMaxStubSize(); Section.advanceStubOffset(getMaxStubSize());
Offset = static_cast<int64_t>(StubOffset); Offset = static_cast<int64_t>(StubOffset);
} }
RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset, RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset,

View File

@ -35,7 +35,7 @@ public:
int64_t decodeAddend(const RelocationEntry &RE) const { int64_t decodeAddend(const RelocationEntry &RE) const {
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
switch (RE.RelType) { switch (RE.RelType) {
default: default:
@ -94,12 +94,12 @@ public:
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
DEBUG(dumpRelocationToResolve(RE, Value)); DEBUG(dumpRelocationToResolve(RE, Value));
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
// If the relocation is PC-relative, the value to be encoded is the // If the relocation is PC-relative, the value to be encoded is the
// pointer difference. // pointer difference.
if (RE.IsPCRel) { if (RE.IsPCRel) {
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Value -= FinalAddress; Value -= FinalAddress;
// ARM PCRel relocations have an effective-PC offset of two instructions // ARM PCRel relocations have an effective-PC offset of two instructions
// (four bytes in Thumb mode, 8 bytes in ARM mode). // (four bytes in Thumb mode, 8 bytes in ARM mode).
@ -132,8 +132,8 @@ public:
break; break;
} }
case MachO::ARM_RELOC_HALF_SECTDIFF: { case MachO::ARM_RELOC_HALF_SECTDIFF: {
uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress; uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress; uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
assert((Value == SectionABase || Value == SectionBBase) && assert((Value == SectionABase || Value == SectionBBase) &&
"Unexpected HALFSECTDIFF relocation value."); "Unexpected HALFSECTDIFF relocation value.");
Value = SectionABase - SectionBBase + RE.Addend; Value = SectionABase - SectionBBase + RE.Addend;
@ -180,21 +180,21 @@ private:
RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
uint8_t *Addr; uint8_t *Addr;
if (i != Stubs.end()) { if (i != Stubs.end()) {
Addr = Section.Address + i->second; Addr = Section.getAddressWithOffset(i->second);
} else { } else {
// Create a new stub function. // Create a new stub function.
Stubs[Value] = Section.StubOffset; Stubs[Value] = Section.getStubOffset();
uint8_t *StubTargetAddr = uint8_t *StubTargetAddr = createStubFunction(
createStubFunction(Section.Address + Section.StubOffset); Section.getAddressWithOffset(Section.getStubOffset()));
RelocationEntry StubRE(RE.SectionID, StubTargetAddr - Section.Address, RelocationEntry StubRE(
MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, RE.SectionID, StubTargetAddr - Section.getAddress(),
2); MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, 2);
if (Value.SymbolName) if (Value.SymbolName)
addRelocationForSymbol(StubRE, Value.SymbolName); addRelocationForSymbol(StubRE, Value.SymbolName);
else else
addRelocationForSection(StubRE, Value.SectionID); addRelocationForSection(StubRE, Value.SectionID);
Addr = Section.Address + Section.StubOffset; Addr = Section.getAddressWithOffset(Section.getStubOffset());
Section.StubOffset += getMaxStubSize(); Section.advanceStubOffset(getMaxStubSize());
} }
RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0, RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
RE.IsPCRel, RE.Size); RE.IsPCRel, RE.Size);
@ -223,7 +223,7 @@ private:
uint32_t RelocType = MachO.getAnyRelocationType(RE); uint32_t RelocType = MachO.getAnyRelocationType(RE);
bool IsPCRel = MachO.getAnyRelocationPCRel(RE); bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
uint64_t Offset = RelI->getOffset(); uint64_t Offset = RelI->getOffset();
uint8_t *LocalAddress = Section.Address + Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out. int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff); Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);

View File

@ -83,10 +83,10 @@ public:
DEBUG(dumpRelocationToResolve(RE, Value)); DEBUG(dumpRelocationToResolve(RE, Value));
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
if (RE.IsPCRel) { if (RE.IsPCRel) {
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation. Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
} }
@ -98,8 +98,8 @@ public:
break; break;
case MachO::GENERIC_RELOC_SECTDIFF: case MachO::GENERIC_RELOC_SECTDIFF:
case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress; uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress; uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
assert((Value == SectionABase || Value == SectionBBase) && assert((Value == SectionABase || Value == SectionBBase) &&
"Unexpected SECTDIFF relocation value."); "Unexpected SECTDIFF relocation value.");
Value = SectionABase - SectionBBase + RE.Addend; Value = SectionABase - SectionBBase + RE.Addend;
@ -138,7 +138,7 @@ private:
bool IsPCRel = Obj.getAnyRelocationPCRel(RE); bool IsPCRel = Obj.getAnyRelocationPCRel(RE);
unsigned Size = Obj.getAnyRelocationLength(RE); unsigned Size = Obj.getAnyRelocationLength(RE);
uint64_t Offset = RelI->getOffset(); uint64_t Offset = RelI->getOffset();
uint8_t *LocalAddress = Section.Address + Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
unsigned NumBytes = 1 << Size; unsigned NumBytes = 1 << Size;
uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes); uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes);

View File

@ -73,14 +73,14 @@ public:
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
DEBUG(dumpRelocationToResolve(RE, Value)); DEBUG(dumpRelocationToResolve(RE, Value));
const SectionEntry &Section = Sections[RE.SectionID]; const SectionEntry &Section = Sections[RE.SectionID];
uint8_t *LocalAddress = Section.Address + RE.Offset; uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
// If the relocation is PC-relative, the value to be encoded is the // If the relocation is PC-relative, the value to be encoded is the
// pointer difference. // pointer difference.
if (RE.IsPCRel) { if (RE.IsPCRel) {
// FIXME: It seems this value needs to be adjusted by 4 for an effective // FIXME: It seems this value needs to be adjusted by 4 for an effective
// PC address. Is that expected? Only for branches, perhaps? // PC address. Is that expected? Only for branches, perhaps?
uint64_t FinalAddress = Section.LoadAddress + RE.Offset; uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Value -= FinalAddress + 4; Value -= FinalAddress + 4;
} }
@ -96,8 +96,8 @@ public:
writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size); writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
break; break;
case MachO::X86_64_RELOC_SUBTRACTOR: { case MachO::X86_64_RELOC_SUBTRACTOR: {
uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress; uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress; uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
assert((Value == SectionABase || Value == SectionBBase) && assert((Value == SectionABase || Value == SectionBBase) &&
"Unexpected SUBTRACTOR relocation value."); "Unexpected SUBTRACTOR relocation value.");
Value = SectionABase - SectionBBase + RE.Addend; Value = SectionABase - SectionBBase + RE.Addend;
@ -124,18 +124,18 @@ private:
RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
uint8_t *Addr; uint8_t *Addr;
if (i != Stubs.end()) { if (i != Stubs.end()) {
Addr = Section.Address + i->second; Addr = Section.getAddressWithOffset(i->second);
} else { } else {
Stubs[Value] = Section.StubOffset; Stubs[Value] = Section.getStubOffset();
uint8_t *GOTEntry = Section.Address + Section.StubOffset; uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset());
RelocationEntry GOTRE(RE.SectionID, Section.StubOffset, RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(),
MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false, MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
3); 3);
if (Value.SymbolName) if (Value.SymbolName)
addRelocationForSymbol(GOTRE, Value.SymbolName); addRelocationForSymbol(GOTRE, Value.SymbolName);
else else
addRelocationForSection(GOTRE, Value.SectionID); addRelocationForSection(GOTRE, Value.SectionID);
Section.StubOffset += 8; Section.advanceStubOffset(8);
Addr = GOTEntry; Addr = GOTEntry;
} }
RelocationEntry TargetRE(RE.SectionID, RE.Offset, RelocationEntry TargetRE(RE.SectionID, RE.Offset,
@ -154,7 +154,7 @@ private:
unsigned Size = Obj.getAnyRelocationLength(RE); unsigned Size = Obj.getAnyRelocationLength(RE);
uint64_t Offset = RelI->getOffset(); uint64_t Offset = RelI->getOffset();
uint8_t *LocalAddress = Sections[SectionID].Address + Offset; uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);
unsigned NumBytes = 1 << Size; unsigned NumBytes = 1 << Size;
ErrorOr<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName(); ErrorOr<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();