1 //===- Target.h -------------------------------------------------*- C++ -*-===// 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 #ifndef LLD_MACHO_TARGET_H 10 #define LLD_MACHO_TARGET_H 11 12 #include "MachOStructs.h" 13 #include "Relocations.h" 14 15 #include "llvm/ADT/BitmaskEnum.h" 16 #include "llvm/BinaryFormat/MachO.h" 17 #include "llvm/Support/MathExtras.h" 18 #include "llvm/Support/MemoryBuffer.h" 19 20 #include <cstddef> 21 #include <cstdint> 22 23 namespace lld { 24 namespace macho { 25 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); 26 27 class Symbol; 28 class Defined; 29 class DylibSymbol; 30 class InputSection; 31 32 class TargetInfo { 33 public: 34 template <class LP> TargetInfo(LP) { 35 // Having these values available in TargetInfo allows us to access them 36 // without having to resort to templates. 37 magic = LP::magic; 38 pageZeroSize = LP::pageZeroSize; 39 headerSize = sizeof(typename LP::mach_header); 40 wordSize = LP::wordSize; 41 p2WordSize = llvm::CTLog2<LP::wordSize>(); 42 } 43 44 virtual ~TargetInfo() = default; 45 46 // Validate the relocation structure and get its addend. 47 virtual int64_t 48 getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset, 49 const llvm::MachO::relocation_info) const = 0; 50 virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va, 51 uint64_t relocVA) const = 0; 52 53 // Write code for lazy binding. See the comments on StubsSection for more 54 // details. 55 virtual void writeStub(uint8_t *buf, const Symbol &) const = 0; 56 virtual void writeStubHelperHeader(uint8_t *buf) const = 0; 57 virtual void writeStubHelperEntry(uint8_t *buf, const Symbol &, 58 uint64_t entryAddr) const = 0; 59 60 // Symbols may be referenced via either the GOT or the stubs section, 61 // depending on the relocation type. prepareSymbolRelocation() will set up the 62 // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those 63 // entries. resolveSymbolVA() may also relax the target instructions to save 64 // on a level of address indirection. 65 virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0; 66 67 virtual const RelocAttrs &getRelocAttrs(uint8_t type) const = 0; 68 69 virtual uint64_t getPageSize() const = 0; 70 71 virtual void populateThunk(InputSection *thunk, Symbol *funcSym) { 72 llvm_unreachable("target does not use thunks"); 73 } 74 75 bool hasAttr(uint8_t type, RelocAttrBits bit) const { 76 return getRelocAttrs(type).hasAttr(bit); 77 } 78 79 bool usesThunks() const { return thunkSize > 0; } 80 81 uint32_t magic; 82 llvm::MachO::CPUType cpuType; 83 uint32_t cpuSubtype; 84 85 uint64_t pageZeroSize; 86 size_t headerSize; 87 size_t stubSize; 88 size_t stubHelperHeaderSize; 89 size_t stubHelperEntrySize; 90 uint8_t p2WordSize; 91 size_t wordSize; 92 93 size_t thunkSize = 0; 94 uint64_t forwardBranchRange = 0; 95 uint64_t backwardBranchRange = 0; 96 97 uint32_t modeDwarfEncoding; 98 uint8_t subtractorRelocType; 99 uint8_t unsignedRelocType; 100 101 // We contrive this value as sufficiently far from any valid address that it 102 // will always be out-of-range for any architecture. UINT64_MAX is not a 103 // good choice because it is (a) only 1 away from wrapping to 0, and (b) the 104 // tombstone value for DenseMap<> and caused weird assertions for me. 105 static constexpr uint64_t outOfRangeVA = 0xfull << 60; 106 }; 107 108 TargetInfo *createX86_64TargetInfo(); 109 TargetInfo *createARM64TargetInfo(); 110 TargetInfo *createARM64_32TargetInfo(); 111 TargetInfo *createARMTargetInfo(uint32_t cpuSubtype); 112 113 struct LP64 { 114 using mach_header = llvm::MachO::mach_header_64; 115 using nlist = structs::nlist_64; 116 using segment_command = llvm::MachO::segment_command_64; 117 using section = llvm::MachO::section_64; 118 using encryption_info_command = llvm::MachO::encryption_info_command_64; 119 120 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64; 121 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64; 122 static constexpr uint32_t encryptionInfoLCType = 123 llvm::MachO::LC_ENCRYPTION_INFO_64; 124 125 static constexpr uint64_t pageZeroSize = 1ull << 32; 126 static constexpr size_t wordSize = 8; 127 }; 128 129 struct ILP32 { 130 using mach_header = llvm::MachO::mach_header; 131 using nlist = structs::nlist; 132 using segment_command = llvm::MachO::segment_command; 133 using section = llvm::MachO::section; 134 using encryption_info_command = llvm::MachO::encryption_info_command; 135 136 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC; 137 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT; 138 static constexpr uint32_t encryptionInfoLCType = 139 llvm::MachO::LC_ENCRYPTION_INFO; 140 141 static constexpr uint64_t pageZeroSize = 1ull << 12; 142 static constexpr size_t wordSize = 4; 143 }; 144 145 extern TargetInfo *target; 146 147 } // namespace macho 148 } // namespace lld 149 150 #endif 151