xref: /freebsd/contrib/llvm-project/lld/ELF/Target.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
16*bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h"
170b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
180b57cec5SDimitry Andric #include <array>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric namespace lld {
210b57cec5SDimitry Andric std::string toString(elf::RelType type);
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace elf {
240b57cec5SDimitry Andric class Defined;
250b57cec5SDimitry Andric class InputFile;
260b57cec5SDimitry Andric class Symbol;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric class TargetInfo {
290b57cec5SDimitry Andric public:
300b57cec5SDimitry Andric   virtual uint32_t calcEFlags() const { return 0; }
310b57cec5SDimitry Andric   virtual RelExpr getRelExpr(RelType type, const Symbol &s,
320b57cec5SDimitry Andric                              const uint8_t *loc) const = 0;
330b57cec5SDimitry Andric   virtual RelType getDynRel(RelType type) const { return 0; }
340b57cec5SDimitry Andric   virtual void writeGotPltHeader(uint8_t *buf) const {}
350b57cec5SDimitry Andric   virtual void writeGotHeader(uint8_t *buf) const {}
360b57cec5SDimitry Andric   virtual void writeGotPlt(uint8_t *buf, const Symbol &s) const {};
37480093f4SDimitry Andric   virtual void writeIgotPlt(uint8_t *buf, const Symbol &s) const {}
380b57cec5SDimitry Andric   virtual int64_t getImplicitAddend(const uint8_t *buf, RelType type) const;
390b57cec5SDimitry Andric   virtual int getTlsGdRelaxSkip(RelType type) const { return 1; }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // If lazy binding is supported, the first entry of the PLT has code
420b57cec5SDimitry Andric   // to call the dynamic linker to resolve PLT entries the first time
430b57cec5SDimitry Andric   // they are called. This function writes that code.
440b57cec5SDimitry Andric   virtual void writePltHeader(uint8_t *buf) const {}
450b57cec5SDimitry Andric 
46480093f4SDimitry Andric   virtual void writePlt(uint8_t *buf, const Symbol &sym,
47480093f4SDimitry Andric                         uint64_t pltEntryAddr) const {}
48480093f4SDimitry Andric   virtual void writeIplt(uint8_t *buf, const Symbol &sym,
49480093f4SDimitry Andric                          uint64_t pltEntryAddr) const {
50480093f4SDimitry Andric     // All but PPC32 and PPC64 use the same format for .plt and .iplt entries.
51480093f4SDimitry Andric     writePlt(buf, sym, pltEntryAddr);
52480093f4SDimitry Andric   }
53480093f4SDimitry Andric   virtual void writeIBTPlt(uint8_t *buf, size_t numEntries) const {}
540b57cec5SDimitry Andric   virtual void addPltHeaderSymbols(InputSection &isec) const {}
550b57cec5SDimitry Andric   virtual void addPltSymbols(InputSection &isec, uint64_t off) const {}
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   // Returns true if a relocation only uses the low bits of a value such that
580b57cec5SDimitry Andric   // all those bits are in the same page. For example, if the relocation
590b57cec5SDimitry Andric   // only uses the low 12 bits in a system with 4k pages. If this is true, the
600b57cec5SDimitry Andric   // bits will always have the same value at runtime and we don't have to emit
610b57cec5SDimitry Andric   // a dynamic relocation.
620b57cec5SDimitry Andric   virtual bool usesOnlyLowPageBits(RelType type) const;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   // Decide whether a Thunk is needed for the relocation from File
650b57cec5SDimitry Andric   // targeting S.
660b57cec5SDimitry Andric   virtual bool needsThunk(RelExpr expr, RelType relocType,
670b57cec5SDimitry Andric                           const InputFile *file, uint64_t branchAddr,
68480093f4SDimitry Andric                           const Symbol &s, int64_t a) const;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // On systems with range extensions we place collections of Thunks at
710b57cec5SDimitry Andric   // regular spacings that enable the majority of branches reach the Thunks.
720b57cec5SDimitry Andric   // a value of 0 means range extension thunks are not supported.
730b57cec5SDimitry Andric   virtual uint32_t getThunkSectionSpacing() const { return 0; }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric   // The function with a prologue starting at Loc was compiled with
760b57cec5SDimitry Andric   // -fsplit-stack and it calls a function compiled without. Adjust the prologue
770b57cec5SDimitry Andric   // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks.
780b57cec5SDimitry Andric   // The symbols st_other flags are needed on PowerPC64 for determining the
790b57cec5SDimitry Andric   // offset to the split-stack prologue.
800b57cec5SDimitry Andric   virtual bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
810b57cec5SDimitry Andric                                                 uint8_t stOther) const;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   // Return true if we can reach dst from src with RelType type.
840b57cec5SDimitry Andric   virtual bool inBranchRange(RelType type, uint64_t src,
850b57cec5SDimitry Andric                              uint64_t dst) const;
860b57cec5SDimitry Andric 
875ffd83dbSDimitry Andric   virtual void relocate(uint8_t *loc, const Relocation &rel,
885ffd83dbSDimitry Andric                         uint64_t val) const = 0;
895ffd83dbSDimitry Andric   void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const {
905ffd83dbSDimitry Andric     relocate(loc, Relocation{R_NONE, type, 0, 0, nullptr}, val);
915ffd83dbSDimitry Andric   }
92*bdd1243dSDimitry Andric   virtual void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const;
935ffd83dbSDimitry Andric 
94753f127fSDimitry Andric   // Do a linker relaxation pass and return true if we changed something.
95753f127fSDimitry Andric   virtual bool relaxOnce(int pass) const { return false; }
96753f127fSDimitry Andric 
975ffd83dbSDimitry Andric   virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type,
985ffd83dbSDimitry Andric                                  JumpModType val) const {}
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   virtual ~TargetInfo();
1010b57cec5SDimitry Andric 
1025ffd83dbSDimitry Andric   // This deletes a jump insn at the end of the section if it is a fall thru to
1035ffd83dbSDimitry Andric   // the next section.  Further, if there is a conditional jump and a direct
1045ffd83dbSDimitry Andric   // jump consecutively, it tries to flip the conditional jump to convert the
1055ffd83dbSDimitry Andric   // direct jump into a fall thru and delete it.  Returns true if a jump
1065ffd83dbSDimitry Andric   // instruction can be deleted.
1075ffd83dbSDimitry Andric   virtual bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
1085ffd83dbSDimitry Andric                                      InputSection *nextIS) const {
1095ffd83dbSDimitry Andric     return false;
1105ffd83dbSDimitry Andric   }
1115ffd83dbSDimitry Andric 
1120b57cec5SDimitry Andric   unsigned defaultCommonPageSize = 4096;
1130b57cec5SDimitry Andric   unsigned defaultMaxPageSize = 4096;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric   uint64_t getImageBase() const;
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
118349cc55cSDimitry Andric   bool gotBaseSymInGotPlt = false;
1190b57cec5SDimitry Andric 
120349cc55cSDimitry Andric   static constexpr RelType noneRel = 0;
1210b57cec5SDimitry Andric   RelType copyRel;
1220b57cec5SDimitry Andric   RelType gotRel;
1230b57cec5SDimitry Andric   RelType pltRel;
1240b57cec5SDimitry Andric   RelType relativeRel;
1250b57cec5SDimitry Andric   RelType iRelativeRel;
1260b57cec5SDimitry Andric   RelType symbolicRel;
1270b57cec5SDimitry Andric   RelType tlsDescRel;
1280b57cec5SDimitry Andric   RelType tlsGotRel;
1290b57cec5SDimitry Andric   RelType tlsModuleIndexRel;
1300b57cec5SDimitry Andric   RelType tlsOffsetRel;
131fe6060f1SDimitry Andric   unsigned gotEntrySize = config->wordsize;
1320b57cec5SDimitry Andric   unsigned pltEntrySize;
1330b57cec5SDimitry Andric   unsigned pltHeaderSize;
134480093f4SDimitry Andric   unsigned ipltEntrySize;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   // At least on x86_64 positions 1 and 2 are used by the first plt entry
1370b57cec5SDimitry Andric   // to support lazy loading.
1380b57cec5SDimitry Andric   unsigned gotPltHeaderEntriesNum = 3;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
1410b57cec5SDimitry Andric   unsigned gotHeaderEntriesNum = 0;
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric   bool needsThunks = false;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   // A 4-byte field corresponding to one or more trap instructions, used to pad
1460b57cec5SDimitry Andric   // executable OutputSections.
1470b57cec5SDimitry Andric   std::array<uint8_t, 4> trapInstr;
1480b57cec5SDimitry Andric 
1495ffd83dbSDimitry Andric   // Stores the NOP instructions of different sizes for the target and is used
1505ffd83dbSDimitry Andric   // to pad sections that are relaxed.
151*bdd1243dSDimitry Andric   std::optional<std::vector<std::vector<uint8_t>>> nopInstrs;
1525ffd83dbSDimitry Andric 
1530b57cec5SDimitry Andric   // If a target needs to rewrite calls to __morestack to instead call
1540b57cec5SDimitry Andric   // __morestack_non_split when a split-stack enabled caller calls a
1550b57cec5SDimitry Andric   // non-split-stack callee this will return true. Otherwise returns false.
1560b57cec5SDimitry Andric   bool needsMoreStackNonSplit = true;
1570b57cec5SDimitry Andric 
158e8d8bef9SDimitry Andric   virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const;
159e8d8bef9SDimitry Andric   virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend,
160e8d8bef9SDimitry Andric                                   const uint8_t *loc) const;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric protected:
1630b57cec5SDimitry Andric   // On FreeBSD x86_64 the first page cannot be mmaped.
164480093f4SDimitry Andric   // On Linux this is controlled by vm.mmap_min_addr. At least on some x86_64
165480093f4SDimitry Andric   // installs this is set to 65536, so the first 15 pages cannot be used.
1660b57cec5SDimitry Andric   // Given that, the smallest value that can be used in here is 0x10000.
1670b57cec5SDimitry Andric   uint64_t defaultImageBase = 0x10000;
1680b57cec5SDimitry Andric };
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric TargetInfo *getAArch64TargetInfo();
1710b57cec5SDimitry Andric TargetInfo *getAMDGPUTargetInfo();
1720b57cec5SDimitry Andric TargetInfo *getARMTargetInfo();
1730b57cec5SDimitry Andric TargetInfo *getAVRTargetInfo();
1740b57cec5SDimitry Andric TargetInfo *getHexagonTargetInfo();
1750b57cec5SDimitry Andric TargetInfo *getMSP430TargetInfo();
1760b57cec5SDimitry Andric TargetInfo *getPPC64TargetInfo();
1770b57cec5SDimitry Andric TargetInfo *getPPCTargetInfo();
1780b57cec5SDimitry Andric TargetInfo *getRISCVTargetInfo();
1790b57cec5SDimitry Andric TargetInfo *getSPARCV9TargetInfo();
1800b57cec5SDimitry Andric TargetInfo *getX86TargetInfo();
1810b57cec5SDimitry Andric TargetInfo *getX86_64TargetInfo();
1820b57cec5SDimitry Andric template <class ELFT> TargetInfo *getMipsTargetInfo();
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric struct ErrorPlace {
1850b57cec5SDimitry Andric   InputSectionBase *isec;
1860b57cec5SDimitry Andric   std::string loc;
187349cc55cSDimitry Andric   std::string srcLoc;
1880b57cec5SDimitry Andric };
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric // Returns input section and corresponding source string for the given location.
1910b57cec5SDimitry Andric ErrorPlace getErrorPlace(const uint8_t *loc);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric static inline std::string getErrorLocation(const uint8_t *loc) {
1940b57cec5SDimitry Andric   return getErrorPlace(loc).loc;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric void writePPC32GlinkSection(uint8_t *buf, size_t numEntries);
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric unsigned getPPCDFormOp(unsigned secondaryOp);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric // In the PowerPC64 Elf V2 abi a function can have 2 entry points.  The first
2020b57cec5SDimitry Andric // is a global entry point (GEP) which typically is used to initialize the TOC
2030b57cec5SDimitry Andric // pointer in general purpose register 2.  The second is a local entry
2040b57cec5SDimitry Andric // point (LEP) which bypasses the TOC pointer initialization code. The
2050b57cec5SDimitry Andric // offset between GEP and LEP is encoded in a function's st_other flags.
2060b57cec5SDimitry Andric // This function will return the offset (in bytes) from the global entry-point
2070b57cec5SDimitry Andric // to the local entry-point.
2080b57cec5SDimitry Andric unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther);
2090b57cec5SDimitry Andric 
210e8d8bef9SDimitry Andric // Write a prefixed instruction, which is a 4-byte prefix followed by a 4-byte
211e8d8bef9SDimitry Andric // instruction (regardless of endianness). Therefore, the prefix is always in
212e8d8bef9SDimitry Andric // lower memory than the instruction.
213e8d8bef9SDimitry Andric void writePrefixedInstruction(uint8_t *loc, uint64_t insn);
214e8d8bef9SDimitry Andric 
2155ffd83dbSDimitry Andric void addPPC64SaveRestore();
2160b57cec5SDimitry Andric uint64_t getPPC64TocBase();
2170b57cec5SDimitry Andric uint64_t getAArch64Page(uint64_t expr);
218753f127fSDimitry Andric void riscvFinalizeRelax(int passes);
219*bdd1243dSDimitry Andric void mergeRISCVAttributesSections();
2200b57cec5SDimitry Andric 
221*bdd1243dSDimitry Andric LLVM_LIBRARY_VISIBILITY extern const TargetInfo *target;
2220b57cec5SDimitry Andric TargetInfo *getTarget();
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric template <class ELFT> bool isMipsPIC(const Defined *sym);
2250b57cec5SDimitry Andric 
2265ffd83dbSDimitry Andric void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
2275ffd83dbSDimitry Andric                       int64_t min, uint64_t max);
228e8d8bef9SDimitry Andric void reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym,
229e8d8bef9SDimitry Andric                       const Twine &msg);
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed integer.
2325ffd83dbSDimitry Andric inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) {
2330b57cec5SDimitry Andric   if (v != llvm::SignExtend64(v, n))
2345ffd83dbSDimitry Andric     reportRangeError(loc, rel, Twine(v), llvm::minIntN(n), llvm::maxIntN(n));
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric // Make sure that V can be represented as an N bit unsigned integer.
2385ffd83dbSDimitry Andric inline void checkUInt(uint8_t *loc, uint64_t v, int n, const Relocation &rel) {
2390b57cec5SDimitry Andric   if ((v >> n) != 0)
2405ffd83dbSDimitry Andric     reportRangeError(loc, rel, Twine(v), 0, llvm::maxUIntN(n));
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed or unsigned integer.
2445ffd83dbSDimitry Andric inline void checkIntUInt(uint8_t *loc, uint64_t v, int n,
2455ffd83dbSDimitry Andric                          const Relocation &rel) {
2460b57cec5SDimitry Andric   // For the error message we should cast V to a signed integer so that error
2470b57cec5SDimitry Andric   // messages show a small negative value rather than an extremely large one
2480b57cec5SDimitry Andric   if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0)
2495ffd83dbSDimitry Andric     reportRangeError(loc, rel, Twine((int64_t)v), llvm::minIntN(n),
2500b57cec5SDimitry Andric                      llvm::maxUIntN(n));
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
2535ffd83dbSDimitry Andric inline void checkAlignment(uint8_t *loc, uint64_t v, int n,
2545ffd83dbSDimitry Andric                            const Relocation &rel) {
2550b57cec5SDimitry Andric   if ((v & (n - 1)) != 0)
2560b57cec5SDimitry Andric     error(getErrorLocation(loc) + "improper alignment for relocation " +
2575ffd83dbSDimitry Andric           lld::toString(rel.type) + ": 0x" + llvm::utohexstr(v) +
2580b57cec5SDimitry Andric           " is not aligned to " + Twine(n) + " bytes");
2590b57cec5SDimitry Andric }
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric // Endianness-aware read/write.
2620b57cec5SDimitry Andric inline uint16_t read16(const void *p) {
2630b57cec5SDimitry Andric   return llvm::support::endian::read16(p, config->endianness);
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric inline uint32_t read32(const void *p) {
2670b57cec5SDimitry Andric   return llvm::support::endian::read32(p, config->endianness);
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric inline uint64_t read64(const void *p) {
2710b57cec5SDimitry Andric   return llvm::support::endian::read64(p, config->endianness);
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric inline void write16(void *p, uint16_t v) {
2750b57cec5SDimitry Andric   llvm::support::endian::write16(p, v, config->endianness);
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric inline void write32(void *p, uint32_t v) {
2790b57cec5SDimitry Andric   llvm::support::endian::write32(p, v, config->endianness);
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric inline void write64(void *p, uint64_t v) {
2830b57cec5SDimitry Andric   llvm::support::endian::write64(p, v, config->endianness);
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric } // namespace elf
2860b57cec5SDimitry Andric } // namespace lld
2870b57cec5SDimitry Andric 
2881fd87a68SDimitry Andric #ifdef __clang__
2891fd87a68SDimitry Andric #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
2901fd87a68SDimitry Andric #endif
2911fd87a68SDimitry Andric #define invokeELFT(f, ...)                                                     \
2921fd87a68SDimitry Andric   switch (config->ekind) {                                                     \
2931fd87a68SDimitry Andric   case ELF32LEKind:                                                            \
2941fd87a68SDimitry Andric     f<ELF32LE>(__VA_ARGS__);                                                   \
2951fd87a68SDimitry Andric     break;                                                                     \
2961fd87a68SDimitry Andric   case ELF32BEKind:                                                            \
2971fd87a68SDimitry Andric     f<ELF32BE>(__VA_ARGS__);                                                   \
2981fd87a68SDimitry Andric     break;                                                                     \
2991fd87a68SDimitry Andric   case ELF64LEKind:                                                            \
3001fd87a68SDimitry Andric     f<ELF64LE>(__VA_ARGS__);                                                   \
3011fd87a68SDimitry Andric     break;                                                                     \
3021fd87a68SDimitry Andric   case ELF64BEKind:                                                            \
3031fd87a68SDimitry Andric     f<ELF64BE>(__VA_ARGS__);                                                   \
3041fd87a68SDimitry Andric     break;                                                                     \
3051fd87a68SDimitry Andric   default:                                                                     \
3061fd87a68SDimitry Andric     llvm_unreachable("unknown config->ekind");                                 \
3071fd87a68SDimitry Andric   }
3081fd87a68SDimitry Andric 
3090b57cec5SDimitry Andric #endif
310