[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
This commit is contained in:
Lang Hames 2014-08-08 23:12:22 +00:00
parent 72260fe2b4
commit e5885cab3e
6 changed files with 49 additions and 44 deletions

View File

@ -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;

View File

@ -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<const MachOObjectFile &>(*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<MachO::RelocationInfoType>(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<Impl &>(*this); }
const Impl &impl() const { return static_cast<const Impl &>(*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<const MachOObjectFile &>(*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<MachO::RelocationInfoType>(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) {}

View File

@ -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));

View File

@ -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));

View File

@ -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));

View File

@ -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));