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_ELF_TARGET_H 10 #define LLD_ELF_TARGET_H 11 12 #include "Config.h" 13 #include "InputSection.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "llvm/Object/ELF.h" 16 #include "llvm/Support/MathExtras.h" 17 #include <array> 18 19 namespace lld { 20 std::string toString(elf::RelType type); 21 22 namespace elf { 23 class Defined; 24 class InputFile; 25 class Symbol; 26 27 class TargetInfo { 28 public: 29 virtual uint32_t calcEFlags() const { return 0; } 30 virtual RelExpr getRelExpr(RelType type, const Symbol &s, 31 const uint8_t *loc) const = 0; 32 virtual RelType getDynRel(RelType type) const { return 0; } 33 virtual void writeGotPltHeader(uint8_t *buf) const {} 34 virtual void writeGotHeader(uint8_t *buf) const {} 35 virtual void writeGotPlt(uint8_t *buf, const Symbol &s) const {}; 36 virtual void writeIgotPlt(uint8_t *buf, const Symbol &s) const {} 37 virtual int64_t getImplicitAddend(const uint8_t *buf, RelType type) const; 38 virtual int getTlsGdRelaxSkip(RelType type) const { return 1; } 39 40 // If lazy binding is supported, the first entry of the PLT has code 41 // to call the dynamic linker to resolve PLT entries the first time 42 // they are called. This function writes that code. 43 virtual void writePltHeader(uint8_t *buf) const {} 44 45 virtual void writePlt(uint8_t *buf, const Symbol &sym, 46 uint64_t pltEntryAddr) const {} 47 virtual void writeIplt(uint8_t *buf, const Symbol &sym, 48 uint64_t pltEntryAddr) const { 49 // All but PPC32 and PPC64 use the same format for .plt and .iplt entries. 50 writePlt(buf, sym, pltEntryAddr); 51 } 52 virtual void writeIBTPlt(uint8_t *buf, size_t numEntries) const {} 53 virtual void addPltHeaderSymbols(InputSection &isec) const {} 54 virtual void addPltSymbols(InputSection &isec, uint64_t off) const {} 55 56 // Returns true if a relocation only uses the low bits of a value such that 57 // all those bits are in the same page. For example, if the relocation 58 // only uses the low 12 bits in a system with 4k pages. If this is true, the 59 // bits will always have the same value at runtime and we don't have to emit 60 // a dynamic relocation. 61 virtual bool usesOnlyLowPageBits(RelType type) const; 62 63 // Decide whether a Thunk is needed for the relocation from File 64 // targeting S. 65 virtual bool needsThunk(RelExpr expr, RelType relocType, 66 const InputFile *file, uint64_t branchAddr, 67 const Symbol &s, int64_t a) const; 68 69 // On systems with range extensions we place collections of Thunks at 70 // regular spacings that enable the majority of branches reach the Thunks. 71 // a value of 0 means range extension thunks are not supported. 72 virtual uint32_t getThunkSectionSpacing() const { return 0; } 73 74 // The function with a prologue starting at Loc was compiled with 75 // -fsplit-stack and it calls a function compiled without. Adjust the prologue 76 // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks. 77 // The symbols st_other flags are needed on PowerPC64 for determining the 78 // offset to the split-stack prologue. 79 virtual bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 80 uint8_t stOther) const; 81 82 // Return true if we can reach dst from src with RelType type. 83 virtual bool inBranchRange(RelType type, uint64_t src, 84 uint64_t dst) const; 85 86 virtual void relocate(uint8_t *loc, const Relocation &rel, 87 uint64_t val) const = 0; 88 void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const { 89 relocate(loc, Relocation{R_NONE, type, 0, 0, nullptr}, val); 90 } 91 92 virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type, 93 JumpModType val) const {} 94 95 virtual ~TargetInfo(); 96 97 // This deletes a jump insn at the end of the section if it is a fall thru to 98 // the next section. Further, if there is a conditional jump and a direct 99 // jump consecutively, it tries to flip the conditional jump to convert the 100 // direct jump into a fall thru and delete it. Returns true if a jump 101 // instruction can be deleted. 102 virtual bool deleteFallThruJmpInsn(InputSection &is, InputFile *file, 103 InputSection *nextIS) const { 104 return false; 105 } 106 107 unsigned defaultCommonPageSize = 4096; 108 unsigned defaultMaxPageSize = 4096; 109 110 uint64_t getImageBase() const; 111 112 // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got. 113 bool gotBaseSymInGotPlt = false; 114 115 static constexpr RelType noneRel = 0; 116 RelType copyRel; 117 RelType gotRel; 118 RelType pltRel; 119 RelType relativeRel; 120 RelType iRelativeRel; 121 RelType symbolicRel; 122 RelType tlsDescRel; 123 RelType tlsGotRel; 124 RelType tlsModuleIndexRel; 125 RelType tlsOffsetRel; 126 unsigned gotEntrySize = config->wordsize; 127 unsigned pltEntrySize; 128 unsigned pltHeaderSize; 129 unsigned ipltEntrySize; 130 131 // At least on x86_64 positions 1 and 2 are used by the first plt entry 132 // to support lazy loading. 133 unsigned gotPltHeaderEntriesNum = 3; 134 135 // On PPC ELF V2 abi, the first entry in the .got is the .TOC. 136 unsigned gotHeaderEntriesNum = 0; 137 138 bool needsThunks = false; 139 140 // A 4-byte field corresponding to one or more trap instructions, used to pad 141 // executable OutputSections. 142 std::array<uint8_t, 4> trapInstr; 143 144 // Stores the NOP instructions of different sizes for the target and is used 145 // to pad sections that are relaxed. 146 llvm::Optional<std::vector<std::vector<uint8_t>>> nopInstrs; 147 148 // If a target needs to rewrite calls to __morestack to instead call 149 // __morestack_non_split when a split-stack enabled caller calls a 150 // non-split-stack callee this will return true. Otherwise returns false. 151 bool needsMoreStackNonSplit = true; 152 153 virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const; 154 virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend, 155 const uint8_t *loc) const; 156 virtual void relaxGot(uint8_t *loc, const Relocation &rel, 157 uint64_t val) const; 158 virtual void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 159 uint64_t val) const; 160 virtual void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 161 uint64_t val) const; 162 virtual void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 163 uint64_t val) const; 164 virtual void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 165 uint64_t val) const; 166 167 protected: 168 // On FreeBSD x86_64 the first page cannot be mmaped. 169 // On Linux this is controlled by vm.mmap_min_addr. At least on some x86_64 170 // installs this is set to 65536, so the first 15 pages cannot be used. 171 // Given that, the smallest value that can be used in here is 0x10000. 172 uint64_t defaultImageBase = 0x10000; 173 }; 174 175 TargetInfo *getAArch64TargetInfo(); 176 TargetInfo *getAMDGPUTargetInfo(); 177 TargetInfo *getARMTargetInfo(); 178 TargetInfo *getAVRTargetInfo(); 179 TargetInfo *getHexagonTargetInfo(); 180 TargetInfo *getMSP430TargetInfo(); 181 TargetInfo *getPPC64TargetInfo(); 182 TargetInfo *getPPCTargetInfo(); 183 TargetInfo *getRISCVTargetInfo(); 184 TargetInfo *getSPARCV9TargetInfo(); 185 TargetInfo *getX86TargetInfo(); 186 TargetInfo *getX86_64TargetInfo(); 187 template <class ELFT> TargetInfo *getMipsTargetInfo(); 188 189 struct ErrorPlace { 190 InputSectionBase *isec; 191 std::string loc; 192 std::string srcLoc; 193 }; 194 195 // Returns input section and corresponding source string for the given location. 196 ErrorPlace getErrorPlace(const uint8_t *loc); 197 198 static inline std::string getErrorLocation(const uint8_t *loc) { 199 return getErrorPlace(loc).loc; 200 } 201 202 void writePPC32GlinkSection(uint8_t *buf, size_t numEntries); 203 204 bool tryRelaxPPC64TocIndirection(const Relocation &rel, uint8_t *bufLoc); 205 unsigned getPPCDFormOp(unsigned secondaryOp); 206 207 // In the PowerPC64 Elf V2 abi a function can have 2 entry points. The first 208 // is a global entry point (GEP) which typically is used to initialize the TOC 209 // pointer in general purpose register 2. The second is a local entry 210 // point (LEP) which bypasses the TOC pointer initialization code. The 211 // offset between GEP and LEP is encoded in a function's st_other flags. 212 // This function will return the offset (in bytes) from the global entry-point 213 // to the local entry-point. 214 unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther); 215 216 // Write a prefixed instruction, which is a 4-byte prefix followed by a 4-byte 217 // instruction (regardless of endianness). Therefore, the prefix is always in 218 // lower memory than the instruction. 219 void writePrefixedInstruction(uint8_t *loc, uint64_t insn); 220 221 void addPPC64SaveRestore(); 222 uint64_t getPPC64TocBase(); 223 uint64_t getAArch64Page(uint64_t expr); 224 225 class AArch64Relaxer { 226 bool safeToRelaxAdrpLdr = true; 227 228 public: 229 explicit AArch64Relaxer(ArrayRef<Relocation> relocs); 230 231 bool tryRelaxAdrpAdd(const Relocation &adrpRel, const Relocation &addRel, 232 uint64_t secAddr, uint8_t *buf) const; 233 bool tryRelaxAdrpLdr(const Relocation &adrpRel, const Relocation &ldrRel, 234 uint64_t secAddr, uint8_t *buf) const; 235 }; 236 237 extern const TargetInfo *target; 238 TargetInfo *getTarget(); 239 240 template <class ELFT> bool isMipsPIC(const Defined *sym); 241 242 void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, 243 int64_t min, uint64_t max); 244 void reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym, 245 const Twine &msg); 246 247 // Make sure that V can be represented as an N bit signed integer. 248 inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) { 249 if (v != llvm::SignExtend64(v, n)) 250 reportRangeError(loc, rel, Twine(v), llvm::minIntN(n), llvm::maxIntN(n)); 251 } 252 253 // Make sure that V can be represented as an N bit unsigned integer. 254 inline void checkUInt(uint8_t *loc, uint64_t v, int n, const Relocation &rel) { 255 if ((v >> n) != 0) 256 reportRangeError(loc, rel, Twine(v), 0, llvm::maxUIntN(n)); 257 } 258 259 // Make sure that V can be represented as an N bit signed or unsigned integer. 260 inline void checkIntUInt(uint8_t *loc, uint64_t v, int n, 261 const Relocation &rel) { 262 // For the error message we should cast V to a signed integer so that error 263 // messages show a small negative value rather than an extremely large one 264 if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0) 265 reportRangeError(loc, rel, Twine((int64_t)v), llvm::minIntN(n), 266 llvm::maxUIntN(n)); 267 } 268 269 inline void checkAlignment(uint8_t *loc, uint64_t v, int n, 270 const Relocation &rel) { 271 if ((v & (n - 1)) != 0) 272 error(getErrorLocation(loc) + "improper alignment for relocation " + 273 lld::toString(rel.type) + ": 0x" + llvm::utohexstr(v) + 274 " is not aligned to " + Twine(n) + " bytes"); 275 } 276 277 // Endianness-aware read/write. 278 inline uint16_t read16(const void *p) { 279 return llvm::support::endian::read16(p, config->endianness); 280 } 281 282 inline uint32_t read32(const void *p) { 283 return llvm::support::endian::read32(p, config->endianness); 284 } 285 286 inline uint64_t read64(const void *p) { 287 return llvm::support::endian::read64(p, config->endianness); 288 } 289 290 inline void write16(void *p, uint16_t v) { 291 llvm::support::endian::write16(p, v, config->endianness); 292 } 293 294 inline void write32(void *p, uint32_t v) { 295 llvm::support::endian::write32(p, v, config->endianness); 296 } 297 298 inline void write64(void *p, uint64_t v) { 299 llvm::support::endian::write64(p, v, config->endianness); 300 } 301 } // namespace elf 302 } // namespace lld 303 304 #ifdef __clang__ 305 #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" 306 #endif 307 #define invokeELFT(f, ...) \ 308 switch (config->ekind) { \ 309 case ELF32LEKind: \ 310 f<ELF32LE>(__VA_ARGS__); \ 311 break; \ 312 case ELF32BEKind: \ 313 f<ELF32BE>(__VA_ARGS__); \ 314 break; \ 315 case ELF64LEKind: \ 316 f<ELF64LE>(__VA_ARGS__); \ 317 break; \ 318 case ELF64BEKind: \ 319 f<ELF64BE>(__VA_ARGS__); \ 320 break; \ 321 default: \ 322 llvm_unreachable("unknown config->ekind"); \ 323 } 324 325 #endif 326