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::MachO; 23 using namespace llvm::support::endian; 24 using namespace lld; 25 using namespace lld::macho; 26 27 namespace { 28 29 struct ARM64 : ARM64Common { 30 ARM64(); 31 void writeStub(uint8_t *buf, const Symbol &) const override; 32 void writeStubHelperHeader(uint8_t *buf) const override; 33 void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &, 34 uint64_t entryAddr) const override; 35 const RelocAttrs &getRelocAttrs(uint8_t type) const override; 36 }; 37 38 } // namespace 39 40 // Random notes on reloc types: 41 // ADDEND always pairs with BRANCH26, PAGE21, or PAGEOFF12 42 // POINTER_TO_GOT: ld64 supports a 4-byte pc-relative form as well as an 8-byte 43 // absolute version of this relocation. The semantics of the absolute relocation 44 // are weird -- it results in the value of the GOT slot being written, instead 45 // of the address. Let's not support it unless we find a real-world use case. 46 47 const RelocAttrs &ARM64::getRelocAttrs(uint8_t type) const { 48 static const std::array<RelocAttrs, 11> relocAttrsArray{{ 49 #define B(x) RelocAttrBits::x 50 {"UNSIGNED", 51 B(UNSIGNED) | B(ABSOLUTE) | B(EXTERN) | B(LOCAL) | B(BYTE4) | B(BYTE8)}, 52 {"SUBTRACTOR", B(SUBTRAHEND) | B(EXTERN) | B(BYTE4) | B(BYTE8)}, 53 {"BRANCH26", B(PCREL) | B(EXTERN) | B(BRANCH) | B(BYTE4)}, 54 {"PAGE21", B(PCREL) | B(EXTERN) | B(BYTE4)}, 55 {"PAGEOFF12", B(ABSOLUTE) | B(EXTERN) | B(BYTE4)}, 56 {"GOT_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(GOT) | B(BYTE4)}, 57 {"GOT_LOAD_PAGEOFF12", 58 B(ABSOLUTE) | B(EXTERN) | B(GOT) | B(LOAD) | B(BYTE4)}, 59 {"POINTER_TO_GOT", B(PCREL) | B(EXTERN) | B(GOT) | B(POINTER) | B(BYTE4)}, 60 {"TLVP_LOAD_PAGE21", B(PCREL) | B(EXTERN) | B(TLV) | B(BYTE4)}, 61 {"TLVP_LOAD_PAGEOFF12", 62 B(ABSOLUTE) | B(EXTERN) | B(TLV) | B(LOAD) | B(BYTE4)}, 63 {"ADDEND", B(ADDEND)}, 64 #undef B 65 }}; 66 assert(type < relocAttrsArray.size() && "invalid relocation type"); 67 if (type >= relocAttrsArray.size()) 68 return invalidRelocAttrs; 69 return relocAttrsArray[type]; 70 } 71 72 static 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 78 void ARM64::writeStub(uint8_t *buf8, const Symbol &sym) const { 79 ::writeStub<LP64>(buf8, stubCode, sym); 80 } 81 82 static constexpr uint32_t stubHelperHeaderCode[] = { 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::writeStubHelperHeader(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 DylibSymbol &sym, 102 uint64_t entryVA) const { 103 ::writeStubHelperEntry(buf8, stubHelperEntryCode, sym, entryVA); 104 } 105 106 ARM64::ARM64() : ARM64Common(LP64()) { 107 cpuType = CPU_TYPE_ARM64; 108 cpuSubtype = CPU_SUBTYPE_ARM64_ALL; 109 110 stubSize = sizeof(stubCode); 111 stubHelperHeaderSize = sizeof(stubHelperHeaderCode); 112 stubHelperEntrySize = sizeof(stubHelperEntryCode); 113 } 114 115 TargetInfo *macho::createARM64TargetInfo() { 116 static ARM64 t; 117 return &t; 118 } 119