xref: /freebsd/contrib/llvm-project/lld/MachO/Arch/ARM64_32.cpp (revision 9c77fb6aaa366cbabc80ee1b834bcfe4df135491)
1 //===- ARM64_32.cpp
2 //----------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Arch/ARM64Common.h"
11 #include "InputFiles.h"
12 #include "Symbols.h"
13 #include "Target.h"
14 
15 #include "lld/Common/ErrorHandler.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/BinaryFormat/MachO.h"
18 
19 using namespace llvm::MachO;
20 using namespace llvm::support::endian;
21 using namespace lld;
22 using namespace lld::macho;
23 
24 namespace {
25 
26 struct ARM64_32 : ARM64Common {
27   ARM64_32();
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   void writeObjCMsgSendStub(uint8_t *buf, Symbol *sym, uint64_t stubsAddr,
33                             uint64_t &stubOffset, uint64_t selrefVA,
34                             Symbol *objcMsgSend) const override;
35 };
36 
37 } // namespace
38 
39 // These are very similar to ARM64's relocation attributes, except that we don't
40 // have the BYTE8 flag set.
41 static constexpr std::array<RelocAttrs, 11> relocAttrsArray{{
42 #define B(x) RelocAttrBits::x
43     {"UNSIGNED", B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4)},
44     {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4)},
45     {"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)},
46     {"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)},
47     {"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)},
48     {"GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)},
49     {"GOT_LOAD_PAGEOFF12",
50      B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)},
51     {"POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)},
52     {"TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)},
53     {"TLVP_LOAD_PAGEOFF12",
54      B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)},
55     {"ADDEND", B(ADDEND)},
56 #undef B
57 }};
58 
59 // The stub code is fairly similar to ARM64's, except that we load pointers into
60 // 32-bit 'w' registers, instead of the 64-bit 'x' ones.
61 
62 static constexpr uint32_t stubCode[] = {
63     0x90000010, // 00: adrp  x16, __la_symbol_ptr@page
64     0xb9400210, // 04: ldr   w16, [x16, __la_symbol_ptr@pageoff]
65     0xd61f0200, // 08: br    x16
66 };
67 
68 void ARM64_32::writeStub(uint8_t *buf8, const Symbol &sym,
69                          uint64_t pointerVA) const {
70   ::writeStub(buf8, stubCode, sym, pointerVA);
71 }
72 
73 static constexpr uint32_t stubHelperHeaderCode[] = {
74     0x90000011, // 00: adrp  x17, _dyld_private@page
75     0x91000231, // 04: add   x17, x17, _dyld_private@pageoff
76     0xa9bf47f0, // 08: stp   x16/x17, [sp, #-16]!
77     0x90000010, // 0c: adrp  x16, dyld_stub_binder@page
78     0xb9400210, // 10: ldr   w16, [x16, dyld_stub_binder@pageoff]
79     0xd61f0200, // 14: br    x16
80 };
81 
82 void ARM64_32::writeStubHelperHeader(uint8_t *buf8) const {
83   ::writeStubHelperHeader<ILP32>(buf8, stubHelperHeaderCode);
84 }
85 
86 static constexpr uint32_t stubHelperEntryCode[] = {
87     0x18000050, // 00: ldr  w16, l0
88     0x14000000, // 04: b    stubHelperHeader
89     0x00000000, // 08: l0: .long 0
90 };
91 
92 void ARM64_32::writeStubHelperEntry(uint8_t *buf8, const Symbol &sym,
93                                     uint64_t entryVA) const {
94   ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA);
95 }
96 
97 void ARM64_32::writeObjCMsgSendStub(uint8_t *buf, Symbol *sym,
98                                     uint64_t stubsAddr, uint64_t &stubOffset,
99                                     uint64_t selrefVA,
100                                     Symbol *objcMsgSend) const {
101   fatal("TODO: implement this");
102 }
103 
104 ARM64_32::ARM64_32() : ARM64Common(ILP32()) {
105   cpuType = CPU_TYPE_ARM64_32;
106   cpuSubtype = CPU_SUBTYPE_ARM64_V8;
107 
108   modeDwarfEncoding = 0x04000000;              // UNWIND_ARM_MODE_DWARF
109   subtractorRelocType = GENERIC_RELOC_INVALID; // FIXME
110   unsignedRelocType = GENERIC_RELOC_INVALID;   // FIXME
111 
112   stubSize = sizeof(stubCode);
113   stubHelperHeaderSize = sizeof(stubHelperHeaderCode);
114   stubHelperEntrySize = sizeof(stubHelperEntryCode);
115 
116   relocAttrs = {relocAttrsArray.data(), relocAttrsArray.size()};
117 }
118 
119 TargetInfo *macho::createARM64_32TargetInfo() {
120   static ARM64_32 t;
121   return &t;
122 }
123