2014-07-18 02:54:50 +08:00
|
|
|
//===----- RuntimeDyldMachOARM.h ---- MachO/ARM specific code. ----*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
|
|
|
|
#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOARM_H
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
#include "../RuntimeDyldMachO.h"
|
2016-04-28 04:54:49 +08:00
|
|
|
#include <string>
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "dyld"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class RuntimeDyldMachOARM
|
|
|
|
: public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> {
|
2014-07-30 11:35:05 +08:00
|
|
|
private:
|
|
|
|
typedef RuntimeDyldMachOCRTPBase<RuntimeDyldMachOARM> ParentT;
|
|
|
|
|
2014-07-18 02:54:50 +08:00
|
|
|
public:
|
2014-09-04 12:53:03 +08:00
|
|
|
|
|
|
|
typedef uint32_t TargetPtrT;
|
|
|
|
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
RuntimeDyldMachOARM(RuntimeDyld::MemoryManager &MM,
|
2016-08-02 04:49:11 +08:00
|
|
|
JITSymbolResolver &Resolver)
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 11:37:06 +08:00
|
|
|
: RuntimeDyldMachOCRTPBase(MM, Resolver) {}
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
unsigned getMaxStubSize() override { return 8; }
|
|
|
|
|
2014-07-18 07:11:30 +08:00
|
|
|
unsigned getStubAlignment() override { return 4; }
|
2014-07-18 02:54:50 +08:00
|
|
|
|
2014-08-09 07:12:22 +08:00
|
|
|
int64_t decodeAddend(const RelocationEntry &RE) const {
|
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
|
2014-08-09 07:12:22 +08:00
|
|
|
|
|
|
|
switch (RE.RelType) {
|
2014-07-30 11:35:05 +08:00
|
|
|
default:
|
2014-08-09 07:12:22 +08:00
|
|
|
return memcpyAddend(RE);
|
2014-07-30 11:35:05 +08:00
|
|
|
case MachO::ARM_RELOC_BR24: {
|
2014-11-06 17:53:05 +08:00
|
|
|
uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
|
2014-07-30 11:35:05 +08:00
|
|
|
Temp &= 0x00ffffff; // Mask out the opcode.
|
|
|
|
// Now we've got the shifted immediate, shift by 2, sign extend and ret.
|
|
|
|
return SignExtend32<26>(Temp << 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
Expected<relocation_iterator>
|
2014-07-18 02:54:50 +08:00
|
|
|
processRelocationRef(unsigned SectionID, relocation_iterator RelI,
|
2014-11-27 00:54:40 +08:00
|
|
|
const ObjectFile &BaseObjT,
|
|
|
|
ObjSectionToIDMap &ObjSectionToID,
|
2014-11-27 13:40:13 +08:00
|
|
|
StubMap &Stubs) override {
|
2014-07-18 02:54:50 +08:00
|
|
|
const MachOObjectFile &Obj =
|
2014-11-27 00:54:40 +08:00
|
|
|
static_cast<const MachOObjectFile &>(BaseObjT);
|
2014-07-18 02:54:50 +08:00
|
|
|
MachO::any_relocation_info RelInfo =
|
|
|
|
Obj.getRelocation(RelI->getRawDataRefImpl());
|
2014-09-12 03:21:14 +08:00
|
|
|
uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
|
2014-07-18 02:54:50 +08:00
|
|
|
|
2014-09-12 03:21:14 +08:00
|
|
|
if (Obj.isRelocationScattered(RelInfo)) {
|
|
|
|
if (RelType == MachO::ARM_RELOC_HALF_SECTDIFF)
|
2014-11-27 00:54:40 +08:00
|
|
|
return processHALFSECTDIFFRelocation(SectionID, RelI, Obj,
|
2014-09-12 03:21:14 +08:00
|
|
|
ObjSectionToID);
|
2015-07-25 01:40:04 +08:00
|
|
|
else if (RelType == MachO::GENERIC_RELOC_VANILLA)
|
|
|
|
return processScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
|
2014-09-12 03:21:14 +08:00
|
|
|
else
|
2015-07-25 01:40:04 +08:00
|
|
|
return ++RelI;
|
2014-09-12 03:21:14 +08:00
|
|
|
}
|
2014-07-18 02:54:50 +08:00
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
// Sanity check relocation type.
|
|
|
|
switch (RelType) {
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PAIR);
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_SECTDIFF);
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_LOCAL_SECTDIFF);
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_PB_LA_PTR);
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_RELOC_BR22);
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_THUMB_32BIT_BRANCH);
|
|
|
|
UNIMPLEMENTED_RELOC(MachO::ARM_RELOC_HALF);
|
|
|
|
default:
|
|
|
|
if (RelType > MachO::ARM_RELOC_HALF_SECTDIFF)
|
2016-04-28 08:49:37 +08:00
|
|
|
return make_error<RuntimeDyldError>(("MachO ARM relocation type " +
|
|
|
|
Twine(RelType) +
|
|
|
|
" is out of range").str());
|
2016-04-28 04:24:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
|
2014-08-09 07:12:22 +08:00
|
|
|
RE.Addend = decodeAddend(RE);
|
2016-04-28 04:24:48 +08:00
|
|
|
RelocationValueRef Value;
|
|
|
|
if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
|
|
|
|
Value = *ValueOrErr;
|
|
|
|
else
|
|
|
|
return ValueOrErr.takeError();
|
2014-07-18 02:54:50 +08:00
|
|
|
|
2014-07-30 11:35:05 +08:00
|
|
|
if (RE.IsPCRel)
|
Remove getRelocationAddress.
Originally added in r139314.
Back then it didn't actually get the address, it got whatever value the
relocation used: address or offset.
The values in different object formats are:
* MachO: Always an offset.
* COFF: Always an address, but when talking about the virtual address of
sections it says: "for simplicity, compilers should set this to zero".
* ELF: An offset for .o files and and address for .so files. In the case of the
.so, the relocation in not linked to any section (sh_info is 0). We can't
really compute an offset.
Some API mappings would be:
* Use getAddress for everything. It would be quite cumbersome. To compute the
address elf has to follow sh_info, which can be corrupted and therefore the
method has to return an ErrorOr. The address of the section is also the same
for every relocation in a section, so we shouldn't have to check the error
and fetch the value for every relocation.
* Use a getValue and make it up to the user to know what it is getting.
* Use a getOffset and:
* Assert for dynamic ELF objects. That is a very peculiar case and it is
probably fair to ask any tool that wants to support it to use ELF.h. The
only tool we have that reads those (llvm-readobj) already does that. The
only other use case I can think of is a dynamic linker.
* Check that COFF .obj files have sections with zero virtual address spaces. If
it turns out that some assembler/compiler produces these, we can change
COFFObjectFile::getRelocationOffset to subtract it. Given COFF format,
this can be done without the need for ErrorOr.
The getRelocationAddress method was never implemented for COFF. It also
had exactly one use in a very peculiar case: a shortcut for adding the
section value to a pcrel reloc on MachO.
Given that, I don't expect that there is any use out there of the C API. If
that is not the case, let me know and I will add it back with the implementation
inlined and do a proper deprecation.
llvm-svn: 241450
2015-07-06 22:55:37 +08:00
|
|
|
makeValueAddendPCRel(Value, RelI, 8);
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
if ((RE.RelType & 0xf) == MachO::ARM_RELOC_BR24)
|
|
|
|
processBranchRelocation(RE, Value, Stubs);
|
|
|
|
else {
|
2014-09-07 12:03:32 +08:00
|
|
|
RE.Addend = Value.Offset;
|
2014-07-18 02:54:50 +08:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(RE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(RE, Value.SectionID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ++RelI;
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:41:21 +08:00
|
|
|
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
|
2014-07-18 02:54:50 +08:00
|
|
|
DEBUG(dumpRelocationToResolve(RE, Value));
|
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
// If the relocation is PC-relative, the value to be encoded is the
|
|
|
|
// pointer difference.
|
|
|
|
if (RE.IsPCRel) {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
|
2014-07-18 02:54:50 +08:00
|
|
|
Value -= FinalAddress;
|
|
|
|
// ARM PCRel relocations have an effective-PC offset of two instructions
|
|
|
|
// (four bytes in Thumb mode, 8 bytes in ARM mode).
|
|
|
|
// FIXME: For now, assume ARM mode.
|
|
|
|
Value -= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (RE.RelType) {
|
|
|
|
case MachO::ARM_RELOC_VANILLA:
|
2014-09-12 01:27:01 +08:00
|
|
|
writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
|
2014-07-18 02:54:50 +08:00
|
|
|
break;
|
|
|
|
case MachO::ARM_RELOC_BR24: {
|
|
|
|
// Mask the value into the target address. We know instructions are
|
|
|
|
// 32-bit aligned, so we can do it all at once.
|
2014-09-12 01:27:01 +08:00
|
|
|
Value += RE.Addend;
|
2014-07-18 02:54:50 +08:00
|
|
|
// The low two bits of the value are not encoded.
|
|
|
|
Value >>= 2;
|
|
|
|
// Mask the value to 24 bits.
|
|
|
|
uint64_t FinalValue = Value & 0xffffff;
|
|
|
|
// FIXME: If the destination is a Thumb function (and the instruction
|
|
|
|
// is a non-predicated BL instruction), we need to change it to a BLX
|
|
|
|
// instruction instead.
|
|
|
|
|
|
|
|
// Insert the value into the instruction.
|
2014-11-06 17:53:05 +08:00
|
|
|
uint32_t Temp = readBytesUnaligned(LocalAddress, 4);
|
|
|
|
writeBytesUnaligned((Temp & ~0xffffff) | FinalValue, LocalAddress, 4);
|
|
|
|
|
2014-07-18 02:54:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-09-12 03:21:14 +08:00
|
|
|
case MachO::ARM_RELOC_HALF_SECTDIFF: {
|
2015-11-24 05:47:41 +08:00
|
|
|
uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
|
|
|
|
uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
|
2014-09-12 03:21:14 +08:00
|
|
|
assert((Value == SectionABase || Value == SectionBBase) &&
|
|
|
|
"Unexpected HALFSECTDIFF relocation value.");
|
|
|
|
Value = SectionABase - SectionBBase + RE.Addend;
|
|
|
|
if (RE.Size & 0x1) // :upper16:
|
|
|
|
Value = (Value >> 16);
|
|
|
|
Value &= 0xffff;
|
|
|
|
|
2014-11-06 17:53:05 +08:00
|
|
|
uint32_t Insn = readBytesUnaligned(LocalAddress, 4);
|
2014-09-12 03:21:14 +08:00
|
|
|
Insn = (Insn & 0xfff0f000) | ((Value & 0xf000) << 4) | (Value & 0x0fff);
|
2014-11-06 17:53:05 +08:00
|
|
|
writeBytesUnaligned(Insn, LocalAddress, 4);
|
2014-09-12 03:21:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid relocation type");
|
2014-07-18 02:54:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
|
2014-09-12 03:21:14 +08:00
|
|
|
const SectionRef &Section) {
|
|
|
|
StringRef Name;
|
|
|
|
Section.getName(Name);
|
|
|
|
|
|
|
|
if (Name == "__nl_symbol_ptr")
|
2016-04-28 04:24:48 +08:00
|
|
|
return populateIndirectSymbolPointersSection(cast<MachOObjectFile>(Obj),
|
|
|
|
Section, SectionID);
|
|
|
|
return Error::success();
|
2014-09-12 03:21:14 +08:00
|
|
|
}
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
private:
|
2014-09-12 03:21:14 +08:00
|
|
|
|
2014-07-18 02:54:50 +08:00
|
|
|
void processBranchRelocation(const RelocationEntry &RE,
|
|
|
|
const RelocationValueRef &Value,
|
|
|
|
StubMap &Stubs) {
|
|
|
|
// This is an ARM branch relocation, need to use a stub function.
|
|
|
|
// Look up for existing stub.
|
|
|
|
SectionEntry &Section = Sections[RE.SectionID];
|
|
|
|
RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
|
|
|
|
uint8_t *Addr;
|
|
|
|
if (i != Stubs.end()) {
|
2015-11-24 05:47:41 +08:00
|
|
|
Addr = Section.getAddressWithOffset(i->second);
|
2014-07-18 02:54:50 +08:00
|
|
|
} else {
|
|
|
|
// Create a new stub function.
|
2015-11-24 05:47:41 +08:00
|
|
|
Stubs[Value] = Section.getStubOffset();
|
|
|
|
uint8_t *StubTargetAddr = createStubFunction(
|
|
|
|
Section.getAddressWithOffset(Section.getStubOffset()));
|
|
|
|
RelocationEntry StubRE(
|
|
|
|
RE.SectionID, StubTargetAddr - Section.getAddress(),
|
|
|
|
MachO::GENERIC_RELOC_VANILLA, Value.Offset, false, 2);
|
2014-07-18 02:54:50 +08:00
|
|
|
if (Value.SymbolName)
|
|
|
|
addRelocationForSymbol(StubRE, Value.SymbolName);
|
|
|
|
else
|
|
|
|
addRelocationForSection(StubRE, Value.SectionID);
|
2015-11-24 05:47:41 +08:00
|
|
|
Addr = Section.getAddressWithOffset(Section.getStubOffset());
|
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
2014-07-18 02:54:50 +08:00
|
|
|
}
|
2014-07-30 11:35:05 +08:00
|
|
|
RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, 0,
|
2014-07-18 02:54:50 +08:00
|
|
|
RE.IsPCRel, RE.Size);
|
|
|
|
resolveRelocation(TargetRE, (uint64_t)Addr);
|
|
|
|
}
|
2014-09-12 03:21:14 +08:00
|
|
|
|
2016-04-28 04:24:48 +08:00
|
|
|
Expected<relocation_iterator>
|
2014-09-12 03:21:14 +08:00
|
|
|
processHALFSECTDIFFRelocation(unsigned SectionID, relocation_iterator RelI,
|
2014-11-27 00:54:40 +08:00
|
|
|
const ObjectFile &BaseTObj,
|
2014-09-12 03:21:14 +08:00
|
|
|
ObjSectionToIDMap &ObjSectionToID) {
|
2014-11-27 00:54:40 +08:00
|
|
|
const MachOObjectFile &MachO =
|
|
|
|
static_cast<const MachOObjectFile&>(BaseTObj);
|
2014-09-12 03:21:14 +08:00
|
|
|
MachO::any_relocation_info RE =
|
2014-11-27 00:54:40 +08:00
|
|
|
MachO.getRelocation(RelI->getRawDataRefImpl());
|
2014-09-12 03:21:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
// For a half-diff relocation the length bits actually record whether this
|
|
|
|
// is a movw/movt, and whether this is arm or thumb.
|
|
|
|
// Bit 0 indicates movw (b0 == 0) or movt (b0 == 1).
|
|
|
|
// Bit 1 indicates arm (b1 == 0) or thumb (b1 == 1).
|
2014-11-27 00:54:40 +08:00
|
|
|
unsigned HalfDiffKindBits = MachO.getAnyRelocationLength(RE);
|
2014-09-12 03:21:14 +08:00
|
|
|
if (HalfDiffKindBits & 0x2)
|
|
|
|
llvm_unreachable("Thumb not yet supported.");
|
|
|
|
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
2014-11-27 00:54:40 +08:00
|
|
|
uint32_t RelocType = MachO.getAnyRelocationType(RE);
|
|
|
|
bool IsPCRel = MachO.getAnyRelocationPCRel(RE);
|
2015-06-30 07:29:12 +08:00
|
|
|
uint64_t Offset = RelI->getOffset();
|
2015-11-24 05:47:41 +08:00
|
|
|
uint8_t *LocalAddress = Section.getAddressWithOffset(Offset);
|
2014-11-06 17:53:05 +08:00
|
|
|
int64_t Immediate = readBytesUnaligned(LocalAddress, 4); // Copy the whole instruction out.
|
2014-09-12 03:21:14 +08:00
|
|
|
Immediate = ((Immediate >> 4) & 0xf000) | (Immediate & 0xfff);
|
|
|
|
|
|
|
|
++RelI;
|
|
|
|
MachO::any_relocation_info RE2 =
|
2014-11-27 00:54:40 +08:00
|
|
|
MachO.getRelocation(RelI->getRawDataRefImpl());
|
|
|
|
uint32_t AddrA = MachO.getScatteredRelocationValue(RE);
|
|
|
|
section_iterator SAI = getSectionByAddress(MachO, AddrA);
|
|
|
|
assert(SAI != MachO.section_end() && "Can't find section for address A");
|
2014-10-08 23:28:58 +08:00
|
|
|
uint64_t SectionABase = SAI->getAddress();
|
2014-09-12 03:21:14 +08:00
|
|
|
uint64_t SectionAOffset = AddrA - SectionABase;
|
|
|
|
SectionRef SectionA = *SAI;
|
2014-10-08 23:28:58 +08:00
|
|
|
bool IsCode = SectionA.isText();
|
2016-04-28 04:24:48 +08:00
|
|
|
uint32_t SectionAID = ~0U;
|
|
|
|
if (auto SectionAIDOrErr =
|
|
|
|
findOrEmitSection(MachO, SectionA, IsCode, ObjSectionToID))
|
|
|
|
SectionAID = *SectionAIDOrErr;
|
|
|
|
else
|
|
|
|
return SectionAIDOrErr.takeError();
|
2014-09-12 03:21:14 +08:00
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
uint32_t AddrB = MachO.getScatteredRelocationValue(RE2);
|
|
|
|
section_iterator SBI = getSectionByAddress(MachO, AddrB);
|
|
|
|
assert(SBI != MachO.section_end() && "Can't find section for address B");
|
2014-10-08 23:28:58 +08:00
|
|
|
uint64_t SectionBBase = SBI->getAddress();
|
2014-09-12 03:21:14 +08:00
|
|
|
uint64_t SectionBOffset = AddrB - SectionBBase;
|
|
|
|
SectionRef SectionB = *SBI;
|
2016-04-28 04:24:48 +08:00
|
|
|
uint32_t SectionBID = ~0U;
|
|
|
|
if (auto SectionBIDOrErr =
|
|
|
|
findOrEmitSection(MachO, SectionB, IsCode, ObjSectionToID))
|
|
|
|
SectionBID = *SectionBIDOrErr;
|
|
|
|
else
|
|
|
|
return SectionBIDOrErr.takeError();
|
2014-09-12 03:21:14 +08:00
|
|
|
|
2014-11-27 00:54:40 +08:00
|
|
|
uint32_t OtherHalf = MachO.getAnyRelocationAddress(RE2) & 0xffff;
|
2014-09-12 03:21:14 +08:00
|
|
|
unsigned Shift = (HalfDiffKindBits & 0x1) ? 16 : 0;
|
|
|
|
uint32_t FullImmVal = (Immediate << Shift) | (OtherHalf << (16 - Shift));
|
|
|
|
int64_t Addend = FullImmVal - (AddrA - AddrB);
|
|
|
|
|
|
|
|
// addend = Encoded - Expected
|
|
|
|
// = Encoded - (AddrA - AddrB)
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
|
|
|
|
<< ", Addend: " << Addend << ", SectionA ID: " << SectionAID
|
|
|
|
<< ", SectionAOffset: " << SectionAOffset
|
|
|
|
<< ", SectionB ID: " << SectionBID
|
|
|
|
<< ", SectionBOffset: " << SectionBOffset << "\n");
|
|
|
|
RelocationEntry R(SectionID, Offset, RelocType, Addend, SectionAID,
|
|
|
|
SectionAOffset, SectionBID, SectionBOffset, IsPCRel,
|
|
|
|
HalfDiffKindBits);
|
|
|
|
|
|
|
|
addRelocationForSection(R, SectionAID);
|
|
|
|
addRelocationForSection(R, SectionBID);
|
|
|
|
|
|
|
|
return ++RelI;
|
|
|
|
}
|
|
|
|
|
2014-07-18 02:54:50 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-07-18 02:54:50 +08:00
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#endif
|