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