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 "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/BinaryFormat/MachO.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/MathExtras.h"
21
22 using namespace llvm;
23 using namespace llvm::MachO;
24 using namespace llvm::support::endian;
25 using namespace lld;
26 using namespace lld::macho;
27
28 namespace {
29
30 struct ARM64 : ARM64Common {
31 ARM64();
32 void writeStub(uint8_t *buf, const Symbol &) const override;
33 void writeStubHelperHeader(uint8_t *buf) const override;
34 void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &,
35 uint64_t entryAddr) const override;
36 const RelocAttrs &getRelocAttrs(uint8_t type) const override;
37 void populateThunk(InputSection *thunk, Symbol *funcSym) override;
38 };
39
40 } // namespace
41
42 // Random notes on reloc types:
43 // ADDEND always pairs with BRANCH26, PAGE21, or PAGEOFF12
44 // POINTER_TO_GOT: ld64 supports a 4-byte pc-relative form as well as an 8-byte
45 // absolute version of this relocation. The semantics of the absolute relocation
46 // are weird -- it results in the value of the GOT slot being written, instead
47 // of the address. Let's not support it unless we find a real-world use case.
48
getRelocAttrs(uint8_t type) const49 const RelocAttrs &ARM64::getRelocAttrs(uint8_t type) const {
50 static const std::array<RelocAttrs, 11> relocAttrsArray{{
51 #define B(x) RelocAttrBits::x
52 {"UNSIGNED",
53 B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)},
54 {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)},
55 {"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
56 {"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)},
57 {"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)},
58 {"GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)},
59 {"GOT_LOAD_PAGEOFF12",
60 B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
61 {"POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
62 {"TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)},
63 {"TLVP_LOAD_PAGEOFF12",
64 B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
65 {"ADDEND", B(ADDEND)},
66 #undef B
67 }};
68 assert(type < relocAttrsArray.size() && "invalid relocation type");
69 if (type >= relocAttrsArray.size())
70 return invalidRelocAttrs;
71 return relocAttrsArray[type];
72 }
73
74 static constexpr uint32_t stubCode[] = {
75 0x90000010, // 00: adrp x16, __la_symbol_ptr@page
76 0xf9400210, // 04: ldr x16, [x16, __la_symbol_ptr@pageoff]
77 0xd61f0200, // 08: br x16
78 };
79
writeStub(uint8_t * buf8,const Symbol & sym) const80 void ARM64::writeStub(uint8_t *buf8, const Symbol &sym) const {
81 ::writeStub<LP64>(buf8, stubCode, sym);
82 }
83
84 static constexpr uint32_t stubHelperHeaderCode[] = {
85 0x90000011, // 00: adrp x17, _dyld_private@page
86 0x91000231, // 04: add x17, x17, _dyld_private@pageoff
87 0xa9bf47f0, // 08: stp x16/x17, [sp, #-16]!
88 0x90000010, // 0c: adrp x16, dyld_stub_binder@page
89 0xf9400210, // 10: ldr x16, [x16, dyld_stub_binder@pageoff]
90 0xd61f0200, // 14: br x16
91 };
92
writeStubHelperHeader(uint8_t * buf8) const93 void ARM64::writeStubHelperHeader(uint8_t *buf8) const {
94 ::writeStubHelperHeader<LP64>(buf8, stubHelperHeaderCode);
95 }
96
97 static constexpr uint32_t stubHelperEntryCode[] = {
98 0x18000050, // 00: ldr w16, l0
99 0x14000000, // 04: b stubHelperHeader
100 0x00000000, // 08: l0: .long 0
101 };
102
writeStubHelperEntry(uint8_t * buf8,const DylibSymbol & sym,uint64_t entryVA) const103 void ARM64::writeStubHelperEntry(uint8_t *buf8, const DylibSymbol &sym,
104 uint64_t entryVA) const {
105 ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA);
106 }
107
108 // A thunk is the relaxed variation of stubCode. We don't need the
109 // extra indirection through a lazy pointer because the target address
110 // is known at link time.
111 static constexpr uint32_t thunkCode[] = {
112 0x90000010, // 00: adrp x16, <thunk.ptr>@page
113 0x91000210, // 04: add x16, [x16,<thunk.ptr>@pageoff]
114 0xd61f0200, // 08: br x16
115 };
116
populateThunk(InputSection * thunk,Symbol * funcSym)117 void ARM64::populateThunk(InputSection *thunk, Symbol *funcSym) {
118 thunk->align = 4;
119 thunk->data = {reinterpret_cast<const uint8_t *>(thunkCode),
120 sizeof(thunkCode)};
121 thunk->relocs.push_back({/*type=*/ARM64_RELOC_PAGEOFF12,
122 /*pcrel=*/false, /*length=*/2,
123 /*offset=*/4, /*addend=*/0,
124 /*referent=*/funcSym});
125 thunk->relocs.push_back({/*type=*/ARM64_RELOC_PAGE21,
126 /*pcrel=*/true, /*length=*/2,
127 /*offset=*/0, /*addend=*/0,
128 /*referent=*/funcSym});
129 }
130
ARM64()131 ARM64::ARM64() : ARM64Common(LP64()) {
132 cpuType = CPU_TYPE_ARM64;
133 cpuSubtype = CPU_SUBTYPE_ARM64_ALL;
134
135 stubSize = sizeof(stubCode);
136 thunkSize = sizeof(thunkCode);
137 branchRange = maxIntN(28) - thunkSize;
138 stubHelperHeaderSize = sizeof(stubHelperHeaderCode);
139 stubHelperEntrySize = sizeof(stubHelperEntryCode);
140 }
141
createARM64TargetInfo()142 TargetInfo *macho::createARM64TargetInfo() {
143 static ARM64 t;
144 return &t;
145 }
146