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, 40bdd1243dSDimitry Andric uint64_t stubOffset, uint64_t selrefsVA, 41bdd1243dSDimitry Andric uint64_t selectorIndex, uint64_t gotAddr, 42bdd1243dSDimitry Andric uint64_t msgSendIndex) 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 120bdd1243dSDimitry Andric void ARM64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr, 121bdd1243dSDimitry Andric uint64_t stubOffset, uint64_t selrefsVA, 122bdd1243dSDimitry Andric uint64_t selectorIndex, uint64_t gotAddr, 123bdd1243dSDimitry Andric uint64_t msgSendIndex) const { 124bdd1243dSDimitry Andric ::writeObjCMsgSendStub<LP64>(buf, objcStubsFastCode, sym, stubsAddr, 125bdd1243dSDimitry Andric stubOffset, selrefsVA, selectorIndex, gotAddr, 126bdd1243dSDimitry Andric msgSendIndex); 127bdd1243dSDimitry Andric } 128bdd1243dSDimitry Andric 129fe6060f1SDimitry Andric // A thunk is the relaxed variation of stubCode. We don't need the 130fe6060f1SDimitry Andric // extra indirection through a lazy pointer because the target address 131fe6060f1SDimitry Andric // is known at link time. 132fe6060f1SDimitry Andric static constexpr uint32_t thunkCode[] = { 133fe6060f1SDimitry Andric 0x90000010, // 00: adrp x16, <thunk.ptr>@page 134fe6060f1SDimitry Andric 0x91000210, // 04: add x16, [x16,<thunk.ptr>@pageoff] 135fe6060f1SDimitry Andric 0xd61f0200, // 08: br x16 136fe6060f1SDimitry Andric }; 137fe6060f1SDimitry Andric 138fe6060f1SDimitry Andric void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym) { 139fe6060f1SDimitry Andric thunk->align = 4; 140fe6060f1SDimitry Andric thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode), 141fe6060f1SDimitry Andric sizeof(thunkCode)}; 142*06c3fb27SDimitry Andric thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGEOFF12, 143fe6060f1SDimitry Andric /*pcrel=*/false, /*length=*/2, 144fe6060f1SDimitry Andric /*offset=*/4, /*addend=*/0, 145*06c3fb27SDimitry Andric /*referent=*/funcSym); 146*06c3fb27SDimitry Andric thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGE21, 147fe6060f1SDimitry Andric /*pcrel=*/true, /*length=*/2, 148fe6060f1SDimitry Andric /*offset=*/0, /*addend=*/0, 149*06c3fb27SDimitry Andric /*referent=*/funcSym); 150fe6060f1SDimitry Andric } 151fe6060f1SDimitry Andric 152fe6060f1SDimitry Andric ARM64::ARM64() : ARM64Common(LP64()) { 153fe6060f1SDimitry Andric cpuType = CPU_TYPE_ARM64; 154fe6060f1SDimitry Andric cpuSubtype = CPU_SUBTYPE_ARM64_ALL; 155fe6060f1SDimitry Andric 156fe6060f1SDimitry Andric stubSize = sizeof(stubCode); 157fe6060f1SDimitry Andric thunkSize = sizeof(thunkCode); 158349cc55cSDimitry Andric 159bdd1243dSDimitry Andric objcStubsFastSize = sizeof(objcStubsFastCode); 160bdd1243dSDimitry Andric objcStubsAlignment = 32; 161bdd1243dSDimitry Andric 162349cc55cSDimitry Andric // Branch immediate is two's complement 26 bits, which is implicitly 163349cc55cSDimitry Andric // multiplied by 4 (since all functions are 4-aligned: The branch range 164349cc55cSDimitry Andric // is -4*(2**(26-1))..4*(2**(26-1) - 1). 165349cc55cSDimitry Andric backwardBranchRange = 128 * 1024 * 1024; 166349cc55cSDimitry Andric forwardBranchRange = backwardBranchRange - 4; 167349cc55cSDimitry Andric 16881ad6265SDimitry Andric modeDwarfEncoding = UNWIND_ARM64_MODE_DWARF; 16981ad6265SDimitry Andric subtractorRelocType = ARM64_RELOC_SUBTRACTOR; 17081ad6265SDimitry Andric unsignedRelocType = ARM64_RELOC_UNSIGNED; 17181ad6265SDimitry Andric 172fe6060f1SDimitry Andric stubHelperHeaderSize = sizeof(stubHelperHeaderCode); 173fe6060f1SDimitry Andric stubHelperEntrySize = sizeof(stubHelperEntryCode); 174fcaf7f86SDimitry Andric 175fcaf7f86SDimitry Andric relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()}; 176fe6060f1SDimitry Andric } 177fe6060f1SDimitry Andric 17881ad6265SDimitry Andric namespace { 17981ad6265SDimitry Andric struct Adrp { 18081ad6265SDimitry Andric uint32_t destRegister; 181bdd1243dSDimitry Andric int64_t addend; 18281ad6265SDimitry Andric }; 18381ad6265SDimitry Andric 18481ad6265SDimitry Andric struct Add { 18581ad6265SDimitry Andric uint8_t destRegister; 18681ad6265SDimitry Andric uint8_t srcRegister; 18781ad6265SDimitry Andric uint32_t addend; 18881ad6265SDimitry Andric }; 18981ad6265SDimitry Andric 19081ad6265SDimitry Andric enum ExtendType { ZeroExtend = 1, Sign64 = 2, Sign32 = 3 }; 19181ad6265SDimitry Andric 19281ad6265SDimitry Andric struct Ldr { 19381ad6265SDimitry Andric uint8_t destRegister; 19481ad6265SDimitry Andric uint8_t baseRegister; 195753f127fSDimitry Andric uint8_t p2Size; 19681ad6265SDimitry Andric bool isFloat; 19781ad6265SDimitry Andric ExtendType extendType; 198753f127fSDimitry Andric int64_t offset; 19981ad6265SDimitry Andric }; 20081ad6265SDimitry Andric } // namespace 20181ad6265SDimitry Andric 20281ad6265SDimitry Andric static bool parseAdrp(uint32_t insn, Adrp &adrp) { 20381ad6265SDimitry Andric if ((insn & 0x9f000000) != 0x90000000) 20481ad6265SDimitry Andric return false; 20581ad6265SDimitry Andric adrp.destRegister = insn & 0x1f; 206bdd1243dSDimitry Andric uint64_t immHi = (insn >> 5) & 0x7ffff; 207bdd1243dSDimitry Andric uint64_t immLo = (insn >> 29) & 0x3; 208bdd1243dSDimitry Andric adrp.addend = SignExtend64<21>(immLo | (immHi << 2)) * 4096; 20981ad6265SDimitry Andric return true; 21081ad6265SDimitry Andric } 21181ad6265SDimitry Andric 21281ad6265SDimitry Andric static bool parseAdd(uint32_t insn, Add &add) { 21381ad6265SDimitry Andric if ((insn & 0xffc00000) != 0x91000000) 21481ad6265SDimitry Andric return false; 21581ad6265SDimitry Andric add.destRegister = insn & 0x1f; 21681ad6265SDimitry Andric add.srcRegister = (insn >> 5) & 0x1f; 21781ad6265SDimitry Andric add.addend = (insn >> 10) & 0xfff; 21881ad6265SDimitry Andric return true; 21981ad6265SDimitry Andric } 22081ad6265SDimitry Andric 22181ad6265SDimitry Andric static bool parseLdr(uint32_t insn, Ldr &ldr) { 22281ad6265SDimitry Andric ldr.destRegister = insn & 0x1f; 22381ad6265SDimitry Andric ldr.baseRegister = (insn >> 5) & 0x1f; 22481ad6265SDimitry Andric uint8_t size = insn >> 30; 22581ad6265SDimitry Andric uint8_t opc = (insn >> 22) & 3; 22681ad6265SDimitry Andric 22781ad6265SDimitry Andric if ((insn & 0x3fc00000) == 0x39400000) { 22881ad6265SDimitry Andric // LDR (immediate), LDRB (immediate), LDRH (immediate) 229753f127fSDimitry Andric ldr.p2Size = size; 23081ad6265SDimitry Andric ldr.extendType = ZeroExtend; 23181ad6265SDimitry Andric ldr.isFloat = false; 23281ad6265SDimitry Andric } else if ((insn & 0x3f800000) == 0x39800000) { 23381ad6265SDimitry Andric // LDRSB (immediate), LDRSH (immediate), LDRSW (immediate) 234753f127fSDimitry Andric ldr.p2Size = size; 23581ad6265SDimitry Andric ldr.extendType = static_cast<ExtendType>(opc); 23681ad6265SDimitry Andric ldr.isFloat = false; 23781ad6265SDimitry Andric } else if ((insn & 0x3f400000) == 0x3d400000) { 23881ad6265SDimitry Andric // LDR (immediate, SIMD&FP) 23981ad6265SDimitry Andric ldr.extendType = ZeroExtend; 24081ad6265SDimitry Andric ldr.isFloat = true; 241753f127fSDimitry Andric if (opc == 1) 242753f127fSDimitry Andric ldr.p2Size = size; 24381ad6265SDimitry Andric else if (size == 0 && opc == 3) 244753f127fSDimitry Andric ldr.p2Size = 4; 24581ad6265SDimitry Andric else 24681ad6265SDimitry Andric return false; 24781ad6265SDimitry Andric } else { 24881ad6265SDimitry Andric return false; 24981ad6265SDimitry Andric } 250753f127fSDimitry Andric ldr.offset = ((insn >> 10) & 0xfff) << ldr.p2Size; 25181ad6265SDimitry Andric return true; 25281ad6265SDimitry Andric } 25381ad6265SDimitry Andric 254753f127fSDimitry Andric static bool isValidAdrOffset(int32_t delta) { return isInt<21>(delta); } 255753f127fSDimitry Andric 25681ad6265SDimitry Andric static void writeAdr(void *loc, uint32_t dest, int32_t delta) { 257753f127fSDimitry Andric assert(isValidAdrOffset(delta)); 25881ad6265SDimitry Andric uint32_t opcode = 0x10000000; 25981ad6265SDimitry Andric uint32_t immHi = (delta & 0x001ffffc) << 3; 26081ad6265SDimitry Andric uint32_t immLo = (delta & 0x00000003) << 29; 26181ad6265SDimitry Andric write32le(loc, opcode | immHi | immLo | dest); 26281ad6265SDimitry Andric } 26381ad6265SDimitry Andric 26481ad6265SDimitry Andric static void writeNop(void *loc) { write32le(loc, 0xd503201f); } 26581ad6265SDimitry Andric 266753f127fSDimitry Andric static bool isLiteralLdrEligible(const Ldr &ldr) { 267753f127fSDimitry Andric return ldr.p2Size > 1 && isShiftedInt<19, 2>(ldr.offset); 268753f127fSDimitry Andric } 269753f127fSDimitry Andric 270753f127fSDimitry Andric static void writeLiteralLdr(void *loc, const Ldr &ldr) { 271753f127fSDimitry Andric assert(isLiteralLdrEligible(ldr)); 272753f127fSDimitry Andric uint32_t imm19 = (ldr.offset / 4 & maskTrailingOnes<uint32_t>(19)) << 5; 273753f127fSDimitry Andric uint32_t opcode; 274753f127fSDimitry Andric switch (ldr.p2Size) { 275753f127fSDimitry Andric case 2: 276753f127fSDimitry Andric if (ldr.isFloat) 27781ad6265SDimitry Andric opcode = 0x1c000000; 27881ad6265SDimitry Andric else 279753f127fSDimitry Andric opcode = ldr.extendType == Sign64 ? 0x98000000 : 0x18000000; 28081ad6265SDimitry Andric break; 281753f127fSDimitry Andric case 3: 282753f127fSDimitry Andric opcode = ldr.isFloat ? 0x5c000000 : 0x58000000; 28381ad6265SDimitry Andric break; 284753f127fSDimitry Andric case 4: 28581ad6265SDimitry Andric opcode = 0x9c000000; 28681ad6265SDimitry Andric break; 28781ad6265SDimitry Andric default: 288753f127fSDimitry Andric llvm_unreachable("Invalid literal ldr size"); 28981ad6265SDimitry Andric } 290753f127fSDimitry Andric write32le(loc, opcode | imm19 | ldr.destRegister); 291753f127fSDimitry Andric } 292753f127fSDimitry Andric 293753f127fSDimitry Andric static bool isImmediateLdrEligible(const Ldr &ldr) { 294753f127fSDimitry Andric // Note: We deviate from ld64's behavior, which converts to immediate loads 295753f127fSDimitry Andric // only if ldr.offset < 4096, even though the offset is divided by the load's 296753f127fSDimitry Andric // size in the 12-bit immediate operand. Only the unsigned offset variant is 297753f127fSDimitry Andric // supported. 298753f127fSDimitry Andric 299753f127fSDimitry Andric uint32_t size = 1 << ldr.p2Size; 300753f127fSDimitry Andric return ldr.offset >= 0 && (ldr.offset % size) == 0 && 301753f127fSDimitry Andric isUInt<12>(ldr.offset >> ldr.p2Size); 302753f127fSDimitry Andric } 303753f127fSDimitry Andric 304753f127fSDimitry Andric static void writeImmediateLdr(void *loc, const Ldr &ldr) { 305753f127fSDimitry Andric assert(isImmediateLdrEligible(ldr)); 306753f127fSDimitry Andric uint32_t opcode = 0x39000000; 307753f127fSDimitry Andric if (ldr.isFloat) { 308753f127fSDimitry Andric opcode |= 0x04000000; 309753f127fSDimitry Andric assert(ldr.extendType == ZeroExtend); 310753f127fSDimitry Andric } 311753f127fSDimitry Andric opcode |= ldr.destRegister; 312753f127fSDimitry Andric opcode |= ldr.baseRegister << 5; 313753f127fSDimitry Andric uint8_t size, opc; 314753f127fSDimitry Andric if (ldr.p2Size == 4) { 315753f127fSDimitry Andric size = 0; 316753f127fSDimitry Andric opc = 3; 317753f127fSDimitry Andric } else { 318753f127fSDimitry Andric opc = ldr.extendType; 319753f127fSDimitry Andric size = ldr.p2Size; 320753f127fSDimitry Andric } 321753f127fSDimitry Andric uint32_t immBits = ldr.offset >> ldr.p2Size; 322753f127fSDimitry Andric write32le(loc, opcode | (immBits << 10) | (opc << 22) | (size << 30)); 32381ad6265SDimitry Andric } 32481ad6265SDimitry Andric 32581ad6265SDimitry Andric // Transforms a pair of adrp+add instructions into an adr instruction if the 32681ad6265SDimitry Andric // target is within the +/- 1 MiB range allowed by the adr's 21 bit signed 32781ad6265SDimitry Andric // immediate offset. 32881ad6265SDimitry Andric // 32981ad6265SDimitry Andric // adrp xN, _foo@PAGE 33081ad6265SDimitry Andric // add xM, xN, _foo@PAGEOFF 33181ad6265SDimitry Andric // -> 33281ad6265SDimitry Andric // adr xM, _foo 33381ad6265SDimitry Andric // nop 334bdd1243dSDimitry Andric static void applyAdrpAdd(uint8_t *buf, const ConcatInputSection *isec, 335bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 336bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 337bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 33881ad6265SDimitry Andric Adrp adrp; 33981ad6265SDimitry Andric Add add; 340bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp) || !parseAdd(ins2, add)) 34181ad6265SDimitry Andric return; 34281ad6265SDimitry Andric if (adrp.destRegister != add.srcRegister) 34381ad6265SDimitry Andric return; 34481ad6265SDimitry Andric 345bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 346bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + add.addend; 347bdd1243dSDimitry Andric int64_t delta = referent - addr1; 348753f127fSDimitry Andric if (!isValidAdrOffset(delta)) 34981ad6265SDimitry Andric return; 35081ad6265SDimitry Andric 351bdd1243dSDimitry Andric writeAdr(buf + offset1, add.destRegister, delta); 352bdd1243dSDimitry Andric writeNop(buf + offset2); 35381ad6265SDimitry Andric } 35481ad6265SDimitry Andric 35581ad6265SDimitry Andric // Transforms two adrp instructions into a single adrp if their referent 35681ad6265SDimitry Andric // addresses are located on the same 4096 byte page. 35781ad6265SDimitry Andric // 35881ad6265SDimitry Andric // adrp xN, _foo@PAGE 35981ad6265SDimitry Andric // adrp xN, _bar@PAGE 36081ad6265SDimitry Andric // -> 36181ad6265SDimitry Andric // adrp xN, _foo@PAGE 36281ad6265SDimitry Andric // nop 363bdd1243dSDimitry Andric static void applyAdrpAdrp(uint8_t *buf, const ConcatInputSection *isec, 364bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 365bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 366bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 36781ad6265SDimitry Andric Adrp adrp1, adrp2; 36881ad6265SDimitry Andric if (!parseAdrp(ins1, adrp1) || !parseAdrp(ins2, adrp2)) 36981ad6265SDimitry Andric return; 37081ad6265SDimitry Andric if (adrp1.destRegister != adrp2.destRegister) 37181ad6265SDimitry Andric return; 37281ad6265SDimitry Andric 373bdd1243dSDimitry Andric uint64_t page1 = pageBits(offset1 + isec->getVA()) + adrp1.addend; 374bdd1243dSDimitry Andric uint64_t page2 = pageBits(offset2 + isec->getVA()) + adrp2.addend; 375bdd1243dSDimitry Andric if (page1 != page2) 37681ad6265SDimitry Andric return; 37781ad6265SDimitry Andric 378bdd1243dSDimitry Andric writeNop(buf + offset2); 37981ad6265SDimitry Andric } 38081ad6265SDimitry Andric 38181ad6265SDimitry Andric // Transforms a pair of adrp+ldr (immediate) instructions into an ldr (literal) 38281ad6265SDimitry Andric // load from a PC-relative address if it is 4-byte aligned and within +/- 1 MiB, 38381ad6265SDimitry Andric // as ldr can encode a signed 19-bit offset that gets multiplied by 4. 38481ad6265SDimitry Andric // 38581ad6265SDimitry Andric // adrp xN, _foo@PAGE 38681ad6265SDimitry Andric // ldr xM, [xN, _foo@PAGEOFF] 38781ad6265SDimitry Andric // -> 38881ad6265SDimitry Andric // nop 38981ad6265SDimitry Andric // ldr xM, _foo 390bdd1243dSDimitry Andric static void applyAdrpLdr(uint8_t *buf, const ConcatInputSection *isec, 391bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 392bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 393bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 39481ad6265SDimitry Andric Adrp adrp; 39581ad6265SDimitry Andric Ldr ldr; 396bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp) || !parseLdr(ins2, ldr)) 39781ad6265SDimitry Andric return; 39881ad6265SDimitry Andric if (adrp.destRegister != ldr.baseRegister) 39981ad6265SDimitry Andric return; 40081ad6265SDimitry Andric 401bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 402bdd1243dSDimitry Andric uint64_t addr2 = isec->getVA() + offset2; 403bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + ldr.offset; 404bdd1243dSDimitry Andric ldr.offset = referent - addr2; 405753f127fSDimitry Andric if (!isLiteralLdrEligible(ldr)) 40681ad6265SDimitry Andric return; 40781ad6265SDimitry Andric 408bdd1243dSDimitry Andric writeNop(buf + offset1); 409bdd1243dSDimitry Andric writeLiteralLdr(buf + offset2, ldr); 410753f127fSDimitry Andric } 411753f127fSDimitry Andric 412753f127fSDimitry Andric // GOT loads are emitted by the compiler as a pair of adrp and ldr instructions, 413753f127fSDimitry Andric // but they may be changed to adrp+add by relaxGotLoad(). This hint performs 414753f127fSDimitry Andric // the AdrpLdr or AdrpAdd transformation depending on whether it was relaxed. 415bdd1243dSDimitry Andric static void applyAdrpLdrGot(uint8_t *buf, const ConcatInputSection *isec, 416bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2) { 417bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 418753f127fSDimitry Andric Add add; 419753f127fSDimitry Andric Ldr ldr; 420753f127fSDimitry Andric if (parseAdd(ins2, add)) 421bdd1243dSDimitry Andric applyAdrpAdd(buf, isec, offset1, offset2); 422753f127fSDimitry Andric else if (parseLdr(ins2, ldr)) 423bdd1243dSDimitry Andric applyAdrpLdr(buf, isec, offset1, offset2); 424753f127fSDimitry Andric } 425753f127fSDimitry Andric 426bdd1243dSDimitry Andric // Optimizes an adrp+add+ldr sequence used for loading from a local symbol's 427bdd1243dSDimitry Andric // address by loading directly if it's close enough, or to an adrp(p)+ldr 428bdd1243dSDimitry Andric // sequence if it's not. 429bdd1243dSDimitry Andric // 430753f127fSDimitry Andric // adrp x0, _foo@PAGE 431753f127fSDimitry Andric // add x1, x0, _foo@PAGEOFF 432753f127fSDimitry Andric // ldr x2, [x1, #off] 433bdd1243dSDimitry Andric static void applyAdrpAddLdr(uint8_t *buf, const ConcatInputSection *isec, 434bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2, 435bdd1243dSDimitry Andric uint64_t offset3) { 436bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 437bdd1243dSDimitry Andric Adrp adrp; 438bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp)) 439753f127fSDimitry Andric return; 440bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 441bdd1243dSDimitry Andric Add add; 442bdd1243dSDimitry Andric if (!parseAdd(ins2, add)) 443bdd1243dSDimitry Andric return; 444bdd1243dSDimitry Andric uint32_t ins3 = read32le(buf + offset3); 445bdd1243dSDimitry Andric Ldr ldr; 446bdd1243dSDimitry Andric if (!parseLdr(ins3, ldr)) 447bdd1243dSDimitry Andric return; 448bdd1243dSDimitry Andric if (adrp.destRegister != add.srcRegister) 449bdd1243dSDimitry Andric return; 450bdd1243dSDimitry Andric if (add.destRegister != ldr.baseRegister) 451753f127fSDimitry Andric return; 452753f127fSDimitry Andric 453753f127fSDimitry Andric // Load from the target address directly. 454753f127fSDimitry Andric // nop 455753f127fSDimitry Andric // nop 456753f127fSDimitry Andric // ldr x2, [_foo + #off] 457bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 458bdd1243dSDimitry Andric uint64_t addr3 = isec->getVA() + offset3; 459bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + add.addend; 460bdd1243dSDimitry Andric Ldr literalLdr = ldr; 461bdd1243dSDimitry Andric literalLdr.offset += referent - addr3; 462753f127fSDimitry Andric if (isLiteralLdrEligible(literalLdr)) { 463bdd1243dSDimitry Andric writeNop(buf + offset1); 464bdd1243dSDimitry Andric writeNop(buf + offset2); 465bdd1243dSDimitry Andric writeLiteralLdr(buf + offset3, literalLdr); 466753f127fSDimitry Andric return; 467753f127fSDimitry Andric } 468753f127fSDimitry Andric 469753f127fSDimitry Andric // Load the target address into a register and load from there indirectly. 470753f127fSDimitry Andric // adr x1, _foo 471753f127fSDimitry Andric // nop 472753f127fSDimitry Andric // ldr x2, [x1, #off] 473bdd1243dSDimitry Andric int64_t adrOffset = referent - addr1; 474753f127fSDimitry Andric if (isValidAdrOffset(adrOffset)) { 475bdd1243dSDimitry Andric writeAdr(buf + offset1, ldr.baseRegister, adrOffset); 476bdd1243dSDimitry Andric // Note: ld64 moves the offset into the adr instruction for AdrpAddLdr, but 477bdd1243dSDimitry Andric // not for AdrpLdrGotLdr. Its effect is the same either way. 478bdd1243dSDimitry Andric writeNop(buf + offset2); 479753f127fSDimitry Andric return; 480753f127fSDimitry Andric } 481753f127fSDimitry Andric 482753f127fSDimitry Andric // Move the target's page offset into the ldr's immediate offset. 483753f127fSDimitry Andric // adrp x0, _foo@PAGE 484753f127fSDimitry Andric // nop 485753f127fSDimitry Andric // ldr x2, [x0, _foo@PAGEOFF + #off] 486bdd1243dSDimitry Andric Ldr immediateLdr = ldr; 487753f127fSDimitry Andric immediateLdr.baseRegister = adrp.destRegister; 488bdd1243dSDimitry Andric immediateLdr.offset += add.addend; 489753f127fSDimitry Andric if (isImmediateLdrEligible(immediateLdr)) { 490bdd1243dSDimitry Andric writeNop(buf + offset2); 491bdd1243dSDimitry Andric writeImmediateLdr(buf + offset3, immediateLdr); 492753f127fSDimitry Andric return; 493753f127fSDimitry Andric } 494bdd1243dSDimitry Andric } 495bdd1243dSDimitry Andric 496bdd1243dSDimitry Andric // Relaxes a GOT-indirect load. 497bdd1243dSDimitry Andric // If the referenced symbol is external and its GOT entry is within +/- 1 MiB, 498bdd1243dSDimitry Andric // the GOT entry can be loaded with a single literal ldr instruction. 499bdd1243dSDimitry Andric // If the referenced symbol is local and thus has been relaxed to adrp+add+ldr, 500bdd1243dSDimitry Andric // we perform the AdrpAddLdr transformation. 501bdd1243dSDimitry Andric static void applyAdrpLdrGotLdr(uint8_t *buf, const ConcatInputSection *isec, 502bdd1243dSDimitry Andric uint64_t offset1, uint64_t offset2, 503bdd1243dSDimitry Andric uint64_t offset3) { 504bdd1243dSDimitry Andric uint32_t ins2 = read32le(buf + offset2); 505bdd1243dSDimitry Andric Add add; 506bdd1243dSDimitry Andric Ldr ldr2; 507bdd1243dSDimitry Andric 508bdd1243dSDimitry Andric if (parseAdd(ins2, add)) { 509bdd1243dSDimitry Andric applyAdrpAddLdr(buf, isec, offset1, offset2, offset3); 510753f127fSDimitry Andric } else if (parseLdr(ins2, ldr2)) { 511753f127fSDimitry Andric // adrp x1, _foo@GOTPAGE 512753f127fSDimitry Andric // ldr x2, [x1, _foo@GOTPAGEOFF] 513753f127fSDimitry Andric // ldr x3, [x2, #off] 514bdd1243dSDimitry Andric 515bdd1243dSDimitry Andric uint32_t ins1 = read32le(buf + offset1); 516bdd1243dSDimitry Andric Adrp adrp; 517bdd1243dSDimitry Andric if (!parseAdrp(ins1, adrp)) 518bdd1243dSDimitry Andric return; 519bdd1243dSDimitry Andric uint32_t ins3 = read32le(buf + offset3); 520bdd1243dSDimitry Andric Ldr ldr3; 521bdd1243dSDimitry Andric if (!parseLdr(ins3, ldr3)) 522bdd1243dSDimitry Andric return; 523bdd1243dSDimitry Andric 524753f127fSDimitry Andric if (ldr2.baseRegister != adrp.destRegister) 525753f127fSDimitry Andric return; 526753f127fSDimitry Andric if (ldr3.baseRegister != ldr2.destRegister) 527753f127fSDimitry Andric return; 528753f127fSDimitry Andric // Loads from the GOT must be pointer sized. 529753f127fSDimitry Andric if (ldr2.p2Size != 3 || ldr2.isFloat) 530753f127fSDimitry Andric return; 531753f127fSDimitry Andric 532bdd1243dSDimitry Andric uint64_t addr1 = isec->getVA() + offset1; 533bdd1243dSDimitry Andric uint64_t addr2 = isec->getVA() + offset2; 534bdd1243dSDimitry Andric uint64_t referent = pageBits(addr1) + adrp.addend + ldr2.offset; 535753f127fSDimitry Andric // Load the GOT entry's address directly. 536753f127fSDimitry Andric // nop 537753f127fSDimitry Andric // ldr x2, _foo@GOTPAGE + _foo@GOTPAGEOFF 538753f127fSDimitry Andric // ldr x3, [x2, #off] 539753f127fSDimitry Andric Ldr literalLdr = ldr2; 540bdd1243dSDimitry Andric literalLdr.offset = referent - addr2; 541753f127fSDimitry Andric if (isLiteralLdrEligible(literalLdr)) { 542bdd1243dSDimitry Andric writeNop(buf + offset1); 543bdd1243dSDimitry Andric writeLiteralLdr(buf + offset2, literalLdr); 544753f127fSDimitry Andric } 545753f127fSDimitry Andric } 54681ad6265SDimitry Andric } 54781ad6265SDimitry Andric 548bdd1243dSDimitry Andric static uint64_t readValue(const uint8_t *&ptr, const uint8_t *end) { 549bdd1243dSDimitry Andric unsigned int n = 0; 550bdd1243dSDimitry Andric uint64_t value = decodeULEB128(ptr, &n, end); 551bdd1243dSDimitry Andric ptr += n; 552bdd1243dSDimitry Andric return value; 553bdd1243dSDimitry Andric } 55481ad6265SDimitry Andric 555bdd1243dSDimitry Andric template <typename Callback> 556bdd1243dSDimitry Andric static void forEachHint(ArrayRef<uint8_t> data, Callback callback) { 557bdd1243dSDimitry Andric std::array<uint64_t, 3> args; 55881ad6265SDimitry Andric 559bdd1243dSDimitry Andric for (const uint8_t *p = data.begin(), *end = data.end(); p < end;) { 560bdd1243dSDimitry Andric uint64_t type = readValue(p, end); 561bdd1243dSDimitry Andric if (type == 0) 562bdd1243dSDimitry Andric break; 563bdd1243dSDimitry Andric 564bdd1243dSDimitry Andric uint64_t argCount = readValue(p, end); 565bdd1243dSDimitry Andric // All known LOH types as of 2022-09 have 3 or fewer arguments; skip others. 566bdd1243dSDimitry Andric if (argCount > 3) { 567bdd1243dSDimitry Andric for (unsigned i = 0; i < argCount; ++i) 568bdd1243dSDimitry Andric readValue(p, end); 569bdd1243dSDimitry Andric continue; 570bdd1243dSDimitry Andric } 571bdd1243dSDimitry Andric 572bdd1243dSDimitry Andric for (unsigned i = 0; i < argCount; ++i) 573bdd1243dSDimitry Andric args[i] = readValue(p, end); 574bdd1243dSDimitry Andric callback(type, ArrayRef<uint64_t>(args.data(), argCount)); 575bdd1243dSDimitry Andric } 576bdd1243dSDimitry Andric } 577bdd1243dSDimitry Andric 578bdd1243dSDimitry Andric // On RISC architectures like arm64, materializing a memory address generally 579bdd1243dSDimitry Andric // takes multiple instructions. If the referenced symbol is located close enough 580bdd1243dSDimitry Andric // in memory, fewer instructions are needed. 581bdd1243dSDimitry Andric // 582bdd1243dSDimitry Andric // Linker optimization hints record where addresses are computed. After 583bdd1243dSDimitry Andric // addresses have been assigned, if possible, we change them to a shorter 584bdd1243dSDimitry Andric // sequence of instructions. The size of the binary is not modified; the 585bdd1243dSDimitry Andric // eliminated instructions are replaced with NOPs. This still leads to faster 586bdd1243dSDimitry Andric // code as the CPU can skip over NOPs quickly. 587bdd1243dSDimitry Andric // 588bdd1243dSDimitry Andric // LOHs are specified by the LC_LINKER_OPTIMIZATION_HINTS load command, which 589bdd1243dSDimitry Andric // points to a sequence of ULEB128-encoded numbers. Each entry specifies a 590bdd1243dSDimitry Andric // transformation kind, and 2 or 3 addresses where the instructions are located. 591bdd1243dSDimitry Andric void ARM64::applyOptimizationHints(uint8_t *outBuf, const ObjFile &obj) const { 592bdd1243dSDimitry Andric ArrayRef<uint8_t> data = obj.getOptimizationHints(); 593bdd1243dSDimitry Andric if (data.empty()) 594bdd1243dSDimitry Andric return; 595bdd1243dSDimitry Andric 596bdd1243dSDimitry Andric const ConcatInputSection *section = nullptr; 597bdd1243dSDimitry Andric uint64_t sectionAddr = 0; 598bdd1243dSDimitry Andric uint8_t *buf = nullptr; 599bdd1243dSDimitry Andric 600bdd1243dSDimitry Andric auto findSection = [&](uint64_t addr) { 601bdd1243dSDimitry Andric if (section && addr >= sectionAddr && 602bdd1243dSDimitry Andric addr < sectionAddr + section->getSize()) 603bdd1243dSDimitry Andric return true; 604bdd1243dSDimitry Andric 605bdd1243dSDimitry Andric auto secIt = std::prev(llvm::upper_bound( 606bdd1243dSDimitry Andric obj.sections, addr, 607bdd1243dSDimitry Andric [](uint64_t off, const Section *sec) { return off < sec->addr; })); 608bdd1243dSDimitry Andric const Section *sec = *secIt; 609bdd1243dSDimitry Andric 610bdd1243dSDimitry Andric auto subsecIt = std::prev(llvm::upper_bound( 611bdd1243dSDimitry Andric sec->subsections, addr - sec->addr, 612bdd1243dSDimitry Andric [](uint64_t off, Subsection subsec) { return off < subsec.offset; })); 613bdd1243dSDimitry Andric const Subsection &subsec = *subsecIt; 614bdd1243dSDimitry Andric const ConcatInputSection *isec = 615bdd1243dSDimitry Andric dyn_cast_or_null<ConcatInputSection>(subsec.isec); 616bdd1243dSDimitry Andric if (!isec || isec->shouldOmitFromOutput()) 617bdd1243dSDimitry Andric return false; 618bdd1243dSDimitry Andric 619bdd1243dSDimitry Andric section = isec; 620bdd1243dSDimitry Andric sectionAddr = subsec.offset + sec->addr; 621bdd1243dSDimitry Andric buf = outBuf + section->outSecOff + section->parent->fileOff; 622bdd1243dSDimitry Andric return true; 623bdd1243dSDimitry Andric }; 624bdd1243dSDimitry Andric 625bdd1243dSDimitry Andric auto isValidOffset = [&](uint64_t offset) { 626bdd1243dSDimitry Andric if (offset < sectionAddr || offset >= sectionAddr + section->getSize()) { 627bdd1243dSDimitry Andric error(toString(&obj) + 628bdd1243dSDimitry Andric ": linker optimization hint spans multiple sections"); 629bdd1243dSDimitry Andric return false; 630bdd1243dSDimitry Andric } 631bdd1243dSDimitry Andric return true; 632bdd1243dSDimitry Andric }; 633bdd1243dSDimitry Andric 634bdd1243dSDimitry Andric bool hasAdrpAdrp = false; 635bdd1243dSDimitry Andric forEachHint(data, [&](uint64_t kind, ArrayRef<uint64_t> args) { 636bdd1243dSDimitry Andric if (kind == LOH_ARM64_ADRP_ADRP) { 637bdd1243dSDimitry Andric hasAdrpAdrp = true; 638bdd1243dSDimitry Andric return; 639bdd1243dSDimitry Andric } 640bdd1243dSDimitry Andric 641bdd1243dSDimitry Andric if (!findSection(args[0])) 642bdd1243dSDimitry Andric return; 643bdd1243dSDimitry Andric switch (kind) { 644bdd1243dSDimitry Andric case LOH_ARM64_ADRP_ADD: 645bdd1243dSDimitry Andric if (isValidOffset(args[1])) 646bdd1243dSDimitry Andric applyAdrpAdd(buf, section, args[0] - sectionAddr, 647bdd1243dSDimitry Andric args[1] - sectionAddr); 64881ad6265SDimitry Andric break; 64981ad6265SDimitry Andric case LOH_ARM64_ADRP_LDR: 650bdd1243dSDimitry Andric if (isValidOffset(args[1])) 651bdd1243dSDimitry Andric applyAdrpLdr(buf, section, args[0] - sectionAddr, 652bdd1243dSDimitry Andric args[1] - sectionAddr); 653bdd1243dSDimitry Andric break; 654bdd1243dSDimitry Andric case LOH_ARM64_ADRP_LDR_GOT: 655bdd1243dSDimitry Andric if (isValidOffset(args[1])) 656bdd1243dSDimitry Andric applyAdrpLdrGot(buf, section, args[0] - sectionAddr, 657bdd1243dSDimitry Andric args[1] - sectionAddr); 65881ad6265SDimitry Andric break; 65981ad6265SDimitry Andric case LOH_ARM64_ADRP_ADD_LDR: 660bdd1243dSDimitry Andric if (isValidOffset(args[1]) && isValidOffset(args[2])) 661bdd1243dSDimitry Andric applyAdrpAddLdr(buf, section, args[0] - sectionAddr, 662bdd1243dSDimitry Andric args[1] - sectionAddr, args[2] - sectionAddr); 663753f127fSDimitry Andric break; 66481ad6265SDimitry Andric case LOH_ARM64_ADRP_LDR_GOT_LDR: 665bdd1243dSDimitry Andric if (isValidOffset(args[1]) && isValidOffset(args[2])) 666bdd1243dSDimitry Andric applyAdrpLdrGotLdr(buf, section, args[0] - sectionAddr, 667bdd1243dSDimitry Andric args[1] - sectionAddr, args[2] - sectionAddr); 668753f127fSDimitry Andric break; 66981ad6265SDimitry Andric case LOH_ARM64_ADRP_ADD_STR: 67081ad6265SDimitry Andric case LOH_ARM64_ADRP_LDR_GOT_STR: 67181ad6265SDimitry Andric // TODO: Implement these 67281ad6265SDimitry Andric break; 67381ad6265SDimitry Andric } 674bdd1243dSDimitry Andric }); 67581ad6265SDimitry Andric 676bdd1243dSDimitry Andric if (!hasAdrpAdrp) 677bdd1243dSDimitry Andric return; 678bdd1243dSDimitry Andric 679bdd1243dSDimitry Andric // AdrpAdrp optimization hints are performed in a second pass because they 680bdd1243dSDimitry Andric // might interfere with other transformations. For instance, consider the 681bdd1243dSDimitry Andric // following input: 682bdd1243dSDimitry Andric // 683bdd1243dSDimitry Andric // adrp x0, _foo@PAGE 684bdd1243dSDimitry Andric // add x1, x0, _foo@PAGEOFF 685bdd1243dSDimitry Andric // adrp x0, _bar@PAGE 686bdd1243dSDimitry Andric // add x2, x0, _bar@PAGEOFF 687bdd1243dSDimitry Andric // 688bdd1243dSDimitry Andric // If we perform the AdrpAdrp relaxation first, we get: 689bdd1243dSDimitry Andric // 690bdd1243dSDimitry Andric // adrp x0, _foo@PAGE 691bdd1243dSDimitry Andric // add x1, x0, _foo@PAGEOFF 692bdd1243dSDimitry Andric // nop 693bdd1243dSDimitry Andric // add x2, x0, _bar@PAGEOFF 694bdd1243dSDimitry Andric // 695bdd1243dSDimitry Andric // If we then apply AdrpAdd to the first two instructions, the add will have a 696bdd1243dSDimitry Andric // garbage value in x0: 697bdd1243dSDimitry Andric // 698bdd1243dSDimitry Andric // adr x1, _foo 699bdd1243dSDimitry Andric // nop 700bdd1243dSDimitry Andric // nop 701bdd1243dSDimitry Andric // add x2, x0, _bar@PAGEOFF 702bdd1243dSDimitry Andric forEachHint(data, [&](uint64_t kind, ArrayRef<uint64_t> args) { 703bdd1243dSDimitry Andric if (kind != LOH_ARM64_ADRP_ADRP) 704bdd1243dSDimitry Andric return; 705bdd1243dSDimitry Andric if (!findSection(args[0])) 706bdd1243dSDimitry Andric return; 707bdd1243dSDimitry Andric if (isValidOffset(args[1])) 708bdd1243dSDimitry Andric applyAdrpAdrp(buf, section, args[0] - sectionAddr, args[1] - sectionAddr); 709bdd1243dSDimitry Andric }); 71081ad6265SDimitry Andric } 71181ad6265SDimitry Andric 712fe6060f1SDimitry Andric TargetInfo *macho::createARM64TargetInfo() { 713fe6060f1SDimitry Andric static ARM64 t; 714fe6060f1SDimitry Andric return &t; 715fe6060f1SDimitry Andric } 716