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 class ConcatInputSection; 32 33 class TargetInfo { 34 public: 35 template <class LP> TargetInfo(LP) { 36 // Having these values available in TargetInfo allows us to access them 37 // without having to resort to templates. 38 magic = LP::magic; 39 pageZeroSize = LP::pageZeroSize; 40 headerSize = sizeof(typename LP::mach_header); 41 wordSize = LP::wordSize; 42 p2WordSize = llvm::CTLog2<LP::wordSize>(); 43 } 44 45 virtual ~TargetInfo() = default; 46 47 // Validate the relocation structure and get its addend. 48 virtual int64_t 49 getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset, 50 const llvm::MachO::relocation_info) const = 0; 51 virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va, 52 uint64_t relocVA) const = 0; 53 54 // Write code for lazy binding. See the comments on StubsSection for more 55 // details. 56 virtual void writeStub(uint8_t *buf, const Symbol &) const = 0; 57 virtual void writeStubHelperHeader(uint8_t *buf) const = 0; 58 virtual void writeStubHelperEntry(uint8_t *buf, const Symbol &, 59 uint64_t entryAddr) const = 0; 60 61 // Symbols may be referenced via either the GOT or the stubs section, 62 // depending on the relocation type. prepareSymbolRelocation() will set up the 63 // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those 64 // entries. resolveSymbolVA() may also relax the target instructions to save 65 // on a level of address indirection. 66 virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0; 67 68 virtual uint64_t getPageSize() const = 0; 69 70 virtual void populateThunk(InputSection *thunk, Symbol *funcSym) { 71 llvm_unreachable("target does not use thunks"); 72 } 73 74 const RelocAttrs &getRelocAttrs(uint8_t type) const { 75 assert(type < relocAttrs.size() && "invalid relocation type"); 76 if (type >= relocAttrs.size()) 77 return invalidRelocAttrs; 78 return relocAttrs[type]; 79 } 80 81 bool hasAttr(uint8_t type, RelocAttrBits bit) const { 82 return getRelocAttrs(type).hasAttr(bit); 83 } 84 85 bool usesThunks() const { return thunkSize > 0; } 86 87 // For now, handleDtraceReloc only implements -no_dtrace_dof, and ensures 88 // that the linking would not fail even when there are user-provided dtrace 89 // symbols. However, unlike ld64, lld currently does not emit __dof sections. 90 virtual void handleDtraceReloc(const Symbol *sym, const Reloc &r, 91 uint8_t *loc) const { 92 llvm_unreachable("Unsupported architecture for dtrace symbols"); 93 } 94 95 96 virtual void applyOptimizationHints(uint8_t *buf, const ConcatInputSection *, 97 llvm::ArrayRef<uint64_t>) const {}; 98 99 uint32_t magic; 100 llvm::MachO::CPUType cpuType; 101 uint32_t cpuSubtype; 102 103 uint64_t pageZeroSize; 104 size_t headerSize; 105 size_t stubSize; 106 size_t stubHelperHeaderSize; 107 size_t stubHelperEntrySize; 108 uint8_t p2WordSize; 109 size_t wordSize; 110 111 size_t thunkSize = 0; 112 uint64_t forwardBranchRange = 0; 113 uint64_t backwardBranchRange = 0; 114 115 uint32_t modeDwarfEncoding; 116 uint8_t subtractorRelocType; 117 uint8_t unsignedRelocType; 118 119 llvm::ArrayRef<RelocAttrs> relocAttrs; 120 121 // We contrive this value as sufficiently far from any valid address that it 122 // will always be out-of-range for any architecture. UINT64_MAX is not a 123 // good choice because it is (a) only 1 away from wrapping to 0, and (b) the 124 // tombstone value for DenseMap<> and caused weird assertions for me. 125 static constexpr uint64_t outOfRangeVA = 0xfull << 60; 126 }; 127 128 TargetInfo *createX86_64TargetInfo(); 129 TargetInfo *createARM64TargetInfo(); 130 TargetInfo *createARM64_32TargetInfo(); 131 TargetInfo *createARMTargetInfo(uint32_t cpuSubtype); 132 133 struct LP64 { 134 using mach_header = llvm::MachO::mach_header_64; 135 using nlist = structs::nlist_64; 136 using segment_command = llvm::MachO::segment_command_64; 137 using section = llvm::MachO::section_64; 138 using encryption_info_command = llvm::MachO::encryption_info_command_64; 139 140 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64; 141 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64; 142 static constexpr uint32_t encryptionInfoLCType = 143 llvm::MachO::LC_ENCRYPTION_INFO_64; 144 145 static constexpr uint64_t pageZeroSize = 1ull << 32; 146 static constexpr size_t wordSize = 8; 147 }; 148 149 struct ILP32 { 150 using mach_header = llvm::MachO::mach_header; 151 using nlist = structs::nlist; 152 using segment_command = llvm::MachO::segment_command; 153 using section = llvm::MachO::section; 154 using encryption_info_command = llvm::MachO::encryption_info_command; 155 156 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC; 157 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT; 158 static constexpr uint32_t encryptionInfoLCType = 159 llvm::MachO::LC_ENCRYPTION_INFO; 160 161 static constexpr uint64_t pageZeroSize = 1ull << 12; 162 static constexpr size_t wordSize = 4; 163 }; 164 165 extern TargetInfo *target; 166 167 } // namespace macho 168 } // namespace lld 169 170 #endif 171