10b57cec5SDimitry Andric //===- Target.h -------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLD_ELF_TARGET_H 100b57cec5SDimitry Andric #define LLD_ELF_TARGET_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "InputSection.h" 130b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 140b57cec5SDimitry Andric #include "llvm/Object/ELF.h" 150b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 160b57cec5SDimitry Andric #include <array> 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric namespace lld { 190b57cec5SDimitry Andric std::string toString(elf::RelType type); 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric namespace elf { 220b57cec5SDimitry Andric class Defined; 230b57cec5SDimitry Andric class InputFile; 240b57cec5SDimitry Andric class Symbol; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric class TargetInfo { 270b57cec5SDimitry Andric public: 280b57cec5SDimitry Andric virtual uint32_t calcEFlags() const { return 0; } 290b57cec5SDimitry Andric virtual RelExpr getRelExpr(RelType type, const Symbol &s, 300b57cec5SDimitry Andric const uint8_t *loc) const = 0; 310b57cec5SDimitry Andric virtual RelType getDynRel(RelType type) const { return 0; } 320b57cec5SDimitry Andric virtual void writeGotPltHeader(uint8_t *buf) const {} 330b57cec5SDimitry Andric virtual void writeGotHeader(uint8_t *buf) const {} 340b57cec5SDimitry Andric virtual void writeGotPlt(uint8_t *buf, const Symbol &s) const {}; 35480093f4SDimitry Andric virtual void writeIgotPlt(uint8_t *buf, const Symbol &s) const {} 360b57cec5SDimitry Andric virtual int64_t getImplicitAddend(const uint8_t *buf, RelType type) const; 370b57cec5SDimitry Andric virtual int getTlsGdRelaxSkip(RelType type) const { return 1; } 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric // If lazy binding is supported, the first entry of the PLT has code 400b57cec5SDimitry Andric // to call the dynamic linker to resolve PLT entries the first time 410b57cec5SDimitry Andric // they are called. This function writes that code. 420b57cec5SDimitry Andric virtual void writePltHeader(uint8_t *buf) const {} 430b57cec5SDimitry Andric 44480093f4SDimitry Andric virtual void writePlt(uint8_t *buf, const Symbol &sym, 45480093f4SDimitry Andric uint64_t pltEntryAddr) const {} 46480093f4SDimitry Andric virtual void writeIplt(uint8_t *buf, const Symbol &sym, 47480093f4SDimitry Andric uint64_t pltEntryAddr) const { 48480093f4SDimitry Andric // All but PPC32 and PPC64 use the same format for .plt and .iplt entries. 49480093f4SDimitry Andric writePlt(buf, sym, pltEntryAddr); 50480093f4SDimitry Andric } 51480093f4SDimitry Andric virtual void writeIBTPlt(uint8_t *buf, size_t numEntries) const {} 520b57cec5SDimitry Andric virtual void addPltHeaderSymbols(InputSection &isec) const {} 530b57cec5SDimitry Andric virtual void addPltSymbols(InputSection &isec, uint64_t off) const {} 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric // Returns true if a relocation only uses the low bits of a value such that 560b57cec5SDimitry Andric // all those bits are in the same page. For example, if the relocation 570b57cec5SDimitry Andric // only uses the low 12 bits in a system with 4k pages. If this is true, the 580b57cec5SDimitry Andric // bits will always have the same value at runtime and we don't have to emit 590b57cec5SDimitry Andric // a dynamic relocation. 600b57cec5SDimitry Andric virtual bool usesOnlyLowPageBits(RelType type) const; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric // Decide whether a Thunk is needed for the relocation from File 630b57cec5SDimitry Andric // targeting S. 640b57cec5SDimitry Andric virtual bool needsThunk(RelExpr expr, RelType relocType, 650b57cec5SDimitry Andric const InputFile *file, uint64_t branchAddr, 66480093f4SDimitry Andric const Symbol &s, int64_t a) const; 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // On systems with range extensions we place collections of Thunks at 690b57cec5SDimitry Andric // regular spacings that enable the majority of branches reach the Thunks. 700b57cec5SDimitry Andric // a value of 0 means range extension thunks are not supported. 710b57cec5SDimitry Andric virtual uint32_t getThunkSectionSpacing() const { return 0; } 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric // The function with a prologue starting at Loc was compiled with 740b57cec5SDimitry Andric // -fsplit-stack and it calls a function compiled without. Adjust the prologue 750b57cec5SDimitry Andric // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks. 760b57cec5SDimitry Andric // The symbols st_other flags are needed on PowerPC64 for determining the 770b57cec5SDimitry Andric // offset to the split-stack prologue. 780b57cec5SDimitry Andric virtual bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 790b57cec5SDimitry Andric uint8_t stOther) const; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // Return true if we can reach dst from src with RelType type. 820b57cec5SDimitry Andric virtual bool inBranchRange(RelType type, uint64_t src, 830b57cec5SDimitry Andric uint64_t dst) const; 840b57cec5SDimitry Andric 855ffd83dbSDimitry Andric virtual void relocate(uint8_t *loc, const Relocation &rel, 865ffd83dbSDimitry Andric uint64_t val) const = 0; 875ffd83dbSDimitry Andric void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const { 885ffd83dbSDimitry Andric relocate(loc, Relocation{R_NONE, type, 0, 0, nullptr}, val); 895ffd83dbSDimitry Andric } 905ffd83dbSDimitry Andric 915ffd83dbSDimitry Andric virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type, 925ffd83dbSDimitry Andric JumpModType val) const {} 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric virtual ~TargetInfo(); 950b57cec5SDimitry Andric 965ffd83dbSDimitry Andric // This deletes a jump insn at the end of the section if it is a fall thru to 975ffd83dbSDimitry Andric // the next section. Further, if there is a conditional jump and a direct 985ffd83dbSDimitry Andric // jump consecutively, it tries to flip the conditional jump to convert the 995ffd83dbSDimitry Andric // direct jump into a fall thru and delete it. Returns true if a jump 1005ffd83dbSDimitry Andric // instruction can be deleted. 1015ffd83dbSDimitry Andric virtual bool deleteFallThruJmpInsn(InputSection &is, InputFile *file, 1025ffd83dbSDimitry Andric InputSection *nextIS) const { 1035ffd83dbSDimitry Andric return false; 1045ffd83dbSDimitry Andric } 1055ffd83dbSDimitry Andric 1060b57cec5SDimitry Andric unsigned defaultCommonPageSize = 4096; 1070b57cec5SDimitry Andric unsigned defaultMaxPageSize = 4096; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric uint64_t getImageBase() const; 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got. 1120b57cec5SDimitry Andric bool gotBaseSymInGotPlt = true; 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric RelType copyRel; 1150b57cec5SDimitry Andric RelType gotRel; 1160b57cec5SDimitry Andric RelType noneRel; 1170b57cec5SDimitry Andric RelType pltRel; 1180b57cec5SDimitry Andric RelType relativeRel; 1190b57cec5SDimitry Andric RelType iRelativeRel; 1200b57cec5SDimitry Andric RelType symbolicRel; 1210b57cec5SDimitry Andric RelType tlsDescRel; 1220b57cec5SDimitry Andric RelType tlsGotRel; 1230b57cec5SDimitry Andric RelType tlsModuleIndexRel; 1240b57cec5SDimitry Andric RelType tlsOffsetRel; 125*fe6060f1SDimitry Andric unsigned gotEntrySize = config->wordsize; 1260b57cec5SDimitry Andric unsigned pltEntrySize; 1270b57cec5SDimitry Andric unsigned pltHeaderSize; 128480093f4SDimitry Andric unsigned ipltEntrySize; 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric // At least on x86_64 positions 1 and 2 are used by the first plt entry 1310b57cec5SDimitry Andric // to support lazy loading. 1320b57cec5SDimitry Andric unsigned gotPltHeaderEntriesNum = 3; 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric // On PPC ELF V2 abi, the first entry in the .got is the .TOC. 1350b57cec5SDimitry Andric unsigned gotHeaderEntriesNum = 0; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric bool needsThunks = false; 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric // A 4-byte field corresponding to one or more trap instructions, used to pad 1400b57cec5SDimitry Andric // executable OutputSections. 1410b57cec5SDimitry Andric std::array<uint8_t, 4> trapInstr; 1420b57cec5SDimitry Andric 1435ffd83dbSDimitry Andric // Stores the NOP instructions of different sizes for the target and is used 1445ffd83dbSDimitry Andric // to pad sections that are relaxed. 1455ffd83dbSDimitry Andric llvm::Optional<std::vector<std::vector<uint8_t>>> nopInstrs; 1465ffd83dbSDimitry Andric 1470b57cec5SDimitry Andric // If a target needs to rewrite calls to __morestack to instead call 1480b57cec5SDimitry Andric // __morestack_non_split when a split-stack enabled caller calls a 1490b57cec5SDimitry Andric // non-split-stack callee this will return true. Otherwise returns false. 1500b57cec5SDimitry Andric bool needsMoreStackNonSplit = true; 1510b57cec5SDimitry Andric 152e8d8bef9SDimitry Andric virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const; 153e8d8bef9SDimitry Andric virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend, 154e8d8bef9SDimitry Andric const uint8_t *loc) const; 1555ffd83dbSDimitry Andric virtual void relaxGot(uint8_t *loc, const Relocation &rel, 1565ffd83dbSDimitry Andric uint64_t val) const; 1575ffd83dbSDimitry Andric virtual void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 1585ffd83dbSDimitry Andric uint64_t val) const; 1595ffd83dbSDimitry Andric virtual void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 1605ffd83dbSDimitry Andric uint64_t val) const; 1615ffd83dbSDimitry Andric virtual void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 1625ffd83dbSDimitry Andric uint64_t val) const; 1635ffd83dbSDimitry Andric virtual void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 1645ffd83dbSDimitry Andric uint64_t val) const; 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric protected: 1670b57cec5SDimitry Andric // On FreeBSD x86_64 the first page cannot be mmaped. 168480093f4SDimitry Andric // On Linux this is controlled by vm.mmap_min_addr. At least on some x86_64 169480093f4SDimitry Andric // installs this is set to 65536, so the first 15 pages cannot be used. 1700b57cec5SDimitry Andric // Given that, the smallest value that can be used in here is 0x10000. 1710b57cec5SDimitry Andric uint64_t defaultImageBase = 0x10000; 1720b57cec5SDimitry Andric }; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric TargetInfo *getAArch64TargetInfo(); 1750b57cec5SDimitry Andric TargetInfo *getAMDGPUTargetInfo(); 1760b57cec5SDimitry Andric TargetInfo *getARMTargetInfo(); 1770b57cec5SDimitry Andric TargetInfo *getAVRTargetInfo(); 1780b57cec5SDimitry Andric TargetInfo *getHexagonTargetInfo(); 1790b57cec5SDimitry Andric TargetInfo *getMSP430TargetInfo(); 1800b57cec5SDimitry Andric TargetInfo *getPPC64TargetInfo(); 1810b57cec5SDimitry Andric TargetInfo *getPPCTargetInfo(); 1820b57cec5SDimitry Andric TargetInfo *getRISCVTargetInfo(); 1830b57cec5SDimitry Andric TargetInfo *getSPARCV9TargetInfo(); 1840b57cec5SDimitry Andric TargetInfo *getX86TargetInfo(); 1850b57cec5SDimitry Andric TargetInfo *getX86_64TargetInfo(); 1860b57cec5SDimitry Andric template <class ELFT> TargetInfo *getMipsTargetInfo(); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric struct ErrorPlace { 1890b57cec5SDimitry Andric InputSectionBase *isec; 1900b57cec5SDimitry Andric std::string loc; 1910b57cec5SDimitry Andric }; 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric // Returns input section and corresponding source string for the given location. 1940b57cec5SDimitry Andric ErrorPlace getErrorPlace(const uint8_t *loc); 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric static inline std::string getErrorLocation(const uint8_t *loc) { 1970b57cec5SDimitry Andric return getErrorPlace(loc).loc; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric void writePPC32GlinkSection(uint8_t *buf, size_t numEntries); 2010b57cec5SDimitry Andric 2025ffd83dbSDimitry Andric bool tryRelaxPPC64TocIndirection(const Relocation &rel, uint8_t *bufLoc); 2030b57cec5SDimitry Andric unsigned getPPCDFormOp(unsigned secondaryOp); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // In the PowerPC64 Elf V2 abi a function can have 2 entry points. The first 2060b57cec5SDimitry Andric // is a global entry point (GEP) which typically is used to initialize the TOC 2070b57cec5SDimitry Andric // pointer in general purpose register 2. The second is a local entry 2080b57cec5SDimitry Andric // point (LEP) which bypasses the TOC pointer initialization code. The 2090b57cec5SDimitry Andric // offset between GEP and LEP is encoded in a function's st_other flags. 2100b57cec5SDimitry Andric // This function will return the offset (in bytes) from the global entry-point 2110b57cec5SDimitry Andric // to the local entry-point. 2120b57cec5SDimitry Andric unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // Returns true if a relocation is a small code model relocation that accesses 2150b57cec5SDimitry Andric // the .toc section. 2160b57cec5SDimitry Andric bool isPPC64SmallCodeModelTocReloc(RelType type); 2170b57cec5SDimitry Andric 218e8d8bef9SDimitry Andric // Write a prefixed instruction, which is a 4-byte prefix followed by a 4-byte 219e8d8bef9SDimitry Andric // instruction (regardless of endianness). Therefore, the prefix is always in 220e8d8bef9SDimitry Andric // lower memory than the instruction. 221e8d8bef9SDimitry Andric void writePrefixedInstruction(uint8_t *loc, uint64_t insn); 222e8d8bef9SDimitry Andric 2235ffd83dbSDimitry Andric void addPPC64SaveRestore(); 2240b57cec5SDimitry Andric uint64_t getPPC64TocBase(); 2250b57cec5SDimitry Andric uint64_t getAArch64Page(uint64_t expr); 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric extern const TargetInfo *target; 2280b57cec5SDimitry Andric TargetInfo *getTarget(); 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric template <class ELFT> bool isMipsPIC(const Defined *sym); 2310b57cec5SDimitry Andric 2325ffd83dbSDimitry Andric void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, 2335ffd83dbSDimitry Andric int64_t min, uint64_t max); 234e8d8bef9SDimitry Andric void reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym, 235e8d8bef9SDimitry Andric const Twine &msg); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed integer. 2385ffd83dbSDimitry Andric inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) { 2390b57cec5SDimitry Andric if (v != llvm::SignExtend64(v, n)) 2405ffd83dbSDimitry Andric reportRangeError(loc, rel, Twine(v), llvm::minIntN(n), llvm::maxIntN(n)); 2410b57cec5SDimitry Andric } 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // Make sure that V can be represented as an N bit unsigned integer. 2445ffd83dbSDimitry Andric inline void checkUInt(uint8_t *loc, uint64_t v, int n, const Relocation &rel) { 2450b57cec5SDimitry Andric if ((v >> n) != 0) 2465ffd83dbSDimitry Andric reportRangeError(loc, rel, Twine(v), 0, llvm::maxUIntN(n)); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed or unsigned integer. 2505ffd83dbSDimitry Andric inline void checkIntUInt(uint8_t *loc, uint64_t v, int n, 2515ffd83dbSDimitry Andric const Relocation &rel) { 2520b57cec5SDimitry Andric // For the error message we should cast V to a signed integer so that error 2530b57cec5SDimitry Andric // messages show a small negative value rather than an extremely large one 2540b57cec5SDimitry Andric if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0) 2555ffd83dbSDimitry Andric reportRangeError(loc, rel, Twine((int64_t)v), llvm::minIntN(n), 2560b57cec5SDimitry Andric llvm::maxUIntN(n)); 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2595ffd83dbSDimitry Andric inline void checkAlignment(uint8_t *loc, uint64_t v, int n, 2605ffd83dbSDimitry Andric const Relocation &rel) { 2610b57cec5SDimitry Andric if ((v & (n - 1)) != 0) 2620b57cec5SDimitry Andric error(getErrorLocation(loc) + "improper alignment for relocation " + 2635ffd83dbSDimitry Andric lld::toString(rel.type) + ": 0x" + llvm::utohexstr(v) + 2640b57cec5SDimitry Andric " is not aligned to " + Twine(n) + " bytes"); 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric // Endianness-aware read/write. 2680b57cec5SDimitry Andric inline uint16_t read16(const void *p) { 2690b57cec5SDimitry Andric return llvm::support::endian::read16(p, config->endianness); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric inline uint32_t read32(const void *p) { 2730b57cec5SDimitry Andric return llvm::support::endian::read32(p, config->endianness); 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric inline uint64_t read64(const void *p) { 2770b57cec5SDimitry Andric return llvm::support::endian::read64(p, config->endianness); 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric inline void write16(void *p, uint16_t v) { 2810b57cec5SDimitry Andric llvm::support::endian::write16(p, v, config->endianness); 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric inline void write32(void *p, uint32_t v) { 2850b57cec5SDimitry Andric llvm::support::endian::write32(p, v, config->endianness); 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric inline void write64(void *p, uint64_t v) { 2890b57cec5SDimitry Andric llvm::support::endian::write64(p, v, config->endianness); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric } // namespace elf 2920b57cec5SDimitry Andric } // namespace lld 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric #endif 295