10 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H
13 #include "../RuntimeDyldMachO.h"
16 #define DEBUG_TYPE "dyld"
38 unsigned NumBytes = 1 << RE.
Size;
45 assert((NumBytes == 4 || NumBytes == 8) &&
"Invalid relocation size.");
52 assert(NumBytes == 4 &&
"Invalid relocation size.");
53 assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
54 "Instruction address is not aligned to 4 bytes.");
71 assert((*p & 0xFC000000) == 0x14000000 &&
"Expected branch instruction.");
76 Addend = (*p & 0x03FFFFFF) << 2;
84 assert((*p & 0x9F000000) == 0x90000000 &&
"Expected adrp instruction.");
89 Addend = ((*p & 0x60000000) >> 29) | ((*p & 0x01FFFFE0) >> 3) << 12;
98 assert((*p & 0x3B000000) == 0x39000000 &&
99 "Only expected load / store instructions.");
106 assert((((*p & 0x3B000000) == 0x39000000) ||
107 ((*p & 0x11C00000) == 0x11000000) ) &&
108 "Expected load / store or add/sub instruction.");
111 Addend = (*p & 0x003FFC00) >> 10;
115 int ImplicitShift = 0;
116 if ((*p & 0x3B000000) == 0x39000000) {
118 ImplicitShift = ((*p >> 30) & 0x3);
119 if (ImplicitShift == 0) {
121 if ((*p & 0x04800000) == 0x04800000)
126 Addend <<= ImplicitShift;
141 assert((NumBytes == 4 || NumBytes == 8) &&
"Invalid relocation size.");
148 assert(NumBytes == 4 &&
"Invalid relocation size.");
149 assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
150 "Instruction address is not aligned to 4 bytes.");
167 assert((*p & 0xFC000000) == 0x14000000 &&
"Expected branch instruction.");
170 assert((Addend & 0x3) == 0 &&
"Branch target is not aligned");
171 assert(isInt<28>(Addend) &&
"Branch target is out of range.");
174 *p = (*p & 0xFC000000) | ((
uint32_t)(Addend >> 2) & 0x03FFFFFF);
181 assert((*p & 0x9F000000) == 0x90000000 &&
"Expected adrp instruction.");
184 assert((Addend & 0xFFF) == 0 &&
"ADRP target is not page aligned.");
185 assert(isInt<33>(Addend) &&
"Invalid page reloc value.");
188 uint32_t ImmLoValue = ((uint64_t)Addend << 17) & 0x60000000;
189 uint32_t ImmHiValue = ((uint64_t)Addend >> 9) & 0x00FFFFE0;
190 *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue;
197 assert((*p & 0x3B000000) == 0x39000000 &&
198 "Only expected load / store instructions.");
206 assert((((*p & 0x3B000000) == 0x39000000) ||
207 ((*p & 0x11C00000) == 0x11000000) ) &&
208 "Expected load / store or add/sub instruction.");
212 int ImplicitShift = 0;
213 if ((*p & 0x3B000000) == 0x39000000) {
215 ImplicitShift = ((*p >> 30) & 0x3);
216 switch (ImplicitShift) {
219 if ((*p & 0x04800000) == 0x04800000) {
221 assert(((Addend & 0xF) == 0) &&
222 "128-bit LDR/STR not 16-byte aligned.");
226 assert(((Addend & 0x1) == 0) &&
"16-bit LDR/STR not 2-byte aligned.");
229 assert(((Addend & 0x3) == 0) &&
"32-bit LDR/STR not 4-byte aligned.");
232 assert(((Addend & 0x7) == 0) &&
"64-bit LDR/STR not 8-byte aligned.");
237 Addend >>= ImplicitShift;
238 assert(isUInt<12>(Addend) &&
"Addend cannot be encoded.");
241 *p = (*p & 0xFFC003FF) | ((
uint32_t)(Addend << 10) & 0x003FFC00);
258 return make_error<RuntimeDyldError>(
"Scattered relocations not supported "
259 "for MachO AArch64");
265 int64_t ExplicitAddend = 0;
278 return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
283 assert((ExplicitAddend == 0 || RE.
Addend == 0) &&
"Relocation has "\
284 "ARM64_RELOC_ADDEND and embedded addend in the instruction.");
286 RE.
Addend = ExplicitAddend;
292 return ValueOrErr.takeError();
302 processGOTRelocation(RE, Value, Stubs);
325 assert(!RE.
IsPCRel &&
"PCRel and ARM64_RELOC_UNSIGNED not supported");
335 assert(RE.
IsPCRel &&
"not PCRel and ARM64_RELOC_BRANCH26 not supported");
338 int64_t PCRelVal = Value - FinalAddress + RE.
Addend;
344 assert(RE.
IsPCRel &&
"not PCRel and ARM64_RELOC_PAGE21 not supported");
348 ((Value + RE.
Addend) & (-4096)) - (FinalAddress & (-4096));
354 assert(!RE.
IsPCRel &&
"PCRel and ARM64_RELOC_PAGEOFF21 not supported");
365 assert((Value == SectionABase || Value == SectionBBase) &&
366 "Unexpected SUBTRACTOR relocation value.");
367 Value = SectionABase - SectionBBase + RE.
Addend;
377 "processRelocationRef!");
391 StubMap::const_iterator
i = Stubs.find(Value);
393 if (i != Stubs.end())
394 Offset = static_cast<int64_t>(i->second);
398 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
400 uintptr_t StubAddress =
401 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
403 unsigned StubOffset = StubAddress - BaseAddress;
404 Stubs[Value] = StubOffset;
406 "GOT entry not aligned");
415 Offset =
static_cast<int64_t
>(StubOffset);
432 uint64_t Offset = RelI->getOffset();
433 uint8_t *LocalAddress =
Sections[SectionID].getAddressWithOffset(Offset);
434 unsigned NumBytes = 1 << Size;
437 if (!SubtrahendNameOrErr)
440 unsigned SectionBID = SubtrahendI->second.getSectionID();
441 uint64_t SectionBOffset = SubtrahendI->second.getOffset();
447 if (!MinuendNameOrErr)
450 unsigned SectionAID = MinuendI->second.getSectionID();
451 uint64_t SectionAOffset = MinuendI->second.getOffset();
454 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
Expected< relocation_iterator > processRelocationRef(unsigned SectionID, relocation_iterator RelI, const ObjectFile &BaseObjT, ObjSectionToIDMap &ObjSectionToID, StubMap &Stubs) override
Parses one or more object file relocations (some object files use relocation pairs) and stores it to ...
uint8_t * getAddress() const
iterator find(StringRef Key)
This class is the base class for all object file types.
Error takeError()
Take ownership of the stored error.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes, MachO::RelocationInfoType RelType, int64_t Addend) const
Extract the addend encoded in the instruction.
RelocationEntry getRelocationEntry(unsigned SectionID, const ObjectFile &BaseTObj, const relocation_iterator &RI) const
Given a relocation_iterator for a non-scattered relocation, construct a RelocationEntry and fill in t...
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override
A object file specific relocation resolver.
int64_t decodeAddend(const RelocationEntry &RE) const
Extract the addend encoded in the instruction / memory location.
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
unsigned getMaxStubSize() override
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
RuntimeDyldMachOTarget - Templated base class for generic MachO linker algorithms and data structures...
Expected< RelocationValueRef > getRelocationValueRef(const ObjectFile &BaseTObj, const relocation_iterator &RI, const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID)
Construct a RelocationValueRef representing the relocation target.
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
uintptr_t getStubOffset() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static ErrorSuccess success()
Create a success value.
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
RuntimeDyldMachOAArch64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
JITSymbolResolver & Resolver
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
SectionEntry - represents a section emitted into memory by the dynamic linker.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned getStubAlignment() override
LLVM Value Representation.
RTDyldSymbolTable GlobalSymbolTable
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Lightweight error class with error context and mandatory checking.
unsigned Size
The size of this relocation (MachO specific).
void advanceStubOffset(unsigned StubSize)
void makeValueAddendPCRel(RelocationValueRef &Value, const relocation_iterator &RI, unsigned OffsetToNextPC)
Make the RelocationValueRef addend PC-relative.
unsigned getPlainRelocationSymbolNum(const MachO::any_relocation_info &RE) const
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
This is a value type class that represents a single section in the list of sections in the object fil...