1 //===- Target.h -------------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_TARGET_H
11 #define LLD_ELF_TARGET_H
13 #include "InputSection.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "llvm/Object/ELF.h"
18 std::string toString(elf::RelType Type);
27 virtual uint32_t calcEFlags() const { return 0; }
28 virtual bool isPicRel(RelType Type) const { return true; }
29 virtual RelType getDynRel(RelType Type) const { return Type; }
30 virtual void writeGotPltHeader(uint8_t *Buf) const {}
31 virtual void writeGotPlt(uint8_t *Buf, const Symbol &S) const {};
32 virtual void writeIgotPlt(uint8_t *Buf, const Symbol &S) const;
33 virtual int64_t getImplicitAddend(const uint8_t *Buf, RelType Type) const;
35 // If lazy binding is supported, the first entry of the PLT has code
36 // to call the dynamic linker to resolve PLT entries the first time
37 // they are called. This function writes that code.
38 virtual void writePltHeader(uint8_t *Buf) const {}
40 virtual void writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
41 uint64_t PltEntryAddr, int32_t Index,
42 unsigned RelOff) const {}
43 virtual void addPltHeaderSymbols(InputSection &IS) const {}
44 virtual void addPltSymbols(InputSection &IS, uint64_t Off) const {}
46 // Returns true if a relocation only uses the low bits of a value such that
47 // all those bits are in in the same page. For example, if the relocation
48 // only uses the low 12 bits in a system with 4k pages. If this is true, the
49 // bits will always have the same value at runtime and we don't have to emit
50 // a dynamic relocation.
51 virtual bool usesOnlyLowPageBits(RelType Type) const;
53 // Decide whether a Thunk is needed for the relocation from File
55 virtual bool needsThunk(RelExpr Expr, RelType RelocType,
56 const InputFile *File, uint64_t BranchAddr,
57 const Symbol &S) const;
58 // Return true if we can reach Dst from Src with Relocation RelocType
59 virtual bool inBranchRange(RelType Type, uint64_t Src,
61 virtual RelExpr getRelExpr(RelType Type, const Symbol &S,
62 const uint8_t *Loc) const = 0;
64 virtual void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const = 0;
66 virtual ~TargetInfo();
68 unsigned TlsGdRelaxSkip = 1;
69 unsigned PageSize = 4096;
70 unsigned DefaultMaxPageSize = 4096;
72 uint64_t getImageBase();
74 // Offset of _GLOBAL_OFFSET_TABLE_ from base of .got section. Use -1 for
76 uint64_t GotBaseSymOff = 0;
78 // On systems with range extensions we place collections of Thunks at
79 // regular spacings that enable the majority of branches reach the Thunks.
80 uint32_t ThunkSectionSpacing = 0;
89 RelType TlsModuleIndexRel;
91 unsigned GotEntrySize = 0;
92 unsigned GotPltEntrySize = 0;
93 unsigned PltEntrySize;
94 unsigned PltHeaderSize;
96 // At least on x86_64 positions 1 and 2 are used by the first plt entry
97 // to support lazy loading.
98 unsigned GotPltHeaderEntriesNum = 3;
100 // Set to 0 for variant 2
101 unsigned TcbSize = 0;
103 bool NeedsThunks = false;
105 // A 4-byte field corresponding to one or more trap instructions, used to pad
106 // executable OutputSections.
107 uint32_t TrapInstr = 0;
109 virtual RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
111 virtual void relaxGot(uint8_t *Loc, uint64_t Val) const;
112 virtual void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const;
113 virtual void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
114 virtual void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
115 virtual void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const;
118 // On FreeBSD x86_64 the first page cannot be mmaped.
119 // On Linux that is controled by vm.mmap_min_addr. At least on some x86_64
120 // installs that is 65536, so the first 15 pages cannot be used.
121 // Given that, the smallest value that can be used in here is 0x10000.
122 uint64_t DefaultImageBase = 0x10000;
125 TargetInfo *getAArch64TargetInfo();
126 TargetInfo *getAMDGPUTargetInfo();
127 TargetInfo *getARMTargetInfo();
128 TargetInfo *getAVRTargetInfo();
129 TargetInfo *getPPC64TargetInfo();
130 TargetInfo *getPPCTargetInfo();
131 TargetInfo *getSPARCV9TargetInfo();
132 TargetInfo *getX32TargetInfo();
133 TargetInfo *getX86TargetInfo();
134 TargetInfo *getX86_64TargetInfo();
135 template <class ELFT> TargetInfo *getMipsTargetInfo();
137 std::string getErrorLocation(const uint8_t *Loc);
139 uint64_t getPPC64TocBase();
140 uint64_t getAArch64Page(uint64_t Expr);
142 extern TargetInfo *Target;
143 TargetInfo *getTarget();
145 template <class ELFT> bool isMipsPIC(const Defined *Sym);
147 static inline void reportRangeError(uint8_t *Loc, RelType Type, const Twine &V,
148 int64_t Min, uint64_t Max) {
149 error(getErrorLocation(Loc) + "relocation " + lld::toString(Type) +
150 " out of range: " + V + " is not in [" + Twine(Min) + ", " +
154 template <unsigned N>
155 static void checkInt(uint8_t *Loc, int64_t V, RelType Type) {
156 if (!llvm::isInt<N>(V))
157 reportRangeError(Loc, Type, Twine(V), llvm::minIntN(N), llvm::maxIntN(N));
160 template <unsigned N>
161 static void checkUInt(uint8_t *Loc, uint64_t V, RelType Type) {
162 if (!llvm::isUInt<N>(V))
163 reportRangeError(Loc, Type, Twine(V), 0, llvm::maxUIntN(N));
166 template <unsigned N>
167 static void checkIntUInt(uint8_t *Loc, uint64_t V, RelType Type) {
168 if (!llvm::isInt<N>(V) && !llvm::isUInt<N>(V))
169 // For the error message we should cast V to a signed integer so that error
170 // messages show a small negative value rather than an extremely large one
171 reportRangeError(Loc, Type, Twine((int64_t)V), llvm::minIntN(N),
175 template <unsigned N>
176 static void checkAlignment(uint8_t *Loc, uint64_t V, RelType Type) {
177 if ((V & (N - 1)) != 0)
178 error(getErrorLocation(Loc) + "improper alignment for relocation " +
179 lld::toString(Type) + ": 0x" + llvm::utohexstr(V) +
180 " is not aligned to " + Twine(N) + " bytes");