1 //===- Target.h -------------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLD_ELF_TARGET_H 11 #define LLD_ELF_TARGET_H 12 13 #include "InputSection.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "llvm/Object/ELF.h" 16 #include "llvm/Support/MathExtras.h" 17 18 namespace lld { 19 std::string toString(elf::RelType Type); 20 21 namespace elf { 22 class Defined; 23 class InputFile; 24 class Symbol; 25 26 class TargetInfo { 27 public: 28 virtual uint32_t calcEFlags() const { return 0; } 29 virtual RelType getDynRel(RelType Type) const { return Type; } 30 virtual void writeGotPltHeader(uint8_t *Buf) const {} 31 virtual void writeGotHeader(uint8_t *Buf) const {} 32 virtual void writeGotPlt(uint8_t *Buf, const Symbol &S) const {}; 33 virtual void writeIgotPlt(uint8_t *Buf, const Symbol &S) const; 34 virtual int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const; 35 36 // If lazy binding is supported, the first entry of the PLT has code 37 // to call the dynamic linker to resolve PLT entries the first time 38 // they are called. This function writes that code. 39 virtual void writePltHeader(uint8_t *Buf) const {} 40 41 virtual void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, 42 uint64_t PltEntryAddr, int32_t Index, 43 unsigned RelOff) const {} 44 virtual void addPltHeaderSymbols(InputSection &IS) const {} 45 virtual void addPltSymbols(InputSection &IS, uint64_t Off) const {} 46 47 unsigned getPltEntryOffset(unsigned Index) const { 48 return Index * PltEntrySize + PltHeaderSize; 49 } 50 51 // Returns true if a relocation only uses the low bits of a value such that 52 // all those bits are in the same page. For example, if the relocation 53 // only uses the low 12 bits in a system with 4k pages. If this is true, the 54 // bits will always have the same value at runtime and we don't have to emit 55 // a dynamic relocation. 56 virtual bool usesOnlyLowPageBits(RelType Type) const; 57 58 // Decide whether a Thunk is needed for the relocation from File 59 // targeting S. 60 virtual bool needsThunk(RelExpr Expr, RelType RelocType, 61 const InputFile *File, uint64_t BranchAddr, 62 const Symbol &S) const; 63 64 // On systems with range extensions we place collections of Thunks at 65 // regular spacings that enable the majority of branches reach the Thunks. 66 // a value of 0 means range extension thunks are not supported. 67 virtual uint32_t getThunkSectionSpacing() const { return 0; } 68 69 // The function with a prologue starting at Loc was compiled with 70 // -fsplit-stack and it calls a function compiled without. Adjust the prologue 71 // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks. 72 // The symbols st_other flags are needed on PowerPC64 for determining the 73 // offset to the split-stack prologue. 74 virtual bool adjustPrologueForCrossSplitStack(uint8_t *Loc, uint8_t *End, 75 uint8_t StOther) const; 76 77 // Return true if we can reach Dst from Src with Relocation RelocType 78 virtual bool inBranchRange(RelType Type, uint64_t Src, 79 uint64_t Dst) const; 80 virtual RelExpr getRelExpr(RelType Type, const Symbol &S, 81 const uint8_t *Loc) const = 0; 82 83 virtual void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const = 0; 84 85 virtual ~TargetInfo(); 86 87 unsigned TlsGdRelaxSkip = 1; 88 unsigned PageSize = 4096; 89 unsigned DefaultMaxPageSize = 4096; 90 91 uint64_t getImageBase(); 92 93 // Offset of _GLOBAL_OFFSET_TABLE_ from base of .got or .got.plt section. 94 uint64_t GotBaseSymOff = 0; 95 // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got. 96 bool GotBaseSymInGotPlt = true; 97 98 RelType CopyRel; 99 RelType GotRel; 100 RelType NoneRel; 101 RelType PltRel; 102 RelType RelativeRel; 103 RelType IRelativeRel; 104 RelType TlsDescRel; 105 RelType TlsGotRel; 106 RelType TlsModuleIndexRel; 107 RelType TlsOffsetRel; 108 unsigned GotEntrySize = 0; 109 unsigned GotPltEntrySize = 0; 110 unsigned PltEntrySize; 111 unsigned PltHeaderSize; 112 113 // At least on x86_64 positions 1 and 2 are used by the first plt entry 114 // to support lazy loading. 115 unsigned GotPltHeaderEntriesNum = 3; 116 117 // On PPC ELF V2 abi, the first entry in the .got is the .TOC. 118 unsigned GotHeaderEntriesNum = 0; 119 120 // For TLS variant 1, the TCB is a fixed size specified by the Target. 121 // For variant 2, the TCB is an unspecified size. 122 // Set to 0 for variant 2. 123 unsigned TcbSize = 0; 124 125 // Set to the offset (in bytes) that the thread pointer is initialized to 126 // point to, relative to the start of the thread local storage. 127 unsigned TlsTpOffset = 0; 128 129 bool NeedsThunks = false; 130 131 // A 4-byte field corresponding to one or more trap instructions, used to pad 132 // executable OutputSections. 133 uint32_t TrapInstr = 0; 134 135 // If a target needs to rewrite calls to __morestack to instead call 136 // __morestack_non_split when a split-stack enabled caller calls a 137 // non-split-stack callee this will return true. Otherwise returns false. 138 bool NeedsMoreStackNonSplit = true; 139 140 virtual RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data, 141 RelExpr Expr) const; 142 virtual void relaxGot(uint8_t *Loc, uint64_t Val) const; 143 virtual void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const; 144 virtual void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const; 145 virtual void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const; 146 virtual void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const; 147 148 protected: 149 // On FreeBSD x86_64 the first page cannot be mmaped. 150 // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64 151 // installs that is 65536, so the first 15 pages cannot be used. 152 // Given that, the smallest value that can be used in here is 0x10000. 153 uint64_t DefaultImageBase = 0x10000; 154 }; 155 156 TargetInfo *getAArch64TargetInfo(); 157 TargetInfo *getAMDGPUTargetInfo(); 158 TargetInfo *getARMTargetInfo(); 159 TargetInfo *getAVRTargetInfo(); 160 TargetInfo *getHexagonTargetInfo(); 161 TargetInfo *getPPC64TargetInfo(); 162 TargetInfo *getPPCTargetInfo(); 163 TargetInfo *getRISCVTargetInfo(); 164 TargetInfo *getSPARCV9TargetInfo(); 165 TargetInfo *getX32TargetInfo(); 166 TargetInfo *getX86TargetInfo(); 167 TargetInfo *getX86_64TargetInfo(); 168 template <class ELFT> TargetInfo *getMipsTargetInfo(); 169 170 struct ErrorPlace { 171 InputSectionBase *IS; 172 std::string Loc; 173 }; 174 175 // Returns input section and corresponding source string for the given location. 176 ErrorPlace getErrorPlace(const uint8_t *Loc); 177 178 static inline std::string getErrorLocation(const uint8_t *Loc) { 179 return getErrorPlace(Loc).Loc; 180 } 181 182 // In the PowerPC64 Elf V2 abi a function can have 2 entry points. The first is 183 // a global entry point (GEP) which typically is used to intiailzie the TOC 184 // pointer in general purpose register 2. The second is a local entry 185 // point (LEP) which bypasses the TOC pointer initialization code. The 186 // offset between GEP and LEP is encoded in a function's st_other flags. 187 // This function will return the offset (in bytes) from the global entry-point 188 // to the local entry-point. 189 unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t StOther); 190 191 uint64_t getPPC64TocBase(); 192 uint64_t getAArch64Page(uint64_t Expr); 193 194 extern TargetInfo *Target; 195 TargetInfo *getTarget(); 196 197 template <class ELFT> bool isMipsPIC(const Defined *Sym); 198 199 static inline void reportRangeError(uint8_t *Loc, RelType Type, const Twine &V, 200 int64_t Min, uint64_t Max) { 201 ErrorPlace ErrPlace = getErrorPlace(Loc); 202 StringRef Hint; 203 if (ErrPlace.IS && ErrPlace.IS->Name.startswith(".debug")) 204 Hint = "; consider recompiling with -fdebug-types-section to reduce size " 205 "of debug sections"; 206 207 error(ErrPlace.Loc + "relocation " + lld::toString(Type) + 208 " out of range: " + V.str() + " is not in [" + Twine(Min).str() + ", " + 209 Twine(Max).str() + "]" + Hint); 210 } 211 212 // Make sure that V can be represented as an N bit signed integer. 213 inline void checkInt(uint8_t *Loc, int64_t V, int N, RelType Type) { 214 if (V != llvm::SignExtend64(V, N)) 215 reportRangeError(Loc, Type, Twine(V), llvm::minIntN(N), llvm::maxIntN(N)); 216 } 217 218 // Make sure that V can be represented as an N bit unsigned integer. 219 inline void checkUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) { 220 if ((V >> N) != 0) 221 reportRangeError(Loc, Type, Twine(V), 0, llvm::maxUIntN(N)); 222 } 223 224 // Make sure that V can be represented as an N bit signed or unsigned integer. 225 inline void checkIntUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) { 226 // For the error message we should cast V to a signed integer so that error 227 // messages show a small negative value rather than an extremely large one 228 if (V != (uint64_t)llvm::SignExtend64(V, N) && (V >> N) != 0) 229 reportRangeError(Loc, Type, Twine((int64_t)V), llvm::minIntN(N), 230 llvm::maxIntN(N)); 231 } 232 233 inline void checkAlignment(uint8_t *Loc, uint64_t V, int N, RelType Type) { 234 if ((V & (N - 1)) != 0) 235 error(getErrorLocation(Loc) + "improper alignment for relocation " + 236 lld::toString(Type) + ": 0x" + llvm::utohexstr(V) + 237 " is not aligned to " + Twine(N) + " bytes"); 238 } 239 240 // Endianness-aware read/write. 241 inline uint16_t read16(const void *P) { 242 return llvm::support::endian::read16(P, Config->Endianness); 243 } 244 245 inline uint32_t read32(const void *P) { 246 return llvm::support::endian::read32(P, Config->Endianness); 247 } 248 249 inline uint64_t read64(const void *P) { 250 return llvm::support::endian::read64(P, Config->Endianness); 251 } 252 253 inline void write16(void *P, uint16_t V) { 254 llvm::support::endian::write16(P, V, Config->Endianness); 255 } 256 257 inline void write32(void *P, uint32_t V) { 258 llvm::support::endian::write32(P, V, Config->Endianness); 259 } 260 261 inline void write64(void *P, uint64_t V) { 262 llvm::support::endian::write64(P, V, Config->Endianness); 263 } 264 } // namespace elf 265 } // namespace lld 266 267 #endif 268