1//===- ARM64.cpp ----------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Arch/ARM64Common.h"
10#include "InputFiles.h"
11#include "Symbols.h"
12#include "SyntheticSections.h"
13#include "Target.h"
14
15#include "lld/Common/ErrorHandler.h"
16#include "mach-o/compact_unwind_encoding.h"
17#include "llvm/BinaryFormat/MachO.h"
18
19using namespace llvm;
20using namespace llvm::MachO;
21using namespace lld;
22using namespace lld::macho;
23
24namespace {
25
26struct ARM64 : ARM64Common {
27 ARM64();
28 void writeStub(uint8_t *buf, const Symbol &, uint64_t) const override;
29 void writeStubHelperHeader(uint8_t *buf) const override;
30 void writeStubHelperEntry(uint8_t *buf, const Symbol &,
31 uint64_t entryAddr) const override;
32
33 void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
34 uint64_t &stubOffset, uint64_t selrefVA,
35 Symbol *objcMsgSend) const override;
36 void populateThunk(InputSection *thunk, Symbol *funcSym,
37 int64_t addend) override;
38
39 void initICFSafeThunkBody(InputSection *thunk,
40 Symbol *targetSym) const override;
41 Symbol *getThunkBranchTarget(InputSection *thunk) const override;
42 uint32_t getICFSafeThunkSize() const override;
43};
44
45} // namespace
46
47// Random notes on reloc types:
48// ADDEND always pairs with BRANCH26, PAGE21, or PAGEOFF12
49// POINTER_TO_GOT: ld64 supports a 4-byte pc-relative form as well as an 8-byte
50// absolute version of this relocation. The semantics of the absolute relocation
51// are weird -- it results in the value of the GOT slot being written, instead
52// of the address. Let's not support it unless we find a real-world use case.
53static constexpr std::array<RelocAttrs, 11> relocAttrsArray{._M_elems: {
54#define B(x) RelocAttrBits::x
55 {.name: "UNSIGNED",
56 B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)},
57 {.name: "SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)},
58 {.name: "BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
59 {.name: "PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)},
60 {.name: "PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)},
61 {.name: "GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)},
62 {.name: "GOT_LOAD_PAGEOFF12",
63 B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
64 {.name: "POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
65 {.name: "TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)},
66 {.name: "TLVP_LOAD_PAGEOFF12",
67 B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
68 {.name: "ADDEND", B(ADDEND)},
69#undef B
70}};
71
72static constexpr uint32_t stubCode[] = {
73 0x90000010, // 00: adrp x16, __la_symbol_ptr@page
74 0xf9400210, // 04: ldr x16, [x16, __la_symbol_ptr@pageoff]
75 0xd61f0200, // 08: br x16
76};
77
78void ARM64::writeStub(uint8_t *buf8, const Symbol &sym,
79 uint64_t pointerVA) const {
80 ::writeStub(buf8, stubCode, sym, pointerVA);
81}
82
83static constexpr uint32_t stubHelperHeaderCode[] = {
84 0x90000011, // 00: adrp x17, _dyld_private@page
85 0x91000231, // 04: add x17, x17, _dyld_private@pageoff
86 0xa9bf47f0, // 08: stp x16/x17, [sp, #-16]!
87 0x90000010, // 0c: adrp x16, dyld_stub_binder@page
88 0xf9400210, // 10: ldr x16, [x16, dyld_stub_binder@pageoff]
89 0xd61f0200, // 14: br x16
90};
91
92void ARM64::writeStubHelperHeader(uint8_t *buf8) const {
93 ::writeStubHelperHeader<LP64>(buf8, stubHelperHeaderCode);
94}
95
96static constexpr uint32_t stubHelperEntryCode[] = {
97 0x18000050, // 00: ldr w16, l0
98 0x14000000, // 04: b stubHelperHeader
99 0x00000000, // 08: l0: .long 0
100};
101
102void ARM64::writeStubHelperEntry(uint8_t *buf8, const Symbol &sym,
103 uint64_t entryVA) const {
104 ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA);
105}
106
107static constexpr uint32_t objcStubsFastCode[] = {
108 0x90000001, // adrp x1, __objc_selrefs@page
109 0xf9400021, // ldr x1, [x1, @selector("foo")@pageoff]
110 0x90000010, // adrp x16, _got@page
111 0xf9400210, // ldr x16, [x16, _objc_msgSend@pageoff]
112 0xd61f0200, // br x16
113 0xd4200020, // brk #0x1
114 0xd4200020, // brk #0x1
115 0xd4200020, // brk #0x1
116};
117
118static constexpr uint32_t objcStubsSmallCode[] = {
119 0x90000001, // adrp x1, __objc_selrefs@page
120 0xf9400021, // ldr x1, [x1, @selector("foo")@pageoff]
121 0x14000000, // b _objc_msgSend
122};
123
124void ARM64::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
125 uint64_t &stubOffset, uint64_t selrefVA,
126 Symbol *objcMsgSend) const {
127 uint64_t objcMsgSendAddr;
128 uint64_t objcStubSize;
129 uint64_t objcMsgSendIndex;
130
131 if (config->objcStubsMode == ObjCStubsMode::fast) {
132 objcStubSize = target->objcStubsFastSize;
133 objcMsgSendAddr = in.got->addr;
134 objcMsgSendIndex = objcMsgSend->gotIndex;
135 ::writeObjCMsgSendFastStub<LP64>(buf, objcStubsFastCode, sym, stubsAddr,
136 stubOffset, selrefVA, gotAddr: objcMsgSendAddr,
137 msgSendIndex: objcMsgSendIndex);
138 } else {
139 assert(config->objcStubsMode == ObjCStubsMode::small);
140 objcStubSize = target->objcStubsSmallSize;
141 if (auto *d = dyn_cast<Defined>(Val: objcMsgSend)) {
142 objcMsgSendAddr = d->getVA();
143 objcMsgSendIndex = 0;
144 } else {
145 objcMsgSendAddr = in.stubs->addr;
146 objcMsgSendIndex = objcMsgSend->stubsIndex;
147 }
148 ::writeObjCMsgSendSmallStub<LP64>(buf, objcStubsSmallCode, sym, stubsAddr,
149 stubOffset, selrefVA, msgSendAddr: objcMsgSendAddr,
150 msgSendIndex: objcMsgSendIndex);
151 }
152 stubOffset += objcStubSize;
153}
154
155// A thunk is the relaxed variation of stubCode. We don't need the
156// extra indirection through a lazy pointer because the target address
157// is known at link time.
158static constexpr uint32_t thunkCode[] = {
159 0x90000010, // 00: adrp x16, <thunk.ptr>@page
160 0x91000210, // 04: add x16, [x16,<thunk.ptr>@pageoff]
161 0xd61f0200, // 08: br x16
162};
163
164void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym,
165 int64_t addend) {
166 thunk->align = 4;
167 thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode),
168 sizeof(thunkCode)};
169 thunk->relocs.emplace_back(/*type=*/args: ARM64_RELOC_PAGEOFF12,
170 /*pcrel=*/args: false, /*length=*/args: 2,
171 /*offset=*/args: 4, /*addend=*/args&: addend,
172 /*referent=*/args&: funcSym);
173 thunk->relocs.emplace_back(/*type=*/args: ARM64_RELOC_PAGE21,
174 /*pcrel=*/args: true, /*length=*/args: 2,
175 /*offset=*/args: 0, /*addend=*/args&: addend,
176 /*referent=*/args&: funcSym);
177}
178// Just a single direct branch to the target function.
179static constexpr uint32_t icfSafeThunkCode[] = {
180 0x14000000, // 08: b target
181};
182
183void ARM64::initICFSafeThunkBody(InputSection *thunk, Symbol *targetSym) const {
184 // The base data here will not be itself modified, we'll just be adding a
185 // reloc below. So we can directly use the constexpr above as the data.
186 thunk->data = {reinterpret_cast<const uint8_t *>(icfSafeThunkCode),
187 sizeof(icfSafeThunkCode)};
188
189 thunk->relocs.emplace_back(/*type=*/args: ARM64_RELOC_BRANCH26,
190 /*pcrel=*/args: true, /*length=*/args: 2,
191 /*offset=*/args: 0, /*addend=*/args: 0,
192 /*referent=*/args&: targetSym);
193}
194
195Symbol *ARM64::getThunkBranchTarget(InputSection *thunk) const {
196 assert(thunk->relocs.size() == 1 &&
197 "expected a single reloc on ARM64 ICF thunk");
198 auto &reloc = thunk->relocs[0];
199 assert(isa<Symbol *>(reloc.referent) &&
200 "ARM64 thunk reloc is expected to point to a Symbol");
201
202 return cast<Symbol *>(Val&: reloc.referent);
203}
204
205uint32_t ARM64::getICFSafeThunkSize() const { return sizeof(icfSafeThunkCode); }
206
207ARM64::ARM64() : ARM64Common(LP64()) {
208 cpuType = CPU_TYPE_ARM64;
209 cpuSubtype = CPU_SUBTYPE_ARM64_ALL;
210
211 stubSize = sizeof(stubCode);
212 thunkSize = sizeof(thunkCode);
213
214 objcStubsFastSize = sizeof(objcStubsFastCode);
215 objcStubsFastAlignment = 32;
216 objcStubsSmallSize = sizeof(objcStubsSmallCode);
217 objcStubsSmallAlignment = 4;
218
219 // Branch immediate is two's complement 26 bits, which is implicitly
220 // multiplied by 4 (since all functions are 4-aligned: The branch range
221 // is -4*(2**(26-1))..4*(2**(26-1) - 1).
222 backwardBranchRange = 128 * 1024 * 1024;
223 forwardBranchRange = backwardBranchRange - 4;
224
225 modeDwarfEncoding = UNWIND_ARM64_MODE_DWARF;
226 subtractorRelocType = ARM64_RELOC_SUBTRACTOR;
227 unsignedRelocType = ARM64_RELOC_UNSIGNED;
228
229 stubHelperHeaderSize = sizeof(stubHelperHeaderCode);
230 stubHelperEntrySize = sizeof(stubHelperEntryCode);
231
232 relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()};
233}
234
235TargetInfo *macho::createARM64TargetInfo() {
236 static ARM64 t;
237 return &t;
238}
239