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