xref: /freebsd/contrib/llvm-project/lld/MachO/Arch/ARM64.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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*0fca6ea1SDimitry Andric                             uint64_t &stubOffset, uint64_t selrefVA,
417a6dacacSDimitry Andric                             Symbol *objcMsgSend) const override;
42fe6060f1SDimitry Andric   void populateThunk(InputSection *thunk, Symbol *funcSym) override;
43bdd1243dSDimitry Andric   void applyOptimizationHints(uint8_t *, const ObjFile &) const override;
44fe6060f1SDimitry Andric };
45fe6060f1SDimitry Andric 
46fe6060f1SDimitry Andric } // namespace
47fe6060f1SDimitry Andric 
48fe6060f1SDimitry Andric // Random notes on reloc types:
49fe6060f1SDimitry Andric // ADDEND always pairs with BRANCH26, PAGE21, or PAGEOFF12
50fe6060f1SDimitry Andric // POINTER_TO_GOT: ld64 supports a 4-byte pc-relative form as well as an 8-byte
51fe6060f1SDimitry Andric // absolute version of this relocation. The semantics of the absolute relocation
52fe6060f1SDimitry Andric // are weird -- it results in the value of the GOT slot being written, instead
53fe6060f1SDimitry Andric // of the address. Let's not support it unless we find a real-world use case.
54fcaf7f86SDimitry Andric static constexpr std::array<RelocAttrs, 11> relocAttrsArray{{
55fe6060f1SDimitry Andric #define B(x) RelocAttrBits::x
56fe6060f1SDimitry Andric     {"UNSIGNED",
57fe6060f1SDimitry Andric      B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)},
58fe6060f1SDimitry Andric     {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)},
59fe6060f1SDimitry Andric     {"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
60fe6060f1SDimitry Andric     {"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)},
61fe6060f1SDimitry Andric     {"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)},
62fe6060f1SDimitry Andric     {"GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)},
63fe6060f1SDimitry Andric     {"GOT_LOAD_PAGEOFF12",
64fe6060f1SDimitry Andric      B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
65fe6060f1SDimitry Andric     {"POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
66fe6060f1SDimitry Andric     {"TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)},
67fe6060f1SDimitry Andric     {"TLVP_LOAD_PAGEOFF12",
68fe6060f1SDimitry Andric      B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
69fe6060f1SDimitry Andric     {"ADDEND", B(ADDEND)},
70fe6060f1SDimitry Andric #undef B
71fe6060f1SDimitry Andric }};
72fe6060f1SDimitry Andric 
73fe6060f1SDimitry Andric static constexpr uint32_t stubCode[] = {
74fe6060f1SDimitry Andric     0x90000010, // 00: adrp  x16, __la_symbol_ptr@page
75fe6060f1SDimitry Andric     0xf9400210, // 04: ldr   x16, [x16, __la_symbol_ptr@pageoff]
76fe6060f1SDimitry Andric     0xd61f0200, // 08: br    x16
77fe6060f1SDimitry Andric };
78fe6060f1SDimitry Andric 
writeStub(uint8_t * buf8,const Symbol & sym,uint64_t pointerVA) const79bdd1243dSDimitry Andric void ARM64::writeStub(uint8_t *buf8, const Symbol &sym,
80bdd1243dSDimitry Andric                       uint64_t pointerVA) const {
81bdd1243dSDimitry Andric   ::writeStub(buf8, stubCode, sym, pointerVA);
82fe6060f1SDimitry Andric }
83fe6060f1SDimitry Andric 
84fe6060f1SDimitry Andric static constexpr uint32_t stubHelperHeaderCode[] = {
85fe6060f1SDimitry Andric     0x90000011, // 00: adrp  x17, _dyld_private@page
86fe6060f1SDimitry Andric     0x91000231, // 04: add   x17, x17, _dyld_private@pageoff
87fe6060f1SDimitry Andric     0xa9bf47f0, // 08: stp   x16/x17, [sp, #-16]!
88fe6060f1SDimitry Andric     0x90000010, // 0c: adrp  x16, dyld_stub_binder@page
89fe6060f1SDimitry Andric     0xf9400210, // 10: ldr   x16, [x16, dyld_stub_binder@pageoff]
90fe6060f1SDimitry Andric     0xd61f0200, // 14: br    x16
91fe6060f1SDimitry Andric };
92fe6060f1SDimitry Andric 
writeStubHelperHeader(uint8_t * buf8) const93fe6060f1SDimitry Andric void ARM64::writeStubHelperHeader(uint8_t *buf8) const {
94fe6060f1SDimitry Andric   ::writeStubHelperHeader<LP64>(buf8, stubHelperHeaderCode);
95fe6060f1SDimitry Andric }
96fe6060f1SDimitry Andric 
97fe6060f1SDimitry Andric static constexpr uint32_t stubHelperEntryCode[] = {
98fe6060f1SDimitry Andric     0x18000050, // 00: ldr  w16, l0
99fe6060f1SDimitry Andric     0x14000000, // 04: b    stubHelperHeader
100fe6060f1SDimitry Andric     0x00000000, // 08: l0: .long 0
101fe6060f1SDimitry Andric };
102fe6060f1SDimitry Andric 
writeStubHelperEntry(uint8_t * buf8,const Symbol & sym,uint64_t entryVA) const10381ad6265SDimitry Andric void ARM64::writeStubHelperEntry(uint8_t *buf8, const Symbol &sym,
104fe6060f1SDimitry Andric                                  uint64_t entryVA) const {
105fe6060f1SDimitry Andric   ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA);
106fe6060f1SDimitry Andric }
107fe6060f1SDimitry Andric 
108bdd1243dSDimitry Andric static constexpr uint32_t objcStubsFastCode[] = {
109bdd1243dSDimitry Andric     0x90000001, // adrp  x1, __objc_selrefs@page
110bdd1243dSDimitry Andric     0xf9400021, // ldr   x1, [x1, @selector("foo")@pageoff]
111bdd1243dSDimitry Andric     0x90000010, // adrp  x16, _got@page
112bdd1243dSDimitry Andric     0xf9400210, // ldr   x16, [x16, _objc_msgSend@pageoff]
113bdd1243dSDimitry Andric     0xd61f0200, // br    x16
114bdd1243dSDimitry Andric     0xd4200020, // brk   #0x1
115bdd1243dSDimitry Andric     0xd4200020, // brk   #0x1
116bdd1243dSDimitry Andric     0xd4200020, // brk   #0x1
117bdd1243dSDimitry Andric };
118bdd1243dSDimitry Andric 
1197a6dacacSDimitry Andric static constexpr uint32_t objcStubsSmallCode[] = {
1207a6dacacSDimitry Andric     0x90000001, // adrp  x1, __objc_selrefs@page
1217a6dacacSDimitry Andric     0xf9400021, // ldr   x1, [x1, @selector("foo")@pageoff]
1227a6dacacSDimitry Andric     0x14000000, // b     _objc_msgSend
1237a6dacacSDimitry Andric };
1247a6dacacSDimitry Andric 
writeObjCMsgSendStub(uint8_t * buf,Symbol * sym,uint64_t stubsAddr,uint64_t & stubOffset,uint64_t selrefVA,Symbol * objcMsgSend) const125bdd1243dSDimitry Andric void ARM64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
126*0fca6ea1SDimitry Andric                                  uint64_t &stubOffset, uint64_t selrefVA,
1277a6dacacSDimitry Andric                                  Symbol *objcMsgSend) const {
1287a6dacacSDimitry Andric   uint64_t objcMsgSendAddr;
1297a6dacacSDimitry Andric   uint64_t objcStubSize;
1307a6dacacSDimitry Andric   uint64_t objcMsgSendIndex;
1317a6dacacSDimitry Andric 
1327a6dacacSDimitry Andric   if (config->objcStubsMode == ObjCStubsMode::fast) {
1337a6dacacSDimitry Andric     objcStubSize = target->objcStubsFastSize;
1347a6dacacSDimitry Andric     objcMsgSendAddr = in.got->addr;
1357a6dacacSDimitry Andric     objcMsgSendIndex = objcMsgSend->gotIndex;
1367a6dacacSDimitry Andric     ::writeObjCMsgSendFastStub<LP64>(buf, objcStubsFastCode, sym, stubsAddr,
137*0fca6ea1SDimitry Andric                                      stubOffset, selrefVA, objcMsgSendAddr,
138*0fca6ea1SDimitry Andric                                      objcMsgSendIndex);
1397a6dacacSDimitry Andric   } else {
1407a6dacacSDimitry Andric     assert(config->objcStubsMode == ObjCStubsMode::small);
1417a6dacacSDimitry Andric     objcStubSize = target->objcStubsSmallSize;
1427a6dacacSDimitry Andric     if (auto *d = dyn_cast<Defined>(objcMsgSend)) {
1437a6dacacSDimitry Andric       objcMsgSendAddr = d->getVA();
1447a6dacacSDimitry Andric       objcMsgSendIndex = 0;
1457a6dacacSDimitry Andric     } else {
1467a6dacacSDimitry Andric       objcMsgSendAddr = in.stubs->addr;
1477a6dacacSDimitry Andric       objcMsgSendIndex = objcMsgSend->stubsIndex;
1487a6dacacSDimitry Andric     }
1497a6dacacSDimitry Andric     ::writeObjCMsgSendSmallStub<LP64>(buf, objcStubsSmallCode, sym, stubsAddr,
150*0fca6ea1SDimitry Andric                                       stubOffset, selrefVA, objcMsgSendAddr,
151*0fca6ea1SDimitry Andric                                       objcMsgSendIndex);
1527a6dacacSDimitry Andric   }
1537a6dacacSDimitry Andric   stubOffset += objcStubSize;
154bdd1243dSDimitry Andric }
155bdd1243dSDimitry Andric 
156fe6060f1SDimitry Andric // A thunk is the relaxed variation of stubCode. We don't need the
157fe6060f1SDimitry Andric // extra indirection through a lazy pointer because the target address
158fe6060f1SDimitry Andric // is known at link time.
159fe6060f1SDimitry Andric static constexpr uint32_t thunkCode[] = {
160fe6060f1SDimitry Andric     0x90000010, // 00: adrp  x16, <thunk.ptr>@page
161fe6060f1SDimitry Andric     0x91000210, // 04: add   x16, [x16,<thunk.ptr>@pageoff]
162fe6060f1SDimitry Andric     0xd61f0200, // 08: br    x16
163fe6060f1SDimitry Andric };
164fe6060f1SDimitry Andric 
populateThunk(InputSection * thunk,Symbol * funcSym)165fe6060f1SDimitry Andric void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym) {
166fe6060f1SDimitry Andric   thunk->align = 4;
167fe6060f1SDimitry Andric   thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode),
168fe6060f1SDimitry Andric                  sizeof(thunkCode)};
16906c3fb27SDimitry Andric   thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGEOFF12,
170fe6060f1SDimitry Andric                              /*pcrel=*/false, /*length=*/2,
171fe6060f1SDimitry Andric                              /*offset=*/4, /*addend=*/0,
17206c3fb27SDimitry Andric                              /*referent=*/funcSym);
17306c3fb27SDimitry Andric   thunk->relocs.emplace_back(/*type=*/ARM64_RELOC_PAGE21,
174fe6060f1SDimitry Andric                              /*pcrel=*/true, /*length=*/2,
175fe6060f1SDimitry Andric                              /*offset=*/0, /*addend=*/0,
17606c3fb27SDimitry Andric                              /*referent=*/funcSym);
177fe6060f1SDimitry Andric }
178fe6060f1SDimitry Andric 
ARM64()179fe6060f1SDimitry Andric ARM64::ARM64() : ARM64Common(LP64()) {
180fe6060f1SDimitry Andric   cpuType = CPU_TYPE_ARM64;
181fe6060f1SDimitry Andric   cpuSubtype = CPU_SUBTYPE_ARM64_ALL;
182fe6060f1SDimitry Andric 
183fe6060f1SDimitry Andric   stubSize = sizeof(stubCode);
184fe6060f1SDimitry Andric   thunkSize = sizeof(thunkCode);
185349cc55cSDimitry Andric 
186bdd1243dSDimitry Andric   objcStubsFastSize = sizeof(objcStubsFastCode);
1877a6dacacSDimitry Andric   objcStubsFastAlignment = 32;
1887a6dacacSDimitry Andric   objcStubsSmallSize = sizeof(objcStubsSmallCode);
1897a6dacacSDimitry Andric   objcStubsSmallAlignment = 4;
190bdd1243dSDimitry Andric 
191349cc55cSDimitry Andric   // Branch immediate is two's complement 26 bits, which is implicitly
192349cc55cSDimitry Andric   // multiplied by 4 (since all functions are 4-aligned: The branch range
193349cc55cSDimitry Andric   // is -4*(2**(26-1))..4*(2**(26-1) - 1).
194349cc55cSDimitry Andric   backwardBranchRange = 128 * 1024 * 1024;
195349cc55cSDimitry Andric   forwardBranchRange = backwardBranchRange - 4;
196349cc55cSDimitry Andric 
19781ad6265SDimitry Andric   modeDwarfEncoding = UNWIND_ARM64_MODE_DWARF;
19881ad6265SDimitry Andric   subtractorRelocType = ARM64_RELOC_SUBTRACTOR;
19981ad6265SDimitry Andric   unsignedRelocType = ARM64_RELOC_UNSIGNED;
20081ad6265SDimitry Andric 
201fe6060f1SDimitry Andric   stubHelperHeaderSize = sizeof(stubHelperHeaderCode);
202fe6060f1SDimitry Andric   stubHelperEntrySize = sizeof(stubHelperEntryCode);
203fcaf7f86SDimitry Andric 
204fcaf7f86SDimitry Andric   relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()};
205fe6060f1SDimitry Andric }
206fe6060f1SDimitry Andric 
20781ad6265SDimitry Andric namespace {
20881ad6265SDimitry Andric struct Adrp {
20981ad6265SDimitry Andric   uint32_t destRegister;
210bdd1243dSDimitry Andric   int64_t addend;
21181ad6265SDimitry Andric };
21281ad6265SDimitry Andric 
21381ad6265SDimitry Andric struct Add {
21481ad6265SDimitry Andric   uint8_t destRegister;
21581ad6265SDimitry Andric   uint8_t srcRegister;
21681ad6265SDimitry Andric   uint32_t addend;
21781ad6265SDimitry Andric };
21881ad6265SDimitry Andric 
21981ad6265SDimitry Andric enum ExtendType { ZeroExtend = 1, Sign64 = 2, Sign32 = 3 };
22081ad6265SDimitry Andric 
22181ad6265SDimitry Andric struct Ldr {
22281ad6265SDimitry Andric   uint8_t destRegister;
22381ad6265SDimitry Andric   uint8_t baseRegister;
224753f127fSDimitry Andric   uint8_t p2Size;
22581ad6265SDimitry Andric   bool isFloat;
22681ad6265SDimitry Andric   ExtendType extendType;
227753f127fSDimitry Andric   int64_t offset;
22881ad6265SDimitry Andric };
22981ad6265SDimitry Andric } // namespace
23081ad6265SDimitry Andric 
parseAdrp(uint32_t insn,Adrp & adrp)23181ad6265SDimitry Andric static bool parseAdrp(uint32_t insn, Adrp &adrp) {
23281ad6265SDimitry Andric   if ((insn & 0x9f000000) != 0x90000000)
23381ad6265SDimitry Andric     return false;
23481ad6265SDimitry Andric   adrp.destRegister = insn & 0x1f;
235bdd1243dSDimitry Andric   uint64_t immHi = (insn >> 5) & 0x7ffff;
236bdd1243dSDimitry Andric   uint64_t immLo = (insn >> 29) & 0x3;
237bdd1243dSDimitry Andric   adrp.addend = SignExtend64<21>(immLo | (immHi << 2)) * 4096;
23881ad6265SDimitry Andric   return true;
23981ad6265SDimitry Andric }
24081ad6265SDimitry Andric 
parseAdd(uint32_t insn,Add & add)24181ad6265SDimitry Andric static bool parseAdd(uint32_t insn, Add &add) {
24281ad6265SDimitry Andric   if ((insn & 0xffc00000) != 0x91000000)
24381ad6265SDimitry Andric     return false;
24481ad6265SDimitry Andric   add.destRegister = insn & 0x1f;
24581ad6265SDimitry Andric   add.srcRegister = (insn >> 5) & 0x1f;
24681ad6265SDimitry Andric   add.addend = (insn >> 10) & 0xfff;
24781ad6265SDimitry Andric   return true;
24881ad6265SDimitry Andric }
24981ad6265SDimitry Andric 
parseLdr(uint32_t insn,Ldr & ldr)25081ad6265SDimitry Andric static bool parseLdr(uint32_t insn, Ldr &ldr) {
25181ad6265SDimitry Andric   ldr.destRegister = insn & 0x1f;
25281ad6265SDimitry Andric   ldr.baseRegister = (insn >> 5) & 0x1f;
25381ad6265SDimitry Andric   uint8_t size = insn >> 30;
25481ad6265SDimitry Andric   uint8_t opc = (insn >> 22) & 3;
25581ad6265SDimitry Andric 
25681ad6265SDimitry Andric   if ((insn & 0x3fc00000) == 0x39400000) {
25781ad6265SDimitry Andric     // LDR (immediate), LDRB (immediate), LDRH (immediate)
258753f127fSDimitry Andric     ldr.p2Size = size;
25981ad6265SDimitry Andric     ldr.extendType = ZeroExtend;
26081ad6265SDimitry Andric     ldr.isFloat = false;
26181ad6265SDimitry Andric   } else if ((insn & 0x3f800000) == 0x39800000) {
26281ad6265SDimitry Andric     // LDRSB (immediate), LDRSH (immediate), LDRSW (immediate)
263753f127fSDimitry Andric     ldr.p2Size = size;
26481ad6265SDimitry Andric     ldr.extendType = static_cast<ExtendType>(opc);
26581ad6265SDimitry Andric     ldr.isFloat = false;
26681ad6265SDimitry Andric   } else if ((insn & 0x3f400000) == 0x3d400000) {
26781ad6265SDimitry Andric     // LDR (immediate, SIMD&FP)
26881ad6265SDimitry Andric     ldr.extendType = ZeroExtend;
26981ad6265SDimitry Andric     ldr.isFloat = true;
270753f127fSDimitry Andric     if (opc == 1)
271753f127fSDimitry Andric       ldr.p2Size = size;
27281ad6265SDimitry Andric     else if (size == 0 && opc == 3)
273753f127fSDimitry Andric       ldr.p2Size = 4;
27481ad6265SDimitry Andric     else
27581ad6265SDimitry Andric       return false;
27681ad6265SDimitry Andric   } else {
27781ad6265SDimitry Andric     return false;
27881ad6265SDimitry Andric   }
279753f127fSDimitry Andric   ldr.offset = ((insn >> 10) & 0xfff) << ldr.p2Size;
28081ad6265SDimitry Andric   return true;
28181ad6265SDimitry Andric }
28281ad6265SDimitry Andric 
isValidAdrOffset(int32_t delta)283753f127fSDimitry Andric static bool isValidAdrOffset(int32_t delta) { return isInt<21>(delta); }
284753f127fSDimitry Andric 
writeAdr(void * loc,uint32_t dest,int32_t delta)28581ad6265SDimitry Andric static void writeAdr(void *loc, uint32_t dest, int32_t delta) {
286753f127fSDimitry Andric   assert(isValidAdrOffset(delta));
28781ad6265SDimitry Andric   uint32_t opcode = 0x10000000;
28881ad6265SDimitry Andric   uint32_t immHi = (delta & 0x001ffffc) << 3;
28981ad6265SDimitry Andric   uint32_t immLo = (delta & 0x00000003) << 29;
29081ad6265SDimitry Andric   write32le(loc, opcode | immHi | immLo | dest);
29181ad6265SDimitry Andric }
29281ad6265SDimitry Andric 
writeNop(void * loc)29381ad6265SDimitry Andric static void writeNop(void *loc) { write32le(loc, 0xd503201f); }
29481ad6265SDimitry Andric 
isLiteralLdrEligible(const Ldr & ldr)295753f127fSDimitry Andric static bool isLiteralLdrEligible(const Ldr &ldr) {
296753f127fSDimitry Andric   return ldr.p2Size > 1 && isShiftedInt<19, 2>(ldr.offset);
297753f127fSDimitry Andric }
298753f127fSDimitry Andric 
writeLiteralLdr(void * loc,const Ldr & ldr)299753f127fSDimitry Andric static void writeLiteralLdr(void *loc, const Ldr &ldr) {
300753f127fSDimitry Andric   assert(isLiteralLdrEligible(ldr));
301753f127fSDimitry Andric   uint32_t imm19 = (ldr.offset / 4 & maskTrailingOnes<uint32_t>(19)) << 5;
302753f127fSDimitry Andric   uint32_t opcode;
303753f127fSDimitry Andric   switch (ldr.p2Size) {
304753f127fSDimitry Andric   case 2:
305753f127fSDimitry Andric     if (ldr.isFloat)
30681ad6265SDimitry Andric       opcode = 0x1c000000;
30781ad6265SDimitry Andric     else
308753f127fSDimitry Andric       opcode = ldr.extendType == Sign64 ? 0x98000000 : 0x18000000;
30981ad6265SDimitry Andric     break;
310753f127fSDimitry Andric   case 3:
311753f127fSDimitry Andric     opcode = ldr.isFloat ? 0x5c000000 : 0x58000000;
31281ad6265SDimitry Andric     break;
313753f127fSDimitry Andric   case 4:
31481ad6265SDimitry Andric     opcode = 0x9c000000;
31581ad6265SDimitry Andric     break;
31681ad6265SDimitry Andric   default:
317753f127fSDimitry Andric     llvm_unreachable("Invalid literal ldr size");
31881ad6265SDimitry Andric   }
319753f127fSDimitry Andric   write32le(loc, opcode | imm19 | ldr.destRegister);
320753f127fSDimitry Andric }
321753f127fSDimitry Andric 
isImmediateLdrEligible(const Ldr & ldr)322753f127fSDimitry Andric static bool isImmediateLdrEligible(const Ldr &ldr) {
323753f127fSDimitry Andric   // Note: We deviate from ld64's behavior, which converts to immediate loads
324753f127fSDimitry Andric   // only if ldr.offset < 4096, even though the offset is divided by the load's
325753f127fSDimitry Andric   // size in the 12-bit immediate operand. Only the unsigned offset variant is
326753f127fSDimitry Andric   // supported.
327753f127fSDimitry Andric 
328753f127fSDimitry Andric   uint32_t size = 1 << ldr.p2Size;
329753f127fSDimitry Andric   return ldr.offset >= 0 && (ldr.offset % size) == 0 &&
330753f127fSDimitry Andric          isUInt<12>(ldr.offset >> ldr.p2Size);
331753f127fSDimitry Andric }
332753f127fSDimitry Andric 
writeImmediateLdr(void * loc,const Ldr & ldr)333753f127fSDimitry Andric static void writeImmediateLdr(void *loc, const Ldr &ldr) {
334753f127fSDimitry Andric   assert(isImmediateLdrEligible(ldr));
335753f127fSDimitry Andric   uint32_t opcode = 0x39000000;
336753f127fSDimitry Andric   if (ldr.isFloat) {
337753f127fSDimitry Andric     opcode |= 0x04000000;
338753f127fSDimitry Andric     assert(ldr.extendType == ZeroExtend);
339753f127fSDimitry Andric   }
340753f127fSDimitry Andric   opcode |= ldr.destRegister;
341753f127fSDimitry Andric   opcode |= ldr.baseRegister << 5;
342753f127fSDimitry Andric   uint8_t size, opc;
343753f127fSDimitry Andric   if (ldr.p2Size == 4) {
344753f127fSDimitry Andric     size = 0;
345753f127fSDimitry Andric     opc = 3;
346753f127fSDimitry Andric   } else {
347753f127fSDimitry Andric     opc = ldr.extendType;
348753f127fSDimitry Andric     size = ldr.p2Size;
349753f127fSDimitry Andric   }
350753f127fSDimitry Andric   uint32_t immBits = ldr.offset >> ldr.p2Size;
351753f127fSDimitry Andric   write32le(loc, opcode | (immBits << 10) | (opc << 22) | (size << 30));
35281ad6265SDimitry Andric }
35381ad6265SDimitry Andric 
35481ad6265SDimitry Andric // Transforms a pair of adrp+add instructions into an adr instruction if the
35581ad6265SDimitry Andric // target is within the +/- 1 MiB range allowed by the adr's 21 bit signed
35681ad6265SDimitry Andric // immediate offset.
35781ad6265SDimitry Andric //
35881ad6265SDimitry Andric //   adrp xN, _foo@PAGE
35981ad6265SDimitry Andric //   add  xM, xN, _foo@PAGEOFF
36081ad6265SDimitry Andric // ->
36181ad6265SDimitry Andric //   adr  xM, _foo
36281ad6265SDimitry Andric //   nop
applyAdrpAdd(uint8_t * buf,const ConcatInputSection * isec,uint64_t offset1,uint64_t offset2)363bdd1243dSDimitry Andric static void applyAdrpAdd(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 adrp;
36881ad6265SDimitry Andric   Add add;
369bdd1243dSDimitry Andric   if (!parseAdrp(ins1, adrp) || !parseAdd(ins2, add))
37081ad6265SDimitry Andric     return;
37181ad6265SDimitry Andric   if (adrp.destRegister != add.srcRegister)
37281ad6265SDimitry Andric     return;
37381ad6265SDimitry Andric 
374bdd1243dSDimitry Andric   uint64_t addr1 = isec->getVA() + offset1;
375bdd1243dSDimitry Andric   uint64_t referent = pageBits(addr1) + adrp.addend + add.addend;
376bdd1243dSDimitry Andric   int64_t delta = referent - addr1;
377753f127fSDimitry Andric   if (!isValidAdrOffset(delta))
37881ad6265SDimitry Andric     return;
37981ad6265SDimitry Andric 
380bdd1243dSDimitry Andric   writeAdr(buf + offset1, add.destRegister, delta);
381bdd1243dSDimitry Andric   writeNop(buf + offset2);
38281ad6265SDimitry Andric }
38381ad6265SDimitry Andric 
38481ad6265SDimitry Andric // Transforms two adrp instructions into a single adrp if their referent
38581ad6265SDimitry Andric // addresses are located on the same 4096 byte page.
38681ad6265SDimitry Andric //
38781ad6265SDimitry Andric //   adrp xN, _foo@PAGE
38881ad6265SDimitry Andric //   adrp xN, _bar@PAGE
38981ad6265SDimitry Andric // ->
39081ad6265SDimitry Andric //   adrp xN, _foo@PAGE
39181ad6265SDimitry Andric //   nop
applyAdrpAdrp(uint8_t * buf,const ConcatInputSection * isec,uint64_t offset1,uint64_t offset2)392bdd1243dSDimitry Andric static void applyAdrpAdrp(uint8_t *buf, const ConcatInputSection *isec,
393bdd1243dSDimitry Andric                           uint64_t offset1, uint64_t offset2) {
394bdd1243dSDimitry Andric   uint32_t ins1 = read32le(buf + offset1);
395bdd1243dSDimitry Andric   uint32_t ins2 = read32le(buf + offset2);
39681ad6265SDimitry Andric   Adrp adrp1, adrp2;
39781ad6265SDimitry Andric   if (!parseAdrp(ins1, adrp1) || !parseAdrp(ins2, adrp2))
39881ad6265SDimitry Andric     return;
39981ad6265SDimitry Andric   if (adrp1.destRegister != adrp2.destRegister)
40081ad6265SDimitry Andric     return;
40181ad6265SDimitry Andric 
402bdd1243dSDimitry Andric   uint64_t page1 = pageBits(offset1 + isec->getVA()) + adrp1.addend;
403bdd1243dSDimitry Andric   uint64_t page2 = pageBits(offset2 + isec->getVA()) + adrp2.addend;
404bdd1243dSDimitry Andric   if (page1 != page2)
40581ad6265SDimitry Andric     return;
40681ad6265SDimitry Andric 
407bdd1243dSDimitry Andric   writeNop(buf + offset2);
40881ad6265SDimitry Andric }
40981ad6265SDimitry Andric 
41081ad6265SDimitry Andric // Transforms a pair of adrp+ldr (immediate) instructions into an ldr (literal)
41181ad6265SDimitry Andric // load from a PC-relative address if it is 4-byte aligned and within +/- 1 MiB,
41281ad6265SDimitry Andric // as ldr can encode a signed 19-bit offset that gets multiplied by 4.
41381ad6265SDimitry Andric //
41481ad6265SDimitry Andric //   adrp xN, _foo@PAGE
41581ad6265SDimitry Andric //   ldr  xM, [xN, _foo@PAGEOFF]
41681ad6265SDimitry Andric // ->
41781ad6265SDimitry Andric //   nop
41881ad6265SDimitry Andric //   ldr  xM, _foo
applyAdrpLdr(uint8_t * buf,const ConcatInputSection * isec,uint64_t offset1,uint64_t offset2)419bdd1243dSDimitry Andric static void applyAdrpLdr(uint8_t *buf, const ConcatInputSection *isec,
420bdd1243dSDimitry Andric                          uint64_t offset1, uint64_t offset2) {
421bdd1243dSDimitry Andric   uint32_t ins1 = read32le(buf + offset1);
422bdd1243dSDimitry Andric   uint32_t ins2 = read32le(buf + offset2);
42381ad6265SDimitry Andric   Adrp adrp;
42481ad6265SDimitry Andric   Ldr ldr;
425bdd1243dSDimitry Andric   if (!parseAdrp(ins1, adrp) || !parseLdr(ins2, ldr))
42681ad6265SDimitry Andric     return;
42781ad6265SDimitry Andric   if (adrp.destRegister != ldr.baseRegister)
42881ad6265SDimitry Andric     return;
42981ad6265SDimitry Andric 
430bdd1243dSDimitry Andric   uint64_t addr1 = isec->getVA() + offset1;
431bdd1243dSDimitry Andric   uint64_t addr2 = isec->getVA() + offset2;
432bdd1243dSDimitry Andric   uint64_t referent = pageBits(addr1) + adrp.addend + ldr.offset;
433bdd1243dSDimitry Andric   ldr.offset = referent - addr2;
434753f127fSDimitry Andric   if (!isLiteralLdrEligible(ldr))
43581ad6265SDimitry Andric     return;
43681ad6265SDimitry Andric 
437bdd1243dSDimitry Andric   writeNop(buf + offset1);
438bdd1243dSDimitry Andric   writeLiteralLdr(buf + offset2, ldr);
439753f127fSDimitry Andric }
440753f127fSDimitry Andric 
441753f127fSDimitry Andric // GOT loads are emitted by the compiler as a pair of adrp and ldr instructions,
442753f127fSDimitry Andric // but they may be changed to adrp+add by relaxGotLoad(). This hint performs
443753f127fSDimitry Andric // the AdrpLdr or AdrpAdd transformation depending on whether it was relaxed.
applyAdrpLdrGot(uint8_t * buf,const ConcatInputSection * isec,uint64_t offset1,uint64_t offset2)444bdd1243dSDimitry Andric static void applyAdrpLdrGot(uint8_t *buf, const ConcatInputSection *isec,
445bdd1243dSDimitry Andric                             uint64_t offset1, uint64_t offset2) {
446bdd1243dSDimitry Andric   uint32_t ins2 = read32le(buf + offset2);
447753f127fSDimitry Andric   Add add;
448753f127fSDimitry Andric   Ldr ldr;
449753f127fSDimitry Andric   if (parseAdd(ins2, add))
450bdd1243dSDimitry Andric     applyAdrpAdd(buf, isec, offset1, offset2);
451753f127fSDimitry Andric   else if (parseLdr(ins2, ldr))
452bdd1243dSDimitry Andric     applyAdrpLdr(buf, isec, offset1, offset2);
453753f127fSDimitry Andric }
454753f127fSDimitry Andric 
455bdd1243dSDimitry Andric // Optimizes an adrp+add+ldr sequence used for loading from a local symbol's
456bdd1243dSDimitry Andric // address by loading directly if it's close enough, or to an adrp(p)+ldr
457bdd1243dSDimitry Andric // sequence if it's not.
458bdd1243dSDimitry Andric //
459753f127fSDimitry Andric //   adrp x0, _foo@PAGE
460753f127fSDimitry Andric //   add  x1, x0, _foo@PAGEOFF
461753f127fSDimitry Andric //   ldr  x2, [x1, #off]
applyAdrpAddLdr(uint8_t * buf,const ConcatInputSection * isec,uint64_t offset1,uint64_t offset2,uint64_t offset3)462bdd1243dSDimitry Andric static void applyAdrpAddLdr(uint8_t *buf, const ConcatInputSection *isec,
463bdd1243dSDimitry Andric                             uint64_t offset1, uint64_t offset2,
464bdd1243dSDimitry Andric                             uint64_t offset3) {
465bdd1243dSDimitry Andric   uint32_t ins1 = read32le(buf + offset1);
466bdd1243dSDimitry Andric   Adrp adrp;
467bdd1243dSDimitry Andric   if (!parseAdrp(ins1, adrp))
468753f127fSDimitry Andric     return;
469bdd1243dSDimitry Andric   uint32_t ins2 = read32le(buf + offset2);
470bdd1243dSDimitry Andric   Add add;
471bdd1243dSDimitry Andric   if (!parseAdd(ins2, add))
472bdd1243dSDimitry Andric     return;
473bdd1243dSDimitry Andric   uint32_t ins3 = read32le(buf + offset3);
474bdd1243dSDimitry Andric   Ldr ldr;
475bdd1243dSDimitry Andric   if (!parseLdr(ins3, ldr))
476bdd1243dSDimitry Andric     return;
477bdd1243dSDimitry Andric   if (adrp.destRegister != add.srcRegister)
478bdd1243dSDimitry Andric     return;
479bdd1243dSDimitry Andric   if (add.destRegister != ldr.baseRegister)
480753f127fSDimitry Andric     return;
481753f127fSDimitry Andric 
482753f127fSDimitry Andric   // Load from the target address directly.
483753f127fSDimitry Andric   //   nop
484753f127fSDimitry Andric   //   nop
485753f127fSDimitry Andric   //   ldr x2, [_foo + #off]
486bdd1243dSDimitry Andric   uint64_t addr1 = isec->getVA() + offset1;
487bdd1243dSDimitry Andric   uint64_t addr3 = isec->getVA() + offset3;
488bdd1243dSDimitry Andric   uint64_t referent = pageBits(addr1) + adrp.addend + add.addend;
489bdd1243dSDimitry Andric   Ldr literalLdr = ldr;
490bdd1243dSDimitry Andric   literalLdr.offset += referent - addr3;
491753f127fSDimitry Andric   if (isLiteralLdrEligible(literalLdr)) {
492bdd1243dSDimitry Andric     writeNop(buf + offset1);
493bdd1243dSDimitry Andric     writeNop(buf + offset2);
494bdd1243dSDimitry Andric     writeLiteralLdr(buf + offset3, literalLdr);
495753f127fSDimitry Andric     return;
496753f127fSDimitry Andric   }
497753f127fSDimitry Andric 
498753f127fSDimitry Andric   // Load the target address into a register and load from there indirectly.
499753f127fSDimitry Andric   //   adr x1, _foo
500753f127fSDimitry Andric   //   nop
501753f127fSDimitry Andric   //   ldr x2, [x1, #off]
502bdd1243dSDimitry Andric   int64_t adrOffset = referent - addr1;
503753f127fSDimitry Andric   if (isValidAdrOffset(adrOffset)) {
504bdd1243dSDimitry Andric     writeAdr(buf + offset1, ldr.baseRegister, adrOffset);
505bdd1243dSDimitry Andric     // Note: ld64 moves the offset into the adr instruction for AdrpAddLdr, but
506bdd1243dSDimitry Andric     // not for AdrpLdrGotLdr. Its effect is the same either way.
507bdd1243dSDimitry Andric     writeNop(buf + offset2);
508753f127fSDimitry Andric     return;
509753f127fSDimitry Andric   }
510753f127fSDimitry Andric 
511753f127fSDimitry Andric   // Move the target's page offset into the ldr's immediate offset.
512753f127fSDimitry Andric   //   adrp x0, _foo@PAGE
513753f127fSDimitry Andric   //   nop
514753f127fSDimitry Andric   //   ldr x2, [x0, _foo@PAGEOFF + #off]
515bdd1243dSDimitry Andric   Ldr immediateLdr = ldr;
516753f127fSDimitry Andric   immediateLdr.baseRegister = adrp.destRegister;
517bdd1243dSDimitry Andric   immediateLdr.offset += add.addend;
518753f127fSDimitry Andric   if (isImmediateLdrEligible(immediateLdr)) {
519bdd1243dSDimitry Andric     writeNop(buf + offset2);
520bdd1243dSDimitry Andric     writeImmediateLdr(buf + offset3, immediateLdr);
521753f127fSDimitry Andric     return;
522753f127fSDimitry Andric   }
523bdd1243dSDimitry Andric }
524bdd1243dSDimitry Andric 
525bdd1243dSDimitry Andric // Relaxes a GOT-indirect load.
526bdd1243dSDimitry Andric // If the referenced symbol is external and its GOT entry is within +/- 1 MiB,
527bdd1243dSDimitry Andric // the GOT entry can be loaded with a single literal ldr instruction.
528bdd1243dSDimitry Andric // If the referenced symbol is local and thus has been relaxed to adrp+add+ldr,
529bdd1243dSDimitry Andric // we perform the AdrpAddLdr transformation.
applyAdrpLdrGotLdr(uint8_t * buf,const ConcatInputSection * isec,uint64_t offset1,uint64_t offset2,uint64_t offset3)530bdd1243dSDimitry Andric static void applyAdrpLdrGotLdr(uint8_t *buf, const ConcatInputSection *isec,
531bdd1243dSDimitry Andric                                uint64_t offset1, uint64_t offset2,
532bdd1243dSDimitry Andric                                uint64_t offset3) {
533bdd1243dSDimitry Andric   uint32_t ins2 = read32le(buf + offset2);
534bdd1243dSDimitry Andric   Add add;
535bdd1243dSDimitry Andric   Ldr ldr2;
536bdd1243dSDimitry Andric 
537bdd1243dSDimitry Andric   if (parseAdd(ins2, add)) {
538bdd1243dSDimitry Andric     applyAdrpAddLdr(buf, isec, offset1, offset2, offset3);
539753f127fSDimitry Andric   } else if (parseLdr(ins2, ldr2)) {
540753f127fSDimitry Andric     // adrp x1, _foo@GOTPAGE
541753f127fSDimitry Andric     // ldr  x2, [x1, _foo@GOTPAGEOFF]
542753f127fSDimitry Andric     // ldr  x3, [x2, #off]
543bdd1243dSDimitry Andric 
544bdd1243dSDimitry Andric     uint32_t ins1 = read32le(buf + offset1);
545bdd1243dSDimitry Andric     Adrp adrp;
546bdd1243dSDimitry Andric     if (!parseAdrp(ins1, adrp))
547bdd1243dSDimitry Andric       return;
548bdd1243dSDimitry Andric     uint32_t ins3 = read32le(buf + offset3);
549bdd1243dSDimitry Andric     Ldr ldr3;
550bdd1243dSDimitry Andric     if (!parseLdr(ins3, ldr3))
551bdd1243dSDimitry Andric       return;
552bdd1243dSDimitry Andric 
553753f127fSDimitry Andric     if (ldr2.baseRegister != adrp.destRegister)
554753f127fSDimitry Andric       return;
555753f127fSDimitry Andric     if (ldr3.baseRegister != ldr2.destRegister)
556753f127fSDimitry Andric       return;
557753f127fSDimitry Andric     // Loads from the GOT must be pointer sized.
558753f127fSDimitry Andric     if (ldr2.p2Size != 3 || ldr2.isFloat)
559753f127fSDimitry Andric       return;
560753f127fSDimitry Andric 
561bdd1243dSDimitry Andric     uint64_t addr1 = isec->getVA() + offset1;
562bdd1243dSDimitry Andric     uint64_t addr2 = isec->getVA() + offset2;
563bdd1243dSDimitry Andric     uint64_t referent = pageBits(addr1) + adrp.addend + ldr2.offset;
564753f127fSDimitry Andric     // Load the GOT entry's address directly.
565753f127fSDimitry Andric     //   nop
566753f127fSDimitry Andric     //   ldr x2, _foo@GOTPAGE + _foo@GOTPAGEOFF
567753f127fSDimitry Andric     //   ldr x3, [x2, #off]
568753f127fSDimitry Andric     Ldr literalLdr = ldr2;
569bdd1243dSDimitry Andric     literalLdr.offset = referent - addr2;
570753f127fSDimitry Andric     if (isLiteralLdrEligible(literalLdr)) {
571bdd1243dSDimitry Andric       writeNop(buf + offset1);
572bdd1243dSDimitry Andric       writeLiteralLdr(buf + offset2, literalLdr);
573753f127fSDimitry Andric     }
574753f127fSDimitry Andric   }
57581ad6265SDimitry Andric }
57681ad6265SDimitry Andric 
readValue(const uint8_t * & ptr,const uint8_t * end)577bdd1243dSDimitry Andric static uint64_t readValue(const uint8_t *&ptr, const uint8_t *end) {
578bdd1243dSDimitry Andric   unsigned int n = 0;
579bdd1243dSDimitry Andric   uint64_t value = decodeULEB128(ptr, &n, end);
580bdd1243dSDimitry Andric   ptr += n;
581bdd1243dSDimitry Andric   return value;
582bdd1243dSDimitry Andric }
58381ad6265SDimitry Andric 
584bdd1243dSDimitry Andric template <typename Callback>
forEachHint(ArrayRef<uint8_t> data,Callback callback)585bdd1243dSDimitry Andric static void forEachHint(ArrayRef<uint8_t> data, Callback callback) {
586bdd1243dSDimitry Andric   std::array<uint64_t, 3> args;
58781ad6265SDimitry Andric 
588bdd1243dSDimitry Andric   for (const uint8_t *p = data.begin(), *end = data.end(); p < end;) {
589bdd1243dSDimitry Andric     uint64_t type = readValue(p, end);
590bdd1243dSDimitry Andric     if (type == 0)
591bdd1243dSDimitry Andric       break;
592bdd1243dSDimitry Andric 
593bdd1243dSDimitry Andric     uint64_t argCount = readValue(p, end);
594bdd1243dSDimitry Andric     // All known LOH types as of 2022-09 have 3 or fewer arguments; skip others.
595bdd1243dSDimitry Andric     if (argCount > 3) {
596bdd1243dSDimitry Andric       for (unsigned i = 0; i < argCount; ++i)
597bdd1243dSDimitry Andric         readValue(p, end);
598bdd1243dSDimitry Andric       continue;
599bdd1243dSDimitry Andric     }
600bdd1243dSDimitry Andric 
601bdd1243dSDimitry Andric     for (unsigned i = 0; i < argCount; ++i)
602bdd1243dSDimitry Andric       args[i] = readValue(p, end);
603bdd1243dSDimitry Andric     callback(type, ArrayRef<uint64_t>(args.data(), argCount));
604bdd1243dSDimitry Andric   }
605bdd1243dSDimitry Andric }
606bdd1243dSDimitry Andric 
607bdd1243dSDimitry Andric // On RISC architectures like arm64, materializing a memory address generally
608bdd1243dSDimitry Andric // takes multiple instructions. If the referenced symbol is located close enough
609bdd1243dSDimitry Andric // in memory, fewer instructions are needed.
610bdd1243dSDimitry Andric //
611bdd1243dSDimitry Andric // Linker optimization hints record where addresses are computed. After
612bdd1243dSDimitry Andric // addresses have been assigned, if possible, we change them to a shorter
613bdd1243dSDimitry Andric // sequence of instructions. The size of the binary is not modified; the
614bdd1243dSDimitry Andric // eliminated instructions are replaced with NOPs. This still leads to faster
615bdd1243dSDimitry Andric // code as the CPU can skip over NOPs quickly.
616bdd1243dSDimitry Andric //
617bdd1243dSDimitry Andric // LOHs are specified by the LC_LINKER_OPTIMIZATION_HINTS load command, which
618bdd1243dSDimitry Andric // points to a sequence of ULEB128-encoded numbers. Each entry specifies a
619bdd1243dSDimitry Andric // transformation kind, and 2 or 3 addresses where the instructions are located.
applyOptimizationHints(uint8_t * outBuf,const ObjFile & obj) const620bdd1243dSDimitry Andric void ARM64::applyOptimizationHints(uint8_t *outBuf, const ObjFile &obj) const {
621bdd1243dSDimitry Andric   ArrayRef<uint8_t> data = obj.getOptimizationHints();
622bdd1243dSDimitry Andric   if (data.empty())
623bdd1243dSDimitry Andric     return;
624bdd1243dSDimitry Andric 
625bdd1243dSDimitry Andric   const ConcatInputSection *section = nullptr;
626bdd1243dSDimitry Andric   uint64_t sectionAddr = 0;
627bdd1243dSDimitry Andric   uint8_t *buf = nullptr;
628bdd1243dSDimitry Andric 
629bdd1243dSDimitry Andric   auto findSection = [&](uint64_t addr) {
630bdd1243dSDimitry Andric     if (section && addr >= sectionAddr &&
631bdd1243dSDimitry Andric         addr < sectionAddr + section->getSize())
632bdd1243dSDimitry Andric       return true;
633bdd1243dSDimitry Andric 
6345f757f3fSDimitry Andric     if (obj.sections.empty())
6355f757f3fSDimitry Andric       return false;
636bdd1243dSDimitry Andric     auto secIt = std::prev(llvm::upper_bound(
637bdd1243dSDimitry Andric         obj.sections, addr,
638bdd1243dSDimitry Andric         [](uint64_t off, const Section *sec) { return off < sec->addr; }));
639bdd1243dSDimitry Andric     const Section *sec = *secIt;
640bdd1243dSDimitry Andric 
6415f757f3fSDimitry Andric     if (sec->subsections.empty())
6425f757f3fSDimitry Andric       return false;
643bdd1243dSDimitry Andric     auto subsecIt = std::prev(llvm::upper_bound(
644bdd1243dSDimitry Andric         sec->subsections, addr - sec->addr,
645bdd1243dSDimitry Andric         [](uint64_t off, Subsection subsec) { return off < subsec.offset; }));
646bdd1243dSDimitry Andric     const Subsection &subsec = *subsecIt;
647bdd1243dSDimitry Andric     const ConcatInputSection *isec =
648bdd1243dSDimitry Andric         dyn_cast_or_null<ConcatInputSection>(subsec.isec);
649bdd1243dSDimitry Andric     if (!isec || isec->shouldOmitFromOutput())
650bdd1243dSDimitry Andric       return false;
651bdd1243dSDimitry Andric 
652bdd1243dSDimitry Andric     section = isec;
653bdd1243dSDimitry Andric     sectionAddr = subsec.offset + sec->addr;
654bdd1243dSDimitry Andric     buf = outBuf + section->outSecOff + section->parent->fileOff;
655bdd1243dSDimitry Andric     return true;
656bdd1243dSDimitry Andric   };
657bdd1243dSDimitry Andric 
658bdd1243dSDimitry Andric   auto isValidOffset = [&](uint64_t offset) {
659bdd1243dSDimitry Andric     if (offset < sectionAddr || offset >= sectionAddr + section->getSize()) {
660bdd1243dSDimitry Andric       error(toString(&obj) +
661bdd1243dSDimitry Andric             ": linker optimization hint spans multiple sections");
662bdd1243dSDimitry Andric       return false;
663bdd1243dSDimitry Andric     }
664bdd1243dSDimitry Andric     return true;
665bdd1243dSDimitry Andric   };
666bdd1243dSDimitry Andric 
667bdd1243dSDimitry Andric   bool hasAdrpAdrp = false;
668bdd1243dSDimitry Andric   forEachHint(data, [&](uint64_t kind, ArrayRef<uint64_t> args) {
669bdd1243dSDimitry Andric     if (kind == LOH_ARM64_ADRP_ADRP) {
670bdd1243dSDimitry Andric       hasAdrpAdrp = true;
671bdd1243dSDimitry Andric       return;
672bdd1243dSDimitry Andric     }
673bdd1243dSDimitry Andric 
674bdd1243dSDimitry Andric     if (!findSection(args[0]))
675bdd1243dSDimitry Andric       return;
676bdd1243dSDimitry Andric     switch (kind) {
677bdd1243dSDimitry Andric     case LOH_ARM64_ADRP_ADD:
678bdd1243dSDimitry Andric       if (isValidOffset(args[1]))
679bdd1243dSDimitry Andric         applyAdrpAdd(buf, section, args[0] - sectionAddr,
680bdd1243dSDimitry Andric                      args[1] - sectionAddr);
68181ad6265SDimitry Andric       break;
68281ad6265SDimitry Andric     case LOH_ARM64_ADRP_LDR:
683bdd1243dSDimitry Andric       if (isValidOffset(args[1]))
684bdd1243dSDimitry Andric         applyAdrpLdr(buf, section, args[0] - sectionAddr,
685bdd1243dSDimitry Andric                      args[1] - sectionAddr);
686bdd1243dSDimitry Andric       break;
687bdd1243dSDimitry Andric     case LOH_ARM64_ADRP_LDR_GOT:
688bdd1243dSDimitry Andric       if (isValidOffset(args[1]))
689bdd1243dSDimitry Andric         applyAdrpLdrGot(buf, section, args[0] - sectionAddr,
690bdd1243dSDimitry Andric                         args[1] - sectionAddr);
69181ad6265SDimitry Andric       break;
69281ad6265SDimitry Andric     case LOH_ARM64_ADRP_ADD_LDR:
693bdd1243dSDimitry Andric       if (isValidOffset(args[1]) && isValidOffset(args[2]))
694bdd1243dSDimitry Andric         applyAdrpAddLdr(buf, section, args[0] - sectionAddr,
695bdd1243dSDimitry Andric                         args[1] - sectionAddr, args[2] - sectionAddr);
696753f127fSDimitry Andric       break;
69781ad6265SDimitry Andric     case LOH_ARM64_ADRP_LDR_GOT_LDR:
698bdd1243dSDimitry Andric       if (isValidOffset(args[1]) && isValidOffset(args[2]))
699bdd1243dSDimitry Andric         applyAdrpLdrGotLdr(buf, section, args[0] - sectionAddr,
700bdd1243dSDimitry Andric                            args[1] - sectionAddr, args[2] - sectionAddr);
701753f127fSDimitry Andric       break;
70281ad6265SDimitry Andric     case LOH_ARM64_ADRP_ADD_STR:
70381ad6265SDimitry Andric     case LOH_ARM64_ADRP_LDR_GOT_STR:
70481ad6265SDimitry Andric       // TODO: Implement these
70581ad6265SDimitry Andric       break;
70681ad6265SDimitry Andric     }
707bdd1243dSDimitry Andric   });
70881ad6265SDimitry Andric 
709bdd1243dSDimitry Andric   if (!hasAdrpAdrp)
710bdd1243dSDimitry Andric     return;
711bdd1243dSDimitry Andric 
712bdd1243dSDimitry Andric   // AdrpAdrp optimization hints are performed in a second pass because they
713bdd1243dSDimitry Andric   // might interfere with other transformations. For instance, consider the
714bdd1243dSDimitry Andric   // following input:
715bdd1243dSDimitry Andric   //
716bdd1243dSDimitry Andric   //   adrp x0, _foo@PAGE
717bdd1243dSDimitry Andric   //   add  x1, x0, _foo@PAGEOFF
718bdd1243dSDimitry Andric   //   adrp x0, _bar@PAGE
719bdd1243dSDimitry Andric   //   add  x2, x0, _bar@PAGEOFF
720bdd1243dSDimitry Andric   //
721bdd1243dSDimitry Andric   // If we perform the AdrpAdrp relaxation first, we get:
722bdd1243dSDimitry Andric   //
723bdd1243dSDimitry Andric   //   adrp x0, _foo@PAGE
724bdd1243dSDimitry Andric   //   add  x1, x0, _foo@PAGEOFF
725bdd1243dSDimitry Andric   //   nop
726bdd1243dSDimitry Andric   //   add x2, x0, _bar@PAGEOFF
727bdd1243dSDimitry Andric   //
728bdd1243dSDimitry Andric   // If we then apply AdrpAdd to the first two instructions, the add will have a
729bdd1243dSDimitry Andric   // garbage value in x0:
730bdd1243dSDimitry Andric   //
731bdd1243dSDimitry Andric   //   adr  x1, _foo
732bdd1243dSDimitry Andric   //   nop
733bdd1243dSDimitry Andric   //   nop
734bdd1243dSDimitry Andric   //   add  x2, x0, _bar@PAGEOFF
735bdd1243dSDimitry Andric   forEachHint(data, [&](uint64_t kind, ArrayRef<uint64_t> args) {
736bdd1243dSDimitry Andric     if (kind != LOH_ARM64_ADRP_ADRP)
737bdd1243dSDimitry Andric       return;
738bdd1243dSDimitry Andric     if (!findSection(args[0]))
739bdd1243dSDimitry Andric       return;
740bdd1243dSDimitry Andric     if (isValidOffset(args[1]))
741bdd1243dSDimitry Andric       applyAdrpAdrp(buf, section, args[0] - sectionAddr, args[1] - sectionAddr);
742bdd1243dSDimitry Andric   });
74381ad6265SDimitry Andric }
74481ad6265SDimitry Andric 
createARM64TargetInfo()745fe6060f1SDimitry Andric TargetInfo *macho::createARM64TargetInfo() {
746fe6060f1SDimitry Andric   static ARM64 t;
747fe6060f1SDimitry Andric   return &t;
748fe6060f1SDimitry Andric }
749