From e5885cab3e4780bef742e7226462fe5b2dc4f5ae Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Fri, 8 Aug 2014 23:12:22 +0000 Subject: [PATCH] [MCJIT] Simplify immediate decoding code in the RuntimeDyldMachO hierarchy. Cleanup only: no functional change. This patch makes RuntimeDyldMachO targets directly responsible for decoding immediates, rather than letting them implement catch a callback from generic code. Since this is a very target specific operation, it makes sense to let the target-specific code drive it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215255 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../RuntimeDyld/RuntimeDyldMachO.cpp | 6 +- .../RuntimeDyld/RuntimeDyldMachO.h | 55 +++++++++---------- .../Targets/RuntimeDyldMachOAArch64.h | 13 +++-- .../RuntimeDyld/Targets/RuntimeDyldMachOARM.h | 13 +++-- .../Targets/RuntimeDyldMachOI386.h | 3 +- .../Targets/RuntimeDyldMachOX86_64.h | 3 +- 6 files changed, 49 insertions(+), 44 deletions(-) diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp index bae2471054d..986daef8a90 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.cpp @@ -27,8 +27,10 @@ using namespace llvm::object; namespace llvm { -int64_t RuntimeDyldMachO::decodeAddend(uint8_t *LocalAddress, unsigned NumBytes, - MachO::RelocationInfoType) const { +int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const { + const SectionEntry &Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section.Address + RE.Offset; + unsigned NumBytes = 1 << RE.Size; int64_t Addend = 0; memcpy(&Addend, LocalAddress, NumBytes); return Addend; diff --git a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h index 30f61e98e32..b18d167fab4 100644 --- a/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h +++ b/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldMachO.h @@ -52,9 +52,31 @@ protected: RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} - /// Extract the addend encoded in the instruction. - int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes, - MachO::RelocationInfoType RelType) const; + /// This convenience method uses memcpy to extract a contiguous addend (the + /// addend size and offset are taken from the corresponding fields of the RE). + int64_t memcpyAddend(const RelocationEntry &RE) const; + + /// Given a relocation_iterator for a non-scattered relocation, construct a + /// RelocationEntry and fill in the common fields. The 'Addend' field is *not* + /// filled in, since immediate encodings are highly target/opcode specific. + /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the + /// memcpyAddend method can be used to read the immediate. + RelocationEntry getRelocationEntry(unsigned SectionID, ObjectImage &ObjImg, + const relocation_iterator &RI) const { + const MachOObjectFile &Obj = + static_cast(*ObjImg.getObjectFile()); + MachO::any_relocation_info RelInfo = + Obj.getRelocation(RI->getRawDataRefImpl()); + + bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo); + unsigned Size = Obj.getAnyRelocationLength(RelInfo); + uint64_t Offset; + RI->getOffset(Offset); + MachO::RelocationInfoType RelType = + static_cast(Obj.getAnyRelocationType(RelInfo)); + + return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size); + } /// Construct a RelocationValueRef representing the relocation target. /// For Symbols in known sections, this will return a RelocationValueRef @@ -119,33 +141,6 @@ private: Impl &impl() { return static_cast(*this); } const Impl &impl() const { return static_cast(*this); } -protected: - - /// Parse the given relocation, which must be a non-scattered, and - /// return a RelocationEntry representing the information. The 'Addend' field - /// will contain the unmodified instruction immediate. - RelocationEntry getBasicRelocationEntry(unsigned SectionID, - ObjectImage &ObjImg, - const relocation_iterator &RI) const { - const MachOObjectFile &Obj = - static_cast(*ObjImg.getObjectFile()); - MachO::any_relocation_info RelInfo = - Obj.getRelocation(RI->getRawDataRefImpl()); - - const SectionEntry &Section = Sections[SectionID]; - bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo); - unsigned Size = Obj.getAnyRelocationLength(RelInfo); - uint64_t Offset; - RI->getOffset(Offset); - uint8_t *LocalAddress = Section.Address + Offset; - unsigned NumBytes = 1 << Size; - MachO::RelocationInfoType RelType = - static_cast(Obj.getAnyRelocationType(RelInfo)); - int64_t Addend = impl().decodeAddend(LocalAddress, NumBytes, RelType); - - return RelocationEntry(SectionID, Offset, RelType, Addend, IsPCRel, Size); - } - public: RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {} diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h index ffdb53a6b18..e7e8fa580ff 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOAArch64.h @@ -28,11 +28,13 @@ public: unsigned getStubAlignment() override { return 8; } /// Extract the addend encoded in the instruction / memory location. - int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes, - MachO::RelocationInfoType RelType) const { + int64_t decodeAddend(const RelocationEntry &RE) const { + const SectionEntry &Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section.Address + RE.Offset; + unsigned NumBytes = 1 << RE.Size; int64_t Addend = 0; // Verify that the relocation has the correct size and alignment. - switch (RelType) { + switch (RE.RelType) { default: llvm_unreachable("Unsupported relocation type!"); case MachO::ARM64_RELOC_UNSIGNED: @@ -49,7 +51,7 @@ public: break; } - switch (RelType) { + switch (RE.RelType) { default: llvm_unreachable("Unsupported relocation type!"); case MachO::ARM64_RELOC_UNSIGNED: @@ -263,7 +265,8 @@ public: RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl()); } - RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI)); + RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI)); + RE.Addend = decodeAddend(RE); RelocationValueRef Value( getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols)); diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h index b7bd0b9d04f..f784fa6b672 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h @@ -28,11 +28,13 @@ public: unsigned getStubAlignment() override { return 4; } - int64_t decodeAddend(uint8_t *LocalAddress, unsigned NumBytes, - MachO::RelocationInfoType RelType) const { - switch (RelType) { + int64_t decodeAddend(const RelocationEntry &RE) const { + const SectionEntry &Section = Sections[RE.SectionID]; + uint8_t *LocalAddress = Section.Address + RE.Offset; + + switch (RE.RelType) { default: - return ParentT::decodeAddend(LocalAddress, NumBytes, RelType); + return memcpyAddend(RE); case MachO::ARM_RELOC_BR24: { uint32_t Temp; memcpy(&Temp, LocalAddress, 4); @@ -55,7 +57,8 @@ public: if (Obj.isRelocationScattered(RelInfo)) return ++++RelI; - RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI)); + RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI)); + RE.Addend = decodeAddend(RE); RelocationValueRef Value( getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols)); diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h index 156287b3af1..e7389776e25 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOI386.h @@ -47,7 +47,8 @@ public: llvm_unreachable("Unhandled scattered relocation."); } - RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI)); + RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI)); + RE.Addend = memcpyAddend(RE); RelocationValueRef Value( getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols)); diff --git a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h index 9106f415422..4a6e76b5d15 100644 --- a/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h +++ b/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h @@ -38,7 +38,8 @@ public: assert(!Obj.isRelocationScattered(RelInfo) && "Scattered relocations not supported on X86_64"); - RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI)); + RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI)); + RE.Addend = memcpyAddend(RE); RelocationValueRef Value( getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));