blob: 3a8b7da7f5406333b656505335ce2bea5a7ca15c [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
34namespace elf2 {
35
36std::unique_ptr<TargetInfo> Target;
37
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
49 error("Relocation " + S + " out of range");
50}
51
52template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
53 if (isUInt<N>(V))
54 return;
55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
56 error("Relocation " + S + " out of range");
57}
58
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
60 if (isInt<N>(V) || isUInt<N>(V))
61 return;
62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
63 error("Relocation " + S + " out of range");
64}
65
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
67 if ((V & (N - 1)) == 0)
68 return;
69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
70 error("Improper alignment for relocation " + S);
71}
72
Rui Ueyamaefc23de2015-10-14 21:30:32 +000073namespace {
74class X86TargetInfo final : public TargetInfo {
75public:
76 X86TargetInfo();
George Rimar77b77792015-11-25 22:15:01 +000077 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimard23970f2015-11-25 20:41:53 +000078 unsigned getDynReloc(unsigned Type) const override;
79 bool isTlsDynReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +000080 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
81 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
82 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +000083 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
84 uint64_t PltEntryAddr, int32_t Index,
85 unsigned RelOff) const override;
George Rimar70e25082015-11-25 11:27:40 +000086 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000087 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000088 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000089 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000090 uint64_t SA, uint64_t ZA = 0,
91 uint8_t *PairedLoc = nullptr) const override;
George Rimar2558e122015-12-09 09:55:54 +000092 bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override;
93 unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
94 uint64_t P, uint64_t SA,
95 const SymbolBody &S) const override;
96
97private:
98 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
99 uint64_t SA) const;
100 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
101 uint64_t SA) const;
102 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
103 uint64_t SA) const;
104 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
105 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000106};
107
108class X86_64TargetInfo final : public TargetInfo {
109public:
110 X86_64TargetInfo();
Igor Kudrine7ad0932015-11-17 17:47:53 +0000111 unsigned getPltRefReloc(unsigned Type) const override;
George Rimard23970f2015-11-25 20:41:53 +0000112 bool isTlsDynReloc(unsigned Type) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +0000113 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000114 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
115 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
116 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000117 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
118 uint64_t PltEntryAddr, int32_t Index,
119 unsigned RelOff) const override;
George Rimarbc590fe2015-10-28 16:48:58 +0000120 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000121 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
122 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000123 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000124 uint64_t SA, uint64_t ZA = 0,
125 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000126 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000127 bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override;
George Rimar48651482015-12-11 08:59:37 +0000128 bool isSizeDynReloc(uint32_t Type, const SymbolBody &S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000129 unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar25411f252015-12-04 11:20:13 +0000130 uint64_t P, uint64_t SA,
131 const SymbolBody &S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000132
133private:
134 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
135 uint64_t SA) const;
136 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
137 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000138 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
139 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000140 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
141 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000142};
143
144class PPC64TargetInfo final : public TargetInfo {
145public:
146 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +0000147 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
148 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
149 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000150 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
151 uint64_t PltEntryAddr, int32_t Index,
152 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000153 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
154 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000155 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000156 uint64_t SA, uint64_t ZA = 0,
157 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000158 bool isRelRelative(uint32_t Type) const override;
159};
160
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000161class AArch64TargetInfo final : public TargetInfo {
162public:
163 AArch64TargetInfo();
Igor Kudrincfe47f52015-12-05 06:20:24 +0000164 unsigned getDynReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000165 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000166 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
167 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
168 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000169 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
170 uint64_t PltEntryAddr, int32_t Index,
171 unsigned RelOff) const override;
Igor Kudrin9606d192015-12-03 08:05:35 +0000172 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
174 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000175 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000176 uint64_t SA, uint64_t ZA = 0,
177 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000178};
179
180template <class ELFT> class MipsTargetInfo final : public TargetInfo {
181public:
182 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000183 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000184 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
185 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
186 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000187 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
188 uint64_t PltEntryAddr, int32_t Index,
189 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000190 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
191 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000192 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000193 uint64_t SA, uint64_t ZA = 0,
194 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000195};
196} // anonymous namespace
197
Rui Ueyama91004392015-10-13 16:08:15 +0000198TargetInfo *createTarget() {
199 switch (Config->EMachine) {
200 case EM_386:
201 return new X86TargetInfo();
202 case EM_AARCH64:
203 return new AArch64TargetInfo();
204 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000205 switch (Config->EKind) {
206 case ELF32LEKind:
207 return new MipsTargetInfo<ELF32LE>();
208 case ELF32BEKind:
209 return new MipsTargetInfo<ELF32BE>();
210 default:
211 error("Unsupported MIPS target");
212 }
Rui Ueyama91004392015-10-13 16:08:15 +0000213 case EM_PPC64:
214 return new PPC64TargetInfo();
215 case EM_X86_64:
216 return new X86_64TargetInfo();
217 }
218 error("Unknown target machine");
219}
220
Rafael Espindola01205f72015-09-22 18:19:46 +0000221TargetInfo::~TargetInfo() {}
222
George Rimar6713cf82015-11-25 21:46:05 +0000223bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000224 return false;
225}
226
Igor Kudrinf6f45472015-11-10 08:39:27 +0000227uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
228
George Rimarbc590fe2015-10-28 16:48:58 +0000229bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
230 return false;
231}
232
Igor Kudrine7ad0932015-11-17 17:47:53 +0000233unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000234
Rafael Espindolaae244002015-10-05 19:30:12 +0000235bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
236
George Rimar48651482015-12-11 08:59:37 +0000237bool TargetInfo::isSizeDynReloc(uint32_t Type, const SymbolBody &S) const {
238 return false;
239}
240
George Rimar6713cf82015-11-25 21:46:05 +0000241unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000242 uint32_t Type, uint64_t P, uint64_t SA,
243 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000244 return 0;
245}
George Rimar77d1cb12015-11-24 09:00:06 +0000246
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000247void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
248
Igor Kudrin351b41d2015-11-16 17:44:08 +0000249void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
250
Rafael Espindola7f074422015-09-22 21:35:51 +0000251X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000252 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000253 PCRelReloc = R_386_PC32;
254 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000255 PltReloc = R_386_JUMP_SLOT;
George Rimar9db204a2015-12-02 09:58:20 +0000256 TlsGotReloc = R_386_TLS_TPOFF;
257 TlsGlobalDynamicReloc = R_386_TLS_GD;
258 TlsLocalDynamicReloc = R_386_TLS_LDM;
259 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
260 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000261 LazyRelocations = true;
262 PltEntrySize = 16;
263 PltZeroEntrySize = 16;
264}
265
266void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
267 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
268}
269
270void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
271 // Skip 6 bytes of "pushl (GOT+4)"
272 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000273}
Rafael Espindola01205f72015-09-22 18:19:46 +0000274
George Rimard23970f2015-11-25 20:41:53 +0000275unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
276 if (Type == R_386_TLS_LE)
277 return R_386_TLS_TPOFF;
278 if (Type == R_386_TLS_LE_32)
279 return R_386_TLS_TPOFF32;
280 return Type;
281}
282
283bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar9db204a2015-12-02 09:58:20 +0000284 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
285 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000286 return Config->Shared;
George Rimar2558e122015-12-09 09:55:54 +0000287 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000288}
289
George Rimar648a2c32015-10-20 08:54:27 +0000290void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000291 uint64_t PltEntryAddr) const {
292 // Executable files and shared object files have
293 // separate procedure linkage tables.
294 if (Config->Shared) {
295 const uint8_t V[] = {
296 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
297 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
298 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
299 };
300 memcpy(Buf, V, sizeof(V));
301 return;
302 }
George Rimar648a2c32015-10-20 08:54:27 +0000303
George Rimar77b77792015-11-25 22:15:01 +0000304 const uint8_t PltData[] = {
305 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
306 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
307 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
308 };
309 memcpy(Buf, PltData, sizeof(PltData));
310 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
311 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
312}
313
314void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
315 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
316 int32_t Index, unsigned RelOff) const {
317 const uint8_t Inst[] = {
318 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
319 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
320 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
321 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000322 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000323 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
324 Buf[1] = Config->Shared ? 0xa3 : 0x25;
325 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
326 write32le(Buf + 7, RelOff);
327 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000328}
329
George Rimar70e25082015-11-25 11:27:40 +0000330bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
331 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
332 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
333 return SS->Sym.getType() == STT_OBJECT;
334 return false;
335}
336
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000337bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar2558e122015-12-09 09:55:54 +0000338 if (S.isTLS() && Type == R_386_TLS_GD)
339 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
340 if (Type == R_386_TLS_GOTIE)
341 return !isTlsOptimized(Type, &S);
342 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000343}
344
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000345bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarb72a9c62015-12-10 09:03:39 +0000346 return (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
347 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000348}
349
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000350void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000351 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000352 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000353 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000354 case R_386_32:
355 add32le(Loc, SA);
356 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000357 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000358 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000359 break;
George Rimar13934772015-11-25 20:20:31 +0000360 case R_386_GOTPC:
361 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
362 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000363 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000364 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000365 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000366 break;
George Rimar9db204a2015-12-02 09:58:20 +0000367 case R_386_TLS_GD:
368 case R_386_TLS_LDM:
369 case R_386_TLS_TPOFF: {
370 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
371 Out<ELF32LE>::Got->getNumEntries() * 4;
372 checkInt<32>(V, Type);
373 write32le(Loc, V);
374 break;
375 }
376 case R_386_TLS_LDO_32:
377 write32le(Loc, SA);
378 break;
George Rimard23970f2015-11-25 20:41:53 +0000379 case R_386_TLS_LE:
380 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
381 break;
382 case R_386_TLS_LE_32:
383 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
384 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000385 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000386 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000387 }
388}
389
George Rimar2558e122015-12-09 09:55:54 +0000390bool X86TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
391 if (Config->Shared || (S && !S->isTLS()))
392 return false;
393 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
394 Type == R_386_TLS_GD ||
395 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
396}
397
398unsigned X86TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
399 uint32_t Type, uint64_t P,
400 uint64_t SA,
401 const SymbolBody &S) const {
402 switch (Type) {
403 case R_386_TLS_GD:
404 if (canBePreempted(&S, true))
405 relocateTlsGdToIe(Loc, BufEnd, P, SA);
406 else
407 relocateTlsGdToLe(Loc, BufEnd, P, SA);
408 // The next relocation should be against __tls_get_addr, so skip it
409 return 1;
410 case R_386_TLS_GOTIE:
411 relocateTlsIeToLe(Loc, BufEnd, P, SA);
412 return 0;
413 case R_386_TLS_LDM:
414 relocateTlsLdToLe(Loc, BufEnd, P, SA);
415 // The next relocation should be against __tls_get_addr, so skip it
416 return 1;
417 case R_386_TLS_LDO_32:
418 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
419 return 0;
420 }
421 llvm_unreachable("Unknown TLS optimization");
422}
423
424// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
425// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
426// how GD can be optimized to IE:
427// leal x@tlsgd(, %ebx, 1),
428// call __tls_get_addr@plt
429// Is converted to:
430// movl %gs:0, %eax
431// addl x@gotntpoff(%ebx), %eax
432void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
433 uint64_t SA) const {
434 const uint8_t Inst[] = {
435 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
436 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
437 };
438 memcpy(Loc - 3, Inst, sizeof(Inst));
439 relocateOne(Loc + 5, BufEnd, R_386_32, P,
440 SA - Out<ELF32LE>::Got->getVA() -
441 Out<ELF32LE>::Got->getNumEntries() * 4);
442}
443
444// GD can be optimized to LE:
445// leal x@tlsgd(, %ebx, 1),
446// call __tls_get_addr@plt
447// Can be converted to:
448// movl %gs:0,%eax
449// addl $x@ntpoff,%eax
450// But gold emits subl $foo@tpoff,%eax instead of addl.
451// These instructions are completely equal in behavior.
452// This method generates subl to be consistent with gold.
453void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
454 uint64_t SA) const {
455 const uint8_t Inst[] = {
456 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
457 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
458 };
459 memcpy(Loc - 3, Inst, sizeof(Inst));
460 relocateOne(Loc + 5, BufEnd, R_386_32, P,
461 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
462}
463
464// LD can be optimized to LE:
465// leal foo(%reg),%eax
466// call ___tls_get_addr
467// Is converted to:
468// movl %gs:0,%eax
469// nop
470// leal 0(%esi,1),%esi
471void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
472 uint64_t SA) const {
473 const uint8_t Inst[] = {
474 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
475 0x90, // nop
476 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
477 };
478 memcpy(Loc - 2, Inst, sizeof(Inst));
479}
480
481// In some conditions, R_386_TLS_GOTIE relocation can be optimized to
482// R_386_TLS_LE so that it does not use GOT.
483// This function does that. Read "ELF Handling For Thread-Local Storage,
484// 5.1 IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
485// by Ulrich Drepper for details.
486void X86TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
487 uint64_t SA) const {
488 // Ulrich's document section 6.2 says that @gotntpoff can be
489 // used with MOVL or ADDL instructions.
490 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
491 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
492 // Note: gold converts to ADDL instead of LEAL.
493 uint8_t *Inst = Loc - 2;
494 uint8_t *RegSlot = Loc - 1;
495 uint8_t Reg = (Loc[-1] >> 3) & 7;
496 bool IsMov = *Inst == 0x8b;
497 *Inst = IsMov ? 0xc7 : 0x8d;
498 if (IsMov)
499 *RegSlot = 0xc0 | ((*RegSlot >> 3) & 7);
500 else
501 *RegSlot = 0x80 | Reg | (Reg << 3);
502 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
503}
504
Rafael Espindola7f074422015-09-22 21:35:51 +0000505X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000506 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000507 PCRelReloc = R_X86_64_PC32;
508 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000509 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000510 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000511 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000512 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000513 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000514 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000515 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000516 LazyRelocations = true;
517 PltEntrySize = 16;
518 PltZeroEntrySize = 16;
519}
520
Igor Kudrin351b41d2015-11-16 17:44:08 +0000521void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
522 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
523}
524
George Rimar648a2c32015-10-20 08:54:27 +0000525void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
526 // Skip 6 bytes of "jmpq *got(%rip)"
527 write32le(Buf, Plt + 6);
528}
529
530void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
531 uint64_t PltEntryAddr) const {
532 const uint8_t PltData[] = {
533 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
534 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
535 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
536 };
537 memcpy(Buf, PltData, sizeof(PltData));
538 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
539 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000540}
Rafael Espindola01205f72015-09-22 18:19:46 +0000541
George Rimar77b77792015-11-25 22:15:01 +0000542void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
543 uint64_t GotEntryAddr,
544 uint64_t PltEntryAddr, int32_t Index,
545 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000546 const uint8_t Inst[] = {
547 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
548 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
549 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
550 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000551 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000552
George Rimar648a2c32015-10-20 08:54:27 +0000553 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
554 write32le(Buf + 7, Index);
555 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000556}
557
George Rimarbc590fe2015-10-28 16:48:58 +0000558bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
559 const SymbolBody &S) const {
560 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
561 Type == R_X86_64_64)
562 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
563 return SS->Sym.getType() == STT_OBJECT;
564 return false;
565}
566
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000567bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000568 if (Type == R_X86_64_TLSGD)
569 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000570 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000571 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000572 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000573}
574
George Rimard23970f2015-11-25 20:41:53 +0000575bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar25411f252015-12-04 11:20:13 +0000576 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000577}
578
Igor Kudrine7ad0932015-11-17 17:47:53 +0000579unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000580 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000581 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000582 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000583}
584
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000585bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000586 if (relocNeedsCopy(Type, S))
587 return false;
588
Rafael Espindola01205f72015-09-22 18:19:46 +0000589 switch (Type) {
590 default:
591 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000592 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000593 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000594 case R_X86_64_PC32:
595 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000596 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000597 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000598 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000599 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000600 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
601 // libc.so.
602 //
603 // The general idea on how to handle such cases is to create a PLT entry
604 // and use that as the function value.
605 //
606 // For the static linking part, we just return true and everything else
607 // will use the the PLT entry as the address.
608 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000609 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000610 // still works. We need the help of the dynamic linker for that. We
611 // let it know that we have a direct reference to a so symbol by creating
612 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000613 // dynamic linker resolves the symbol to the value of the symbol we created.
614 // This is true even for got entries, so pointer equality is maintained.
615 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000616 // real function is a dedicated got entry used by the plt. That is
617 // identified by special relocation types (R_X86_64_JUMP_SLOT,
618 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000619 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000620 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000621 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000622 }
623}
Rafael Espindolac4010882015-09-22 20:54:08 +0000624
Rafael Espindolaae244002015-10-05 19:30:12 +0000625bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
626 switch (Type) {
627 default:
628 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000629 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000630 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000631 case R_X86_64_PC8:
632 case R_X86_64_PC16:
633 case R_X86_64_PC32:
634 case R_X86_64_PC64:
635 case R_X86_64_PLT32:
George Rimar48651482015-12-11 08:59:37 +0000636 case R_X86_64_SIZE32:
637 case R_X86_64_SIZE64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000638 return true;
639 }
640}
641
George Rimar48651482015-12-11 08:59:37 +0000642bool X86_64TargetInfo::isSizeDynReloc(uint32_t Type,
643 const SymbolBody &S) const {
644 return (Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64) &&
645 canBePreempted(&S, false);
646}
647
George Rimar77d1cb12015-11-24 09:00:06 +0000648bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000649 const SymbolBody *S) const {
650 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000651 return false;
George Rimar25411f252015-12-04 11:20:13 +0000652 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
653 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000654 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
655}
656
657// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
658// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
659// how LD can be optimized to LE:
660// leaq bar@tlsld(%rip), %rdi
661// callq __tls_get_addr@PLT
662// leaq bar@dtpoff(%rax), %rcx
663// Is converted to:
664// .word 0x6666
665// .byte 0x66
666// mov %fs:0,%rax
667// leaq bar@tpoff(%rax), %rcx
668void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
669 uint64_t P, uint64_t SA) const {
670 const uint8_t Inst[] = {
671 0x66, 0x66, //.word 0x6666
672 0x66, //.byte 0x66
673 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
674 };
675 memcpy(Loc - 3, Inst, sizeof(Inst));
676}
677
678// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
679// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
680// how GD can be optimized to LE:
681// .byte 0x66
682// leaq x@tlsgd(%rip), %rdi
683// .word 0x6666
684// rex64
685// call __tls_get_addr@plt
686// Is converted to:
687// mov %fs:0x0,%rax
688// lea x@tpoff,%rax
689void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
690 uint64_t P, uint64_t SA) const {
691 const uint8_t Inst[] = {
692 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
693 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
694 };
695 memcpy(Loc - 4, Inst, sizeof(Inst));
696 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000697}
698
George Rimar25411f252015-12-04 11:20:13 +0000699// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
700// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
701// how GD can be optimized to IE:
702// .byte 0x66
703// leaq x@tlsgd(%rip), %rdi
704// .word 0x6666
705// rex64
706// call __tls_get_addr@plt
707// Is converted to:
708// mov %fs:0x0,%rax
709// addq x@tpoff,%rax
710void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
711 uint64_t P, uint64_t SA) const {
712 const uint8_t Inst[] = {
713 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
714 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
715 };
716 memcpy(Loc - 4, Inst, sizeof(Inst));
717 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
718}
719
George Rimar77d1cb12015-11-24 09:00:06 +0000720// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000721// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000722// This function does that. Read "ELF Handling For Thread-Local Storage,
723// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
724// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000725void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
726 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000727 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
728 // used in MOVQ or ADDQ instructions only.
729 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
730 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
731 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
732 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
733 uint8_t *Prefix = Loc - 3;
734 uint8_t *Inst = Loc - 2;
735 uint8_t *RegSlot = Loc - 1;
736 uint8_t Reg = Loc[-1] >> 3;
737 bool IsMov = *Inst == 0x8b;
738 bool RspAdd = !IsMov && Reg == 4;
739 // r12 and rsp registers requires special handling.
740 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
741 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
742 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
743 // The same true for rsp. So we convert to addq for them, saving 1 byte that
744 // we dont have.
745 if (RspAdd)
746 *Inst = 0x81;
747 else
748 *Inst = IsMov ? 0xc7 : 0x8d;
749 if (*Prefix == 0x4c)
750 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
751 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
752 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
753}
754
George Rimar6713cf82015-11-25 21:46:05 +0000755// This function applies a TLS relocation with an optimization as described
756// in the Ulrich's document. As a result of rewriting instructions at the
757// relocation target, relocations immediately follow the TLS relocation (which
758// would be applied to rewritten instructions) may have to be skipped.
759// This function returns a number of relocations that need to be skipped.
760unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
761 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000762 uint64_t SA,
763 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000764 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000765 case R_X86_64_DTPOFF32:
766 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
767 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000768 case R_X86_64_GOTTPOFF:
769 relocateTlsIeToLe(Loc, BufEnd, P, SA);
770 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000771 case R_X86_64_TLSGD: {
772 if (canBePreempted(&S, true))
773 relocateTlsGdToIe(Loc, BufEnd, P, SA);
774 else
775 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000776 // The next relocation should be against __tls_get_addr, so skip it
777 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000778 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000779 case R_X86_64_TLSLD:
780 relocateTlsLdToLe(Loc, BufEnd, P, SA);
781 // The next relocation should be against __tls_get_addr, so skip it
782 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000783 }
784 llvm_unreachable("Unknown TLS optimization");
785}
786
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000787void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000788 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000789 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000790 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000791 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000792 checkUInt<32>(SA, Type);
793 write32le(Loc, SA);
794 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000795 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000796 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000797 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000798 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000799 case R_X86_64_64:
800 write64le(Loc, SA);
801 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000802 case R_X86_64_DTPOFF32:
803 write32le(Loc, SA);
804 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000805 case R_X86_64_DTPOFF64:
806 write64le(Loc, SA);
807 break;
808 case R_X86_64_GOTPCREL:
809 case R_X86_64_PC32:
810 case R_X86_64_PLT32:
811 case R_X86_64_TLSGD:
812 case R_X86_64_TLSLD:
813 write32le(Loc, SA - P);
814 break;
George Rimar48651482015-12-11 08:59:37 +0000815 case R_X86_64_SIZE32:
816 write32le(Loc, ZA);
817 break;
818 case R_X86_64_SIZE64:
819 write64le(Loc, ZA);
820 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000821 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000822 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000823 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000824 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000825 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000826 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000827 case R_X86_64_TPOFF64:
828 write32le(Loc, SA - P);
829 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000830 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000831 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000832 }
833}
834
Hal Finkel3c8cc672015-10-12 20:56:18 +0000835// Relocation masks following the #lo(value), #hi(value), #ha(value),
836// #higher(value), #highera(value), #highest(value), and #highesta(value)
837// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
838// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000839static uint16_t applyPPCLo(uint64_t V) { return V; }
840static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
841static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
842static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
843static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000844static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000845static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
846
Rafael Espindolac4010882015-09-22 20:54:08 +0000847PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000848 PCRelReloc = R_PPC64_REL24;
849 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000850 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000851 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000852
853 // We need 64K pages (at least under glibc/Linux, the loader won't
854 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000855 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000856
857 // The PPC64 ELF ABI v1 spec, says:
858 //
859 // It is normally desirable to put segments with different characteristics
860 // in separate 256 Mbyte portions of the address space, to give the
861 // operating system full paging flexibility in the 64-bit address space.
862 //
863 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
864 // use 0x10000000 as the starting address.
865 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000866}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000867
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000868uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000869 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
870 // order. The TOC starts where the first of these sections starts.
871
872 // FIXME: This obviously does not do the right thing when there is no .got
873 // section, but there is a .toc or .tocbss section.
874 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
875 if (!TocVA)
876 TocVA = Out<ELF64BE>::Plt->getVA();
877
878 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
879 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
880 // code (crt1.o) assumes that you can get from the TOC base to the
881 // start of the .toc section with only a single (signed) 16-bit relocation.
882 return TocVA + 0x8000;
883}
884
George Rimar648a2c32015-10-20 08:54:27 +0000885void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
886void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
887 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000888void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
889 uint64_t GotEntryAddr,
890 uint64_t PltEntryAddr, int32_t Index,
891 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000892 uint64_t Off = GotEntryAddr - getPPC64TocBase();
893
894 // FIXME: What we should do, in theory, is get the offset of the function
895 // descriptor in the .opd section, and use that as the offset from %r2 (the
896 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
897 // be a pointer to the function descriptor in the .opd section. Using
898 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
899
Hal Finkelfa92f682015-10-13 21:47:34 +0000900 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000901 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
902 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
903 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
904 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
905 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
906 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
907 write32be(Buf + 28, 0x4e800420); // bctr
908}
909
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000910bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000911 if (relocNeedsPlt(Type, S))
912 return true;
913
914 switch (Type) {
915 default: return false;
916 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000917 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000918 case R_PPC64_GOT16_HA:
919 case R_PPC64_GOT16_HI:
920 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000921 case R_PPC64_GOT16_LO_DS:
922 return true;
923 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000924}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000925
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000926bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000927 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000928 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000929}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000930
Hal Finkelbe0823d2015-10-12 20:58:52 +0000931bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
932 switch (Type) {
933 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000934 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000935 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000936 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000937 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000938 }
939}
940
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000941void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000942 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000943 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000944 uint64_t TB = getPPC64TocBase();
945
Hal Finkel3c8cc672015-10-12 20:56:18 +0000946 // For a TOC-relative relocation, adjust the addend and proceed in terms of
947 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000948 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000949 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
950 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000951 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
952 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000953 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
954 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000955 default: break;
956 }
957
Hal Finkel3c8cc672015-10-12 20:56:18 +0000958 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000959 case R_PPC64_ADDR14: {
960 checkAlignment<4>(SA, Type);
961 // Preserve the AA/LK bits in the branch instruction
962 uint8_t AALK = Loc[3];
963 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
964 break;
965 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000966 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000967 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000968 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000969 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000970 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000971 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000972 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000973 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000974 case R_PPC64_ADDR16_HA:
975 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000976 break;
977 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000978 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000979 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000980 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000981 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982 break;
983 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000984 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000985 break;
986 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000987 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000988 break;
989 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000990 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000991 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000992 case R_PPC64_ADDR16_LO:
993 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000994 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000995 case R_PPC64_ADDR16_LO_DS:
996 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000997 break;
998 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000999 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001000 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001001 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001002 case R_PPC64_ADDR64:
1003 write64be(Loc, SA);
1004 break;
1005 case R_PPC64_REL16_HA:
1006 write16be(Loc, applyPPCHa(SA - P));
1007 break;
1008 case R_PPC64_REL16_HI:
1009 write16be(Loc, applyPPCHi(SA - P));
1010 break;
1011 case R_PPC64_REL16_LO:
1012 write16be(Loc, applyPPCLo(SA - P));
1013 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001014 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001015 // If we have an undefined weak symbol, we might get here with a symbol
1016 // address of zero. That could overflow, but the code must be unreachable,
1017 // so don't bother doing anything at all.
1018 if (!SA)
1019 break;
1020
Hal Finkeldaedc122015-10-12 23:16:53 +00001021 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1022 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001023 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001024
1025 if (!InPlt && Out<ELF64BE>::Opd) {
1026 // If this is a local call, and we currently have the address of a
1027 // function-descriptor, get the underlying code address instead.
1028 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1029 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001030 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001031
1032 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001033 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001034 }
1035
Hal Finkel3c8cc672015-10-12 20:56:18 +00001036 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001037 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001038 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001039
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001040 uint32_t Nop = 0x60000000;
1041 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1042 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 break;
1044 }
1045 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001046 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001047 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048 break;
1049 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001050 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001052 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001053 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001054 break;
1055 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001056 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001057 }
1058}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001059
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001060AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +00001061 CopyReloc = R_AARCH64_COPY;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001062 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001063 PltReloc = R_AARCH64_JUMP_SLOT;
1064 LazyRelocations = true;
1065 PltEntrySize = 16;
1066 PltZeroEntrySize = 32;
1067}
George Rimar648a2c32015-10-20 08:54:27 +00001068
Igor Kudrincfe47f52015-12-05 06:20:24 +00001069unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
1070 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1071 return Type;
1072 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
1073 error("Relocation " + S + " cannot be used when making a shared object; "
1074 "recompile with -fPIC.");
1075}
1076
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001077unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
1078
1079void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1080 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1081}
1082
George Rimar648a2c32015-10-20 08:54:27 +00001083void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001084 uint64_t PltEntryAddr) const {
1085 const uint8_t PltData[] = {
1086 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1087 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1088 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1089 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1090 0x20, 0x02, 0x1f, 0xd6, // br x17
1091 0x1f, 0x20, 0x03, 0xd5, // nop
1092 0x1f, 0x20, 0x03, 0xd5, // nop
1093 0x1f, 0x20, 0x03, 0xd5 // nop
1094 };
1095 memcpy(Buf, PltData, sizeof(PltData));
1096
1097 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
1098 GotEntryAddr + 16);
1099 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
1100 GotEntryAddr + 16);
1101 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
1102 GotEntryAddr + 16);
1103}
1104
George Rimar77b77792015-11-25 22:15:01 +00001105void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1106 uint64_t GotEntryAddr,
1107 uint64_t PltEntryAddr, int32_t Index,
1108 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001109 const uint8_t Inst[] = {
1110 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1111 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1112 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1113 0x20, 0x02, 0x1f, 0xd6 // br x17
1114 };
1115 memcpy(Buf, Inst, sizeof(Inst));
1116
1117 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1118 GotEntryAddr);
1119 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1120 GotEntryAddr);
1121 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1122 GotEntryAddr);
1123}
1124
Igor Kudrin9606d192015-12-03 08:05:35 +00001125bool AArch64TargetInfo::relocNeedsCopy(uint32_t Type,
1126 const SymbolBody &S) const {
1127 if (Config->Shared)
1128 return false;
1129 switch (Type) {
1130 default:
1131 return false;
1132 case R_AARCH64_ABS16:
1133 case R_AARCH64_ABS32:
1134 case R_AARCH64_ABS64:
1135 case R_AARCH64_ADD_ABS_LO12_NC:
1136 case R_AARCH64_ADR_PREL_LO21:
1137 case R_AARCH64_ADR_PREL_PG_HI21:
1138 case R_AARCH64_LDST8_ABS_LO12_NC:
1139 case R_AARCH64_LDST32_ABS_LO12_NC:
1140 case R_AARCH64_LDST64_ABS_LO12_NC:
1141 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1142 return SS->Sym.getType() == STT_OBJECT;
1143 return false;
1144 }
1145}
1146
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001147bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
1148 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001149 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
1150 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001151}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001152
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001153bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
1154 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001155 switch (Type) {
1156 default:
1157 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001158 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001159 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001160 return canBePreempted(&S, true);
1161 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001162}
Davide Italiano1d750a62015-09-27 08:45:38 +00001163
Davide Italianoef4be6b2015-10-06 19:01:32 +00001164static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001165 uint32_t ImmLo = (Imm & 0x3) << 29;
1166 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1167 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001168 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001169}
1170
Davide Italiano318ca222015-10-02 22:13:51 +00001171// Page(Expr) is the page address of the expression Expr, defined
1172// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001173// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001174static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001175 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001176}
1177
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001178void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001179 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001180 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001181 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001182 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001183 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001184 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001185 break;
1186 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001187 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001189 break;
1190 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001191 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001192 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001193 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001194 // This relocation stores 12 bits and there's no instruction
1195 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001196 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1197 // bits in Loc are zero.
1198 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001199 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001200 case R_AARCH64_ADR_GOT_PAGE: {
1201 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1202 checkInt<33>(X, Type);
1203 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1204 break;
1205 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001206 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001207 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001208 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001209 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001210 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001211 }
1212 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001213 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001214 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001215 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001216 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001217 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001218 case R_AARCH64_CALL26:
1219 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001220 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001221 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001222 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1223 break;
1224 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001225 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001226 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001227 or32le(Loc, (SA & 0xFF8) << 7);
1228 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001229 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001230 or32le(Loc, (SA & 0xFFF) << 10);
1231 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001232 case R_AARCH64_LDST32_ABS_LO12_NC:
1233 or32le(Loc, (SA & 0xFFC) << 8);
1234 break;
1235 case R_AARCH64_LDST64_ABS_LO12_NC:
1236 or32le(Loc, (SA & 0xFF8) << 7);
1237 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001238 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001239 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001240 write16le(Loc, SA - P);
1241 break;
1242 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001243 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001244 write32le(Loc, SA - P);
1245 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001246 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001247 write64le(Loc, SA - P);
1248 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001249 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001250 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001251 }
1252}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001253
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001254template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001255 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001256 GotHeaderEntriesNum = 2;
1257}
1258
1259template <class ELFT>
1260void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001261 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001262 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1263 // Module pointer
1264 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001265}
1266
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001267template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001268void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1269template <class ELFT>
1270void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1271 uint64_t PltEntryAddr) const {}
1272template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001273void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1274 uint64_t GotEntryAddr,
1275 uint64_t PltEntryAddr, int32_t Index,
1276 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001277
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001278template <class ELFT>
1279bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1280 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001281 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001282}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001283
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001284template <class ELFT>
1285bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1286 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001287 return false;
1288}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001289
Simon Atanasyan35031192015-12-15 06:06:34 +00001290static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001291
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001292template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001293void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001294 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001295 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001296 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001297 switch (Type) {
1298 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001299 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001300 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001301 case R_MIPS_CALL16:
1302 case R_MIPS_GOT16: {
1303 int64_t V = SA - getMipsGpAddr<ELFT>();
1304 if (Type == R_MIPS_GOT16)
1305 checkInt<16>(V, Type);
1306 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1307 break;
1308 }
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001309 case R_MIPS_HI16: {
1310 uint32_t Instr = read32<E>(Loc);
1311 if (PairedLoc) {
1312 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001313 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001314 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001315 } else {
1316 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001317 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001318 }
1319 break;
1320 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001321 case R_MIPS_JALR:
1322 // Ignore this optimization relocation for now
1323 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001324 case R_MIPS_LO16: {
1325 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001326 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001327 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1328 break;
1329 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001330 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001331 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001332 }
1333}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001334
Rui Ueyama24e39522015-12-03 20:57:45 +00001335template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001336 const unsigned GPOffset = 0x7ff0;
1337 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1338}
1339
1340template uint32_t getMipsGpAddr<ELF32LE>();
1341template uint32_t getMipsGpAddr<ELF32BE>();
1342template uint64_t getMipsGpAddr<ELF64LE>();
1343template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001344}
1345}