diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp @@ -80,22 +80,23 @@ } #ifndef NDEBUG -static void dumpSectionMemory(const SectionEntry &S, StringRef State) { - dbgs() << "----- Contents of section " << S.getName() << " " << State +static void dumpSectionMemory(const SectionList::SectionEntryProxy &S, + StringRef State) { + dbgs() << "----- Contents of section " << S->getName() << " " << State << " -----"; - if (S.getAddress() == nullptr) { + if (S->getAddress() == nullptr) { dbgs() << "\n
\n"; return; } const unsigned ColsPerRow = 16; - uint8_t *DataAddr = S.getAddress(); - uint64_t LoadAddr = S.getLoadAddress(); + uint8_t *DataAddr = S->getAddress(); + uint64_t LoadAddr = S->getLoadAddress(); unsigned StartPadding = LoadAddr & (ColsPerRow - 1); - unsigned BytesRemaining = S.getSize(); + unsigned BytesRemaining = S->getSize(); if (StartPadding) { dbgs() << "\n" << format("0x%016" PRIx64, @@ -147,7 +148,7 @@ // symbol for the relocation is located. The SectionID in the relocation // entry provides the section to which the relocation will be applied. int Idx = it->first; - uint64_t Addr = Sections[Idx].getLoadAddress(); + uint64_t Addr = Sections[Idx]->getLoadAddress(); LLVM_DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t" << format("%p", (uintptr_t)Addr) << "\n"); resolveRelocationList(it->second, Addr); @@ -159,7 +160,7 @@ uint64_t TargetAddress) { std::lock_guard locked(lock); for (unsigned i = 0, e = Sections.size(); i != e; ++i) { - if (Sections[i].getAddress() == LocalAddress) { + if (Sections[i]->getAddress() == LocalAddress) { reassignSectionAddress(i, TargetAddress); return; } @@ -383,7 +384,7 @@ // stubs created for this section. if (NotifyStubEmitted) { StringRef FileName = Obj.getFileName(); - StringRef SectionName = Sections[SectionID].getName(); + StringRef SectionName = Sections[SectionID]->getName(); for (auto &KV : Stubs) { auto &VR = KV.first; @@ -881,7 +882,7 @@ // Debug info sections are linked as if their load address was zero if (!IsRequired) - Sections.back().setLoadAddress(0); + Sections.back()->setLoadAddress(0); return SectionID; } @@ -1050,10 +1051,10 @@ // "big enough" type. LLVM_DEBUG( dbgs() << "Reassigning address for section " << SectionID << " (" - << Sections[SectionID].getName() << "): " - << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress()) + << Sections[SectionID]->getName() << "): " + << format("0x%016" PRIx64, Sections[SectionID]->getLoadAddress()) << " -> " << format("0x%016" PRIx64, Addr) << "\n"); - Sections[SectionID].setLoadAddress(Addr); + Sections[SectionID]->setLoadAddress(Addr); } void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, @@ -1061,7 +1062,7 @@ for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { const RelocationEntry &RE = Relocs[i]; // Ignore relocations for sections that were not loaded - if (Sections[RE.SectionID].getAddress() == nullptr) + if (Sections[RE.SectionID]->getAddress() == nullptr) continue; resolveRelocation(RE, Value); } @@ -1244,7 +1245,7 @@ auto I = ObjSecToIDMap.find(Sec); if (I != ObjSecToIDMap.end()) - return RTDyld.Sections[I->second].getLoadAddress(); + return RTDyld.Sections[I->second]->getLoadAddress(); return 0; } diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.h @@ -25,19 +25,20 @@ class RuntimeDyldELF : public RuntimeDyldImpl { - void resolveRelocation(const SectionEntry &Section, uint64_t Offset, + void resolveRelocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend, uint64_t SymOffset = 0, SID SectionID = 0); - void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset, + void resolveX86_64Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend, uint64_t SymOffset); - void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset, + void resolveX86Relocation(const SectionEntryProxy Section, uint64_t Offset, uint32_t Value, uint32_t Type, int32_t Addend); - void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset, - uint64_t Value, uint32_t Type, int64_t Addend); + void resolveAArch64Relocation(const SectionEntryProxy Section, + uint64_t Offset, uint64_t Value, uint32_t Type, + int64_t Addend); bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI, const RelocationValueRef &Value); @@ -45,19 +46,20 @@ void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value, relocation_iterator RelI, StubMap &Stubs); - void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset, + void resolveARMRelocation(const SectionEntryProxy Section, uint64_t Offset, uint32_t Value, uint32_t Type, int32_t Addend); - void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset, + void resolvePPC32Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend); - void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset, + void resolvePPC64Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend); - void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset, - uint64_t Value, uint32_t Type, int64_t Addend); + void resolveSystemZRelocation(const SectionEntryProxy Section, + uint64_t Offset, uint64_t Value, uint32_t Type, + int64_t Addend); - void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset, + void resolveBPFRelocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend); unsigned getMaxStubSize() const override { @@ -98,7 +100,9 @@ size_t getGOTEntrySize() override; private: - SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } + SectionEntryProxy getSection(unsigned SectionID) { + return Sections[SectionID]; + } // Allocate no GOT entries for use in the given section. uint64_t allocateGOTEntries(unsigned no); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -228,9 +228,9 @@ void RuntimeDyldELF::registerEHFrames() { for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { SID EHFrameSID = UnregisteredEHFrameSections[i]; - uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress(); - uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress(); - size_t EHFrameSize = Sections[EHFrameSID].getSize(); + uint8_t *EHFrameAddr = Sections[EHFrameSID]->getAddress(); + uint64_t EHFrameLoadAddr = Sections[EHFrameSID]->getLoadAddress(); + size_t EHFrameSize = Sections[EHFrameSID]->getSize(); MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); } UnregisteredEHFrameSections.clear(); @@ -263,7 +263,7 @@ } } -void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend, uint64_t SymOffset) { @@ -274,10 +274,10 @@ case ELF::R_X86_64_NONE: break; case ELF::R_X86_64_64: { - support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = + support::ulittle64_t::ref(Section->getAddressWithOffset(Offset)) = Value + Addend; LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " - << format("%p\n", Section.getAddressWithOffset(Offset))); + << format("%p\n", Section->getAddressWithOffset(Offset))); break; } case ELF::R_X86_64_32: @@ -287,33 +287,33 @@ (Type == ELF::R_X86_64_32S && ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); - support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = + support::ulittle32_t::ref(Section->getAddressWithOffset(Offset)) = TruncatedAddr; LLVM_DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " - << format("%p\n", Section.getAddressWithOffset(Offset))); + << format("%p\n", Section->getAddressWithOffset(Offset))); break; } case ELF::R_X86_64_PC8: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); int64_t RealOffset = Value + Addend - FinalAddress; assert(isInt<8>(RealOffset)); int8_t TruncOffset = (RealOffset & 0xFF); - Section.getAddress()[Offset] = TruncOffset; + Section->getAddress()[Offset] = TruncOffset; break; } case ELF::R_X86_64_PC32: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); int64_t RealOffset = Value + Addend - FinalAddress; assert(isInt<32>(RealOffset)); int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); - support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = + support::ulittle32_t::ref(Section->getAddressWithOffset(Offset)) = TruncOffset; break; } case ELF::R_X86_64_PC64: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); int64_t RealOffset = Value + Addend - FinalAddress; - support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = + support::ulittle64_t::ref(Section->getAddressWithOffset(Offset)) = RealOffset; LLVM_DEBUG(dbgs() << "Writing " << format("%p", RealOffset) << " at " << format("%p\n", FinalAddress)); @@ -322,26 +322,27 @@ case ELF::R_X86_64_GOTOFF64: { // Compute Value - GOTBase. uint64_t GOTBase = 0; - for (const auto &Section : Sections) { - if (Section.getName() == ".got") { - GOTBase = Section.getLoadAddressWithOffset(0); + for (const auto Section : Sections) { + if (Section->getName() == ".got") { + GOTBase = Section->getLoadAddressWithOffset(0); break; } } assert(GOTBase != 0 && "missing GOT"); int64_t GOTOffset = Value - GOTBase + Addend; - support::ulittle64_t::ref(Section.getAddressWithOffset(Offset)) = GOTOffset; + support::ulittle64_t::ref(Section->getAddressWithOffset(Offset)) = + GOTOffset; break; } } } -void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveX86Relocation(const SectionEntryProxy Section, uint64_t Offset, uint32_t Value, uint32_t Type, int32_t Addend) { switch (Type) { case ELF::R_386_32: { - support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = + support::ulittle32_t::ref(Section->getAddressWithOffset(Offset)) = Value + Addend; break; } @@ -350,9 +351,9 @@ case ELF::R_386_PLT32: case ELF::R_386_PC32: { uint32_t FinalAddress = - Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; + Section->getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; uint32_t RealOffset = Value + Addend - FinalAddress; - support::ulittle32_t::ref(Section.getAddressWithOffset(Offset)) = + support::ulittle32_t::ref(Section->getAddressWithOffset(Offset)) = RealOffset; break; } @@ -364,17 +365,17 @@ } } -void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend) { uint32_t *TargetPtr = - reinterpret_cast(Section.getAddressWithOffset(Offset)); - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + reinterpret_cast(Section->getAddressWithOffset(Offset)); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); // Data should use target endian. Code should always use little endian. bool isBE = Arch == Triple::aarch64_be; LLVM_DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" - << format("%llx", Section.getAddressWithOffset(Offset)) + << format("%llx", Section->getAddressWithOffset(Offset)) << " FinalAddress: 0x" << format("%llx", FinalAddress) << " Value: 0x" << format("%llx", Value) << " Type: 0x" << format("%x", Type) << " Addend: 0x" @@ -484,17 +485,18 @@ } } -void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveARMRelocation(const SectionEntryProxy Section, uint64_t Offset, uint32_t Value, uint32_t Type, int32_t Addend) { // TODO: Add Thumb relocations. uint32_t *TargetPtr = - reinterpret_cast(Section.getAddressWithOffset(Offset)); - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; + reinterpret_cast(Section->getAddressWithOffset(Offset)); + uint32_t FinalAddress = + Section->getLoadAddressWithOffset(Offset) & 0xFFFFFFFF; Value += Addend; LLVM_DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " - << Section.getAddressWithOffset(Offset) + << Section->getAddressWithOffset(Offset) << " FinalAddress: " << format("%p", FinalAddress) << " Value: " << format("%x", Value) << " Type: " << format("%x", Type) @@ -708,10 +710,10 @@ return ((value + 0x8000) >> 48) & 0xffff; } -void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntry &Section, +void RuntimeDyldELF::resolvePPC32Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend) { - uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); + uint8_t *LocalAddress = Section->getAddressWithOffset(Offset); switch (Type) { default: llvm_unreachable("Relocation type not implemented yet!"); @@ -728,10 +730,10 @@ } } -void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, +void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend) { - uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); + uint8_t *LocalAddress = Section->getAddressWithOffset(Offset); switch (Type) { default: llvm_unreachable("Relocation type not implemented yet!"); @@ -775,17 +777,17 @@ writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); } break; case ELF::R_PPC64_REL16_LO: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); uint64_t Delta = Value - FinalAddress + Addend; writeInt16BE(LocalAddress, applyPPClo(Delta)); } break; case ELF::R_PPC64_REL16_HI: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); uint64_t Delta = Value - FinalAddress + Addend; writeInt16BE(LocalAddress, applyPPChi(Delta)); } break; case ELF::R_PPC64_REL16_HA: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); uint64_t Delta = Value - FinalAddress + Addend; writeInt16BE(LocalAddress, applyPPCha(Delta)); } break; @@ -796,7 +798,7 @@ writeInt32BE(LocalAddress, Result); } break; case ELF::R_PPC64_REL24: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); int64_t delta = static_cast(Value - FinalAddress + Addend); if (SignExtend64<26>(delta) != delta) llvm_unreachable("Relocation R_PPC64_REL24 overflow"); @@ -805,14 +807,14 @@ writeInt32BE(LocalAddress, (Inst & 0xFC000003) | (delta & 0x03FFFFFC)); } break; case ELF::R_PPC64_REL32: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); int64_t delta = static_cast(Value - FinalAddress + Addend); if (SignExtend64<32>(delta) != delta) llvm_unreachable("Relocation R_PPC64_REL32 overflow"); writeInt32BE(LocalAddress, delta); } break; case ELF::R_PPC64_REL64: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); uint64_t Delta = Value - FinalAddress + Addend; writeInt64BE(LocalAddress, Delta); } break; @@ -822,42 +824,47 @@ } } -void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend) { - uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); + uint8_t *LocalAddress = Section->getAddressWithOffset(Offset); switch (Type) { default: llvm_unreachable("Relocation type not implemented yet!"); break; case ELF::R_390_PC16DBL: case ELF::R_390_PLT16DBL: { - int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); + int64_t Delta = + (Value + Addend) - Section->getLoadAddressWithOffset(Offset); assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); writeInt16BE(LocalAddress, Delta / 2); break; } case ELF::R_390_PC32DBL: case ELF::R_390_PLT32DBL: { - int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); + int64_t Delta = + (Value + Addend) - Section->getLoadAddressWithOffset(Offset); assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); writeInt32BE(LocalAddress, Delta / 2); break; } case ELF::R_390_PC16: { - int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); + int64_t Delta = + (Value + Addend) - Section->getLoadAddressWithOffset(Offset); assert(int16_t(Delta) == Delta && "R_390_PC16 overflow"); writeInt16BE(LocalAddress, Delta); break; } case ELF::R_390_PC32: { - int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); + int64_t Delta = + (Value + Addend) - Section->getLoadAddressWithOffset(Offset); assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); writeInt32BE(LocalAddress, Delta); break; } case ELF::R_390_PC64: { - int64_t Delta = (Value + Addend) - Section.getLoadAddressWithOffset(Offset); + int64_t Delta = + (Value + Addend) - Section->getLoadAddressWithOffset(Offset); writeInt64BE(LocalAddress, Delta); break; } @@ -876,7 +883,7 @@ } } -void RuntimeDyldELF::resolveBPFRelocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveBPFRelocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend) { bool isBE = Arch == Triple::bpfeb; @@ -888,17 +895,18 @@ case ELF::R_BPF_NONE: break; case ELF::R_BPF_64_64: { - write(isBE, Section.getAddressWithOffset(Offset), Value + Addend); + write(isBE, Section->getAddressWithOffset(Offset), Value + Addend); LLVM_DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " - << format("%p\n", Section.getAddressWithOffset(Offset))); + << format("%p\n", Section->getAddressWithOffset(Offset))); break; } case ELF::R_BPF_64_32: { Value += Addend; assert(Value <= UINT32_MAX); - write(isBE, Section.getAddressWithOffset(Offset), static_cast(Value)); + write(isBE, Section->getAddressWithOffset(Offset), + static_cast(Value)); LLVM_DEBUG(dbgs() << "Writing " << format("%p", Value) << " at " - << format("%p\n", Section.getAddressWithOffset(Offset))); + << format("%p\n", Section->getAddressWithOffset(Offset))); break; } } @@ -926,12 +934,12 @@ // symbol in the target address space. void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE, uint64_t Value) { - const SectionEntry &Section = Sections[RE.SectionID]; + const SectionEntryProxy Section = Sections[RE.SectionID]; return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend, RE.SymOffset, RE.SectionID); } -void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, +void RuntimeDyldELF::resolveRelocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type, int64_t Addend, uint64_t SymOffset, SID SectionID) { @@ -974,7 +982,7 @@ } void *RuntimeDyldELF::computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const { - return (void *)(Sections[SectionID].getObjAddress() + Offset); + return (void *)(Sections[SectionID]->getObjAddress() + Offset); } void RuntimeDyldELF::processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value) { @@ -1030,13 +1038,14 @@ const auto &SymInfo = Loc->second; Address = - uint64_t(Sections[SymInfo.getSectionID()].getLoadAddressWithOffset( + uint64_t(Sections[SymInfo.getSectionID()]->getLoadAddressWithOffset( SymInfo.getOffset())); } else { - Address = uint64_t(Sections[Value.SectionID].getLoadAddress()); + Address = uint64_t(Sections[Value.SectionID]->getLoadAddress()); } uint64_t Offset = RelI->getOffset(); - uint64_t SourceAddress = Sections[SectionID].getLoadAddressWithOffset(Offset); + uint64_t SourceAddress = + Sections[SectionID]->getLoadAddressWithOffset(Offset); // R_AARCH64_CALL26 requires immediate to be in range -2^27 <= imm < 2^27 // If distance between source and target is out of range then we should @@ -1056,7 +1065,7 @@ StubMap &Stubs) { LLVM_DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation."); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; uint64_t Offset = RelI->getOffset(); unsigned RelType = RelI->getType(); @@ -1064,26 +1073,26 @@ StubMap::const_iterator i = Stubs.find(Value); if (i != Stubs.end()) { resolveRelocation(Section, Offset, - (uint64_t)Section.getAddressWithOffset(i->second), + (uint64_t)Section->getAddressWithOffset(i->second), RelType, 0); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + Stubs[Value] = Section->getStubOffset(); uint8_t *StubTargetAddr = createStubFunction( - Section.getAddressWithOffset(Section.getStubOffset())); + Section->getAddressWithOffset(Section->getStubOffset())); - RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.getAddress(), + RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section->getAddress(), ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); RelocationEntry REmovk_g2(SectionID, - StubTargetAddr - Section.getAddress() + 4, + StubTargetAddr - Section->getAddress() + 4, ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); RelocationEntry REmovk_g1(SectionID, - StubTargetAddr - Section.getAddress() + 8, + StubTargetAddr - Section->getAddress() + 8, ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); RelocationEntry REmovk_g0(SectionID, - StubTargetAddr - Section.getAddress() + 12, + StubTargetAddr - Section->getAddress() + 12, ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); if (Value.SymbolName) { @@ -1098,10 +1107,10 @@ addRelocationForSection(REmovk_g0, Value.SectionID); } resolveRelocation(Section, Offset, - reinterpret_cast(Section.getAddressWithOffset( - Section.getStubOffset())), + reinterpret_cast(Section->getAddressWithOffset( + Section->getStubOffset())), RelType, 0); - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); } } @@ -1224,34 +1233,35 @@ RelType == ELF::R_ARM_JUMP24) { // This is an ARM branch relocation, need to use a stub function. LLVM_DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.\n"); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; // Look for an existing stub. StubMap::const_iterator i = Stubs.find(Value); if (i != Stubs.end()) { - resolveRelocation( - Section, Offset, - reinterpret_cast(Section.getAddressWithOffset(i->second)), - RelType, 0); + resolveRelocation(Section, Offset, + reinterpret_cast( + Section->getAddressWithOffset(i->second)), + RelType, 0); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + Stubs[Value] = Section->getStubOffset(); uint8_t *StubTargetAddr = createStubFunction( - Section.getAddressWithOffset(Section.getStubOffset())); - RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(), + Section->getAddressWithOffset(Section->getStubOffset())); + RelocationEntry RE(SectionID, StubTargetAddr - Section->getAddress(), ELF::R_ARM_ABS32, Value.Addend); if (Value.SymbolName) addRelocationForSymbol(RE, Value.SymbolName); else addRelocationForSection(RE, Value.SectionID); - resolveRelocation(Section, Offset, reinterpret_cast( - Section.getAddressWithOffset( - Section.getStubOffset())), - RelType, 0); - Section.advanceStubOffset(getMaxStubSize()); + resolveRelocation( + Section, Offset, + reinterpret_cast( + Section->getAddressWithOffset(Section->getStubOffset())), + RelType, 0); + Section->advanceStubOffset(getMaxStubSize()); } } else { uint32_t *Placeholder = @@ -1272,7 +1282,7 @@ if (RelType == ELF::R_MIPS_26) { // This is an Mips branch relocation, need to use a stub function. LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; // Extract the addend from the instruction. // We shift up by two since the Value will be down shifted again @@ -1290,18 +1300,19 @@ } else { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + Stubs[Value] = Section->getStubOffset(); unsigned AbiVariant = Obj.getPlatformFlags(); uint8_t *StubTargetAddr = createStubFunction( - Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant); + Section->getAddressWithOffset(Section->getStubOffset()), + AbiVariant); // Creating Hi and Lo relocations for the filled stub instructions. - RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(), + RelocationEntry REHi(SectionID, StubTargetAddr - Section->getAddress(), ELF::R_MIPS_HI16, Value.Addend); RelocationEntry RELo(SectionID, - StubTargetAddr - Section.getAddress() + 4, + StubTargetAddr - Section->getAddress() + 4, ELF::R_MIPS_LO16, Value.Addend); if (Value.SymbolName) { @@ -1312,9 +1323,10 @@ addRelocationForSection(RELo, Value.SectionID); } - RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset()); + RelocationEntry RE(SectionID, Offset, RelType, + Section->getStubOffset()); addRelocationForSection(RE, SectionID); - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); } } else if (RelType == ELF::R_MIPS_HI16 || RelType == ELF::R_MIPS_PCHI16) { int64_t Addend = (Opcode & 0x0000ffff) << 16; @@ -1374,7 +1386,7 @@ } else if (RelType == ELF::R_MIPS_26) { // This is an Mips branch relocation, need to use a stub function. LLVM_DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; // Look up for existing stub. StubMap::const_iterator i = Stubs.find(Value); @@ -1385,19 +1397,21 @@ } else { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + Stubs[Value] = Section->getStubOffset(); unsigned AbiVariant = Obj.getPlatformFlags(); uint8_t *StubTargetAddr = createStubFunction( - Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant); + Section->getAddressWithOffset(Section->getStubOffset()), + AbiVariant); if (IsMipsN32ABI) { // Creating Hi and Lo relocations for the filled stub instructions. - RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(), + RelocationEntry REHi(SectionID, + StubTargetAddr - Section->getAddress(), ELF::R_MIPS_HI16, Value.Addend); RelocationEntry RELo(SectionID, - StubTargetAddr - Section.getAddress() + 4, + StubTargetAddr - Section->getAddress() + 4, ELF::R_MIPS_LO16, Value.Addend); if (Value.SymbolName) { addRelocationForSymbol(REHi, Value.SymbolName); @@ -1410,16 +1424,16 @@ // Creating Highest, Higher, Hi and Lo relocations for the filled stub // instructions. RelocationEntry REHighest(SectionID, - StubTargetAddr - Section.getAddress(), + StubTargetAddr - Section->getAddress(), ELF::R_MIPS_HIGHEST, Value.Addend); RelocationEntry REHigher(SectionID, - StubTargetAddr - Section.getAddress() + 4, + StubTargetAddr - Section->getAddress() + 4, ELF::R_MIPS_HIGHER, Value.Addend); RelocationEntry REHi(SectionID, - StubTargetAddr - Section.getAddress() + 12, + StubTargetAddr - Section->getAddress() + 12, ELF::R_MIPS_HI16, Value.Addend); RelocationEntry RELo(SectionID, - StubTargetAddr - Section.getAddress() + 20, + StubTargetAddr - Section->getAddress() + 20, ELF::R_MIPS_LO16, Value.Addend); if (Value.SymbolName) { addRelocationForSymbol(REHighest, Value.SymbolName); @@ -1433,9 +1447,10 @@ addRelocationForSection(RELo, Value.SectionID); } } - RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset()); + RelocationEntry RE(SectionID, Offset, RelType, + Section->getStubOffset()); addRelocationForSection(RE, SectionID); - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); } } else { processSimpleRelocation(SectionID, Offset, RelType, Value); @@ -1450,8 +1465,8 @@ // an external symbol (either Value.SymbolName is set, or SymType is // Symbol::ST_Unknown) or if the target address is not within the // signed 24-bits branch address. - SectionEntry &Section = Sections[SectionID]; - uint8_t *Target = Section.getAddressWithOffset(Offset); + SectionEntryProxy Section = Sections[SectionID]; + uint8_t *Target = Section->getAddressWithOffset(Offset); bool RangeOverflow = false; bool IsExtern = Value.SymbolName || SymType == SymbolRef::ST_Unknown; if (!IsExtern) { @@ -1470,7 +1485,7 @@ } } uint8_t *RelocTarget = - Sections[Value.SectionID].getAddressWithOffset(Value.Addend); + Sections[Value.SectionID]->getAddressWithOffset(Value.Addend); int64_t delta = static_cast(Target - RelocTarget); // If it is within 26-bits branch range, just set the branch target if (SignExtend64<26>(delta) != delta) { @@ -1490,24 +1505,24 @@ // Symbol function stub already created, just relocate to it resolveRelocation(Section, Offset, reinterpret_cast( - Section.getAddressWithOffset(i->second)), + Section->getAddressWithOffset(i->second)), RelType, 0); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + Stubs[Value] = Section->getStubOffset(); uint8_t *StubTargetAddr = createStubFunction( - Section.getAddressWithOffset(Section.getStubOffset()), + Section->getAddressWithOffset(Section->getStubOffset()), AbiVariant); - RelocationEntry RE(SectionID, StubTargetAddr - Section.getAddress(), + RelocationEntry RE(SectionID, StubTargetAddr - Section->getAddress(), ELF::R_PPC64_ADDR64, Value.Addend); // Generates the 64-bits address loads as exemplified in section // 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 // the offset according to the target endianness. - uint64_t StubRelocOffset = StubTargetAddr - Section.getAddress(); + uint64_t StubRelocOffset = StubTargetAddr - Section->getAddress(); if (!IsTargetLittleEndian) StubRelocOffset += 2; @@ -1532,11 +1547,12 @@ addRelocationForSection(REl, Value.SectionID); } - resolveRelocation(Section, Offset, reinterpret_cast( - Section.getAddressWithOffset( - Section.getStubOffset())), - RelType, 0); - Section.advanceStubOffset(getMaxStubSize()); + resolveRelocation( + Section, Offset, + reinterpret_cast( + Section->getAddressWithOffset(Section->getStubOffset())), + RelType, 0); + Section->advanceStubOffset(getMaxStubSize()); } if (IsExtern || (AbiVariant == 2 && Value.SectionID != SectionID)) { // Restore the TOC for external calls @@ -1613,22 +1629,22 @@ // a stub for every relocation, so using a GOT in JIT code should be // no less space efficient than using an explicit constant pool. LLVM_DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation."); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; // Look for an existing stub. StubMap::const_iterator i = Stubs.find(Value); uintptr_t StubAddress; if (i != Stubs.end()) { - StubAddress = uintptr_t(Section.getAddressWithOffset(i->second)); + StubAddress = uintptr_t(Section->getAddressWithOffset(i->second)); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - uintptr_t BaseAddress = uintptr_t(Section.getAddress()); + uintptr_t BaseAddress = uintptr_t(Section->getAddress()); uintptr_t StubAlignment = getStubAlignment(); StubAddress = - (BaseAddress + Section.getStubOffset() + StubAlignment - 1) & + (BaseAddress + Section->getStubOffset() + StubAlignment - 1) & -StubAlignment; unsigned StubOffset = StubAddress - BaseAddress; @@ -1640,7 +1656,7 @@ addRelocationForSymbol(RE, Value.SymbolName); else addRelocationForSection(RE, Value.SectionID); - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); } if (RelType == ELF::R_390_GOTENT) @@ -1669,27 +1685,27 @@ if (Value.SymbolName) { // This is a call to an external function. // Look for an existing stub. - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; StubMap::const_iterator i = Stubs.find(Value); uintptr_t StubAddress; if (i != Stubs.end()) { - StubAddress = uintptr_t(Section.getAddress()) + i->second; + StubAddress = uintptr_t(Section->getAddress()) + i->second; LLVM_DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function (equivalent to a PLT entry). LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - uintptr_t BaseAddress = uintptr_t(Section.getAddress()); + uintptr_t BaseAddress = uintptr_t(Section->getAddress()); uintptr_t StubAlignment = getStubAlignment(); StubAddress = - (BaseAddress + Section.getStubOffset() + StubAlignment - 1) & + (BaseAddress + Section->getStubOffset() + StubAlignment - 1) & -StubAlignment; unsigned StubOffset = StubAddress - BaseAddress; Stubs[Value] = StubOffset; createStubFunction((uint8_t *)StubAddress); // Bump our stub offset counter - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); // Allocate a GOT Entry uint64_t GOTOffset = allocateGOTEntries(1); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldImpl.h @@ -238,6 +238,84 @@ typedef StringMap RTDyldSymbolTable; +class SectionList : SmallVector { + typedef SmallVector ListTy; + +public: + typedef unsigned SID; // Type for SectionIDs + + class SectionEntryProxy { + ListTy &Sections; + SID const Sid; + + public: + SectionEntryProxy(ListTy &Sections, SID Sid) + : Sections{Sections}, Sid(Sid) {} + SectionEntry const *operator->() const { return Sections.data() + Sid; } + SectionEntry *operator->() { return &Sections[Sid]; } + SectionEntryProxy &operator=(SectionEntry const &SE) { + Sections[Sid] = SE; + return *this; + } + }; + class SectionEntryConstProxy { + ListTy const &Sections; + SID const Sid; + + public: + SectionEntryConstProxy(ListTy const &Sections, SID Sid) + : Sections{Sections}, Sid(Sid) {} + SectionEntry const *operator->() const { return Sections.data() + Sid; } + }; + + struct iterator + : std::iterator { + ListTy &Sections; + SID Sid; + + public: + iterator(ListTy &Sections, SID Sid) : Sections(Sections), Sid(Sid) {} + value_type operator*() { return {Sections, Sid}; } + iterator operator++() { + ++Sid; + return *this; + } + iterator operator++(int) { + iterator other(*this); + ++Sid; + return other; + } + + bool operator==(iterator const &other) { return Sid == other.Sid; } + bool operator!=(iterator const &other) { return Sid != other.Sid; } + bool operator<(iterator const &other) { return Sid < other.Sid; } + bool operator<=(iterator const &other) { return Sid <= other.Sid; } + bool operator>(iterator const &other) { return Sid > other.Sid; } + bool operator>=(iterator const &other) { return Sid >= other.Sid; } + }; + + SectionEntryProxy operator[](SID Index) { + return {*static_cast(this), Index}; + } + SectionEntryConstProxy operator[](SID Index) const { + return {*static_cast(this), Index}; + } + + using ListTy::ListTy; + using ListTy::push_back; + using ListTy::size; + + SectionEntryProxy front() { return {*static_cast(this), 0}; } + + SectionEntryProxy back() { + return {*static_cast(this), (SID)size() - 1}; + } + + iterator begin() { return {*static_cast(this), 0}; } + + iterator end() { return {*static_cast(this), (SID)size()}; } +}; + class RuntimeDyldImpl { friend class RuntimeDyld::LoadedObjectInfo; protected: @@ -249,14 +327,16 @@ // The symbol resolver to use for external symbols. JITSymbolResolver &Resolver; + using SID = SectionList::SID; + using SectionEntryProxy = SectionList::SectionEntryProxy; + using SectionEntryConstProxy = SectionList::SectionEntryConstProxy; + + static constexpr SID RTDYLD_INVALID_SECTION_ID = (SID)(-1); + // A list of all sections emitted by the dynamic linker. These sections are // referenced in the code by means of their index in this list - SectionID. - typedef SmallVector SectionList; SectionList Sections; - typedef unsigned SID; // Type for SectionIDs -#define RTDYLD_INVALID_SECTION_ID ((RuntimeDyldImpl::SID)(-1)) - // Keep a map of sections from object file to the SectionID which // references it. typedef std::map ObjSectionToIDMap; @@ -477,16 +557,17 @@ loadObject(const object::ObjectFile &Obj) = 0; uint64_t getSectionLoadAddress(unsigned SectionID) const { - return Sections[SectionID].getLoadAddress(); + return Sections[SectionID]->getLoadAddress(); } uint8_t *getSectionAddress(unsigned SectionID) const { - return Sections[SectionID].getAddress(); + return Sections[SectionID]->getAddress(); } StringRef getSectionContent(unsigned SectionID) const { - return StringRef(reinterpret_cast(Sections[SectionID].getAddress()), - Sections[SectionID].getStubOffset() + getMaxStubSize()); + return StringRef( + reinterpret_cast(Sections[SectionID]->getAddress()), + Sections[SectionID]->getStubOffset() + getMaxStubSize()); } uint8_t* getSymbolLocalAddress(StringRef Name) const { diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -129,7 +129,9 @@ std::unique_ptr loadObject(const object::ObjectFile &O) override; - SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } + SectionEntryProxy getSection(unsigned SectionID) { + return Sections[SectionID]; + } bool isCompatibleFile(const object::ObjectFile &Obj) const override; }; diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -45,7 +45,7 @@ int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const { unsigned NumBytes = 1 << RE.Size; - uint8_t *Src = Sections[RE.SectionID].getAddress() + RE.Offset; + uint8_t *Src = Sections[RE.SectionID]->getAddress() + RE.Offset; return static_cast(readBytesUnaligned(Src, NumBytes)); } @@ -61,12 +61,12 @@ MachO::any_relocation_info RE = Obj.getRelocation(RelI->getRawDataRefImpl()); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; uint32_t RelocType = Obj.getAnyRelocationType(RE); bool IsPCRel = Obj.getAnyRelocationPCRel(RE); unsigned Size = Obj.getAnyRelocationLength(RE); uint64_t Offset = RelI->getOffset(); - uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); + uint8_t *LocalAddress = Section->getAddressWithOffset(Offset); unsigned NumBytes = 1 << Size; int64_t Addend = readBytesUnaligned(LocalAddress, NumBytes); @@ -147,9 +147,9 @@ void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const { - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddress() + RE.Offset; - uint64_t FinalAddress = Section.getLoadAddress() + RE.Offset; + SectionEntryConstProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddress() + RE.Offset; + uint64_t FinalAddress = Section->getLoadAddress() + RE.Offset; dbgs() << "resolveRelocation Section: " << RE.SectionID << " LocalAddress: " << format("%p", LocalAddress) @@ -196,7 +196,7 @@ "Pointers section does not contain a whole number of stubs?"); LLVM_DEBUG(dbgs() << "Populating pointer table section " - << Sections[PTSectionID].getName() << ", Section ID " + << Sections[PTSectionID]->getName() << ", Section ID " << PTSectionID << ", " << NumPTEntries << " entries, " << PTEntrySize << " bytes each:\n"); @@ -307,7 +307,8 @@ return Ret; } -static int64_t computeDelta(SectionEntry *A, SectionEntry *B) { +static int64_t computeDelta(SectionList::SectionEntryProxy A, + SectionList::SectionEntryProxy B) { int64_t ObjDistance = static_cast(A->getObjAddress()) - static_cast(B->getObjAddress()); int64_t MemDistance = A->getLoadAddress() - B->getLoadAddress(); @@ -322,16 +323,16 @@ 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 = nullptr; + SectionEntryProxy Text = Sections[SectionInfo.TextSID]; + SectionEntryProxy EHFrame = Sections[SectionInfo.EHFrameSID]; + SID ExceptTabIndex = RTDYLD_INVALID_SECTION_ID; if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID) - ExceptTab = &Sections[SectionInfo.ExceptTabSID]; + ExceptTabIndex = SectionInfo.ExceptTabSID; int64_t DeltaForText = computeDelta(Text, EHFrame); int64_t DeltaForEH = 0; - if (ExceptTab) - DeltaForEH = computeDelta(ExceptTab, EHFrame); + if (ExceptTabIndex != RTDYLD_INVALID_SECTION_ID) + DeltaForEH = computeDelta(Sections[ExceptTabIndex], EHFrame); uint8_t *P = EHFrame->getAddress(); uint8_t *End = P + EHFrame->getSize(); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFAArch64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFAArch64.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFAArch64.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFAArch64.h @@ -74,14 +74,14 @@ uint64_t getImageBase() { if (!ImageBase) { ImageBase = std::numeric_limits::max(); - for (const SectionEntry &Section : Sections) + for (const SectionEntryProxy &Section : Sections) // The Sections list may contain sections that weren't loaded for // whatever reason: they may be debug sections, and ProcessAllSections // is false, or they may be sections that contain 0 bytes. If the // section isn't loaded, the load address will be 0, and it should not // be included in the ImageBase calculation. - if (Section.getLoadAddress() != 0) - ImageBase = std::min(ImageBase, Section.getLoadAddress()); + if (Section->getLoadAddress() != 0) + ImageBase = std::min(ImageBase, Section->getLoadAddress()); } return ImageBase; } @@ -100,7 +100,7 @@ uint64_t Offset, uint64_t RelType, uint64_t Addend, StubMap &Stubs) { uintptr_t StubOffset; - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; RelocationValueRef OriginalRelValueRef; OriginalRelValueRef.SectionID = SectionID; @@ -113,10 +113,10 @@ LLVM_DEBUG(dbgs() << " Create a new stub function for " << TargetName.data() << "\n"); - StubOffset = Section.getStubOffset(); + StubOffset = Section->getStubOffset(); Stubs[OriginalRelValueRef] = StubOffset; - createStubFunction(Section.getAddressWithOffset(StubOffset)); - Section.advanceStubOffset(getMaxStubSize()); + createStubFunction(Section->getAddressWithOffset(StubOffset)); + Section->advanceStubOffset(getMaxStubSize()); } else { LLVM_DEBUG(dbgs() << " Stub function found for " << TargetName.data() << "\n"); @@ -125,7 +125,7 @@ // Resolve original relocation to stub function. const RelocationEntry RE(SectionID, Offset, RelType, Addend); - resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset)); + resolveRelocation(RE, Section->getLoadAddressWithOffset(StubOffset)); // adjust relocation info so resolution writes to the stub function // Here an internal relocation type is used for resolving long branch via @@ -165,8 +165,8 @@ // Determine the Addend used to adjust the relocation value. uint64_t Addend = 0; - SectionEntry &AddendSection = Sections[SectionID]; - uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset; + SectionEntryProxy AddendSection = Sections[SectionID]; + uintptr_t ObjTarget = AddendSection->getObjAddress() + Offset; uint8_t *Displacement = (uint8_t *)ObjTarget; switch (RelType) { @@ -244,8 +244,8 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { const auto Section = Sections[RE.SectionID]; - uint8_t *Target = Section.getAddressWithOffset(RE.Offset); - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint8_t *Target = Section->getAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); switch (RE.RelType) { default: diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFI386.h @@ -59,8 +59,8 @@ // Determine the Addend used to adjust the relocation value. uint64_t Addend = 0; - SectionEntry &AddendSection = Sections[SectionID]; - uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset; + SectionEntryProxy AddendSection = Sections[SectionID]; + uintptr_t ObjTarget = AddendSection->getObjAddress() + Offset; uint8_t *Displacement = (uint8_t *)ObjTarget; switch (RelType) { @@ -130,7 +130,7 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { const auto Section = Sections[RE.SectionID]; - uint8_t *Target = Section.getAddressWithOffset(RE.Offset); + uint8_t *Target = Section->getAddressWithOffset(RE.Offset); switch (RE.RelType) { case COFF::IMAGE_REL_I386_ABSOLUTE: @@ -141,7 +141,7 @@ uint64_t Result = RE.Sections.SectionA == static_cast(-1) ? Value - : Sections[RE.Sections.SectionA].getLoadAddressWithOffset( + : Sections[RE.Sections.SectionA]->getLoadAddressWithOffset( RE.Addend); assert(Result <= UINT32_MAX && "relocation overflow"); LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset @@ -156,8 +156,8 @@ // The target's 32-bit RVA. // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase uint64_t Result = - Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend) - - Sections[0].getLoadAddress(); + Sections[RE.Sections.SectionA]->getLoadAddressWithOffset(RE.Addend) - + Sections[0]->getLoadAddress(); assert(Result <= UINT32_MAX && "relocation overflow"); LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset << " RelType: IMAGE_REL_I386_DIR32NB" @@ -171,8 +171,8 @@ // 32-bit relative displacement to the target. uint64_t Result = RE.Sections.SectionA == static_cast(-1) ? Value - : Sections[RE.Sections.SectionA].getLoadAddress(); - Result = Result - Section.getLoadAddress() + RE.Addend - 4 - RE.Offset; + : Sections[RE.Sections.SectionA]->getLoadAddress(); + Result = Result - Section->getLoadAddress() + RE.Addend - 4 - RE.Offset; assert(static_cast(Result) <= INT32_MAX && "relocation overflow"); assert(static_cast(Result) >= INT32_MIN && diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFThumb.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFThumb.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFThumb.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFThumb.h @@ -81,8 +81,8 @@ // Determine the Addend used to adjust the relocation value. uint64_t Addend = 0; - SectionEntry &AddendSection = Sections[SectionID]; - uintptr_t ObjTarget = AddendSection.getObjAddress() + Offset; + SectionEntryProxy AddendSection = Sections[SectionID]; + uintptr_t ObjTarget = AddendSection->getObjAddress() + Offset; uint8_t *Displacement = (uint8_t *)ObjTarget; switch (RelType) { @@ -173,7 +173,7 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { const auto Section = Sections[RE.SectionID]; - uint8_t *Target = Section.getAddressWithOffset(RE.Offset); + uint8_t *Target = Section->getAddressWithOffset(RE.Offset); int ISASelectionBit = RE.IsTargetThumbFunc ? 1 : 0; switch (RE.RelType) { @@ -186,7 +186,8 @@ uint64_t Result = RE.Sections.SectionA == static_cast(-1) ? Value - : Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend); + : Sections[RE.Sections.SectionA]->getLoadAddressWithOffset( + RE.Addend); Result |= ISASelectionBit; assert(Result <= UINT32_MAX && "relocation overflow"); LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset @@ -200,8 +201,8 @@ case COFF::IMAGE_REL_ARM_ADDR32NB: { // The target's 32-bit RVA. // NOTE: use Section[0].getLoadAddress() as an approximation of ImageBase - uint64_t Result = Sections[RE.Sections.SectionA].getLoadAddress() - - Sections[0].getLoadAddress() + RE.Addend; + uint64_t Result = Sections[RE.Sections.SectionA]->getLoadAddress() - + Sections[0]->getLoadAddress() + RE.Addend; assert(Result <= UINT32_MAX && "relocation overflow"); LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset << " RelType: IMAGE_REL_ARM_ADDR32NB" @@ -233,7 +234,7 @@ case COFF::IMAGE_REL_ARM_MOV32T: { // 32-bit VA of the target applied to a contiguous MOVW+MOVT pair. uint64_t Result = - Sections[RE.Sections.SectionA].getLoadAddressWithOffset(RE.Addend); + Sections[RE.Sections.SectionA]->getLoadAddressWithOffset(RE.Addend); assert(Result <= UINT32_MAX && "relocation overflow"); LLVM_DEBUG(dbgs() << "\t\tOffset: " << RE.Offset << " RelType: IMAGE_REL_ARM_MOV32T" @@ -261,8 +262,9 @@ } case COFF::IMAGE_REL_ARM_BRANCH20T: { // The most significant 20-bits of the signed 21-bit relative displacement - uint64_t Value = - RE.Addend - (Sections[RE.SectionID].getLoadAddress() + RE.Offset) - 4; + uint64_t Value = RE.Addend - + (Sections[RE.SectionID]->getLoadAddress() + RE.Offset) - + 4; assert(static_cast(RE.Addend) <= INT32_MAX && "relocation overflow"); assert(static_cast(RE.Addend) >= INT32_MIN && @@ -276,8 +278,9 @@ } case COFF::IMAGE_REL_ARM_BRANCH24T: { // The most significant 24-bits of the signed 25-bit relative displacement - uint64_t Value = - RE.Addend - (Sections[RE.SectionID].getLoadAddress() + RE.Offset) - 4; + uint64_t Value = RE.Addend - + (Sections[RE.SectionID]->getLoadAddress() + RE.Offset) - + 4; assert(static_cast(RE.Addend) <= INT32_MAX && "relocation overflow"); assert(static_cast(RE.Addend) >= INT32_MIN && @@ -291,8 +294,9 @@ } case COFF::IMAGE_REL_ARM_BLX23T: { // The most significant 24-bits of the signed 25-bit relative displacement - uint64_t Value = - RE.Addend - (Sections[RE.SectionID].getLoadAddress() + RE.Offset) - 4; + uint64_t Value = RE.Addend - + (Sections[RE.SectionID]->getLoadAddress() + RE.Offset) - + 4; assert(static_cast(RE.Addend) <= INT32_MAX && "relocation overflow"); assert(static_cast(RE.Addend) >= INT32_MIN && diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h @@ -35,14 +35,14 @@ uint64_t getImageBase() { if (!ImageBase) { ImageBase = std::numeric_limits::max(); - for (const SectionEntry &Section : Sections) + for (const SectionEntryProxy Section : Sections) // The Sections list may contain sections that weren't loaded for // whatever reason: they may be debug sections, and ProcessAllSections // is false, or they may be sections that contain 0 bytes. If the // section isn't loaded, the load address will be 0, and it should not // be included in the ImageBase calculation. - if (Section.getLoadAddress() != 0) - ImageBase = std::min(ImageBase, Section.getLoadAddress()); + if (Section->getLoadAddress() != 0) + ImageBase = std::min(ImageBase, Section->getLoadAddress()); } return ImageBase; } @@ -84,8 +84,8 @@ // symbol location). For external symbols, Value will be the address of the // symbol in the target address space. void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *Target = Section.getAddressWithOffset(RE.Offset); + const SectionEntryProxy Section = Sections[RE.SectionID]; + uint8_t *Target = Section->getAddressWithOffset(RE.Offset); switch (RE.RelType) { @@ -95,7 +95,7 @@ case COFF::IMAGE_REL_AMD64_REL32_3: case COFF::IMAGE_REL_AMD64_REL32_4: case COFF::IMAGE_REL_AMD64_REL32_5: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); // Delta is the distance from the start of the reloc to the end of the // instruction with the reloc. uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32); @@ -145,7 +145,7 @@ uint64_t Offset, uint64_t RelType, uint64_t Addend, StubMap &Stubs) { uintptr_t StubOffset; - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; RelocationValueRef OriginalRelValueRef; OriginalRelValueRef.SectionID = SectionID; @@ -158,10 +158,10 @@ LLVM_DEBUG(dbgs() << " Create a new stub function for " << TargetName.data() << "\n"); - StubOffset = Section.getStubOffset(); + StubOffset = Section->getStubOffset(); Stubs[OriginalRelValueRef] = StubOffset; - createStubFunction(Section.getAddressWithOffset(StubOffset)); - Section.advanceStubOffset(getMaxStubSize()); + createStubFunction(Section->getAddressWithOffset(StubOffset)); + Section->advanceStubOffset(getMaxStubSize()); } else { LLVM_DEBUG(dbgs() << " Stub function found for " << TargetName.data() << "\n"); @@ -171,12 +171,12 @@ // FIXME: If RelType == COFF::IMAGE_REL_AMD64_ADDR32NB we should be able // to ignore the __ImageBase requirement and just forward to the stub // directly as an offset of this section: - // write32BitOffset(Section.getAddressWithOffset(Offset), 0, StubOffset); + // write32BitOffset(Section->getAddressWithOffset(Offset), 0, StubOffset); // .xdata exception handler's aren't having this though. // Resolve original relocation to stub function. const RelocationEntry RE(SectionID, Offset, RelType, Addend); - resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset)); + resolveRelocation(RE, Section->getLoadAddressWithOffset(StubOffset)); // adjust relocation info so resolution writes to the stub function Addend = 0; @@ -208,8 +208,8 @@ uint64_t RelType = RelI->getType(); uint64_t Offset = RelI->getOffset(); uint64_t Addend = 0; - SectionEntry &Section = Sections[SectionID]; - uintptr_t ObjTarget = Section.getObjAddress() + Offset; + SectionEntryProxy Section = Sections[SectionID]; + uintptr_t ObjTarget = Section->getObjAddress() + Offset; Expected TargetNameOrErr = Symbol->getName(); if (!TargetNameOrErr) @@ -270,9 +270,9 @@ void registerEHFrames() override { for (auto const &EHFrameSID : UnregisteredEHFrameSections) { - uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress(); - uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress(); - size_t EHFrameSize = Sections[EHFrameSID].getSize(); + uint8_t *EHFrameAddr = Sections[EHFrameSID]->getAddress(); + uint64_t EHFrameLoadAddr = Sections[EHFrameSID]->getLoadAddress(); + size_t EHFrameSize = Sections[EHFrameSID]->getSize(); MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); RegisteredEHFrameSections.push_back(EHFrameSID); } diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.h @@ -28,14 +28,17 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override; protected: - void resolveMIPSO32Relocation(const SectionEntry &Section, uint64_t Offset, - uint32_t Value, uint32_t Type, int32_t Addend); - void resolveMIPSN32Relocation(const SectionEntry &Section, uint64_t Offset, - uint64_t Value, uint32_t Type, int64_t Addend, - uint64_t SymOffset, SID SectionID); - void resolveMIPSN64Relocation(const SectionEntry &Section, uint64_t Offset, - uint64_t Value, uint32_t Type, int64_t Addend, - uint64_t SymOffset, SID SectionID); + void resolveMIPSO32Relocation(const SectionEntryProxy Section, + uint64_t Offset, uint32_t Value, uint32_t Type, + int32_t Addend); + void resolveMIPSN32Relocation(const SectionEntryProxy Section, + uint64_t Offset, uint64_t Value, uint32_t Type, + int64_t Addend, uint64_t SymOffset, + SID SectionID); + void resolveMIPSN64Relocation(const SectionEntryProxy Section, + uint64_t Offset, uint64_t Value, uint32_t Type, + int64_t Addend, uint64_t SymOffset, + SID SectionID); private: /// A object file specific relocation resolver @@ -49,11 +52,12 @@ /// \param Value Target symbol address to apply the relocation action void applyRelocation(const RelocationEntry &RE, uint64_t Value); - int64_t evaluateMIPS32Relocation(const SectionEntry &Section, uint64_t Offset, - uint64_t Value, uint32_t Type); - int64_t evaluateMIPS64Relocation(const SectionEntry &Section, + int64_t evaluateMIPS32Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, - uint32_t Type, int64_t Addend, + uint32_t Type); + int64_t evaluateMIPS64Relocation(const SectionEntryProxy Section, + uint64_t Offset, uint64_t Value, + uint32_t Type, int64_t Addend, uint64_t SymOffset, SID SectionID); void applyMIPSRelocation(uint8_t *TargetPtr, int64_t CalculatedValue, diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.cpp --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldELFMips.cpp @@ -13,7 +13,7 @@ void RuntimeDyldELFMips::resolveRelocation(const RelocationEntry &RE, uint64_t Value) { - const SectionEntry &Section = Sections[RE.SectionID]; + const SectionEntryProxy Section = Sections[RE.SectionID]; if (IsMipsO32ABI) resolveMIPSO32Relocation(Section, RE.Offset, Value, RE.RelType, RE.Addend); else if (IsMipsN32ABI) { @@ -30,7 +30,7 @@ uint64_t Value, uint64_t Addend) { if (IsMipsN32ABI) { - const SectionEntry &Section = Sections[RE.SectionID]; + const SectionEntryProxy Section = Sections[RE.SectionID]; Value = evaluateMIPS64Relocation(Section, RE.Offset, Value, RE.RelType, Addend, RE.SymOffset, RE.SectionID); return Value; @@ -41,8 +41,8 @@ void RuntimeDyldELFMips::applyRelocation(const RelocationEntry &RE, uint64_t Value) { if (IsMipsN32ABI) { - const SectionEntry &Section = Sections[RE.SectionID]; - applyMIPSRelocation(Section.getAddressWithOffset(RE.Offset), Value, + const SectionEntryProxy Section = Sections[RE.SectionID]; + applyMIPSRelocation(Section->getAddressWithOffset(RE.Offset), Value, RE.RelType); return; } @@ -50,14 +50,14 @@ } int64_t -RuntimeDyldELFMips::evaluateMIPS32Relocation(const SectionEntry &Section, +RuntimeDyldELFMips::evaluateMIPS32Relocation(const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, uint32_t Type) { LLVM_DEBUG(dbgs() << "evaluateMIPS32Relocation, LocalAddress: 0x" - << format("%llx", Section.getAddressWithOffset(Offset)) + << format("%llx", Section->getAddressWithOffset(Offset)) << " FinalAddress: 0x" - << format("%llx", Section.getLoadAddressWithOffset(Offset)) + << format("%llx", Section->getLoadAddressWithOffset(Offset)) << " Value: 0x" << format("%llx", Value) << " Type: 0x" << format("%x", Type) << "\n"); @@ -75,44 +75,44 @@ case ELF::R_MIPS_LO16: return Value; case ELF::R_MIPS_PC32: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return Value - FinalAddress; } case ELF::R_MIPS_PC16: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return (Value - FinalAddress) >> 2; } case ELF::R_MIPS_PC19_S2: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return (Value - (FinalAddress & ~0x3)) >> 2; } case ELF::R_MIPS_PC21_S2: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return (Value - FinalAddress) >> 2; } case ELF::R_MIPS_PC26_S2: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return (Value - FinalAddress) >> 2; } case ELF::R_MIPS_PCHI16: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return (Value - FinalAddress + 0x8000) >> 16; } case ELF::R_MIPS_PCLO16: { - uint32_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint32_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return Value - FinalAddress; } } } int64_t RuntimeDyldELFMips::evaluateMIPS64Relocation( - const SectionEntry &Section, uint64_t Offset, uint64_t Value, uint32_t Type, - int64_t Addend, uint64_t SymOffset, SID SectionID) { + const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, + uint32_t Type, int64_t Addend, uint64_t SymOffset, SID SectionID) { LLVM_DEBUG(dbgs() << "evaluateMIPS64Relocation, LocalAddress: 0x" - << format("%llx", Section.getAddressWithOffset(Offset)) + << format("%llx", Section->getAddressWithOffset(Offset)) << " FinalAddress: 0x" - << format("%llx", Section.getLoadAddressWithOffset(Offset)) + << format("%llx", Section->getLoadAddressWithOffset(Offset)) << " Value: 0x" << format("%llx", Value) << " Type: 0x" << format("%x", Type) << " Addend: 0x" << format("%llx", Addend) @@ -175,35 +175,35 @@ return Value + Addend - (GOTAddr + 0x7ff0); } case ELF::R_MIPS_PC16: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return ((Value + Addend - FinalAddress) >> 2) & 0xffff; } case ELF::R_MIPS_PC32: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return Value + Addend - FinalAddress; } case ELF::R_MIPS_PC18_S3: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return ((Value + Addend - (FinalAddress & ~0x7)) >> 3) & 0x3ffff; } case ELF::R_MIPS_PC19_S2: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return ((Value + Addend - (FinalAddress & ~0x3)) >> 2) & 0x7ffff; } case ELF::R_MIPS_PC21_S2: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return ((Value + Addend - FinalAddress) >> 2) & 0x1fffff; } case ELF::R_MIPS_PC26_S2: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return ((Value + Addend - FinalAddress) >> 2) & 0x3ffffff; } case ELF::R_MIPS_PCHI16: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return ((Value + Addend - FinalAddress + 0x8000) >> 16) & 0xffff; } case ELF::R_MIPS_PCLO16: { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(Offset); return (Value + Addend - FinalAddress) & 0xffff; } } @@ -263,17 +263,17 @@ } void RuntimeDyldELFMips::resolveMIPSN32Relocation( - const SectionEntry &Section, uint64_t Offset, uint64_t Value, uint32_t Type, - int64_t Addend, uint64_t SymOffset, SID SectionID) { + const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, + uint32_t Type, int64_t Addend, uint64_t SymOffset, SID SectionID) { int64_t CalculatedValue = evaluateMIPS64Relocation( Section, Offset, Value, Type, Addend, SymOffset, SectionID); - applyMIPSRelocation(Section.getAddressWithOffset(Offset), CalculatedValue, + applyMIPSRelocation(Section->getAddressWithOffset(Offset), CalculatedValue, Type); } void RuntimeDyldELFMips::resolveMIPSN64Relocation( - const SectionEntry &Section, uint64_t Offset, uint64_t Value, uint32_t Type, - int64_t Addend, uint64_t SymOffset, SID SectionID) { + const SectionEntryProxy Section, uint64_t Offset, uint64_t Value, + uint32_t Type, int64_t Addend, uint64_t SymOffset, SID SectionID) { uint32_t r_type = Type & 0xff; uint32_t r_type2 = (Type >> 8) & 0xff; uint32_t r_type3 = (Type >> 16) & 0xff; @@ -296,20 +296,20 @@ CalculatedValue, SymOffset, SectionID); } - applyMIPSRelocation(Section.getAddressWithOffset(Offset), CalculatedValue, + applyMIPSRelocation(Section->getAddressWithOffset(Offset), CalculatedValue, RelType); } -void RuntimeDyldELFMips::resolveMIPSO32Relocation(const SectionEntry &Section, - uint64_t Offset, - uint32_t Value, uint32_t Type, - int32_t Addend) { - uint8_t *TargetPtr = Section.getAddressWithOffset(Offset); +void RuntimeDyldELFMips::resolveMIPSO32Relocation( + const SectionEntryProxy Section, uint64_t Offset, uint32_t Value, + uint32_t Type, int32_t Addend) { + uint8_t *TargetPtr = Section->getAddressWithOffset(Offset); Value += Addend; LLVM_DEBUG(dbgs() << "resolveMIPSO32Relocation, LocalAddress: " - << Section.getAddressWithOffset(Offset) << " FinalAddress: " - << format("%p", Section.getLoadAddressWithOffset(Offset)) + << Section->getAddressWithOffset(Offset) + << " FinalAddress: " + << format("%p", Section->getLoadAddressWithOffset(Offset)) << " Value: " << format("%x", Value) << " Type: " << format("%x", Type) << " Addend: " << format("%x", Addend) << " SymOffset: " << format("%x", Offset) << "\n"); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h @@ -32,8 +32,8 @@ /// Extract the addend encoded in the instruction / memory location. Expected decodeAddend(const RelocationEntry &RE) const { - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); + SectionEntryConstProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddressWithOffset(RE.Offset); unsigned NumBytes = 1 << RE.Size; int64_t Addend = 0; // Verify that the relocation has the correct size and alignment. @@ -355,8 +355,8 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { LLVM_DEBUG(dumpRelocationToResolve(RE, Value)); - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); + const SectionEntryProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddressWithOffset(RE.Offset); MachO::RelocationInfoType RelType = static_cast(RE.RelType); @@ -389,7 +389,7 @@ case MachO::ARM64_RELOC_BRANCH26: { assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported"); // Check if branch is in range. - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); int64_t PCRelVal = Value - FinalAddress + RE.Addend; encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); break; @@ -398,7 +398,7 @@ case MachO::ARM64_RELOC_PAGE21: { assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported"); // Adjust for PC-relative relocation and offset. - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); int64_t PCRelVal = ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096)); encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); @@ -415,8 +415,8 @@ break; } case MachO::ARM64_RELOC_SUBTRACTOR: { - uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress(); - uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress(); + uint64_t SectionABase = Sections[RE.Sections.SectionA]->getLoadAddress(); + uint64_t SectionBBase = Sections[RE.Sections.SectionB]->getLoadAddress(); assert((Value == SectionABase || Value == SectionBBase) && "Unexpected SUBTRACTOR relocation value."); Value = SectionABase - SectionBBase + RE.Addend; @@ -444,7 +444,7 @@ assert((RE.RelType == MachO::ARM64_RELOC_POINTER_TO_GOT && (RE.Size == 2 || RE.Size == 3)) || RE.Size == 2); - SectionEntry &Section = Sections[RE.SectionID]; + SectionEntryProxy Section = Sections[RE.SectionID]; StubMap::const_iterator i = Stubs.find(Value); int64_t Offset; if (i != Stubs.end()) @@ -452,10 +452,10 @@ else { // FIXME: There must be a better way to do this then to check and fix the // alignment every time!!! - uintptr_t BaseAddress = uintptr_t(Section.getAddress()); + uintptr_t BaseAddress = uintptr_t(Section->getAddress()); uintptr_t StubAlignment = getStubAlignment(); uintptr_t StubAddress = - (BaseAddress + Section.getStubOffset() + StubAlignment - 1) & + (BaseAddress + Section->getStubOffset() + StubAlignment - 1) & -StubAlignment; unsigned StubOffset = StubAddress - BaseAddress; Stubs[Value] = StubOffset; @@ -468,7 +468,7 @@ addRelocationForSymbol(GOTRE, Value.SymbolName); else addRelocationForSection(GOTRE, Value.SectionID); - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); Offset = static_cast(StubOffset); } RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset, @@ -487,7 +487,7 @@ unsigned Size = Obj.getAnyRelocationLength(RE); uint64_t Offset = RelI->getOffset(); - uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset); + uint8_t *LocalAddress = Sections[SectionID]->getAddressWithOffset(Offset); unsigned NumBytes = 1 << Size; Expected SubtrahendNameOrErr = RelI->getSymbol()->getName(); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h @@ -49,11 +49,11 @@ } bool isAddrTargetThumb(unsigned SectionID, uint64_t Offset) { - auto TargetObjAddr = Sections[SectionID].getObjAddress() + Offset; + auto TargetObjAddr = Sections[SectionID]->getObjAddress() + Offset; for (auto &KV : GlobalSymbolTable) { auto &Entry = KV.second; auto SymbolObjAddr = - Sections[Entry.getSectionID()].getObjAddress() + Entry.getOffset(); + Sections[Entry.getSectionID()]->getObjAddress() + Entry.getOffset(); if (TargetObjAddr == SymbolObjAddr) return (Entry.getFlags().getTargetFlags() & ARMJITSymbolFlags::Thumb); } @@ -61,8 +61,8 @@ } Expected decodeAddend(const RelocationEntry &RE) const { - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); + SectionEntryConstProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddressWithOffset(RE.Offset); switch (RE.RelType) { default: @@ -202,13 +202,13 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { LLVM_DEBUG(dumpRelocationToResolve(RE, Value)); - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); + const SectionEntryProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddressWithOffset(RE.Offset); // If the relocation is PC-relative, the value to be encoded is the // pointer difference. if (RE.IsPCRel) { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); Value -= FinalAddress; // ARM PCRel relocations have an effective-PC offset of two instructions // (four bytes in Thumb mode, 8 bytes in ARM mode). @@ -257,8 +257,8 @@ break; } case MachO::ARM_RELOC_HALF_SECTDIFF: { - uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress(); - uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress(); + uint64_t SectionABase = Sections[RE.Sections.SectionA]->getLoadAddress(); + uint64_t SectionBBase = Sections[RE.Sections.SectionB]->getLoadAddress(); assert((Value == SectionABase || Value == SectionBBase) && "Unexpected HALFSECTDIFF relocation value."); Value = SectionABase - SectionBBase + RE.Addend; @@ -307,15 +307,15 @@ StubMap &Stubs) { // This is an ARM branch relocation, need to use a stub function. // Look up for existing stub. - SectionEntry &Section = Sections[RE.SectionID]; + SectionEntryProxy Section = Sections[RE.SectionID]; RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); uint8_t *Addr; if (i != Stubs.end()) { - Addr = Section.getAddressWithOffset(i->second); + Addr = Section->getAddressWithOffset(i->second); } else { // Create a new stub function. - assert(Section.getStubOffset() % 4 == 0 && "Misaligned stub"); - Stubs[Value] = Section.getStubOffset(); + assert(Section->getStubOffset() % 4 == 0 && "Misaligned stub"); + Stubs[Value] = Section->getStubOffset(); uint32_t StubOpcode = 0; if (RE.RelType == MachO::ARM_RELOC_BR24) StubOpcode = 0xe51ff004; // ldr pc, [pc, #-4] @@ -323,18 +323,18 @@ StubOpcode = 0xf000f8df; // ldr pc, [pc] else llvm_unreachable("Unrecognized relocation"); - Addr = Section.getAddressWithOffset(Section.getStubOffset()); + Addr = Section->getAddressWithOffset(Section->getStubOffset()); writeBytesUnaligned(StubOpcode, Addr, 4); uint8_t *StubTargetAddr = Addr + 4; RelocationEntry StubRE( - RE.SectionID, StubTargetAddr - Section.getAddress(), + RE.SectionID, StubTargetAddr - Section->getAddress(), MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, 2); StubRE.IsTargetThumbFunc = RE.IsTargetThumbFunc; if (Value.SymbolName) addRelocationForSymbol(StubRE, Value.SymbolName); else addRelocationForSection(StubRE, Value.SectionID); - Section.advanceStubOffset(getMaxStubSize()); + Section->advanceStubOffset(getMaxStubSize()); } RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0, RE.IsPCRel, RE.Size); @@ -357,11 +357,11 @@ unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE); bool IsThumb = HalfDiffKindBits & 0x2; - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; uint32_t RelocType = MachO.getAnyRelocationType(RE); bool IsPCRel = MachO.getAnyRelocationPCRel(RE); uint64_t Offset = RelI->getOffset(); - uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); + uint8_t *LocalAddress = Section->getAddressWithOffset(Offset); int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out. if (IsThumb) diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h @@ -98,11 +98,11 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { LLVM_DEBUG(dumpRelocationToResolve(RE, Value)); - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); + const SectionEntryProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddressWithOffset(RE.Offset); if (RE.IsPCRel) { - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation. } @@ -112,8 +112,8 @@ break; case MachO::GENERIC_RELOC_SECTDIFF: case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { - uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress(); - uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress(); + uint64_t SectionABase = Sections[RE.Sections.SectionA]->getLoadAddress(); + uint64_t SectionBBase = Sections[RE.Sections.SectionB]->getLoadAddress(); assert((Value == SectionABase || Value == SectionBBase) && "Unexpected SECTDIFF relocation value."); Value = SectionABase - SectionBBase + RE.Addend; @@ -151,12 +151,12 @@ MachO::any_relocation_info RE = Obj.getRelocation(RelI->getRawDataRefImpl()); - SectionEntry &Section = Sections[SectionID]; + SectionEntryProxy Section = Sections[SectionID]; uint32_t RelocType = Obj.getAnyRelocationType(RE); bool IsPCRel = Obj.getAnyRelocationPCRel(RE); unsigned Size = Obj.getAnyRelocationLength(RE); uint64_t Offset = RelI->getOffset(); - uint8_t *LocalAddress = Section.getAddressWithOffset(Offset); + uint8_t *LocalAddress = Section->getAddressWithOffset(Offset); unsigned NumBytes = 1 << Size; uint64_t Addend = readBytesUnaligned(LocalAddress, NumBytes); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h @@ -85,15 +85,15 @@ void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { LLVM_DEBUG(dumpRelocationToResolve(RE, Value)); - const SectionEntry &Section = Sections[RE.SectionID]; - uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); + const SectionEntryProxy Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section->getAddressWithOffset(RE.Offset); // If the relocation is PC-relative, the value to be encoded is the // pointer difference. if (RE.IsPCRel) { // FIXME: It seems this value needs to be adjusted by 4 for an effective // PC address. Is that expected? Only for branches, perhaps? - uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); + uint64_t FinalAddress = Section->getLoadAddressWithOffset(RE.Offset); Value -= FinalAddress + 4; } @@ -109,8 +109,8 @@ writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size); break; case MachO::X86_64_RELOC_SUBTRACTOR: { - uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress(); - uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress(); + uint64_t SectionABase = Sections[RE.Sections.SectionA]->getLoadAddress(); + uint64_t SectionBBase = Sections[RE.Sections.SectionB]->getLoadAddress(); assert((Value == SectionABase || Value == SectionBBase) && "Unexpected SUBTRACTOR relocation value."); Value = SectionABase - SectionBBase + RE.Addend; @@ -128,25 +128,26 @@ private: void processGOTRelocation(const RelocationEntry &RE, RelocationValueRef &Value, StubMap &Stubs) { - SectionEntry &Section = Sections[RE.SectionID]; + SectionEntryProxy Section = Sections[RE.SectionID]; assert(RE.IsPCRel); assert(RE.Size == 2); Value.Offset -= RE.Addend; RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); uint8_t *Addr; if (i != Stubs.end()) { - Addr = Section.getAddressWithOffset(i->second); + Addr = Section->getAddressWithOffset(i->second); } else { - Stubs[Value] = Section.getStubOffset(); - uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset()); - RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(), + Stubs[Value] = Section->getStubOffset(); + uint8_t *GOTEntry = + Section->getAddressWithOffset(Section->getStubOffset()); + RelocationEntry GOTRE(RE.SectionID, Section->getStubOffset(), MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false, 3); if (Value.SymbolName) addRelocationForSymbol(GOTRE, Value.SymbolName); else addRelocationForSection(GOTRE, Value.SectionID); - Section.advanceStubOffset(8); + Section->advanceStubOffset(8); Addr = GOTEntry; } RelocationEntry TargetRE(RE.SectionID, RE.Offset, @@ -165,7 +166,7 @@ unsigned Size = Obj.getAnyRelocationLength(RE); uint64_t Offset = RelI->getOffset(); - uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset); + uint8_t *LocalAddress = Sections[SectionID]->getAddressWithOffset(Offset); unsigned NumBytes = 1 << Size; int64_t Addend = SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);