blob: fe8b0b71ed20236033c60f6ce12bb6e799d8d4be [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,
Simon Atanasyan09b3e362015-12-01 21:24:45 +000090 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000091};
92
93class X86_64TargetInfo final : public TargetInfo {
94public:
95 X86_64TargetInfo();
Igor Kudrine7ad0932015-11-17 17:47:53 +000096 unsigned getPltRefReloc(unsigned Type) const override;
George Rimard23970f2015-11-25 20:41:53 +000097 bool isTlsDynReloc(unsigned Type) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +000098 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +000099 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
100 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
101 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000102 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
103 uint64_t PltEntryAddr, int32_t Index,
104 unsigned RelOff) const override;
George Rimarbc590fe2015-10-28 16:48:58 +0000105 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000106 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
107 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000108 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000109 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000110 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000111 bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override;
112 unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar25411f252015-12-04 11:20:13 +0000113 uint64_t P, uint64_t SA,
114 const SymbolBody &S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000115
116private:
117 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
118 uint64_t SA) const;
119 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
120 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000121 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
122 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000123 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
124 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000125};
126
127class PPC64TargetInfo final : public TargetInfo {
128public:
129 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +0000130 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
131 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
132 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000133 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
134 uint64_t PltEntryAddr, int32_t Index,
135 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
137 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000138 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000139 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000140 bool isRelRelative(uint32_t Type) const override;
141};
142
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000143class AArch64TargetInfo final : public TargetInfo {
144public:
145 AArch64TargetInfo();
Igor Kudrincfe47f52015-12-05 06:20:24 +0000146 unsigned getDynReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000147 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000148 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
149 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
150 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000151 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
152 uint64_t PltEntryAddr, int32_t Index,
153 unsigned RelOff) const override;
Igor Kudrin9606d192015-12-03 08:05:35 +0000154 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
156 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000157 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000158 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000159};
160
161template <class ELFT> class MipsTargetInfo final : public TargetInfo {
162public:
163 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000164 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000165 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
166 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
167 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000168 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
169 uint64_t PltEntryAddr, int32_t Index,
170 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000171 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
172 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000173 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000174 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000175};
176} // anonymous namespace
177
Rui Ueyama91004392015-10-13 16:08:15 +0000178TargetInfo *createTarget() {
179 switch (Config->EMachine) {
180 case EM_386:
181 return new X86TargetInfo();
182 case EM_AARCH64:
183 return new AArch64TargetInfo();
184 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000185 switch (Config->EKind) {
186 case ELF32LEKind:
187 return new MipsTargetInfo<ELF32LE>();
188 case ELF32BEKind:
189 return new MipsTargetInfo<ELF32BE>();
190 default:
191 error("Unsupported MIPS target");
192 }
Rui Ueyama91004392015-10-13 16:08:15 +0000193 case EM_PPC64:
194 return new PPC64TargetInfo();
195 case EM_X86_64:
196 return new X86_64TargetInfo();
197 }
198 error("Unknown target machine");
199}
200
Rafael Espindola01205f72015-09-22 18:19:46 +0000201TargetInfo::~TargetInfo() {}
202
George Rimar6713cf82015-11-25 21:46:05 +0000203bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000204 return false;
205}
206
Igor Kudrinf6f45472015-11-10 08:39:27 +0000207uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
208
George Rimarbc590fe2015-10-28 16:48:58 +0000209bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
210 return false;
211}
212
Igor Kudrine7ad0932015-11-17 17:47:53 +0000213unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000214
Rafael Espindolaae244002015-10-05 19:30:12 +0000215bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
216
George Rimar6713cf82015-11-25 21:46:05 +0000217unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000218 uint32_t Type, uint64_t P, uint64_t SA,
219 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000220 return 0;
221}
George Rimar77d1cb12015-11-24 09:00:06 +0000222
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000223void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
224
Igor Kudrin351b41d2015-11-16 17:44:08 +0000225void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
226
Rafael Espindola7f074422015-09-22 21:35:51 +0000227X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000228 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000229 PCRelReloc = R_386_PC32;
230 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000231 PltReloc = R_386_JUMP_SLOT;
George Rimar9db204a2015-12-02 09:58:20 +0000232 TlsGotReloc = R_386_TLS_TPOFF;
233 TlsGlobalDynamicReloc = R_386_TLS_GD;
234 TlsLocalDynamicReloc = R_386_TLS_LDM;
235 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
236 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000237 LazyRelocations = true;
238 PltEntrySize = 16;
239 PltZeroEntrySize = 16;
240}
241
242void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
243 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
244}
245
246void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
247 // Skip 6 bytes of "pushl (GOT+4)"
248 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000249}
Rafael Espindola01205f72015-09-22 18:19:46 +0000250
George Rimard23970f2015-11-25 20:41:53 +0000251unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
252 if (Type == R_386_TLS_LE)
253 return R_386_TLS_TPOFF;
254 if (Type == R_386_TLS_LE_32)
255 return R_386_TLS_TPOFF32;
256 return Type;
257}
258
259bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar9db204a2015-12-02 09:58:20 +0000260 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
261 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000262 return Config->Shared;
263 return false;
264}
265
George Rimar648a2c32015-10-20 08:54:27 +0000266void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000267 uint64_t PltEntryAddr) const {
268 // Executable files and shared object files have
269 // separate procedure linkage tables.
270 if (Config->Shared) {
271 const uint8_t V[] = {
272 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
273 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
274 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
275 };
276 memcpy(Buf, V, sizeof(V));
277 return;
278 }
George Rimar648a2c32015-10-20 08:54:27 +0000279
George Rimar77b77792015-11-25 22:15:01 +0000280 const uint8_t PltData[] = {
281 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
282 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
283 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
284 };
285 memcpy(Buf, PltData, sizeof(PltData));
286 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
287 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
288}
289
290void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
291 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
292 int32_t Index, unsigned RelOff) const {
293 const uint8_t Inst[] = {
294 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
295 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
296 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
297 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000298 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000299 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
300 Buf[1] = Config->Shared ? 0xa3 : 0x25;
301 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
302 write32le(Buf + 7, RelOff);
303 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000304}
305
George Rimar70e25082015-11-25 11:27:40 +0000306bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
307 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
308 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
309 return SS->Sym.getType() == STT_OBJECT;
310 return false;
311}
312
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000313bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000314 return Type == R_386_TLS_GOTIE || Type == R_386_GOT32 ||
315 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000316}
317
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000318bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000319 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000320}
321
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000322void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000323 uint64_t P, uint64_t SA,
324 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000325 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000326 case R_386_32:
327 add32le(Loc, SA);
328 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000329 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000330 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000331 break;
George Rimar13934772015-11-25 20:20:31 +0000332 case R_386_GOTPC:
333 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
334 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000335 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000336 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000337 break;
George Rimar9db204a2015-12-02 09:58:20 +0000338 case R_386_TLS_GD:
339 case R_386_TLS_LDM:
340 case R_386_TLS_TPOFF: {
341 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
342 Out<ELF32LE>::Got->getNumEntries() * 4;
343 checkInt<32>(V, Type);
344 write32le(Loc, V);
345 break;
346 }
347 case R_386_TLS_LDO_32:
348 write32le(Loc, SA);
349 break;
George Rimard23970f2015-11-25 20:41:53 +0000350 case R_386_TLS_LE:
351 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
352 break;
353 case R_386_TLS_LE_32:
354 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
355 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000356 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000357 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000358 }
359}
360
Rafael Espindola7f074422015-09-22 21:35:51 +0000361X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000362 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000363 PCRelReloc = R_X86_64_PC32;
364 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000365 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000366 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000367 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000368 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000369 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000370 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000371 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000372 LazyRelocations = true;
373 PltEntrySize = 16;
374 PltZeroEntrySize = 16;
375}
376
Igor Kudrin351b41d2015-11-16 17:44:08 +0000377void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
378 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
379}
380
George Rimar648a2c32015-10-20 08:54:27 +0000381void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
382 // Skip 6 bytes of "jmpq *got(%rip)"
383 write32le(Buf, Plt + 6);
384}
385
386void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
387 uint64_t PltEntryAddr) const {
388 const uint8_t PltData[] = {
389 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
390 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
391 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
392 };
393 memcpy(Buf, PltData, sizeof(PltData));
394 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
395 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000396}
Rafael Espindola01205f72015-09-22 18:19:46 +0000397
George Rimar77b77792015-11-25 22:15:01 +0000398void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
399 uint64_t GotEntryAddr,
400 uint64_t PltEntryAddr, int32_t Index,
401 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000402 const uint8_t Inst[] = {
403 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
404 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
405 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
406 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000407 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000408
George Rimar648a2c32015-10-20 08:54:27 +0000409 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
410 write32le(Buf + 7, Index);
411 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000412}
413
George Rimarbc590fe2015-10-28 16:48:58 +0000414bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
415 const SymbolBody &S) const {
416 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
417 Type == R_X86_64_64)
418 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
419 return SS->Sym.getType() == STT_OBJECT;
420 return false;
421}
422
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000423bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000424 if (Type == R_X86_64_TLSGD)
425 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000426 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000427 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000428 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000429}
430
George Rimard23970f2015-11-25 20:41:53 +0000431bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar25411f252015-12-04 11:20:13 +0000432 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000433}
434
Igor Kudrine7ad0932015-11-17 17:47:53 +0000435unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000436 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000437 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000438 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000439}
440
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000441bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000442 if (relocNeedsCopy(Type, S))
443 return false;
444
Rafael Espindola01205f72015-09-22 18:19:46 +0000445 switch (Type) {
446 default:
447 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000448 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000449 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000450 case R_X86_64_PC32:
451 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000452 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000453 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000454 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000455 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000456 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
457 // libc.so.
458 //
459 // The general idea on how to handle such cases is to create a PLT entry
460 // and use that as the function value.
461 //
462 // For the static linking part, we just return true and everything else
463 // will use the the PLT entry as the address.
464 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000465 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000466 // still works. We need the help of the dynamic linker for that. We
467 // let it know that we have a direct reference to a so symbol by creating
468 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000469 // dynamic linker resolves the symbol to the value of the symbol we created.
470 // This is true even for got entries, so pointer equality is maintained.
471 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000472 // real function is a dedicated got entry used by the plt. That is
473 // identified by special relocation types (R_X86_64_JUMP_SLOT,
474 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000475 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000476 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000477 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000478 }
479}
Rafael Espindolac4010882015-09-22 20:54:08 +0000480
Rafael Espindolaae244002015-10-05 19:30:12 +0000481bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
482 switch (Type) {
483 default:
484 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000485 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000486 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000487 case R_X86_64_PC8:
488 case R_X86_64_PC16:
489 case R_X86_64_PC32:
490 case R_X86_64_PC64:
491 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000492 return true;
493 }
494}
495
George Rimar77d1cb12015-11-24 09:00:06 +0000496bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000497 const SymbolBody *S) const {
498 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000499 return false;
George Rimar25411f252015-12-04 11:20:13 +0000500 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
501 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000502 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
503}
504
505// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
506// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
507// how LD can be optimized to LE:
508// leaq bar@tlsld(%rip), %rdi
509// callq __tls_get_addr@PLT
510// leaq bar@dtpoff(%rax), %rcx
511// Is converted to:
512// .word 0x6666
513// .byte 0x66
514// mov %fs:0,%rax
515// leaq bar@tpoff(%rax), %rcx
516void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
517 uint64_t P, uint64_t SA) const {
518 const uint8_t Inst[] = {
519 0x66, 0x66, //.word 0x6666
520 0x66, //.byte 0x66
521 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
522 };
523 memcpy(Loc - 3, Inst, sizeof(Inst));
524}
525
526// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
527// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
528// how GD can be optimized to LE:
529// .byte 0x66
530// leaq x@tlsgd(%rip), %rdi
531// .word 0x6666
532// rex64
533// call __tls_get_addr@plt
534// Is converted to:
535// mov %fs:0x0,%rax
536// lea x@tpoff,%rax
537void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
538 uint64_t P, uint64_t SA) const {
539 const uint8_t Inst[] = {
540 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
541 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
542 };
543 memcpy(Loc - 4, Inst, sizeof(Inst));
544 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000545}
546
George Rimar25411f252015-12-04 11:20:13 +0000547// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
548// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
549// how GD can be optimized to IE:
550// .byte 0x66
551// leaq x@tlsgd(%rip), %rdi
552// .word 0x6666
553// rex64
554// call __tls_get_addr@plt
555// Is converted to:
556// mov %fs:0x0,%rax
557// addq x@tpoff,%rax
558void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
559 uint64_t P, uint64_t SA) const {
560 const uint8_t Inst[] = {
561 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
562 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
563 };
564 memcpy(Loc - 4, Inst, sizeof(Inst));
565 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
566}
567
George Rimar77d1cb12015-11-24 09:00:06 +0000568// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
569// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
570// This function does that. Read "ELF Handling For Thread-Local Storage,
571// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
572// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000573void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
574 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000575 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
576 // used in MOVQ or ADDQ instructions only.
577 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
578 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
579 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
580 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
581 uint8_t *Prefix = Loc - 3;
582 uint8_t *Inst = Loc - 2;
583 uint8_t *RegSlot = Loc - 1;
584 uint8_t Reg = Loc[-1] >> 3;
585 bool IsMov = *Inst == 0x8b;
586 bool RspAdd = !IsMov && Reg == 4;
587 // r12 and rsp registers requires special handling.
588 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
589 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
590 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
591 // The same true for rsp. So we convert to addq for them, saving 1 byte that
592 // we dont have.
593 if (RspAdd)
594 *Inst = 0x81;
595 else
596 *Inst = IsMov ? 0xc7 : 0x8d;
597 if (*Prefix == 0x4c)
598 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
599 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
600 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
601}
602
George Rimar6713cf82015-11-25 21:46:05 +0000603// This function applies a TLS relocation with an optimization as described
604// in the Ulrich's document. As a result of rewriting instructions at the
605// relocation target, relocations immediately follow the TLS relocation (which
606// would be applied to rewritten instructions) may have to be skipped.
607// This function returns a number of relocations that need to be skipped.
608unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
609 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000610 uint64_t SA,
611 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000612 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000613 case R_X86_64_DTPOFF32:
614 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
615 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000616 case R_X86_64_GOTTPOFF:
617 relocateTlsIeToLe(Loc, BufEnd, P, SA);
618 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000619 case R_X86_64_TLSGD: {
620 if (canBePreempted(&S, true))
621 relocateTlsGdToIe(Loc, BufEnd, P, SA);
622 else
623 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000624 // The next relocation should be against __tls_get_addr, so skip it
625 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000626 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000627 case R_X86_64_TLSLD:
628 relocateTlsLdToLe(Loc, BufEnd, P, SA);
629 // The next relocation should be against __tls_get_addr, so skip it
630 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000631 }
632 llvm_unreachable("Unknown TLS optimization");
633}
634
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000635void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000636 uint64_t P, uint64_t SA,
637 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000638 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000639 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000640 checkUInt<32>(SA, Type);
641 write32le(Loc, SA);
642 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000643 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000644 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000645 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000646 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000647 case R_X86_64_64:
648 write64le(Loc, SA);
649 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000650 case R_X86_64_DTPOFF32:
651 write32le(Loc, SA);
652 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000653 case R_X86_64_DTPOFF64:
654 write64le(Loc, SA);
655 break;
656 case R_X86_64_GOTPCREL:
657 case R_X86_64_PC32:
658 case R_X86_64_PLT32:
659 case R_X86_64_TLSGD:
660 case R_X86_64_TLSLD:
661 write32le(Loc, SA - P);
662 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000663 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000664 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000665 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000666 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000667 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000668 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000669 case R_X86_64_TPOFF64:
670 write32le(Loc, SA - P);
671 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000672 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000673 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000674 }
675}
676
Hal Finkel3c8cc672015-10-12 20:56:18 +0000677// Relocation masks following the #lo(value), #hi(value), #ha(value),
678// #higher(value), #highera(value), #highest(value), and #highesta(value)
679// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
680// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000681static uint16_t applyPPCLo(uint64_t V) { return V; }
682static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
683static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
684static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
685static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000686static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000687static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
688
Rafael Espindolac4010882015-09-22 20:54:08 +0000689PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000690 PCRelReloc = R_PPC64_REL24;
691 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000692 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000693 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000694
695 // We need 64K pages (at least under glibc/Linux, the loader won't
696 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000697 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000698
699 // The PPC64 ELF ABI v1 spec, says:
700 //
701 // It is normally desirable to put segments with different characteristics
702 // in separate 256 Mbyte portions of the address space, to give the
703 // operating system full paging flexibility in the 64-bit address space.
704 //
705 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
706 // use 0x10000000 as the starting address.
707 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000708}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000709
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000710uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000711 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
712 // order. The TOC starts where the first of these sections starts.
713
714 // FIXME: This obviously does not do the right thing when there is no .got
715 // section, but there is a .toc or .tocbss section.
716 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
717 if (!TocVA)
718 TocVA = Out<ELF64BE>::Plt->getVA();
719
720 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
721 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
722 // code (crt1.o) assumes that you can get from the TOC base to the
723 // start of the .toc section with only a single (signed) 16-bit relocation.
724 return TocVA + 0x8000;
725}
726
George Rimar648a2c32015-10-20 08:54:27 +0000727void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
728void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
729 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000730void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
731 uint64_t GotEntryAddr,
732 uint64_t PltEntryAddr, int32_t Index,
733 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000734 uint64_t Off = GotEntryAddr - getPPC64TocBase();
735
736 // FIXME: What we should do, in theory, is get the offset of the function
737 // descriptor in the .opd section, and use that as the offset from %r2 (the
738 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
739 // be a pointer to the function descriptor in the .opd section. Using
740 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
741
Hal Finkelfa92f682015-10-13 21:47:34 +0000742 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000743 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
744 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
745 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
746 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
747 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
748 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
749 write32be(Buf + 28, 0x4e800420); // bctr
750}
751
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000752bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000753 if (relocNeedsPlt(Type, S))
754 return true;
755
756 switch (Type) {
757 default: return false;
758 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000759 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000760 case R_PPC64_GOT16_HA:
761 case R_PPC64_GOT16_HI:
762 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000763 case R_PPC64_GOT16_LO_DS:
764 return true;
765 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000766}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000767
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000768bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000769 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000770 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000771}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000772
Hal Finkelbe0823d2015-10-12 20:58:52 +0000773bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
774 switch (Type) {
775 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000776 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000777 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000778 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000779 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000780 }
781}
782
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000783void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000784 uint64_t P, uint64_t SA,
785 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000786 uint64_t TB = getPPC64TocBase();
787
Hal Finkel3c8cc672015-10-12 20:56:18 +0000788 // For a TOC-relative relocation, adjust the addend and proceed in terms of
789 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000790 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000791 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
792 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000793 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
794 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000795 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
796 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000797 default: break;
798 }
799
Hal Finkel3c8cc672015-10-12 20:56:18 +0000800 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000801 case R_PPC64_ADDR14: {
802 checkAlignment<4>(SA, Type);
803 // Preserve the AA/LK bits in the branch instruction
804 uint8_t AALK = Loc[3];
805 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
806 break;
807 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000808 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000809 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000810 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000811 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000812 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000813 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000814 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000815 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000816 case R_PPC64_ADDR16_HA:
817 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000818 break;
819 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000820 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000821 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000822 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000823 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000824 break;
825 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000826 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000827 break;
828 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000829 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000830 break;
831 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000832 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000833 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000834 case R_PPC64_ADDR16_LO:
835 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000836 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000837 case R_PPC64_ADDR16_LO_DS:
838 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000839 break;
840 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000841 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000842 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000843 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000844 case R_PPC64_ADDR64:
845 write64be(Loc, SA);
846 break;
847 case R_PPC64_REL16_HA:
848 write16be(Loc, applyPPCHa(SA - P));
849 break;
850 case R_PPC64_REL16_HI:
851 write16be(Loc, applyPPCHi(SA - P));
852 break;
853 case R_PPC64_REL16_LO:
854 write16be(Loc, applyPPCLo(SA - P));
855 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000856 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000857 // If we have an undefined weak symbol, we might get here with a symbol
858 // address of zero. That could overflow, but the code must be unreachable,
859 // so don't bother doing anything at all.
860 if (!SA)
861 break;
862
Hal Finkeldaedc122015-10-12 23:16:53 +0000863 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
864 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000865 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000866
867 if (!InPlt && Out<ELF64BE>::Opd) {
868 // If this is a local call, and we currently have the address of a
869 // function-descriptor, get the underlying code address instead.
870 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
871 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000872 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000873
874 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000875 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000876 }
877
Hal Finkel3c8cc672015-10-12 20:56:18 +0000878 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000879 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000880 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000881
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000882 uint32_t Nop = 0x60000000;
883 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
884 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000885 break;
886 }
887 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000888 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000889 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000890 break;
891 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000892 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000893 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000894 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000895 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000896 break;
897 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000898 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000899 }
900}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000901
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000902AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +0000903 CopyReloc = R_AARCH64_COPY;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000904 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000905 PltReloc = R_AARCH64_JUMP_SLOT;
906 LazyRelocations = true;
907 PltEntrySize = 16;
908 PltZeroEntrySize = 32;
909}
George Rimar648a2c32015-10-20 08:54:27 +0000910
Igor Kudrincfe47f52015-12-05 06:20:24 +0000911unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
912 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
913 return Type;
914 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
915 error("Relocation " + S + " cannot be used when making a shared object; "
916 "recompile with -fPIC.");
917}
918
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000919unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
920
921void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
922 write64le(Buf, Out<ELF64LE>::Plt->getVA());
923}
924
George Rimar648a2c32015-10-20 08:54:27 +0000925void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000926 uint64_t PltEntryAddr) const {
927 const uint8_t PltData[] = {
928 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
929 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
930 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
931 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
932 0x20, 0x02, 0x1f, 0xd6, // br x17
933 0x1f, 0x20, 0x03, 0xd5, // nop
934 0x1f, 0x20, 0x03, 0xd5, // nop
935 0x1f, 0x20, 0x03, 0xd5 // nop
936 };
937 memcpy(Buf, PltData, sizeof(PltData));
938
939 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
940 GotEntryAddr + 16);
941 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
942 GotEntryAddr + 16);
943 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
944 GotEntryAddr + 16);
945}
946
George Rimar77b77792015-11-25 22:15:01 +0000947void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
948 uint64_t GotEntryAddr,
949 uint64_t PltEntryAddr, int32_t Index,
950 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000951 const uint8_t Inst[] = {
952 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
953 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
954 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
955 0x20, 0x02, 0x1f, 0xd6 // br x17
956 };
957 memcpy(Buf, Inst, sizeof(Inst));
958
959 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
960 GotEntryAddr);
961 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
962 GotEntryAddr);
963 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
964 GotEntryAddr);
965}
966
Igor Kudrin9606d192015-12-03 08:05:35 +0000967bool AArch64TargetInfo::relocNeedsCopy(uint32_t Type,
968 const SymbolBody &S) const {
969 if (Config->Shared)
970 return false;
971 switch (Type) {
972 default:
973 return false;
974 case R_AARCH64_ABS16:
975 case R_AARCH64_ABS32:
976 case R_AARCH64_ABS64:
977 case R_AARCH64_ADD_ABS_LO12_NC:
978 case R_AARCH64_ADR_PREL_LO21:
979 case R_AARCH64_ADR_PREL_PG_HI21:
980 case R_AARCH64_LDST8_ABS_LO12_NC:
981 case R_AARCH64_LDST32_ABS_LO12_NC:
982 case R_AARCH64_LDST64_ABS_LO12_NC:
983 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
984 return SS->Sym.getType() == STT_OBJECT;
985 return false;
986 }
987}
988
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000989bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
990 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000991 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
992 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000993}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000994
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000995bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
996 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000997 switch (Type) {
998 default:
999 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001000 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001001 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001002 return canBePreempted(&S, true);
1003 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001004}
Davide Italiano1d750a62015-09-27 08:45:38 +00001005
Davide Italianoef4be6b2015-10-06 19:01:32 +00001006static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001007 uint32_t ImmLo = (Imm & 0x3) << 29;
1008 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1009 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001010 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001011}
1012
Davide Italiano318ca222015-10-02 22:13:51 +00001013// Page(Expr) is the page address of the expression Expr, defined
1014// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001015// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001016static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001017 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001018}
1019
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001020void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001021 uint32_t Type, uint64_t P, uint64_t SA,
1022 uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001023 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001024 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001025 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001026 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001027 break;
1028 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001029 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001030 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001031 break;
1032 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001033 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001034 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001035 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001036 // This relocation stores 12 bits and there's no instruction
1037 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001038 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1039 // bits in Loc are zero.
1040 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001041 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001042 case R_AARCH64_ADR_GOT_PAGE: {
1043 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1044 checkInt<33>(X, Type);
1045 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1046 break;
1047 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001048 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001049 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001050 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001051 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001052 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001053 }
1054 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001055 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001056 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001057 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001058 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001059 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001060 case R_AARCH64_CALL26:
1061 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001062 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001063 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001064 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1065 break;
1066 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001067 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001068 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001069 or32le(Loc, (SA & 0xFF8) << 7);
1070 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001071 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001072 or32le(Loc, (SA & 0xFFF) << 10);
1073 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001074 case R_AARCH64_LDST32_ABS_LO12_NC:
1075 or32le(Loc, (SA & 0xFFC) << 8);
1076 break;
1077 case R_AARCH64_LDST64_ABS_LO12_NC:
1078 or32le(Loc, (SA & 0xFF8) << 7);
1079 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001080 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001081 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001082 write16le(Loc, SA - P);
1083 break;
1084 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001085 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001086 write32le(Loc, SA - P);
1087 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001088 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001089 write64le(Loc, SA - P);
1090 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001091 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001092 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001093 }
1094}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001095
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001096template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001097 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001098 GotHeaderEntriesNum = 2;
1099}
1100
1101template <class ELFT>
1102void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001103 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001104 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1105 // Module pointer
1106 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001107}
1108
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001109template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001110void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1111template <class ELFT>
1112void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1113 uint64_t PltEntryAddr) const {}
1114template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001115void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1116 uint64_t GotEntryAddr,
1117 uint64_t PltEntryAddr, int32_t Index,
1118 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001119
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001120template <class ELFT>
1121bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1122 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001123 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001124}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001125
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001126template <class ELFT>
1127bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1128 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001129 return false;
1130}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001131
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001132template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001133void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001134 uint32_t Type, uint64_t P, uint64_t SA,
1135 uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001136 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001137 switch (Type) {
1138 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001139 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001140 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001141 case R_MIPS_CALL16:
1142 case R_MIPS_GOT16: {
1143 int64_t V = SA - getMipsGpAddr<ELFT>();
1144 if (Type == R_MIPS_GOT16)
1145 checkInt<16>(V, Type);
1146 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1147 break;
1148 }
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001149 case R_MIPS_HI16: {
1150 uint32_t Instr = read32<E>(Loc);
1151 if (PairedLoc) {
1152 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001153 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001154 write32<E>(Loc, (Instr & 0xffff0000) | (((SA + AHL) >> 16) & 0xffff));
1155 } else {
1156 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
1157 write32<E>(Loc, (Instr & 0xffff0000) | ((SA >> 16) & 0xffff));
1158 }
1159 break;
1160 }
1161 case R_MIPS_LO16: {
1162 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001163 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001164 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1165 break;
1166 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001167 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001168 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001169 }
1170}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001171
Rui Ueyama24e39522015-12-03 20:57:45 +00001172template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001173 const unsigned GPOffset = 0x7ff0;
1174 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1175}
1176
1177template uint32_t getMipsGpAddr<ELF32LE>();
1178template uint32_t getMipsGpAddr<ELF32BE>();
1179template uint64_t getMipsGpAddr<ELF64LE>();
1180template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001181}
1182}