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