1fe6060f1SDimitry Andric //===- ARM64.cpp ----------------------------------------------------------===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric 9fe6060f1SDimitry Andric #include "Arch/ARM64Common.h" 10fe6060f1SDimitry Andric #include "InputFiles.h" 11fe6060f1SDimitry Andric #include "Symbols.h" 12fe6060f1SDimitry Andric #include "SyntheticSections.h" 13fe6060f1SDimitry Andric #include "Target.h" 14fe6060f1SDimitry Andric 15fe6060f1SDimitry Andric #include "lld/Common/ErrorHandler.h" 1681ad6265SDimitry Andric #include "mach-o/compact_unwind_encoding.h" 17fe6060f1SDimitry Andric #include "llvm/ADT/SmallVector.h" 18fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h" 19fe6060f1SDimitry Andric #include "llvm/BinaryFormat/MachO.h" 20fe6060f1SDimitry Andric #include "llvm/Support/Endian.h" 21bdd1243dSDimitry Andric #include "llvm/Support/LEB128.h" 22fe6060f1SDimitry Andric #include "llvm/Support/MathExtras.h" 23fe6060f1SDimitry Andric 24fe6060f1SDimitry Andric using namespace llvm; 25fe6060f1SDimitry Andric using namespace llvm::MachO; 26fe6060f1SDimitry Andric using namespace llvm::support::endian; 27fe6060f1SDimitry Andric using namespace lld; 28fe6060f1SDimitry Andric using namespace lld::macho; 29fe6060f1SDimitry Andric 30fe6060f1SDimitry Andric namespace { 31fe6060f1SDimitry Andric 32fe6060f1SDimitry Andric struct ARM64 : ARM64Common { 33fe6060f1SDimitry Andric ARM64(); 34bdd1243dSDimitry Andric void writeStub(uint8_t *buf, const Symbol &, uint64_t) const override; 35fe6060f1SDimitry Andric void writeStubHelperHeader(uint8_t *buf) const override; 3681ad6265SDimitry Andric void writeStubHelperEntry(uint8_t *buf, const Symbol &, 37fe6060f1SDimitry Andric uint64_t entryAddr) const override; 38bdd1243dSDimitry Andric 39bdd1243dSDimitry Andric void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, 40*7a6dacacSDimitry Andric uint64_t &stubOffset, uint64_t selrefsVA, 41*7a6dacacSDimitry Andric uint64_t selectorIndex, 42*7a6dacacSDimitry Andric Symbol *objcMsgSend) const override; 43fe6060f1SDimitry Andric void populateThunk(InputSection *thunk, Symbol *funcSym) override; 44bdd1243dSDimitry Andric void applyOptimizationHints(uint8_t *, const ObjFile &) const override; 45fe6060f1SDimitry Andric }; 46fe6060f1SDimitry Andric 47fe6060f1SDimitry Andric } // namespace 48fe6060f1SDimitry Andric 49fe6060f1SDimitry Andric // Random notes on reloc types: 50fe6060f1SDimitry Andric // ADDEND always pairs with BRANCH26, PAGE21, or PAGEOFF12 51fe6060f1SDimitry Andric // POINTER_TO_GOT: ld64 supports a 4-byte pc-relative form as well as an 8-byte 52fe6060f1SDimitry Andric // absolute version of this relocation. The semantics of the absolute relocation 53fe6060f1SDimitry Andric // are weird -- it results in the value of the GOT slot being written, instead 54fe6060f1SDimitry Andric // of the address. Let's not support it unless we find a real-world use case. 55fcaf7f86SDimitry Andric static constexpr std::array<RelocAttrs, 11> relocAttrsArray{{ 56fe6060f1SDimitry Andric #define B(x) RelocAttrBits::x 57fe6060f1SDimitry Andric {"UNSIGNED", 58fe6060f1SDimitry Andric B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)}, 59fe6060f1SDimitry Andric {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)}, 60fe6060f1SDimitry Andric {"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)}, 61fe6060f1SDimitry Andric {"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)}, 62fe6060f1SDimitry Andric {"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)}, 63fe6060f1SDimitry Andric {"GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)}, 64fe6060f1SDimitry Andric {"GOT_LOAD_PAGEOFF12", 65fe6060f1SDimitry Andric B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)}, 66fe6060f1SDimitry Andric {"POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)}, 67fe6060f1SDimitry Andric {"TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)}, 68fe6060f1SDimitry Andric {"TLVP_LOAD_PAGEOFF12", 69fe6060f1SDimitry Andric B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)}, 70fe6060f1SDimitry Andric {"ADDEND", B(ADDEND)}, 71fe6060f1SDimitry Andric #undef B 72fe6060f1SDimitry Andric }}; 73fe6060f1SDimitry Andric 74fe6060f1SDimitry Andric static constexpr uint32_t stubCode[] = { 75fe6060f1SDimitry Andric 0x90000010, // 00: adrp x16, __la_symbol_ptr@page 76fe6060f1SDimitry Andric 0xf9400210, // 04: ldr x16, [x16, __la_symbol_ptr@pageoff] 77fe6060f1SDimitry Andric 0xd61f0200, // 08: br x16 78fe6060f1SDimitry Andric }; 79fe6060f1SDimitry Andric 80bdd1243dSDimitry Andric void ARM64::writeStub(uint8_t *buf8, const Symbol &sym, 81bdd1243dSDimitry Andric uint64_t pointerVA) const { 82bdd1243dSDimitry Andric ::writeStub(buf8, stubCode, sym, pointerVA); 83fe6060f1SDimitry Andric } 84fe6060f1SDimitry Andric 85fe6060f1SDimitry Andric static constexpr uint32_t stubHelperHeaderCode[] = { 86fe6060f1SDimitry Andric 0x90000011, // 00: adrp x17, _dyld_private@page 87fe6060f1SDimitry Andric 0x91000231, // 04: add x17, x17, _dyld_private@pageoff 88fe6060f1SDimitry Andric 0xa9bf47f0, // 08: stp x16/x17, [sp, #-16]! 89fe6060f1SDimitry Andric 0x90000010, // 0c: adrp x16, dyld_stub_binder@page 90fe6060f1SDimitry Andric 0xf9400210, // 10: ldr x16, [x16, dyld_stub_binder@pageoff] 91fe6060f1SDimitry Andric 0xd61f0200, // 14: br x16 92fe6060f1SDimitry Andric }; 93fe6060f1SDimitry Andric 94fe6060f1SDimitry Andric void ARM64::writeStubHelperHeader(uint8_t *buf8) const { 95fe6060f1SDimitry Andric ::writeStubHelperHeader<LP64>(buf8, stubHelperHeaderCode); 96fe6060f1SDimitry Andric } 97fe6060f1SDimitry Andric 98fe6060f1SDimitry Andric static constexpr uint32_t stubHelperEntryCode[] = { 99fe6060f1SDimitry Andric 0x18000050, // 00: ldr w16, l0 100fe6060f1SDimitry Andric 0x14000000, // 04: b stubHelperHeader 101fe6060f1SDimitry Andric 0x00000000, // 08: l0: .long 0 102fe6060f1SDimitry Andric }; 103fe6060f1SDimitry Andric 10481ad6265SDimitry Andric void ARM64::writeStubHelperEntry(uint8_t *buf8, const Symbol &sym, 105fe6060f1SDimitry Andric uint64_t entryVA) const { 106fe6060f1SDimitry Andric ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA); 107fe6060f1SDimitry Andric } 108fe6060f1SDimitry Andric 109bdd1243dSDimitry Andric static constexpr uint32_t objcStubsFastCode[] = { 110bdd1243dSDimitry Andric 0x90000001, // adrp x1, __objc_selrefs@page 111bdd1243dSDimitry Andric 0xf9400021, // ldr x1, [x1, @selector("foo")@pageoff] 112bdd1243dSDimitry Andric 0x90000010, // adrp x16, _got@page 113bdd1243dSDimitry Andric 0xf9400210, // ldr x16, [x16, _objc_msgSend@pageoff] 114bdd1243dSDimitry Andric 0xd61f0200, // br x16 115bdd1243dSDimitry Andric 0xd4200020, // brk #0x1 116bdd1243dSDimitry Andric 0xd4200020, // brk #0x1 117bdd1243dSDimitry Andric 0xd4200020, // brk #0x1 118bdd1243dSDimitry Andric }; 119bdd1243dSDimitry Andric 120*7a6dacacSDimitry Andric static constexpr uint32_t objcStubsSmallCode[] = { 121*7a6dacacSDimitry Andric 0x90000001, // adrp x1, __objc_selrefs@page 122*7a6dacacSDimitry Andric 0xf9400021, // ldr x1, [x1, @selector("foo")@pageoff] 123*7a6dacacSDimitry Andric 0x14000000, // b _objc_msgSend 124*7a6dacacSDimitry Andric }; 125*7a6dacacSDimitry Andric 126bdd1243dSDimitry Andric void ARM64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, 127*7a6dacacSDimitry Andric uint64_t &stubOffset, uint64_t selrefsVA, 128*7a6dacacSDimitry Andric uint64_t selectorIndex, 129*7a6dacacSDimitry Andric Symbol *objcMsgSend) const { 130*7a6dacacSDimitry Andric uint64_t objcMsgSendAddr; 131*7a6dacacSDimitry Andric uint64_t objcStubSize; 132*7a6dacacSDimitry Andric uint64_t objcMsgSendIndex; 133*7a6dacacSDimitry Andric 134*7a6dacacSDimitry Andric if (config->objcStubsMode == ObjCStubsMode::fast) { 135*7a6dacacSDimitry Andric objcStubSize = target->objcStubsFastSize; 136*7a6dacacSDimitry Andric objcMsgSendAddr = in.got->addr; 137*7a6dacacSDimitry Andric objcMsgSendIndex = objcMsgSend->gotIndex; 138*7a6dacacSDimitry Andric ::writeObjCMsgSendFastStub<LP64>(buf, objcStubsFastCode, sym, stubsAddr, 139*7a6dacacSDimitry Andric stubOffset, selrefsVA, selectorIndex, 140*7a6dacacSDimitry Andric objcMsgSendAddr, objcMsgSendIndex); 141*7a6dacacSDimitry Andric } else { 142*7a6dacacSDimitry Andric assert(config->objcStubsMode == ObjCStubsMode::small); 143*7a6dacacSDimitry Andric objcStubSize = target->objcStubsSmallSize; 144*7a6dacacSDimitry Andric if (auto *d = dyn_cast<Defined>(objcMsgSend)) { 145*7a6dacacSDimitry Andric objcMsgSendAddr = d->getVA(); 146*7a6dacacSDimitry Andric objcMsgSendIndex = 0; 147*7a6dacacSDimitry Andric } else { 148*7a6dacacSDimitry Andric objcMsgSendAddr = in.stubs->addr; 149*7a6dacacSDimitry Andric objcMsgSendIndex = objcMsgSend->stubsIndex; 150*7a6dacacSDimitry Andric } 151*7a6dacacSDimitry Andric ::writeObjCMsgSendSmallStub<LP64>(buf, objcStubsSmallCode, sym, stubsAddr, 152*7a6dacacSDimitry Andric stubOffset, selrefsVA, selectorIndex, 153*7a6dacacSDimitry Andric objcMsgSendAddr, objcMsgSendIndex); 154*7a6dacacSDimitry Andric } 155*7a6dacacSDimitry Andric stubOffset += objcStubSize; 156bdd1243dSDimitry Andric } 157bdd1243dSDimitry Andric 158fe6060f1SDimitry Andric // A thunk is the relaxed variation of stubCode. We don't need the 159fe6060f1SDimitry Andric // extra indirection through a lazy pointer because the target address 160fe6060f1SDimitry Andric // is known at link time. 161fe6060f1SDimitry Andric static constexpr uint32_t thunkCode[] = { 162fe6060f1SDimitry Andric 0x90000010, // 00: adrp x16, <thunk.ptr>@page 163fe6060f1SDimitry Andric 0x91000210, // 04: add x16, [x16,<thunk.ptr>@pageoff] 164fe6060f1SDimitry Andric 0xd61f0200, // 08: br x16 165fe6060f1SDimitry Andric }; 166fe6060f1SDimitry Andric 167fe6060f1SDimitry Andric void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym) { 168fe6060f1SDimitry Andric thunk->align = 4; 169fe6060f1SDimitry Andric thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode), 170fe6060f1SDimitry Andric sizeof(thunkCode)}; 17106c3fb27SDimitry Andric thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGEOFF12, 172fe6060f1SDimitry Andric /*pcrel=*/false, /*length=*/2, 173fe6060f1SDimitry Andric /*offset=*/4, /*addend=*/0, 17406c3fb27SDimitry Andric /*referent=*/funcSym); 17506c3fb27SDimitry Andric thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGE21, 176fe6060f1SDimitry Andric /*pcrel=*/true, /*length=*/2, 177fe6060f1SDimitry Andric /*offset=*/0, /*addend=*/0, 17806c3fb27SDimitry Andric /*referent=*/funcSym); 179fe6060f1SDimitry Andric } 180fe6060f1SDimitry Andric 181fe6060f1SDimitry Andric ARM64::ARM64() : ARM64Common(LP64()) { 182fe6060f1SDimitry Andric cpuType = CPU_TYPE_ARM64; 183fe6060f1SDimitry Andric cpuSubtype = CPU_SUBTYPE_ARM64_ALL; 184fe6060f1SDimitry Andric 185fe6060f1SDimitry Andric stubSize = sizeof(stubCode); 186fe6060f1SDimitry Andric thunkSize = sizeof(thunkCode); 187349cc55cSDimitry Andric 188bdd1243dSDimitry Andric objcStubsFastSize = sizeof(objcStubsFastCode); 189*7a6dacacSDimitry Andric objcStubsFastAlignment = 32; 190*7a6dacacSDimitry Andric objcStubsSmallSize = sizeof(objcStubsSmallCode); 191*7a6dacacSDimitry Andric objcStubsSmallAlignment = 4; 192bdd1243dSDimitry Andric 193349cc55cSDimitry Andric // Branch immediate is two's complement 26 bits, which is implicitly 194349cc55cSDimitry Andric // multiplied by 4 (since all functions are 4-aligned: The branch range 195349cc55cSDimitry Andric // is -4*(2**(26-1))..4*(2**(26-1) - 1). 196349cc55cSDimitry Andric backwardBranchRange = 128 * 1024 * 1024; 197349cc55cSDimitry Andric forwardBranchRange = backwardBranchRange - 4; 198349cc55cSDimitry Andric 19981ad6265SDimitry Andric modeDwarfEncoding = UNWIND_ARM64_MODE_DWARF; 20081ad6265SDimitry Andric subtractorRelocType = ARM64_RELOC_SUBTRACTOR; 20181ad6265SDimitry Andric unsignedRelocType = ARM64_RELOC_UNSIGNED; 20281ad6265SDimitry Andric 203fe6060f1SDimitry Andric stubHelperHeaderSize = sizeof(stubHelperHeaderCode); 204fe6060f1SDimitry Andric stubHelperEntrySize = sizeof(stubHelperEntryCode); 205fcaf7f86SDimitry Andric 206fcaf7f86SDimitry Andric relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()}; 207fe6060f1SDimitry Andric } 208fe6060f1SDimitry Andric 20981ad6265SDimitry Andric namespace { 21081ad6265SDimitry Andric struct Adrp { 21181ad6265SDimitry Andric uint32_t destRegister; 212bdd1243dSDimitry Andric int64_t addend; 21381ad6265SDimitry Andric }; 21481ad6265SDimitry Andric 21581ad6265SDimitry Andric struct Add { 21681ad6265SDimitry Andric uint8_t destRegister; 21781ad6265SDimitry Andric uint8_t srcRegister; 21881ad6265SDimitry Andric uint32_t addend; 21981ad6265SDimitry Andric }; 22081ad6265SDimitry Andric 22181ad6265SDimitry Andric enum ExtendType { ZeroExtend = 1, Sign64 = 2, Sign32 = 3 }; 22281ad6265SDimitry Andric 22381ad6265SDimitry Andric struct Ldr { 22481ad6265SDimitry Andric uint8_t destRegister; 22581ad6265SDimitry Andric uint8_t baseRegister; 226753f127fSDimitry Andric uint8_t p2Size; 22781ad6265SDimitry Andric bool isFloat; 22881ad6265SDimitry Andric ExtendType extendType; 229753f127fSDimitry Andric int64_t offset; 23081ad6265SDimitry Andric }; 23181ad6265SDimitry Andric } // namespace 23281ad6265SDimitry Andric 23381ad6265SDimitry Andric static bool parseAdrp(uint32_t insn, Adrp &adrp) { 23481ad6265SDimitry Andric if ((insn & 0x9f000000) != 0x90000000) 23581ad6265SDimitry Andric return false; 23681ad6265SDimitry Andric adrp.destRegister = insn & 0x1f; 237bdd1243dSDimitry Andric uint64_t immHi = (insn >> 5) & 0x7ffff; 238bdd1243dSDimitry Andric uint64_t immLo = (insn >> 29) & 0x3; 239bdd1243dSDimitry Andric adrp.addend = SignExtend64<21>(immLo | (immHi << 2)) * 4096; 24081ad6265SDimitry Andric return true; 24181ad6265SDimitry Andric } 24281ad6265SDimitry Andric 24381ad6265SDimitry Andric static bool parseAdd(uint32_t insn, Add &add) { 24481ad6265SDimitry Andric if ((insn & 0xffc00000) != 0x91000000) 24581ad6265SDimitry Andric return false; 24681ad6265SDimitry Andric add.destRegister = insn & 0x1f; 24781ad6265SDimitry Andric add.srcRegister = (insn >> 5) & 0x1f; 24881ad6265SDimitry Andric add.addend = (insn >> 10) & 0xfff; 24981ad6265SDimitry Andric return true; 25081ad6265SDimitry Andric } 25181ad6265SDimitry Andric 25281ad6265SDimitry Andric static bool parseLdr(uint32_t insn, Ldr &ldr) { 25381ad6265SDimitry Andric ldr.destRegister = insn & 0x1f; 25481ad6265SDimitry Andric ldr.baseRegister = (insn >> 5) & 0x1f; 25581ad6265SDimitry Andric uint8_t size = insn >> 30; 25681ad6265SDimitry Andric uint8_t opc = (insn >> 22) & 3; 25781ad6265SDimitry Andric 25881ad6265SDimitry Andric if ((insn & 0x3fc00000) == 0x39400000) { 25981ad6265SDimitry Andric // LDR (immediate), LDRB (immediate), LDRH (immediate) 260753f127fSDimitry Andric ldr.p2Size = size; 26181ad6265SDimitry Andric ldr.extendType = ZeroExtend; 26281ad6265SDimitry Andric ldr.isFloat = false; 26381ad6265SDimitry Andric } else if ((insn & 0x3f800000) == 0x39800000) { 26481ad6265SDimitry Andric // LDRSB (immediate), LDRSH (immediate), LDRSW (immediate) 265753f127fSDimitry Andric ldr.p2Size = size; 26681ad6265SDimitry Andric ldr.extendType = static_cast<ExtendType>(opc); 26781ad6265SDimitry Andric ldr.isFloat = false; 26881ad6265SDimitry Andric } else if ((insn & 0x3f400000) == 0x3d400000) { 26981ad6265SDimitry Andric // LDR (immediate, SIMD&FP) 27081ad6265SDimitry Andric ldr.extendType = ZeroExtend; 27181ad6265SDimitry Andric ldr.isFloat = true; 272753f127fSDimitry Andric if (opc == 1) 273753f127fSDimitry Andric ldr.p2Size = size; 27481ad6265SDimitry Andric else if (size == 0 && opc == 3) 275753f127fSDimitry Andric ldr.p2Size = 4; 27681ad6265SDimitry Andric else 27781ad6265SDimitry Andric return false; 27881ad6265SDimitry Andric } else { 27981ad6265SDimitry Andric return false; 28081ad6265SDimitry Andric } 281753f127fSDimitry Andric ldr.offset = ((insn >> 10) & 0xfff) << ldr.p2Size; 28281ad6265SDimitry Andric return true; 28381ad6265SDimitry Andric } 28481ad6265SDimitry Andric 285753f127fSDimitry Andric static bool isValidAdrOffset(int32_t delta) { return isInt<21>(delta); } 286753f127fSDimitry Andric 28781ad6265SDimitry Andric static void writeAdr(void *loc, uint32_t dest, int32_t delta) { 288753f127fSDimitry Andric assert(isValidAdrOffset(delta)); 28981ad6265SDimitry Andric uint32_t opcode = 0x10000000; 29081ad6265SDimitry Andric uint32_t immHi = (delta & 0x001ffffc) << 3; 29181ad6265SDimitry Andric uint32_t immLo = (delta & 0x00000003) << 29; 29281ad6265SDimitry Andric write32le(loc, opcode | immHi | immLo | dest); 29381ad6265SDimitry Andric } 29481ad6265SDimitry Andric 29581ad6265SDimitry Andric static void writeNop(void *loc) { write32le(loc, 0xd503201f); } 29681ad6265SDimitry Andric 297753f127fSDimitry Andric static bool isLiteralLdrEligible(const Ldr &ldr) { 298753f127fSDimitry Andric return ldr.p2Size > 1 && isShiftedInt<19, 2>(ldr.offset); 299753f127fSDimitry Andric } 300753f127fSDimitry Andric 301753f127fSDimitry Andric static void writeLiteralLdr(void *loc, const Ldr &ldr) { 302753f127fSDimitry Andric assert(isLiteralLdrEligible(ldr)); 303753f127fSDimitry Andric uint32_t imm19 = (ldr.offset / 4 & maskTrailingOnes<uint32_t>(19)) << 5; 304753f127fSDimitry Andric uint32_t opcode; 305753f127fSDimitry Andric switch (ldr.p2Size) { 306753f127fSDimitry Andric case 2: 307753f127fSDimitry Andric if (ldr.isFloat) 30881ad6265SDimitry Andric opcode = 0x1c000000; 30981ad6265SDimitry Andric else 310753f127fSDimitry Andric opcode = ldr.extendType == Sign64 ? 0x98000000 : 0x18000000; 31181ad6265SDimitry Andric break; 312753f127fSDimitry Andric case 3: 313753f127fSDimitry Andric opcode = ldr.isFloat ? 0x5c000000 : 0x58000000; 31481ad6265SDimitry Andric break; 315753f127fSDimitry Andric case 4: 31681ad6265SDimitry Andric opcode = 0x9c000000; 31781ad6265SDimitry Andric break; 31881ad6265SDimitry Andric default: 319753f127fSDimitry Andric llvm_unreachable("Invalid literal ldr size"); 32081ad6265SDimitry Andric } 321753f127fSDimitry Andric write32le(loc, opcode | imm19 | ldr.destRegister); 322753f127fSDimitry Andric } 323753f127fSDimitry Andric 324753f127fSDimitry Andric static bool isImmediateLdrEligible(const Ldr &ldr) { 325753f127fSDimitry Andric // Note: We deviate from ld64's behavior, which converts to immediate loads 326753f127fSDimitry Andric // only if ldr.offset < 4096, even though the offset is divided by the load's 327753f127fSDimitry Andric // size in the 12-bit immediate operand. Only the unsigned offset variant is 328753f127fSDimitry Andric // supported. 329753f127fSDimitry Andric 330753f127fSDimitry Andric uint32_t size = 1 << ldr.p2Size; 331753f127fSDimitry Andric return ldr.offset >= 0 && (ldr.offset % size) == 0 && 332753f127fSDimitry Andric isUInt<12>(ldr.offset >> ldr.p2Size); 333753f127fSDimitry Andric } 334753f127fSDimitry Andric 335753f127fSDimitry Andric static void writeImmediateLdr(void *loc, const Ldr &ldr) { 336753f127fSDimitry Andric assert(isImmediateLdrEligible(ldr)); 337753f127fSDimitry Andric uint32_t opcode = 0x39000000; 338753f127fSDimitry Andric if (ldr.isFloat) { 339753f127fSDimitry Andric opcode |= 0x04000000; 340753f127fSDimitry Andric assert(ldr.extendType == ZeroExtend); 341753f127fSDimitry Andric } 342753f127fSDimitry Andric opcode |= ldr.destRegister; 343753f127fSDimitry Andric opcode |= ldr.baseRegister << 5; 344753f127fSDimitry Andric uint8_t size, opc; 345753f127fSDimitry Andric if (ldr.p2Size == 4) { 346753f127fSDimitry Andric size = 0; 347753f127fSDimitry Andric opc = 3; 348753f127fSDimitry Andric } else { 349753f127fSDimitry Andric opc = ldr.extendType; 350753f127fSDimitry Andric size = ldr.p2Size; 351753f127fSDimitry Andric } 352753f127fSDimitry Andric uint32_t immBits = ldr.offset >> ldr.p2Size; 353753f127fSDimitry Andric write32le(loc, opcode | (immBits << 10) | (opc << 22) | (size << 30)); 35481ad6265SDimitry Andric } 35581ad6265SDimitry Andric 35681ad6265SDimitry Andric // Transforms a pair of adrp+add instructions into an adr instruction if the 35781ad6265SDimitry Andric // target is within the +/- 1 MiB range allowed by the adr's 21 bit signed 35881ad6265SDimitry Andric // immediate offset. 35981ad6265SDimitry Andric // 36081ad6265SDimitry Andric // adrp xN, _foo@PAGE 36181ad6265SDimitry Andric // add xM, xN, _foo@PAGEOFF 36281ad6265SDimitry Andric // -> 36381ad6265SDimitry Andric // adr xM, _foo 36481ad6265SDimitry Andric // nop 365bdd1243dSDimitry Andric static void applyAdrpAdd(uint8_t *buf, const ConcatInputSection *isec, 366bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 367bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 368bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 36981ad6265SDimitry Andric Adrp adrp; 37081ad6265SDimitry Andric Add add; 371bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp) || !parseAdd(ins2, add)) 37281ad6265SDimitry Andric return; 37381ad6265SDimitry Andric if (adrp.destRegister != add.srcRegister) 37481ad6265SDimitry Andric return; 37581ad6265SDimitry Andric 376bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 377bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + add.addend; 378bdd1243dSDimitry Andric int64_t delta = referent - addr1; 379753f127fSDimitry Andric if (!isValidAdrOffset(delta)) 38081ad6265SDimitry Andric return; 38181ad6265SDimitry Andric 382bdd1243dSDimitry Andric writeAdr(buf + offset1, add.destRegister, delta); 383bdd1243dSDimitry Andric writeNop(buf + offset2); 38481ad6265SDimitry Andric } 38581ad6265SDimitry Andric 38681ad6265SDimitry Andric // Transforms two adrp instructions into a single adrp if their referent 38781ad6265SDimitry Andric // addresses are located on the same 4096 byte page. 38881ad6265SDimitry Andric // 38981ad6265SDimitry Andric // adrp xN, _foo@PAGE 39081ad6265SDimitry Andric // adrp xN, _bar@PAGE 39181ad6265SDimitry Andric // -> 39281ad6265SDimitry Andric // adrp xN, _foo@PAGE 39381ad6265SDimitry Andric // nop 394bdd1243dSDimitry Andric static void applyAdrpAdrp(uint8_t *buf, const ConcatInputSection *isec, 395bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 396bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 397bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 39881ad6265SDimitry Andric Adrp adrp1, adrp2; 39981ad6265SDimitry Andric if (!parseAdrp(ins1, adrp1) || !parseAdrp(ins2, adrp2)) 40081ad6265SDimitry Andric return; 40181ad6265SDimitry Andric if (adrp1.destRegister != adrp2.destRegister) 40281ad6265SDimitry Andric return; 40381ad6265SDimitry Andric 404bdd1243dSDimitry Andric uint64_t page1 = pageBits(offset1 + isec->getVA()) + adrp1.addend; 405bdd1243dSDimitry Andric uint64_t page2 = pageBits(offset2 + isec->getVA()) + adrp2.addend; 406bdd1243dSDimitry Andric if (page1 != page2) 40781ad6265SDimitry Andric return; 40881ad6265SDimitry Andric 409bdd1243dSDimitry Andric writeNop(buf + offset2); 41081ad6265SDimitry Andric } 41181ad6265SDimitry Andric 41281ad6265SDimitry Andric // Transforms a pair of adrp+ldr (immediate) instructions into an ldr (literal) 41381ad6265SDimitry Andric // load from a PC-relative address if it is 4-byte aligned and within +/- 1 MiB, 41481ad6265SDimitry Andric // as ldr can encode a signed 19-bit offset that gets multiplied by 4. 41581ad6265SDimitry Andric // 41681ad6265SDimitry Andric // adrp xN, _foo@PAGE 41781ad6265SDimitry Andric // ldr xM, [xN, _foo@PAGEOFF] 41881ad6265SDimitry Andric // -> 41981ad6265SDimitry Andric // nop 42081ad6265SDimitry Andric // ldr xM, _foo 421bdd1243dSDimitry Andric static void applyAdrpLdr(uint8_t *buf, const ConcatInputSection *isec, 422bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 423bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 424bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 42581ad6265SDimitry Andric Adrp adrp; 42681ad6265SDimitry Andric Ldr ldr; 427bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp) || !parseLdr(ins2, ldr)) 42881ad6265SDimitry Andric return; 42981ad6265SDimitry Andric if (adrp.destRegister != ldr.baseRegister) 43081ad6265SDimitry Andric return; 43181ad6265SDimitry Andric 432bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 433bdd1243dSDimitry Andric uint64_t addr2 = isec->getVA() + offset2; 434bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + ldr.offset; 435bdd1243dSDimitry Andric ldr.offset = referent - addr2; 436753f127fSDimitry Andric if (!isLiteralLdrEligible(ldr)) 43781ad6265SDimitry Andric return; 43881ad6265SDimitry Andric 439bdd1243dSDimitry Andric writeNop(buf + offset1); 440bdd1243dSDimitry Andric writeLiteralLdr(buf + offset2, ldr); 441753f127fSDimitry Andric } 442753f127fSDimitry Andric 443753f127fSDimitry Andric // GOT loads are emitted by the compiler as a pair of adrp and ldr instructions, 444753f127fSDimitry Andric // but they may be changed to adrp+add by relaxGotLoad(). This hint performs 445753f127fSDimitry Andric // the AdrpLdr or AdrpAdd transformation depending on whether it was relaxed. 446bdd1243dSDimitry Andric static void applyAdrpLdrGot(uint8_t *buf, const ConcatInputSection *isec, 447bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 448bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 449753f127fSDimitry Andric Add add; 450753f127fSDimitry Andric Ldr ldr; 451753f127fSDimitry Andric if (parseAdd(ins2, add)) 452bdd1243dSDimitry Andric applyAdrpAdd(buf, isec, offset1, offset2); 453753f127fSDimitry Andric else if (parseLdr(ins2, ldr)) 454bdd1243dSDimitry Andric applyAdrpLdr(buf, isec, offset1, offset2); 455753f127fSDimitry Andric } 456753f127fSDimitry Andric 457bdd1243dSDimitry Andric // Optimizes an adrp+add+ldr sequence used for loading from a local symbol's 458bdd1243dSDimitry Andric // address by loading directly if it's close enough, or to an adrp(p)+ldr 459bdd1243dSDimitry Andric // sequence if it's not. 460bdd1243dSDimitry Andric // 461753f127fSDimitry Andric // adrp x0, _foo@PAGE 462753f127fSDimitry Andric // add x1, x0, _foo@PAGEOFF 463753f127fSDimitry Andric // ldr x2, [x1, #off] 464bdd1243dSDimitry Andric static void applyAdrpAddLdr(uint8_t *buf, const ConcatInputSection *isec, 465bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2, 466bdd1243dSDimitry Andric uint64_t offset3) { 467bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 468bdd1243dSDimitry Andric Adrp adrp; 469bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp)) 470753f127fSDimitry Andric return; 471bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 472bdd1243dSDimitry Andric Add add; 473bdd1243dSDimitry Andric if (!parseAdd(ins2, add)) 474bdd1243dSDimitry Andric return; 475bdd1243dSDimitry Andric uint32_t ins3 = read32le(buf + offset3); 476bdd1243dSDimitry Andric Ldr ldr; 477bdd1243dSDimitry Andric if (!parseLdr(ins3, ldr)) 478bdd1243dSDimitry Andric return; 479bdd1243dSDimitry Andric if (adrp.destRegister != add.srcRegister) 480bdd1243dSDimitry Andric return; 481bdd1243dSDimitry Andric if (add.destRegister != ldr.baseRegister) 482753f127fSDimitry Andric return; 483753f127fSDimitry Andric 484753f127fSDimitry Andric // Load from the target address directly. 485753f127fSDimitry Andric // nop 486753f127fSDimitry Andric // nop 487753f127fSDimitry Andric // ldr x2, [_foo + #off] 488bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 489bdd1243dSDimitry Andric uint64_t addr3 = isec->getVA() + offset3; 490bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + add.addend; 491bdd1243dSDimitry Andric Ldr literalLdr = ldr; 492bdd1243dSDimitry Andric literalLdr.offset += referent - addr3; 493753f127fSDimitry Andric if (isLiteralLdrEligible(literalLdr)) { 494bdd1243dSDimitry Andric writeNop(buf + offset1); 495bdd1243dSDimitry Andric writeNop(buf + offset2); 496bdd1243dSDimitry Andric writeLiteralLdr(buf + offset3, literalLdr); 497753f127fSDimitry Andric return; 498753f127fSDimitry Andric } 499753f127fSDimitry Andric 500753f127fSDimitry Andric // Load the target address into a register and load from there indirectly. 501753f127fSDimitry Andric // adr x1, _foo 502753f127fSDimitry Andric // nop 503753f127fSDimitry Andric // ldr x2, [x1, #off] 504bdd1243dSDimitry Andric int64_t adrOffset = referent - addr1; 505753f127fSDimitry Andric if (isValidAdrOffset(adrOffset)) { 506bdd1243dSDimitry Andric writeAdr(buf + offset1, ldr.baseRegister, adrOffset); 507bdd1243dSDimitry Andric // Note: ld64 moves the offset into the adr instruction for AdrpAddLdr, but 508bdd1243dSDimitry Andric // not for AdrpLdrGotLdr. Its effect is the same either way. 509bdd1243dSDimitry Andric writeNop(buf + offset2); 510753f127fSDimitry Andric return; 511753f127fSDimitry Andric } 512753f127fSDimitry Andric 513753f127fSDimitry Andric // Move the target's page offset into the ldr's immediate offset. 514753f127fSDimitry Andric // adrp x0, _foo@PAGE 515753f127fSDimitry Andric // nop 516753f127fSDimitry Andric // ldr x2, [x0, _foo@PAGEOFF + #off] 517bdd1243dSDimitry Andric Ldr immediateLdr = ldr; 518753f127fSDimitry Andric immediateLdr.baseRegister = adrp.destRegister; 519bdd1243dSDimitry Andric immediateLdr.offset += add.addend; 520753f127fSDimitry Andric if (isImmediateLdrEligible(immediateLdr)) { 521bdd1243dSDimitry Andric writeNop(buf + offset2); 522bdd1243dSDimitry Andric writeImmediateLdr(buf + offset3, immediateLdr); 523753f127fSDimitry Andric return; 524753f127fSDimitry Andric } 525bdd1243dSDimitry Andric } 526bdd1243dSDimitry Andric 527bdd1243dSDimitry Andric // Relaxes a GOT-indirect load. 528bdd1243dSDimitry Andric // If the referenced symbol is external and its GOT entry is within +/- 1 MiB, 529bdd1243dSDimitry Andric // the GOT entry can be loaded with a single literal ldr instruction. 530bdd1243dSDimitry Andric // If the referenced symbol is local and thus has been relaxed to adrp+add+ldr, 531bdd1243dSDimitry Andric // we perform the AdrpAddLdr transformation. 532bdd1243dSDimitry Andric static void applyAdrpLdrGotLdr(uint8_t *buf, const ConcatInputSection *isec, 533bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2, 534bdd1243dSDimitry Andric uint64_t offset3) { 535bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 536bdd1243dSDimitry Andric Add add; 537bdd1243dSDimitry Andric Ldr ldr2; 538bdd1243dSDimitry Andric 539bdd1243dSDimitry Andric if (parseAdd(ins2, add)) { 540bdd1243dSDimitry Andric applyAdrpAddLdr(buf, isec, offset1, offset2, offset3); 541753f127fSDimitry Andric } else if (parseLdr(ins2, ldr2)) { 542753f127fSDimitry Andric // adrp x1, _foo@GOTPAGE 543753f127fSDimitry Andric // ldr x2, [x1, _foo@GOTPAGEOFF] 544753f127fSDimitry Andric // ldr x3, [x2, #off] 545bdd1243dSDimitry Andric 546bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 547bdd1243dSDimitry Andric Adrp adrp; 548bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp)) 549bdd1243dSDimitry Andric return; 550bdd1243dSDimitry Andric uint32_t ins3 = read32le(buf + offset3); 551bdd1243dSDimitry Andric Ldr ldr3; 552bdd1243dSDimitry Andric if (!parseLdr(ins3, ldr3)) 553bdd1243dSDimitry Andric return; 554bdd1243dSDimitry Andric 555753f127fSDimitry Andric if (ldr2.baseRegister != adrp.destRegister) 556753f127fSDimitry Andric return; 557753f127fSDimitry Andric if (ldr3.baseRegister != ldr2.destRegister) 558753f127fSDimitry Andric return; 559753f127fSDimitry Andric // Loads from the GOT must be pointer sized. 560753f127fSDimitry Andric if (ldr2.p2Size != 3 || ldr2.isFloat) 561753f127fSDimitry Andric return; 562753f127fSDimitry Andric 563bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 564bdd1243dSDimitry Andric uint64_t addr2 = isec->getVA() + offset2; 565bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + ldr2.offset; 566753f127fSDimitry Andric // Load the GOT entry's address directly. 567753f127fSDimitry Andric // nop 568753f127fSDimitry Andric // ldr x2, _foo@GOTPAGE + _foo@GOTPAGEOFF 569753f127fSDimitry Andric // ldr x3, [x2, #off] 570753f127fSDimitry Andric Ldr literalLdr = ldr2; 571bdd1243dSDimitry Andric literalLdr.offset = referent - addr2; 572753f127fSDimitry Andric if (isLiteralLdrEligible(literalLdr)) { 573bdd1243dSDimitry Andric writeNop(buf + offset1); 574bdd1243dSDimitry Andric writeLiteralLdr(buf + offset2, literalLdr); 575753f127fSDimitry Andric } 576753f127fSDimitry Andric } 57781ad6265SDimitry Andric } 57881ad6265SDimitry Andric 579bdd1243dSDimitry Andric static uint64_t readValue(const uint8_t *&ptr, const uint8_t *end) { 580bdd1243dSDimitry Andric unsigned int n = 0; 581bdd1243dSDimitry Andric uint64_t value = decodeULEB128(ptr, &n, end); 582bdd1243dSDimitry Andric ptr += n; 583bdd1243dSDimitry Andric return value; 584bdd1243dSDimitry Andric } 58581ad6265SDimitry Andric 586bdd1243dSDimitry Andric template <typename Callback> 587bdd1243dSDimitry Andric static void forEachHint(ArrayRef<uint8_t> data, Callback callback) { 588bdd1243dSDimitry Andric std::array<uint64_t, 3> args; 58981ad6265SDimitry Andric 590bdd1243dSDimitry Andric for (const uint8_t *p = data.begin(), *end = data.end(); p < end;) { 591bdd1243dSDimitry Andric uint64_t type = readValue(p, end); 592bdd1243dSDimitry Andric if (type == 0) 593bdd1243dSDimitry Andric break; 594bdd1243dSDimitry Andric 595bdd1243dSDimitry Andric uint64_t argCount = readValue(p, end); 596bdd1243dSDimitry Andric // All known LOH types as of 2022-09 have 3 or fewer arguments; skip others. 597bdd1243dSDimitry Andric if (argCount > 3) { 598bdd1243dSDimitry Andric for (unsigned i = 0; i < argCount; ++i) 599bdd1243dSDimitry Andric readValue(p, end); 600bdd1243dSDimitry Andric continue; 601bdd1243dSDimitry Andric } 602bdd1243dSDimitry Andric 603bdd1243dSDimitry Andric for (unsigned i = 0; i < argCount; ++i) 604bdd1243dSDimitry Andric args[i] = readValue(p, end); 605bdd1243dSDimitry Andric callback(type, ArrayRef<uint64_t>(args.data(), argCount)); 606bdd1243dSDimitry Andric } 607bdd1243dSDimitry Andric } 608bdd1243dSDimitry Andric 609bdd1243dSDimitry Andric // On RISC architectures like arm64, materializing a memory address generally 610bdd1243dSDimitry Andric // takes multiple instructions. If the referenced symbol is located close enough 611bdd1243dSDimitry Andric // in memory, fewer instructions are needed. 612bdd1243dSDimitry Andric // 613bdd1243dSDimitry Andric // Linker optimization hints record where addresses are computed. After 614bdd1243dSDimitry Andric // addresses have been assigned, if possible, we change them to a shorter 615bdd1243dSDimitry Andric // sequence of instructions. The size of the binary is not modified; the 616bdd1243dSDimitry Andric // eliminated instructions are replaced with NOPs. This still leads to faster 617bdd1243dSDimitry Andric // code as the CPU can skip over NOPs quickly. 618bdd1243dSDimitry Andric // 619bdd1243dSDimitry Andric // LOHs are specified by the LC_LINKER_OPTIMIZATION_HINTS load command, which 620bdd1243dSDimitry Andric // points to a sequence of ULEB128-encoded numbers. Each entry specifies a 621bdd1243dSDimitry Andric // transformation kind, and 2 or 3 addresses where the instructions are located. 622bdd1243dSDimitry Andric void ARM64::applyOptimizationHints(uint8_t *outBuf, const ObjFile &obj) const { 623bdd1243dSDimitry Andric ArrayRef<uint8_t> data = obj.getOptimizationHints(); 624bdd1243dSDimitry Andric if (data.empty()) 625bdd1243dSDimitry Andric return; 626bdd1243dSDimitry Andric 627bdd1243dSDimitry Andric const ConcatInputSection *section = nullptr; 628bdd1243dSDimitry Andric uint64_t sectionAddr = 0; 629bdd1243dSDimitry Andric uint8_t *buf = nullptr; 630bdd1243dSDimitry Andric 631bdd1243dSDimitry Andric auto findSection = [&](uint64_t addr) { 632bdd1243dSDimitry Andric if (section && addr >= sectionAddr && 633bdd1243dSDimitry Andric addr < sectionAddr + section->getSize()) 634bdd1243dSDimitry Andric return true; 635bdd1243dSDimitry Andric 6365f757f3fSDimitry Andric if (obj.sections.empty()) 6375f757f3fSDimitry Andric return false; 638bdd1243dSDimitry Andric auto secIt = std::prev(llvm::upper_bound( 639bdd1243dSDimitry Andric obj.sections, addr, 640bdd1243dSDimitry Andric [](uint64_t off, const Section *sec) { return off < sec->addr; })); 641bdd1243dSDimitry Andric const Section *sec = *secIt; 642bdd1243dSDimitry Andric 6435f757f3fSDimitry Andric if (sec->subsections.empty()) 6445f757f3fSDimitry Andric return false; 645bdd1243dSDimitry Andric auto subsecIt = std::prev(llvm::upper_bound( 646bdd1243dSDimitry Andric sec->subsections, addr - sec->addr, 647bdd1243dSDimitry Andric [](uint64_t off, Subsection subsec) { return off < subsec.offset; })); 648bdd1243dSDimitry Andric const Subsection &subsec = *subsecIt; 649bdd1243dSDimitry Andric const ConcatInputSection *isec = 650bdd1243dSDimitry Andric dyn_cast_or_null<ConcatInputSection>(subsec.isec); 651bdd1243dSDimitry Andric if (!isec || isec->shouldOmitFromOutput()) 652bdd1243dSDimitry Andric return false; 653bdd1243dSDimitry Andric 654bdd1243dSDimitry Andric section = isec; 655bdd1243dSDimitry Andric sectionAddr = subsec.offset + sec->addr; 656bdd1243dSDimitry Andric buf = outBuf + section->outSecOff + section->parent->fileOff; 657bdd1243dSDimitry Andric return true; 658bdd1243dSDimitry Andric }; 659bdd1243dSDimitry Andric 660bdd1243dSDimitry Andric auto isValidOffset = [&](uint64_t offset) { 661bdd1243dSDimitry Andric if (offset < sectionAddr || offset >= sectionAddr + section->getSize()) { 662bdd1243dSDimitry Andric error(toString(&obj) + 663bdd1243dSDimitry Andric ": linker optimization hint spans multiple sections"); 664bdd1243dSDimitry Andric return false; 665bdd1243dSDimitry Andric } 666bdd1243dSDimitry Andric return true; 667bdd1243dSDimitry Andric }; 668bdd1243dSDimitry Andric 669bdd1243dSDimitry Andric bool hasAdrpAdrp = false; 670bdd1243dSDimitry Andric forEachHint(data, [&](uint64_t kind, ArrayRef<uint64_t> args) { 671bdd1243dSDimitry Andric if (kind == LOH_ARM64_ADRP_ADRP) { 672bdd1243dSDimitry Andric hasAdrpAdrp = true; 673bdd1243dSDimitry Andric return; 674bdd1243dSDimitry Andric } 675bdd1243dSDimitry Andric 676bdd1243dSDimitry Andric if (!findSection(args[0])) 677bdd1243dSDimitry Andric return; 678bdd1243dSDimitry Andric switch (kind) { 679bdd1243dSDimitry Andric case LOH_ARM64_ADRP_ADD: 680bdd1243dSDimitry Andric if (isValidOffset(args[1])) 681bdd1243dSDimitry Andric applyAdrpAdd(buf, section, args[0] - sectionAddr, 682bdd1243dSDimitry Andric args[1] - sectionAddr); 68381ad6265SDimitry Andric break; 68481ad6265SDimitry Andric case LOH_ARM64_ADRP_LDR: 685bdd1243dSDimitry Andric if (isValidOffset(args[1])) 686bdd1243dSDimitry Andric applyAdrpLdr(buf, section, args[0] - sectionAddr, 687bdd1243dSDimitry Andric args[1] - sectionAddr); 688bdd1243dSDimitry Andric break; 689bdd1243dSDimitry Andric case LOH_ARM64_ADRP_LDR_GOT: 690bdd1243dSDimitry Andric if (isValidOffset(args[1])) 691bdd1243dSDimitry Andric applyAdrpLdrGot(buf, section, args[0] - sectionAddr, 692bdd1243dSDimitry Andric args[1] - sectionAddr); 69381ad6265SDimitry Andric break; 69481ad6265SDimitry Andric case LOH_ARM64_ADRP_ADD_LDR: 695bdd1243dSDimitry Andric if (isValidOffset(args[1]) && isValidOffset(args[2])) 696bdd1243dSDimitry Andric applyAdrpAddLdr(buf, section, args[0] - sectionAddr, 697bdd1243dSDimitry Andric args[1] - sectionAddr, args[2] - sectionAddr); 698753f127fSDimitry Andric break; 69981ad6265SDimitry Andric case LOH_ARM64_ADRP_LDR_GOT_LDR: 700bdd1243dSDimitry Andric if (isValidOffset(args[1]) && isValidOffset(args[2])) 701bdd1243dSDimitry Andric applyAdrpLdrGotLdr(buf, section, args[0] - sectionAddr, 702bdd1243dSDimitry Andric args[1] - sectionAddr, args[2] - sectionAddr); 703753f127fSDimitry Andric break; 70481ad6265SDimitry Andric case LOH_ARM64_ADRP_ADD_STR: 70581ad6265SDimitry Andric case LOH_ARM64_ADRP_LDR_GOT_STR: 70681ad6265SDimitry Andric // TODO: Implement these 70781ad6265SDimitry Andric break; 70881ad6265SDimitry Andric } 709bdd1243dSDimitry Andric }); 71081ad6265SDimitry Andric 711bdd1243dSDimitry Andric if (!hasAdrpAdrp) 712bdd1243dSDimitry Andric return; 713bdd1243dSDimitry Andric 714bdd1243dSDimitry Andric // AdrpAdrp optimization hints are performed in a second pass because they 715bdd1243dSDimitry Andric // might interfere with other transformations. For instance, consider the 716bdd1243dSDimitry Andric // following input: 717bdd1243dSDimitry Andric // 718bdd1243dSDimitry Andric // adrp x0, _foo@PAGE 719bdd1243dSDimitry Andric // add x1, x0, _foo@PAGEOFF 720bdd1243dSDimitry Andric // adrp x0, _bar@PAGE 721bdd1243dSDimitry Andric // add x2, x0, _bar@PAGEOFF 722bdd1243dSDimitry Andric // 723bdd1243dSDimitry Andric // If we perform the AdrpAdrp relaxation first, we get: 724bdd1243dSDimitry Andric // 725bdd1243dSDimitry Andric // adrp x0, _foo@PAGE 726bdd1243dSDimitry Andric // add x1, x0, _foo@PAGEOFF 727bdd1243dSDimitry Andric // nop 728bdd1243dSDimitry Andric // add x2, x0, _bar@PAGEOFF 729bdd1243dSDimitry Andric // 730bdd1243dSDimitry Andric // If we then apply AdrpAdd to the first two instructions, the add will have a 731bdd1243dSDimitry Andric // garbage value in x0: 732bdd1243dSDimitry Andric // 733bdd1243dSDimitry Andric // adr x1, _foo 734bdd1243dSDimitry Andric // nop 735bdd1243dSDimitry Andric // nop 736bdd1243dSDimitry Andric // add x2, x0, _bar@PAGEOFF 737bdd1243dSDimitry Andric forEachHint(data, [&](uint64_t kind, ArrayRef<uint64_t> args) { 738bdd1243dSDimitry Andric if (kind != LOH_ARM64_ADRP_ADRP) 739bdd1243dSDimitry Andric return; 740bdd1243dSDimitry Andric if (!findSection(args[0])) 741bdd1243dSDimitry Andric return; 742bdd1243dSDimitry Andric if (isValidOffset(args[1])) 743bdd1243dSDimitry Andric applyAdrpAdrp(buf, section, args[0] - sectionAddr, args[1] - sectionAddr); 744bdd1243dSDimitry Andric }); 74581ad6265SDimitry Andric } 74681ad6265SDimitry Andric 747fe6060f1SDimitry Andric TargetInfo *macho::createARM64TargetInfo() { 748fe6060f1SDimitry Andric static ARM64 t; 749fe6060f1SDimitry Andric return &t; 750fe6060f1SDimitry Andric } 751