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 1281ad6265SDimitry Andric #include "Config.h" 130b57cec5SDimitry Andric #include "InputSection.h" 140b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 150b57cec5SDimitry Andric #include "llvm/Object/ELF.h" 160b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 170b57cec5SDimitry Andric #include <array> 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric namespace lld { 200b57cec5SDimitry Andric std::string toString(elf::RelType type); 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric namespace elf { 230b57cec5SDimitry Andric class Defined; 240b57cec5SDimitry Andric class InputFile; 250b57cec5SDimitry Andric class Symbol; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric class TargetInfo { 280b57cec5SDimitry Andric public: 290b57cec5SDimitry Andric virtual uint32_t calcEFlags() const { return 0; } 300b57cec5SDimitry Andric virtual RelExpr getRelExpr(RelType type, const Symbol &s, 310b57cec5SDimitry Andric const uint8_t *loc) const = 0; 320b57cec5SDimitry Andric virtual RelType getDynRel(RelType type) const { return 0; } 330b57cec5SDimitry Andric virtual void writeGotPltHeader(uint8_t *buf) const {} 340b57cec5SDimitry Andric virtual void writeGotHeader(uint8_t *buf) const {} 350b57cec5SDimitry Andric virtual void writeGotPlt(uint8_t *buf, const Symbol &s) const {}; 36480093f4SDimitry Andric virtual void writeIgotPlt(uint8_t *buf, const Symbol &s) const {} 370b57cec5SDimitry Andric virtual int64_t getImplicitAddend(const uint8_t *buf, RelType type) const; 380b57cec5SDimitry Andric virtual int getTlsGdRelaxSkip(RelType type) const { return 1; } 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric // If lazy binding is supported, the first entry of the PLT has code 410b57cec5SDimitry Andric // to call the dynamic linker to resolve PLT entries the first time 420b57cec5SDimitry Andric // they are called. This function writes that code. 430b57cec5SDimitry Andric virtual void writePltHeader(uint8_t *buf) const {} 440b57cec5SDimitry Andric 45480093f4SDimitry Andric virtual void writePlt(uint8_t *buf, const Symbol &sym, 46480093f4SDimitry Andric uint64_t pltEntryAddr) const {} 47480093f4SDimitry Andric virtual void writeIplt(uint8_t *buf, const Symbol &sym, 48480093f4SDimitry Andric uint64_t pltEntryAddr) const { 49480093f4SDimitry Andric // All but PPC32 and PPC64 use the same format for .plt and .iplt entries. 50480093f4SDimitry Andric writePlt(buf, sym, pltEntryAddr); 51480093f4SDimitry Andric } 52480093f4SDimitry Andric virtual void writeIBTPlt(uint8_t *buf, size_t numEntries) const {} 530b57cec5SDimitry Andric virtual void addPltHeaderSymbols(InputSection &isec) const {} 540b57cec5SDimitry Andric virtual void addPltSymbols(InputSection &isec, uint64_t off) const {} 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric // Returns true if a relocation only uses the low bits of a value such that 570b57cec5SDimitry Andric // all those bits are in the same page. For example, if the relocation 580b57cec5SDimitry Andric // only uses the low 12 bits in a system with 4k pages. If this is true, the 590b57cec5SDimitry Andric // bits will always have the same value at runtime and we don't have to emit 600b57cec5SDimitry Andric // a dynamic relocation. 610b57cec5SDimitry Andric virtual bool usesOnlyLowPageBits(RelType type) const; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric // Decide whether a Thunk is needed for the relocation from File 640b57cec5SDimitry Andric // targeting S. 650b57cec5SDimitry Andric virtual bool needsThunk(RelExpr expr, RelType relocType, 660b57cec5SDimitry Andric const InputFile *file, uint64_t branchAddr, 67480093f4SDimitry Andric const Symbol &s, int64_t a) const; 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric // On systems with range extensions we place collections of Thunks at 700b57cec5SDimitry Andric // regular spacings that enable the majority of branches reach the Thunks. 710b57cec5SDimitry Andric // a value of 0 means range extension thunks are not supported. 720b57cec5SDimitry Andric virtual uint32_t getThunkSectionSpacing() const { return 0; } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // The function with a prologue starting at Loc was compiled with 750b57cec5SDimitry Andric // -fsplit-stack and it calls a function compiled without. Adjust the prologue 760b57cec5SDimitry Andric // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks. 770b57cec5SDimitry Andric // The symbols st_other flags are needed on PowerPC64 for determining the 780b57cec5SDimitry Andric // offset to the split-stack prologue. 790b57cec5SDimitry Andric virtual bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end, 800b57cec5SDimitry Andric uint8_t stOther) const; 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric // Return true if we can reach dst from src with RelType type. 830b57cec5SDimitry Andric virtual bool inBranchRange(RelType type, uint64_t src, 840b57cec5SDimitry Andric uint64_t dst) const; 850b57cec5SDimitry Andric 865ffd83dbSDimitry Andric virtual void relocate(uint8_t *loc, const Relocation &rel, 875ffd83dbSDimitry Andric uint64_t val) const = 0; 885ffd83dbSDimitry Andric void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const { 895ffd83dbSDimitry Andric relocate(loc, Relocation{R_NONE, type, 0, 0, nullptr}, val); 905ffd83dbSDimitry Andric } 915ffd83dbSDimitry Andric 92*753f127fSDimitry Andric // Do a linker relaxation pass and return true if we changed something. 93*753f127fSDimitry Andric virtual bool relaxOnce(int pass) const { return false; } 94*753f127fSDimitry Andric 955ffd83dbSDimitry Andric virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type, 965ffd83dbSDimitry Andric JumpModType val) const {} 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric virtual ~TargetInfo(); 990b57cec5SDimitry Andric 1005ffd83dbSDimitry Andric // This deletes a jump insn at the end of the section if it is a fall thru to 1015ffd83dbSDimitry Andric // the next section. Further, if there is a conditional jump and a direct 1025ffd83dbSDimitry Andric // jump consecutively, it tries to flip the conditional jump to convert the 1035ffd83dbSDimitry Andric // direct jump into a fall thru and delete it. Returns true if a jump 1045ffd83dbSDimitry Andric // instruction can be deleted. 1055ffd83dbSDimitry Andric virtual bool deleteFallThruJmpInsn(InputSection &is, InputFile *file, 1065ffd83dbSDimitry Andric InputSection *nextIS) const { 1075ffd83dbSDimitry Andric return false; 1085ffd83dbSDimitry Andric } 1095ffd83dbSDimitry Andric 1100b57cec5SDimitry Andric unsigned defaultCommonPageSize = 4096; 1110b57cec5SDimitry Andric unsigned defaultMaxPageSize = 4096; 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric uint64_t getImageBase() const; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got. 116349cc55cSDimitry Andric bool gotBaseSymInGotPlt = false; 1170b57cec5SDimitry Andric 118349cc55cSDimitry Andric static constexpr RelType noneRel = 0; 1190b57cec5SDimitry Andric RelType copyRel; 1200b57cec5SDimitry Andric RelType gotRel; 1210b57cec5SDimitry Andric RelType pltRel; 1220b57cec5SDimitry Andric RelType relativeRel; 1230b57cec5SDimitry Andric RelType iRelativeRel; 1240b57cec5SDimitry Andric RelType symbolicRel; 1250b57cec5SDimitry Andric RelType tlsDescRel; 1260b57cec5SDimitry Andric RelType tlsGotRel; 1270b57cec5SDimitry Andric RelType tlsModuleIndexRel; 1280b57cec5SDimitry Andric RelType tlsOffsetRel; 129fe6060f1SDimitry Andric unsigned gotEntrySize = config->wordsize; 1300b57cec5SDimitry Andric unsigned pltEntrySize; 1310b57cec5SDimitry Andric unsigned pltHeaderSize; 132480093f4SDimitry Andric unsigned ipltEntrySize; 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric // At least on x86_64 positions 1 and 2 are used by the first plt entry 1350b57cec5SDimitry Andric // to support lazy loading. 1360b57cec5SDimitry Andric unsigned gotPltHeaderEntriesNum = 3; 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // On PPC ELF V2 abi, the first entry in the .got is the .TOC. 1390b57cec5SDimitry Andric unsigned gotHeaderEntriesNum = 0; 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric bool needsThunks = false; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric // A 4-byte field corresponding to one or more trap instructions, used to pad 1440b57cec5SDimitry Andric // executable OutputSections. 1450b57cec5SDimitry Andric std::array<uint8_t, 4> trapInstr; 1460b57cec5SDimitry Andric 1475ffd83dbSDimitry Andric // Stores the NOP instructions of different sizes for the target and is used 1485ffd83dbSDimitry Andric // to pad sections that are relaxed. 1495ffd83dbSDimitry Andric llvm::Optional<std::vector<std::vector<uint8_t>>> nopInstrs; 1505ffd83dbSDimitry Andric 1510b57cec5SDimitry Andric // If a target needs to rewrite calls to __morestack to instead call 1520b57cec5SDimitry Andric // __morestack_non_split when a split-stack enabled caller calls a 1530b57cec5SDimitry Andric // non-split-stack callee this will return true. Otherwise returns false. 1540b57cec5SDimitry Andric bool needsMoreStackNonSplit = true; 1550b57cec5SDimitry Andric 156e8d8bef9SDimitry Andric virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const; 157e8d8bef9SDimitry Andric virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend, 158e8d8bef9SDimitry Andric const uint8_t *loc) const; 1595ffd83dbSDimitry Andric virtual void relaxGot(uint8_t *loc, const Relocation &rel, 1605ffd83dbSDimitry Andric uint64_t val) const; 1615ffd83dbSDimitry Andric virtual void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, 1625ffd83dbSDimitry Andric uint64_t val) const; 1635ffd83dbSDimitry Andric virtual void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, 1645ffd83dbSDimitry Andric uint64_t val) const; 1655ffd83dbSDimitry Andric virtual void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, 1665ffd83dbSDimitry Andric uint64_t val) const; 1675ffd83dbSDimitry Andric virtual void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, 1685ffd83dbSDimitry Andric uint64_t val) const; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric protected: 1710b57cec5SDimitry Andric // On FreeBSD x86_64 the first page cannot be mmaped. 172480093f4SDimitry Andric // On Linux this is controlled by vm.mmap_min_addr. At least on some x86_64 173480093f4SDimitry Andric // installs this is set to 65536, so the first 15 pages cannot be used. 1740b57cec5SDimitry Andric // Given that, the smallest value that can be used in here is 0x10000. 1750b57cec5SDimitry Andric uint64_t defaultImageBase = 0x10000; 1760b57cec5SDimitry Andric }; 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric TargetInfo *getAArch64TargetInfo(); 1790b57cec5SDimitry Andric TargetInfo *getAMDGPUTargetInfo(); 1800b57cec5SDimitry Andric TargetInfo *getARMTargetInfo(); 1810b57cec5SDimitry Andric TargetInfo *getAVRTargetInfo(); 1820b57cec5SDimitry Andric TargetInfo *getHexagonTargetInfo(); 1830b57cec5SDimitry Andric TargetInfo *getMSP430TargetInfo(); 1840b57cec5SDimitry Andric TargetInfo *getPPC64TargetInfo(); 1850b57cec5SDimitry Andric TargetInfo *getPPCTargetInfo(); 1860b57cec5SDimitry Andric TargetInfo *getRISCVTargetInfo(); 1870b57cec5SDimitry Andric TargetInfo *getSPARCV9TargetInfo(); 1880b57cec5SDimitry Andric TargetInfo *getX86TargetInfo(); 1890b57cec5SDimitry Andric TargetInfo *getX86_64TargetInfo(); 1900b57cec5SDimitry Andric template <class ELFT> TargetInfo *getMipsTargetInfo(); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric struct ErrorPlace { 1930b57cec5SDimitry Andric InputSectionBase *isec; 1940b57cec5SDimitry Andric std::string loc; 195349cc55cSDimitry Andric std::string srcLoc; 1960b57cec5SDimitry Andric }; 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // Returns input section and corresponding source string for the given location. 1990b57cec5SDimitry Andric ErrorPlace getErrorPlace(const uint8_t *loc); 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric static inline std::string getErrorLocation(const uint8_t *loc) { 2020b57cec5SDimitry Andric return getErrorPlace(loc).loc; 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric void writePPC32GlinkSection(uint8_t *buf, size_t numEntries); 2060b57cec5SDimitry Andric 2075ffd83dbSDimitry Andric bool tryRelaxPPC64TocIndirection(const Relocation &rel, uint8_t *bufLoc); 2080b57cec5SDimitry Andric unsigned getPPCDFormOp(unsigned secondaryOp); 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric // In the PowerPC64 Elf V2 abi a function can have 2 entry points. The first 2110b57cec5SDimitry Andric // is a global entry point (GEP) which typically is used to initialize the TOC 2120b57cec5SDimitry Andric // pointer in general purpose register 2. The second is a local entry 2130b57cec5SDimitry Andric // point (LEP) which bypasses the TOC pointer initialization code. The 2140b57cec5SDimitry Andric // offset between GEP and LEP is encoded in a function's st_other flags. 2150b57cec5SDimitry Andric // This function will return the offset (in bytes) from the global entry-point 2160b57cec5SDimitry Andric // to the local entry-point. 2170b57cec5SDimitry Andric unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther); 2180b57cec5SDimitry Andric 219e8d8bef9SDimitry Andric // Write a prefixed instruction, which is a 4-byte prefix followed by a 4-byte 220e8d8bef9SDimitry Andric // instruction (regardless of endianness). Therefore, the prefix is always in 221e8d8bef9SDimitry Andric // lower memory than the instruction. 222e8d8bef9SDimitry Andric void writePrefixedInstruction(uint8_t *loc, uint64_t insn); 223e8d8bef9SDimitry Andric 2245ffd83dbSDimitry Andric void addPPC64SaveRestore(); 2250b57cec5SDimitry Andric uint64_t getPPC64TocBase(); 2260b57cec5SDimitry Andric uint64_t getAArch64Page(uint64_t expr); 227*753f127fSDimitry Andric void riscvFinalizeRelax(int passes); 2280b57cec5SDimitry Andric 22904eeddc0SDimitry Andric class AArch64Relaxer { 23004eeddc0SDimitry Andric bool safeToRelaxAdrpLdr = true; 23104eeddc0SDimitry Andric 23204eeddc0SDimitry Andric public: 23304eeddc0SDimitry Andric explicit AArch64Relaxer(ArrayRef<Relocation> relocs); 23404eeddc0SDimitry Andric 2351fd87a68SDimitry Andric bool tryRelaxAdrpAdd(const Relocation &adrpRel, const Relocation &addRel, 2361fd87a68SDimitry Andric uint64_t secAddr, uint8_t *buf) const; 23704eeddc0SDimitry Andric bool tryRelaxAdrpLdr(const Relocation &adrpRel, const Relocation &ldrRel, 23804eeddc0SDimitry Andric uint64_t secAddr, uint8_t *buf) const; 23904eeddc0SDimitry Andric }; 24004eeddc0SDimitry Andric 2410b57cec5SDimitry Andric extern const TargetInfo *target; 2420b57cec5SDimitry Andric TargetInfo *getTarget(); 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric template <class ELFT> bool isMipsPIC(const Defined *sym); 2450b57cec5SDimitry Andric 2465ffd83dbSDimitry Andric void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, 2475ffd83dbSDimitry Andric int64_t min, uint64_t max); 248e8d8bef9SDimitry Andric void reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym, 249e8d8bef9SDimitry Andric const Twine &msg); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed integer. 2525ffd83dbSDimitry Andric inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) { 2530b57cec5SDimitry Andric if (v != llvm::SignExtend64(v, n)) 2545ffd83dbSDimitry Andric reportRangeError(loc, rel, Twine(v), llvm::minIntN(n), llvm::maxIntN(n)); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // Make sure that V can be represented as an N bit unsigned integer. 2585ffd83dbSDimitry Andric inline void checkUInt(uint8_t *loc, uint64_t v, int n, const Relocation &rel) { 2590b57cec5SDimitry Andric if ((v >> n) != 0) 2605ffd83dbSDimitry Andric reportRangeError(loc, rel, Twine(v), 0, llvm::maxUIntN(n)); 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed or unsigned integer. 2645ffd83dbSDimitry Andric inline void checkIntUInt(uint8_t *loc, uint64_t v, int n, 2655ffd83dbSDimitry Andric const Relocation &rel) { 2660b57cec5SDimitry Andric // For the error message we should cast V to a signed integer so that error 2670b57cec5SDimitry Andric // messages show a small negative value rather than an extremely large one 2680b57cec5SDimitry Andric if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0) 2695ffd83dbSDimitry Andric reportRangeError(loc, rel, Twine((int64_t)v), llvm::minIntN(n), 2700b57cec5SDimitry Andric llvm::maxUIntN(n)); 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2735ffd83dbSDimitry Andric inline void checkAlignment(uint8_t *loc, uint64_t v, int n, 2745ffd83dbSDimitry Andric const Relocation &rel) { 2750b57cec5SDimitry Andric if ((v & (n - 1)) != 0) 2760b57cec5SDimitry Andric error(getErrorLocation(loc) + "improper alignment for relocation " + 2775ffd83dbSDimitry Andric lld::toString(rel.type) + ": 0x" + llvm::utohexstr(v) + 2780b57cec5SDimitry Andric " is not aligned to " + Twine(n) + " bytes"); 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric // Endianness-aware read/write. 2820b57cec5SDimitry Andric inline uint16_t read16(const void *p) { 2830b57cec5SDimitry Andric return llvm::support::endian::read16(p, config->endianness); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric inline uint32_t read32(const void *p) { 2870b57cec5SDimitry Andric return llvm::support::endian::read32(p, config->endianness); 2880b57cec5SDimitry Andric } 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric inline uint64_t read64(const void *p) { 2910b57cec5SDimitry Andric return llvm::support::endian::read64(p, config->endianness); 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric inline void write16(void *p, uint16_t v) { 2950b57cec5SDimitry Andric llvm::support::endian::write16(p, v, config->endianness); 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric inline void write32(void *p, uint32_t v) { 2990b57cec5SDimitry Andric llvm::support::endian::write32(p, v, config->endianness); 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric inline void write64(void *p, uint64_t v) { 3030b57cec5SDimitry Andric llvm::support::endian::write64(p, v, config->endianness); 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric } // namespace elf 3060b57cec5SDimitry Andric } // namespace lld 3070b57cec5SDimitry Andric 3081fd87a68SDimitry Andric #ifdef __clang__ 3091fd87a68SDimitry Andric #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" 3101fd87a68SDimitry Andric #endif 3111fd87a68SDimitry Andric #define invokeELFT(f, ...) \ 3121fd87a68SDimitry Andric switch (config->ekind) { \ 3131fd87a68SDimitry Andric case ELF32LEKind: \ 3141fd87a68SDimitry Andric f<ELF32LE>(__VA_ARGS__); \ 3151fd87a68SDimitry Andric break; \ 3161fd87a68SDimitry Andric case ELF32BEKind: \ 3171fd87a68SDimitry Andric f<ELF32BE>(__VA_ARGS__); \ 3181fd87a68SDimitry Andric break; \ 3191fd87a68SDimitry Andric case ELF64LEKind: \ 3201fd87a68SDimitry Andric f<ELF64LE>(__VA_ARGS__); \ 3211fd87a68SDimitry Andric break; \ 3221fd87a68SDimitry Andric case ELF64BEKind: \ 3231fd87a68SDimitry Andric f<ELF64BE>(__VA_ARGS__); \ 3241fd87a68SDimitry Andric break; \ 3251fd87a68SDimitry Andric default: \ 3261fd87a68SDimitry Andric llvm_unreachable("unknown config->ekind"); \ 3271fd87a68SDimitry Andric } 3281fd87a68SDimitry Andric 3290b57cec5SDimitry Andric #endif 330