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;
46 ErrStream <<
"Unsupported relocation type: " 49 return make_error<StringError>(std::move(ErrMsg),
54 if (NumBytes != 4 && NumBytes != 8) {
58 ErrStream <<
"Invalid relocation size for relocation " 61 return make_error<StringError>(std::move(ErrMsg),
71 assert(NumBytes == 4 &&
"Invalid relocation size.");
72 assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
73 "Instruction address is not aligned to 4 bytes.");
91 assert(((*p & 0xFC000000) == 0x14000000 ||
92 (*p & 0xFC000000) == 0x94000000) &&
93 "Expected branch instruction.");
98 Addend = (*p & 0x03FFFFFF) << 2;
106 assert((*p & 0x9F000000) == 0x90000000 &&
"Expected adrp instruction.");
111 Addend = ((*p & 0x60000000) >> 29) | ((*p & 0x01FFFFE0) >> 3) << 12;
120 assert((*p & 0x3B000000) == 0x39000000 &&
121 "Only expected load / store instructions.");
128 assert((((*p & 0x3B000000) == 0x39000000) ||
129 ((*p & 0x11C00000) == 0x11000000) ) &&
130 "Expected load / store or add/sub instruction.");
133 Addend = (*p & 0x003FFC00) >> 10;
137 int ImplicitShift = 0;
138 if ((*p & 0x3B000000) == 0x39000000) {
140 ImplicitShift = ((*p >> 30) & 0x3);
141 if (ImplicitShift == 0) {
143 if ((*p & 0x04800000) == 0x04800000)
148 Addend <<= ImplicitShift;
164 assert((NumBytes == 4 || NumBytes == 8) &&
"Invalid relocation size.");
171 assert(NumBytes == 4 &&
"Invalid relocation size.");
172 assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
173 "Instruction address is not aligned to 4 bytes.");
191 assert(((*p & 0xFC000000) == 0x14000000 ||
192 (*p & 0xFC000000) == 0x94000000) &&
193 "Expected branch instruction.");
196 assert((Addend & 0x3) == 0 &&
"Branch target is not aligned");
197 assert(isInt<28>(Addend) &&
"Branch target is out of range.");
200 *p = (*p & 0xFC000000) | ((
uint32_t)(Addend >> 2) & 0x03FFFFFF);
207 assert((*p & 0x9F000000) == 0x90000000 &&
"Expected adrp instruction.");
210 assert((Addend & 0xFFF) == 0 &&
"ADRP target is not page aligned.");
211 assert(isInt<33>(Addend) &&
"Invalid page reloc value.");
214 uint32_t ImmLoValue = ((uint64_t)Addend << 17) & 0x60000000;
215 uint32_t ImmHiValue = ((uint64_t)Addend >> 9) & 0x00FFFFE0;
216 *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue;
223 assert((*p & 0x3B000000) == 0x39000000 &&
224 "Only expected load / store instructions.");
232 assert((((*p & 0x3B000000) == 0x39000000) ||
233 ((*p & 0x11C00000) == 0x11000000) ) &&
234 "Expected load / store or add/sub instruction.");
238 int ImplicitShift = 0;
239 if ((*p & 0x3B000000) == 0x39000000) {
241 ImplicitShift = ((*p >> 30) & 0x3);
242 switch (ImplicitShift) {
245 if ((*p & 0x04800000) == 0x04800000) {
247 assert(((Addend & 0xF) == 0) &&
248 "128-bit LDR/STR not 16-byte aligned.");
252 assert(((Addend & 0x1) == 0) &&
"16-bit LDR/STR not 2-byte aligned.");
255 assert(((Addend & 0x3) == 0) &&
"32-bit LDR/STR not 4-byte aligned.");
258 assert(((Addend & 0x7) == 0) &&
"64-bit LDR/STR not 8-byte aligned.");
263 Addend >>= ImplicitShift;
264 assert(isUInt<12>(Addend) &&
"Addend cannot be encoded.");
267 *p = (*p & 0xFFC003FF) | ((
uint32_t)(Addend << 10) & 0x003FFC00);
284 return make_error<RuntimeDyldError>(
"Scattered relocations not supported " 285 "for MachO AArch64");
291 int64_t ExplicitAddend = 0;
304 return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
312 return make_error<StringError>(
"ARM64_RELOC_POINTER_TO_GOT supports " 313 "32-bit pc-rel or 64-bit absolute only",
320 return Addend.takeError();
322 assert((ExplicitAddend == 0 || RE.
Addend == 0) &&
"Relocation has "\
323 "ARM64_RELOC_ADDEND and embedded addend in the instruction.");
325 RE.
Addend = ExplicitAddend;
331 return ValueOrErr.takeError();
337 }
else if (!IsExtern && RE.
IsPCRel)
345 processGOTRelocation(RE, Value, Stubs);
368 assert(!RE.
IsPCRel &&
"PCRel and ARM64_RELOC_UNSIGNED not supported");
380 "ARM64_RELOC_POINTER_TO_GOT only supports 32-bit pc-rel or 64-bit " 391 assert(RE.
IsPCRel &&
"not PCRel and ARM64_RELOC_BRANCH26 not supported");
394 int64_t PCRelVal = Value - FinalAddress + RE.
Addend;
400 assert(RE.
IsPCRel &&
"not PCRel and ARM64_RELOC_PAGE21 not supported");
404 ((Value + RE.
Addend) & (-4096)) - (FinalAddress & (-4096));
410 assert(!RE.
IsPCRel &&
"PCRel and ARM64_RELOC_PAGEOFF21 not supported");
421 assert((Value == SectionABase || Value == SectionBBase) &&
422 "Unexpected SUBTRACTOR relocation value.");
423 Value = SectionABase - SectionBBase + RE.
Addend;
433 "processRelocationRef!");
449 StubMap::const_iterator i = Stubs.find(Value);
451 if (i != Stubs.end())
452 Offset = static_cast<int64_t>(i->second);
456 uintptr_t BaseAddress = uintptr_t(Section.
getAddress());
458 uintptr_t StubAddress =
459 (BaseAddress + Section.
getStubOffset() + StubAlignment - 1) &
461 unsigned StubOffset = StubAddress - BaseAddress;
462 Stubs[Value] = StubOffset;
464 "GOT entry not aligned");
473 Offset =
static_cast<int64_t
>(StubOffset);
490 uint64_t
Offset = RelI->getOffset();
491 uint8_t *LocalAddress =
Sections[SectionID].getAddressWithOffset(Offset);
492 unsigned NumBytes = 1 <<
Size;
495 if (!SubtrahendNameOrErr)
498 unsigned SectionBID = SubtrahendI->second.getSectionID();
499 uint64_t SectionBOffset = SubtrahendI->second.getOffset();
505 if (!MinuendNameOrErr)
508 unsigned SectionAID = MinuendI->second.getSectionID();
509 uint64_t SectionAOffset = MinuendI->second.getOffset();
512 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
520 static const char *getRelocName(
uint32_t RelocType) {
534 return "Unrecognized arm64 addend";
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
This class represents lattice values for constants.
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
uint64_t getLoadAddressWithOffset(unsigned OffsetBytes) const
Return the load address of this section with an offset.
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 ...
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
iterator find(StringRef Key)
void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const
Dump information about the relocation entry (RE) and resolved value.
This class is the base class for all object file types.
uint8_t * getAddress() const
Error takeError()
Take ownership of the stored error.
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
bool IsPCRel
True if this is a PCRel relocation (MachO specific).
unsigned SectionID
SectionID - the section this relocation points to.
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.
unsigned getMaxStubSize() override
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
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)
Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, const SectionRef &Section)
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...
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Symbol resolution interface.
void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes, MachO::RelocationInfoType RelType, int64_t Addend) const
Extract the addend encoded in the instruction.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
static ErrorSuccess success()
Create a success value.
RuntimeDyldMachOAArch64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver)
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
uint32_t RelType
RelType - relocation type.
uintptr_t getStubOffset() const
Expected< int64_t > decodeAddend(const RelocationEntry &RE) const
Extract the addend encoded in the instruction / memory location.
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
uint64_t Offset
Offset - offset into the section.
std::map< SectionRef, unsigned > ObjSectionToIDMap
unsigned getPlainRelocationSymbolNum(const MachO::any_relocation_info &RE) const
uint8_t * getAddressWithOffset(unsigned OffsetBytes) const
Return the address of this section with an offset.
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
A raw_ostream that writes to an std::string.
LLVM Value Representation.
RTDyldSymbolTable GlobalSymbolTable
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.
This is a value type class that represents a single section in the list of sections in the object fil...
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...