LLVM API Documentation

RuntimeDyldMachOAArch64.h
Go to the documentation of this file.
00001 //===-- RuntimeDyldMachOAArch64.h -- MachO/AArch64 specific code. -*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H
00011 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H
00012 
00013 #include "../RuntimeDyldMachO.h"
00014 #include "llvm/Support/Endian.h"
00015 
00016 #define DEBUG_TYPE "dyld"
00017 
00018 namespace llvm {
00019 
00020 class RuntimeDyldMachOAArch64
00021     : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOAArch64> {
00022 public:
00023 
00024   typedef uint64_t TargetPtrT;
00025 
00026   RuntimeDyldMachOAArch64(RTDyldMemoryManager *MM)
00027       : RuntimeDyldMachOCRTPBase(MM) {}
00028 
00029   unsigned getMaxStubSize() override { return 8; }
00030 
00031   unsigned getStubAlignment() override { return 8; }
00032 
00033   /// Extract the addend encoded in the instruction / memory location.
00034   int64_t decodeAddend(const RelocationEntry &RE) const {
00035     const SectionEntry &Section = Sections[RE.SectionID];
00036     uint8_t *LocalAddress = Section.Address + RE.Offset;
00037     unsigned NumBytes = 1 << RE.Size;
00038     int64_t Addend = 0;
00039     // Verify that the relocation has the correct size and alignment.
00040     switch (RE.RelType) {
00041     default:
00042       llvm_unreachable("Unsupported relocation type!");
00043     case MachO::ARM64_RELOC_UNSIGNED:
00044       assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size.");
00045       break;
00046     case MachO::ARM64_RELOC_BRANCH26:
00047     case MachO::ARM64_RELOC_PAGE21:
00048     case MachO::ARM64_RELOC_PAGEOFF12:
00049     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
00050     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
00051       assert(NumBytes == 4 && "Invalid relocation size.");
00052       assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
00053              "Instruction address is not aligned to 4 bytes.");
00054       break;
00055     }
00056 
00057     switch (RE.RelType) {
00058     default:
00059       llvm_unreachable("Unsupported relocation type!");
00060     case MachO::ARM64_RELOC_UNSIGNED:
00061       // This could be an unaligned memory location.
00062       if (NumBytes == 4)
00063         Addend = *reinterpret_cast<support::ulittle32_t *>(LocalAddress);
00064       else
00065         Addend = *reinterpret_cast<support::ulittle64_t *>(LocalAddress);
00066       break;
00067     case MachO::ARM64_RELOC_BRANCH26: {
00068       // Verify that the relocation points to the expected branch instruction.
00069       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00070       assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction.");
00071 
00072       // Get the 26 bit addend encoded in the branch instruction and sign-extend
00073       // to 64 bit. The lower 2 bits are always zeros and are therefore implicit
00074       // (<< 2).
00075       Addend = (*p & 0x03FFFFFF) << 2;
00076       Addend = SignExtend64(Addend, 28);
00077       break;
00078     }
00079     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
00080     case MachO::ARM64_RELOC_PAGE21: {
00081       // Verify that the relocation points to the expected adrp instruction.
00082       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00083       assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction.");
00084 
00085       // Get the 21 bit addend encoded in the adrp instruction and sign-extend
00086       // to 64 bit. The lower 12 bits (4096 byte page) are always zeros and are
00087       // therefore implicit (<< 12).
00088       Addend = ((*p & 0x60000000) >> 29) | ((*p & 0x01FFFFE0) >> 3) << 12;
00089       Addend = SignExtend64(Addend, 33);
00090       break;
00091     }
00092     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: {
00093       // Verify that the relocation points to one of the expected load / store
00094       // instructions.
00095       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00096       (void)p;
00097       assert((*p & 0x3B000000) == 0x39000000 &&
00098              "Only expected load / store instructions.");
00099     } // fall-through
00100     case MachO::ARM64_RELOC_PAGEOFF12: {
00101       // Verify that the relocation points to one of the expected load / store
00102       // or add / sub instructions.
00103       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00104       assert((((*p & 0x3B000000) == 0x39000000) ||
00105               ((*p & 0x11C00000) == 0x11000000)   ) &&
00106              "Expected load / store  or add/sub instruction.");
00107 
00108       // Get the 12 bit addend encoded in the instruction.
00109       Addend = (*p & 0x003FFC00) >> 10;
00110 
00111       // Check which instruction we are decoding to obtain the implicit shift
00112       // factor of the instruction.
00113       int ImplicitShift = 0;
00114       if ((*p & 0x3B000000) == 0x39000000) { // << load / store
00115         // For load / store instructions the size is encoded in bits 31:30.
00116         ImplicitShift = ((*p >> 30) & 0x3);
00117         if (ImplicitShift == 0) {
00118           // Check if this a vector op to get the correct shift value.
00119           if ((*p & 0x04800000) == 0x04800000)
00120             ImplicitShift = 4;
00121         }
00122       }
00123       // Compensate for implicit shift.
00124       Addend <<= ImplicitShift;
00125       break;
00126     }
00127     }
00128     return Addend;
00129   }
00130 
00131   /// Extract the addend encoded in the instruction.
00132   void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
00133                     MachO::RelocationInfoType RelType, int64_t Addend) const {
00134     // Verify that the relocation has the correct alignment.
00135     switch (RelType) {
00136     default:
00137       llvm_unreachable("Unsupported relocation type!");
00138     case MachO::ARM64_RELOC_UNSIGNED:
00139       assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size.");
00140       break;
00141     case MachO::ARM64_RELOC_BRANCH26:
00142     case MachO::ARM64_RELOC_PAGE21:
00143     case MachO::ARM64_RELOC_PAGEOFF12:
00144     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
00145     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
00146       assert(NumBytes == 4 && "Invalid relocation size.");
00147       assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
00148              "Instruction address is not aligned to 4 bytes.");
00149       break;
00150     }
00151 
00152     switch (RelType) {
00153     default:
00154       llvm_unreachable("Unsupported relocation type!");
00155     case MachO::ARM64_RELOC_UNSIGNED:
00156       // This could be an unaligned memory location.
00157       if (NumBytes == 4)
00158         *reinterpret_cast<support::ulittle32_t *>(LocalAddress) = Addend;
00159       else
00160         *reinterpret_cast<support::ulittle64_t *>(LocalAddress) = Addend;
00161       break;
00162     case MachO::ARM64_RELOC_BRANCH26: {
00163       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00164       // Verify that the relocation points to the expected branch instruction.
00165       assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction.");
00166 
00167       // Verify addend value.
00168       assert((Addend & 0x3) == 0 && "Branch target is not aligned");
00169       assert(isInt<28>(Addend) && "Branch target is out of range.");
00170 
00171       // Encode the addend as 26 bit immediate in the branch instruction.
00172       *p = (*p & 0xFC000000) | ((uint32_t)(Addend >> 2) & 0x03FFFFFF);
00173       break;
00174     }
00175     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
00176     case MachO::ARM64_RELOC_PAGE21: {
00177       // Verify that the relocation points to the expected adrp instruction.
00178       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00179       assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction.");
00180 
00181       // Check that the addend fits into 21 bits (+ 12 lower bits).
00182       assert((Addend & 0xFFF) == 0 && "ADRP target is not page aligned.");
00183       assert(isInt<33>(Addend) && "Invalid page reloc value.");
00184 
00185       // Encode the addend into the instruction.
00186       uint32_t ImmLoValue = (uint32_t)(Addend << 17) & 0x60000000;
00187       uint32_t ImmHiValue = (uint32_t)(Addend >> 9) & 0x00FFFFE0;
00188       *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue;
00189       break;
00190     }
00191     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: {
00192       // Verify that the relocation points to one of the expected load / store
00193       // instructions.
00194       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00195       assert((*p & 0x3B000000) == 0x39000000 &&
00196              "Only expected load / store instructions.");
00197       (void)p;
00198     } // fall-through
00199     case MachO::ARM64_RELOC_PAGEOFF12: {
00200       // Verify that the relocation points to one of the expected load / store
00201       // or add / sub instructions.
00202       auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
00203       assert((((*p & 0x3B000000) == 0x39000000) ||
00204               ((*p & 0x11C00000) == 0x11000000)   ) &&
00205              "Expected load / store  or add/sub instruction.");
00206 
00207       // Check which instruction we are decoding to obtain the implicit shift
00208       // factor of the instruction and verify alignment.
00209       int ImplicitShift = 0;
00210       if ((*p & 0x3B000000) == 0x39000000) { // << load / store
00211         // For load / store instructions the size is encoded in bits 31:30.
00212         ImplicitShift = ((*p >> 30) & 0x3);
00213         switch (ImplicitShift) {
00214         case 0:
00215           // Check if this a vector op to get the correct shift value.
00216           if ((*p & 0x04800000) == 0x04800000) {
00217             ImplicitShift = 4;
00218             assert(((Addend & 0xF) == 0) &&
00219                    "128-bit LDR/STR not 16-byte aligned.");
00220           }
00221           break;
00222         case 1:
00223           assert(((Addend & 0x1) == 0) && "16-bit LDR/STR not 2-byte aligned.");
00224           break;
00225         case 2:
00226           assert(((Addend & 0x3) == 0) && "32-bit LDR/STR not 4-byte aligned.");
00227           break;
00228         case 3:
00229           assert(((Addend & 0x7) == 0) && "64-bit LDR/STR not 8-byte aligned.");
00230           break;
00231         }
00232       }
00233       // Compensate for implicit shift.
00234       Addend >>= ImplicitShift;
00235       assert(isUInt<12>(Addend) && "Addend cannot be encoded.");
00236 
00237       // Encode the addend into the instruction.
00238       *p = (*p & 0xFFC003FF) | ((uint32_t)(Addend << 10) & 0x003FFC00);
00239       break;
00240     }
00241     }
00242   }
00243 
00244   relocation_iterator
00245   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
00246                        ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
00247                        const SymbolTableMap &Symbols, StubMap &Stubs) override {
00248     const MachOObjectFile &Obj =
00249         static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
00250     MachO::any_relocation_info RelInfo =
00251         Obj.getRelocation(RelI->getRawDataRefImpl());
00252 
00253     assert(!Obj.isRelocationScattered(RelInfo) && "");
00254 
00255     // ARM64 has an ARM64_RELOC_ADDEND relocation type that carries an explicit
00256     // addend for the following relocation. If found: (1) store the associated
00257     // addend, (2) consume the next relocation, and (3) use the stored addend to
00258     // override the addend.
00259     int64_t ExplicitAddend = 0;
00260     if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_ADDEND) {
00261       assert(!Obj.getPlainRelocationExternal(RelInfo));
00262       assert(!Obj.getAnyRelocationPCRel(RelInfo));
00263       assert(Obj.getAnyRelocationLength(RelInfo) == 2);
00264       int64_t RawAddend = Obj.getPlainRelocationSymbolNum(RelInfo);
00265       // Sign-extend the 24-bit to 64-bit.
00266       ExplicitAddend = SignExtend64(RawAddend, 24);
00267       ++RelI;
00268       RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());
00269     }
00270 
00271     RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
00272     RE.Addend = decodeAddend(RE);
00273     RelocationValueRef Value(
00274         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
00275 
00276     assert((ExplicitAddend == 0 || RE.Addend == 0) && "Relocation has "\
00277       "ARM64_RELOC_ADDEND and embedded addend in the instruction.");
00278     if (ExplicitAddend) {
00279       RE.Addend = ExplicitAddend;
00280       Value.Offset = ExplicitAddend;
00281     }
00282 
00283     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
00284     if (!IsExtern && RE.IsPCRel)
00285       makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
00286 
00287     RE.Addend = Value.Offset;
00288 
00289     if (RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 ||
00290         RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12)
00291       processGOTRelocation(RE, Value, Stubs);
00292     else {
00293       if (Value.SymbolName)
00294         addRelocationForSymbol(RE, Value.SymbolName);
00295       else
00296         addRelocationForSection(RE, Value.SectionID);
00297     }
00298 
00299     return ++RelI;
00300   }
00301 
00302   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
00303     DEBUG(dumpRelocationToResolve(RE, Value));
00304 
00305     const SectionEntry &Section = Sections[RE.SectionID];
00306     uint8_t *LocalAddress = Section.Address + RE.Offset;
00307     MachO::RelocationInfoType RelType =
00308       static_cast<MachO::RelocationInfoType>(RE.RelType);
00309 
00310     switch (RelType) {
00311     default:
00312       llvm_unreachable("Invalid relocation type!");
00313     case MachO::ARM64_RELOC_UNSIGNED: {
00314       assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_UNSIGNED not supported");
00315       // Mask in the target value a byte at a time (we don't have an alignment
00316       // guarantee for the target address, so this is safest).
00317       if (RE.Size < 2)
00318         llvm_unreachable("Invalid size for ARM64_RELOC_UNSIGNED");
00319 
00320       encodeAddend(LocalAddress, 1 << RE.Size, RelType, Value + RE.Addend);
00321       break;
00322     }
00323     case MachO::ARM64_RELOC_BRANCH26: {
00324       assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported");
00325       // Check if branch is in range.
00326       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
00327       int64_t PCRelVal = Value - FinalAddress + RE.Addend;
00328       encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
00329       break;
00330     }
00331     case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
00332     case MachO::ARM64_RELOC_PAGE21: {
00333       assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported");
00334       // Adjust for PC-relative relocation and offset.
00335       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
00336       int64_t PCRelVal =
00337         ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096));
00338       encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
00339       break;
00340     }
00341     case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
00342     case MachO::ARM64_RELOC_PAGEOFF12: {
00343       assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_PAGEOFF21 not supported");
00344       // Add the offset from the symbol.
00345       Value += RE.Addend;
00346       // Mask out the page address and only use the lower 12 bits.
00347       Value &= 0xFFF;
00348       encodeAddend(LocalAddress, /*Size=*/4, RelType, Value);
00349       break;
00350     }
00351     case MachO::ARM64_RELOC_SUBTRACTOR:
00352     case MachO::ARM64_RELOC_POINTER_TO_GOT:
00353     case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
00354     case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
00355       llvm_unreachable("Relocation type not yet implemented!");
00356     case MachO::ARM64_RELOC_ADDEND:
00357       llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by "
00358                        "processRelocationRef!");
00359     }
00360   }
00361 
00362   void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
00363                        const SectionRef &Section) {}
00364 
00365 private:
00366   void processGOTRelocation(const RelocationEntry &RE,
00367                             RelocationValueRef &Value, StubMap &Stubs) {
00368     assert(RE.Size == 2);
00369     SectionEntry &Section = Sections[RE.SectionID];
00370     StubMap::const_iterator i = Stubs.find(Value);
00371     uintptr_t Addr;
00372     if (i != Stubs.end())
00373       Addr = reinterpret_cast<uintptr_t>(Section.Address) + i->second;
00374     else {
00375       // FIXME: There must be a better way to do this then to check and fix the
00376       // alignment every time!!!
00377       uintptr_t BaseAddress = uintptr_t(Section.Address);
00378       uintptr_t StubAlignment = getStubAlignment();
00379       uintptr_t StubAddress =
00380           (BaseAddress + Section.StubOffset + StubAlignment - 1) &
00381           -StubAlignment;
00382       unsigned StubOffset = StubAddress - BaseAddress;
00383       Stubs[Value] = StubOffset;
00384       assert(((StubAddress % getStubAlignment()) == 0) &&
00385              "GOT entry not aligned");
00386       RelocationEntry GOTRE(RE.SectionID, StubOffset,
00387                             MachO::ARM64_RELOC_UNSIGNED, Value.Offset,
00388                             /*IsPCRel=*/false, /*Size=*/3);
00389       if (Value.SymbolName)
00390         addRelocationForSymbol(GOTRE, Value.SymbolName);
00391       else
00392         addRelocationForSection(GOTRE, Value.SectionID);
00393       Section.StubOffset = StubOffset + getMaxStubSize();
00394       Addr = StubAddress;
00395     }
00396     RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, /*Addend=*/0,
00397                              RE.IsPCRel, RE.Size);
00398     resolveRelocation(TargetRE, static_cast<uint64_t>(Addr));
00399   }
00400 };
00401 }
00402 
00403 #undef DEBUG_TYPE
00404 
00405 #endif