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