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