blob: 1a699e5a1e9f175e68621c36f2b2777b6c4a244a [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 Kudrindb7de9f2015-11-17 18:01:30 +0000146 unsigned getPltRefReloc(unsigned Type) const override;
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;
Igor Kudrin9606d192015-12-03 08:05:35 +0000153 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000154 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
155 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000156 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000157 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000158};
159
160template <class ELFT> class MipsTargetInfo final : public TargetInfo {
161public:
162 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000163 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000164 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
165 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
166 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000167 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
168 uint64_t PltEntryAddr, int32_t Index,
169 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
171 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000172 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000173 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000174};
175} // anonymous namespace
176
Rui Ueyama91004392015-10-13 16:08:15 +0000177TargetInfo *createTarget() {
178 switch (Config->EMachine) {
179 case EM_386:
180 return new X86TargetInfo();
181 case EM_AARCH64:
182 return new AArch64TargetInfo();
183 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000184 switch (Config->EKind) {
185 case ELF32LEKind:
186 return new MipsTargetInfo<ELF32LE>();
187 case ELF32BEKind:
188 return new MipsTargetInfo<ELF32BE>();
189 default:
190 error("Unsupported MIPS target");
191 }
Rui Ueyama91004392015-10-13 16:08:15 +0000192 case EM_PPC64:
193 return new PPC64TargetInfo();
194 case EM_X86_64:
195 return new X86_64TargetInfo();
196 }
197 error("Unknown target machine");
198}
199
Rafael Espindola01205f72015-09-22 18:19:46 +0000200TargetInfo::~TargetInfo() {}
201
George Rimar6713cf82015-11-25 21:46:05 +0000202bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000203 return false;
204}
205
Igor Kudrinf6f45472015-11-10 08:39:27 +0000206uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
207
George Rimarbc590fe2015-10-28 16:48:58 +0000208bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
209 return false;
210}
211
Igor Kudrine7ad0932015-11-17 17:47:53 +0000212unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000213
Rafael Espindolaae244002015-10-05 19:30:12 +0000214bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
215
George Rimar6713cf82015-11-25 21:46:05 +0000216unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000217 uint32_t Type, uint64_t P, uint64_t SA,
218 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000219 return 0;
220}
George Rimar77d1cb12015-11-24 09:00:06 +0000221
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000222void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
223
Igor Kudrin351b41d2015-11-16 17:44:08 +0000224void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
225
Rafael Espindola7f074422015-09-22 21:35:51 +0000226X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000227 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000228 PCRelReloc = R_386_PC32;
229 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000230 PltReloc = R_386_JUMP_SLOT;
George Rimar9db204a2015-12-02 09:58:20 +0000231 TlsGotReloc = R_386_TLS_TPOFF;
232 TlsGlobalDynamicReloc = R_386_TLS_GD;
233 TlsLocalDynamicReloc = R_386_TLS_LDM;
234 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
235 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000236 LazyRelocations = true;
237 PltEntrySize = 16;
238 PltZeroEntrySize = 16;
239}
240
241void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
242 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
243}
244
245void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
246 // Skip 6 bytes of "pushl (GOT+4)"
247 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000248}
Rafael Espindola01205f72015-09-22 18:19:46 +0000249
George Rimard23970f2015-11-25 20:41:53 +0000250unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
251 if (Type == R_386_TLS_LE)
252 return R_386_TLS_TPOFF;
253 if (Type == R_386_TLS_LE_32)
254 return R_386_TLS_TPOFF32;
255 return Type;
256}
257
258bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar9db204a2015-12-02 09:58:20 +0000259 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
260 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000261 return Config->Shared;
262 return false;
263}
264
George Rimar648a2c32015-10-20 08:54:27 +0000265void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000266 uint64_t PltEntryAddr) const {
267 // Executable files and shared object files have
268 // separate procedure linkage tables.
269 if (Config->Shared) {
270 const uint8_t V[] = {
271 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
272 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
273 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
274 };
275 memcpy(Buf, V, sizeof(V));
276 return;
277 }
George Rimar648a2c32015-10-20 08:54:27 +0000278
George Rimar77b77792015-11-25 22:15:01 +0000279 const uint8_t PltData[] = {
280 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
281 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
282 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
283 };
284 memcpy(Buf, PltData, sizeof(PltData));
285 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
286 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
287}
288
289void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
290 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
291 int32_t Index, unsigned RelOff) const {
292 const uint8_t Inst[] = {
293 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
294 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
295 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
296 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000297 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000298 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
299 Buf[1] = Config->Shared ? 0xa3 : 0x25;
300 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
301 write32le(Buf + 7, RelOff);
302 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000303}
304
George Rimar70e25082015-11-25 11:27:40 +0000305bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
306 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
307 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
308 return SS->Sym.getType() == STT_OBJECT;
309 return false;
310}
311
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000312bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000313 return Type == R_386_TLS_GOTIE || Type == R_386_GOT32 ||
314 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000315}
316
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000317bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000318 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000319}
320
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000321void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000322 uint64_t P, uint64_t SA,
323 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000324 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000325 case R_386_32:
326 add32le(Loc, SA);
327 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000328 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000329 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000330 break;
George Rimar13934772015-11-25 20:20:31 +0000331 case R_386_GOTPC:
332 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
333 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000334 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000335 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000336 break;
George Rimar9db204a2015-12-02 09:58:20 +0000337 case R_386_TLS_GD:
338 case R_386_TLS_LDM:
339 case R_386_TLS_TPOFF: {
340 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
341 Out<ELF32LE>::Got->getNumEntries() * 4;
342 checkInt<32>(V, Type);
343 write32le(Loc, V);
344 break;
345 }
346 case R_386_TLS_LDO_32:
347 write32le(Loc, SA);
348 break;
George Rimard23970f2015-11-25 20:41:53 +0000349 case R_386_TLS_LE:
350 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
351 break;
352 case R_386_TLS_LE_32:
353 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
354 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000355 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000356 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000357 }
358}
359
Rafael Espindola7f074422015-09-22 21:35:51 +0000360X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000361 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000362 PCRelReloc = R_X86_64_PC32;
363 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000364 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000365 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000366 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000367 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000368 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000369 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000370 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000371 LazyRelocations = true;
372 PltEntrySize = 16;
373 PltZeroEntrySize = 16;
374}
375
Igor Kudrin351b41d2015-11-16 17:44:08 +0000376void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
377 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
378}
379
George Rimar648a2c32015-10-20 08:54:27 +0000380void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
381 // Skip 6 bytes of "jmpq *got(%rip)"
382 write32le(Buf, Plt + 6);
383}
384
385void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
386 uint64_t PltEntryAddr) const {
387 const uint8_t PltData[] = {
388 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
389 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
390 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
391 };
392 memcpy(Buf, PltData, sizeof(PltData));
393 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
394 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000395}
Rafael Espindola01205f72015-09-22 18:19:46 +0000396
George Rimar77b77792015-11-25 22:15:01 +0000397void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
398 uint64_t GotEntryAddr,
399 uint64_t PltEntryAddr, int32_t Index,
400 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000401 const uint8_t Inst[] = {
402 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
403 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
404 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
405 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000406 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000407
George Rimar648a2c32015-10-20 08:54:27 +0000408 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
409 write32le(Buf + 7, Index);
410 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000411}
412
George Rimarbc590fe2015-10-28 16:48:58 +0000413bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
414 const SymbolBody &S) const {
415 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
416 Type == R_X86_64_64)
417 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
418 return SS->Sym.getType() == STT_OBJECT;
419 return false;
420}
421
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000422bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000423 if (Type == R_X86_64_TLSGD)
424 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000425 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000426 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000427 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000428}
429
George Rimard23970f2015-11-25 20:41:53 +0000430bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar25411f252015-12-04 11:20:13 +0000431 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000432}
433
Igor Kudrine7ad0932015-11-17 17:47:53 +0000434unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000435 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000436 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000437 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000438}
439
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000440bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000441 if (relocNeedsCopy(Type, S))
442 return false;
443
Rafael Espindola01205f72015-09-22 18:19:46 +0000444 switch (Type) {
445 default:
446 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000447 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000448 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000449 case R_X86_64_PC32:
450 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000451 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000452 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000453 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000454 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000455 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
456 // libc.so.
457 //
458 // The general idea on how to handle such cases is to create a PLT entry
459 // and use that as the function value.
460 //
461 // For the static linking part, we just return true and everything else
462 // will use the the PLT entry as the address.
463 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000464 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000465 // still works. We need the help of the dynamic linker for that. We
466 // let it know that we have a direct reference to a so symbol by creating
467 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000468 // dynamic linker resolves the symbol to the value of the symbol we created.
469 // This is true even for got entries, so pointer equality is maintained.
470 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000471 // real function is a dedicated got entry used by the plt. That is
472 // identified by special relocation types (R_X86_64_JUMP_SLOT,
473 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000474 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000475 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000476 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000477 }
478}
Rafael Espindolac4010882015-09-22 20:54:08 +0000479
Rafael Espindolaae244002015-10-05 19:30:12 +0000480bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
481 switch (Type) {
482 default:
483 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000484 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000485 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000486 case R_X86_64_PC8:
487 case R_X86_64_PC16:
488 case R_X86_64_PC32:
489 case R_X86_64_PC64:
490 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000491 return true;
492 }
493}
494
George Rimar77d1cb12015-11-24 09:00:06 +0000495bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000496 const SymbolBody *S) const {
497 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000498 return false;
George Rimar25411f252015-12-04 11:20:13 +0000499 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
500 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000501 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
502}
503
504// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
505// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
506// how LD can be optimized to LE:
507// leaq bar@tlsld(%rip), %rdi
508// callq __tls_get_addr@PLT
509// leaq bar@dtpoff(%rax), %rcx
510// Is converted to:
511// .word 0x6666
512// .byte 0x66
513// mov %fs:0,%rax
514// leaq bar@tpoff(%rax), %rcx
515void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
516 uint64_t P, uint64_t SA) const {
517 const uint8_t Inst[] = {
518 0x66, 0x66, //.word 0x6666
519 0x66, //.byte 0x66
520 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
521 };
522 memcpy(Loc - 3, Inst, sizeof(Inst));
523}
524
525// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
526// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
527// how GD can be optimized to LE:
528// .byte 0x66
529// leaq x@tlsgd(%rip), %rdi
530// .word 0x6666
531// rex64
532// call __tls_get_addr@plt
533// Is converted to:
534// mov %fs:0x0,%rax
535// lea x@tpoff,%rax
536void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
537 uint64_t P, uint64_t SA) const {
538 const uint8_t Inst[] = {
539 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
540 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
541 };
542 memcpy(Loc - 4, Inst, sizeof(Inst));
543 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000544}
545
George Rimar25411f252015-12-04 11:20:13 +0000546// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
547// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
548// how GD can be optimized to IE:
549// .byte 0x66
550// leaq x@tlsgd(%rip), %rdi
551// .word 0x6666
552// rex64
553// call __tls_get_addr@plt
554// Is converted to:
555// mov %fs:0x0,%rax
556// addq x@tpoff,%rax
557void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
558 uint64_t P, uint64_t SA) const {
559 const uint8_t Inst[] = {
560 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
561 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
562 };
563 memcpy(Loc - 4, Inst, sizeof(Inst));
564 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
565}
566
George Rimar77d1cb12015-11-24 09:00:06 +0000567// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
568// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
569// This function does that. Read "ELF Handling For Thread-Local Storage,
570// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
571// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000572void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
573 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000574 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
575 // used in MOVQ or ADDQ instructions only.
576 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
577 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
578 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
579 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
580 uint8_t *Prefix = Loc - 3;
581 uint8_t *Inst = Loc - 2;
582 uint8_t *RegSlot = Loc - 1;
583 uint8_t Reg = Loc[-1] >> 3;
584 bool IsMov = *Inst == 0x8b;
585 bool RspAdd = !IsMov && Reg == 4;
586 // r12 and rsp registers requires special handling.
587 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
588 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
589 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
590 // The same true for rsp. So we convert to addq for them, saving 1 byte that
591 // we dont have.
592 if (RspAdd)
593 *Inst = 0x81;
594 else
595 *Inst = IsMov ? 0xc7 : 0x8d;
596 if (*Prefix == 0x4c)
597 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
598 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
599 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
600}
601
George Rimar6713cf82015-11-25 21:46:05 +0000602// This function applies a TLS relocation with an optimization as described
603// in the Ulrich's document. As a result of rewriting instructions at the
604// relocation target, relocations immediately follow the TLS relocation (which
605// would be applied to rewritten instructions) may have to be skipped.
606// This function returns a number of relocations that need to be skipped.
607unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
608 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000609 uint64_t SA,
610 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000611 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000612 case R_X86_64_DTPOFF32:
613 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
614 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000615 case R_X86_64_GOTTPOFF:
616 relocateTlsIeToLe(Loc, BufEnd, P, SA);
617 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000618 case R_X86_64_TLSGD: {
619 if (canBePreempted(&S, true))
620 relocateTlsGdToIe(Loc, BufEnd, P, SA);
621 else
622 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000623 // The next relocation should be against __tls_get_addr, so skip it
624 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000625 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000626 case R_X86_64_TLSLD:
627 relocateTlsLdToLe(Loc, BufEnd, P, SA);
628 // The next relocation should be against __tls_get_addr, so skip it
629 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000630 }
631 llvm_unreachable("Unknown TLS optimization");
632}
633
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000634void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000635 uint64_t P, uint64_t SA,
636 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000637 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000638 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000639 checkUInt<32>(SA, Type);
640 write32le(Loc, SA);
641 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000642 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000643 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000644 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000645 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000646 case R_X86_64_64:
647 write64le(Loc, SA);
648 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000649 case R_X86_64_DTPOFF32:
650 write32le(Loc, SA);
651 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000652 case R_X86_64_DTPOFF64:
653 write64le(Loc, SA);
654 break;
655 case R_X86_64_GOTPCREL:
656 case R_X86_64_PC32:
657 case R_X86_64_PLT32:
658 case R_X86_64_TLSGD:
659 case R_X86_64_TLSLD:
660 write32le(Loc, SA - P);
661 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000662 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000663 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000664 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000665 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000666 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000667 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000668 case R_X86_64_TPOFF64:
669 write32le(Loc, SA - P);
670 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000671 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000672 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000673 }
674}
675
Hal Finkel3c8cc672015-10-12 20:56:18 +0000676// Relocation masks following the #lo(value), #hi(value), #ha(value),
677// #higher(value), #highera(value), #highest(value), and #highesta(value)
678// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
679// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000680static uint16_t applyPPCLo(uint64_t V) { return V; }
681static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
682static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
683static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
684static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000685static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000686static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
687
Rafael Espindolac4010882015-09-22 20:54:08 +0000688PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000689 PCRelReloc = R_PPC64_REL24;
690 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000691 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000692 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000693
694 // We need 64K pages (at least under glibc/Linux, the loader won't
695 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000696 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000697
698 // The PPC64 ELF ABI v1 spec, says:
699 //
700 // It is normally desirable to put segments with different characteristics
701 // in separate 256 Mbyte portions of the address space, to give the
702 // operating system full paging flexibility in the 64-bit address space.
703 //
704 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
705 // use 0x10000000 as the starting address.
706 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000707}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000708
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000709uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000710 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
711 // order. The TOC starts where the first of these sections starts.
712
713 // FIXME: This obviously does not do the right thing when there is no .got
714 // section, but there is a .toc or .tocbss section.
715 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
716 if (!TocVA)
717 TocVA = Out<ELF64BE>::Plt->getVA();
718
719 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
720 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
721 // code (crt1.o) assumes that you can get from the TOC base to the
722 // start of the .toc section with only a single (signed) 16-bit relocation.
723 return TocVA + 0x8000;
724}
725
George Rimar648a2c32015-10-20 08:54:27 +0000726void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
727void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
728 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000729void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
730 uint64_t GotEntryAddr,
731 uint64_t PltEntryAddr, int32_t Index,
732 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000733 uint64_t Off = GotEntryAddr - getPPC64TocBase();
734
735 // FIXME: What we should do, in theory, is get the offset of the function
736 // descriptor in the .opd section, and use that as the offset from %r2 (the
737 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
738 // be a pointer to the function descriptor in the .opd section. Using
739 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
740
Hal Finkelfa92f682015-10-13 21:47:34 +0000741 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000742 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
743 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
744 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
745 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
746 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
747 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
748 write32be(Buf + 28, 0x4e800420); // bctr
749}
750
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000751bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000752 if (relocNeedsPlt(Type, S))
753 return true;
754
755 switch (Type) {
756 default: return false;
757 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000758 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000759 case R_PPC64_GOT16_HA:
760 case R_PPC64_GOT16_HI:
761 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000762 case R_PPC64_GOT16_LO_DS:
763 return true;
764 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000765}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000766
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000767bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000768 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000769 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000770}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000771
Hal Finkelbe0823d2015-10-12 20:58:52 +0000772bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
773 switch (Type) {
774 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000775 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000776 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000777 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000778 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000779 }
780}
781
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000782void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000783 uint64_t P, uint64_t SA,
784 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000785 uint64_t TB = getPPC64TocBase();
786
Hal Finkel3c8cc672015-10-12 20:56:18 +0000787 // For a TOC-relative relocation, adjust the addend and proceed in terms of
788 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000789 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000790 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
791 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000792 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
793 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000794 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
795 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000796 default: break;
797 }
798
Hal Finkel3c8cc672015-10-12 20:56:18 +0000799 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000800 case R_PPC64_ADDR14: {
801 checkAlignment<4>(SA, Type);
802 // Preserve the AA/LK bits in the branch instruction
803 uint8_t AALK = Loc[3];
804 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
805 break;
806 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000807 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000808 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000809 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000810 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000811 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000812 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000813 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000814 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000815 case R_PPC64_ADDR16_HA:
816 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000817 break;
818 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000819 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000820 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000821 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000822 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000823 break;
824 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000825 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000826 break;
827 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000828 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000829 break;
830 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000831 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000832 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000833 case R_PPC64_ADDR16_LO:
834 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000835 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000836 case R_PPC64_ADDR16_LO_DS:
837 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000838 break;
839 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000840 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000841 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000842 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000843 case R_PPC64_ADDR64:
844 write64be(Loc, SA);
845 break;
846 case R_PPC64_REL16_HA:
847 write16be(Loc, applyPPCHa(SA - P));
848 break;
849 case R_PPC64_REL16_HI:
850 write16be(Loc, applyPPCHi(SA - P));
851 break;
852 case R_PPC64_REL16_LO:
853 write16be(Loc, applyPPCLo(SA - P));
854 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000855 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000856 // If we have an undefined weak symbol, we might get here with a symbol
857 // address of zero. That could overflow, but the code must be unreachable,
858 // so don't bother doing anything at all.
859 if (!SA)
860 break;
861
Hal Finkeldaedc122015-10-12 23:16:53 +0000862 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
863 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000864 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000865
866 if (!InPlt && Out<ELF64BE>::Opd) {
867 // If this is a local call, and we currently have the address of a
868 // function-descriptor, get the underlying code address instead.
869 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
870 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000871 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000872
873 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000874 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000875 }
876
Hal Finkel3c8cc672015-10-12 20:56:18 +0000877 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000878 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000879 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000880
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000881 uint32_t Nop = 0x60000000;
882 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
883 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000884 break;
885 }
886 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000887 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000888 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000889 break;
890 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000891 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000892 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000893 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000894 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000895 break;
896 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000897 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000898 }
899}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000900
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000901AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +0000902 CopyReloc = R_AARCH64_COPY;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000903 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000904 PltReloc = R_AARCH64_JUMP_SLOT;
905 LazyRelocations = true;
906 PltEntrySize = 16;
907 PltZeroEntrySize = 32;
908}
George Rimar648a2c32015-10-20 08:54:27 +0000909
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000910unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
911
912void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
913 write64le(Buf, Out<ELF64LE>::Plt->getVA());
914}
915
George Rimar648a2c32015-10-20 08:54:27 +0000916void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000917 uint64_t PltEntryAddr) const {
918 const uint8_t PltData[] = {
919 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
920 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
921 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
922 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
923 0x20, 0x02, 0x1f, 0xd6, // br x17
924 0x1f, 0x20, 0x03, 0xd5, // nop
925 0x1f, 0x20, 0x03, 0xd5, // nop
926 0x1f, 0x20, 0x03, 0xd5 // nop
927 };
928 memcpy(Buf, PltData, sizeof(PltData));
929
930 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
931 GotEntryAddr + 16);
932 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
933 GotEntryAddr + 16);
934 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
935 GotEntryAddr + 16);
936}
937
George Rimar77b77792015-11-25 22:15:01 +0000938void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
939 uint64_t GotEntryAddr,
940 uint64_t PltEntryAddr, int32_t Index,
941 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000942 const uint8_t Inst[] = {
943 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
944 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
945 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
946 0x20, 0x02, 0x1f, 0xd6 // br x17
947 };
948 memcpy(Buf, Inst, sizeof(Inst));
949
950 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
951 GotEntryAddr);
952 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
953 GotEntryAddr);
954 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
955 GotEntryAddr);
956}
957
Igor Kudrin9606d192015-12-03 08:05:35 +0000958bool AArch64TargetInfo::relocNeedsCopy(uint32_t Type,
959 const SymbolBody &S) const {
960 if (Config->Shared)
961 return false;
962 switch (Type) {
963 default:
964 return false;
965 case R_AARCH64_ABS16:
966 case R_AARCH64_ABS32:
967 case R_AARCH64_ABS64:
968 case R_AARCH64_ADD_ABS_LO12_NC:
969 case R_AARCH64_ADR_PREL_LO21:
970 case R_AARCH64_ADR_PREL_PG_HI21:
971 case R_AARCH64_LDST8_ABS_LO12_NC:
972 case R_AARCH64_LDST32_ABS_LO12_NC:
973 case R_AARCH64_LDST64_ABS_LO12_NC:
974 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
975 return SS->Sym.getType() == STT_OBJECT;
976 return false;
977 }
978}
979
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000980bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
981 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000982 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
983 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000984}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000985
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000986bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
987 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000988 switch (Type) {
989 default:
990 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000991 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000992 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000993 return canBePreempted(&S, true);
994 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000995}
Davide Italiano1d750a62015-09-27 08:45:38 +0000996
Davide Italianoef4be6b2015-10-06 19:01:32 +0000997static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000998 uint32_t ImmLo = (Imm & 0x3) << 29;
999 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1000 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001001 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001002}
1003
Davide Italiano318ca222015-10-02 22:13:51 +00001004// Page(Expr) is the page address of the expression Expr, defined
1005// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001006// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001007static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001008 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001009}
1010
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001011void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001012 uint32_t Type, uint64_t P, uint64_t SA,
1013 uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001014 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001015 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001016 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001017 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001018 break;
1019 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001020 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001021 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001022 break;
1023 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001024 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001025 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001026 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001027 // This relocation stores 12 bits and there's no instruction
1028 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001029 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1030 // bits in Loc are zero.
1031 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001032 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001033 case R_AARCH64_ADR_GOT_PAGE: {
1034 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1035 checkInt<33>(X, Type);
1036 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1037 break;
1038 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001039 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001040 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001041 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001042 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001043 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001044 }
1045 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001046 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001047 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001048 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001049 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001050 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001051 case R_AARCH64_CALL26:
1052 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001053 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001054 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001055 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1056 break;
1057 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001058 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001059 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001060 or32le(Loc, (SA & 0xFF8) << 7);
1061 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001062 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001063 or32le(Loc, (SA & 0xFFF) << 10);
1064 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001065 case R_AARCH64_LDST32_ABS_LO12_NC:
1066 or32le(Loc, (SA & 0xFFC) << 8);
1067 break;
1068 case R_AARCH64_LDST64_ABS_LO12_NC:
1069 or32le(Loc, (SA & 0xFF8) << 7);
1070 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001071 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001072 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001073 write16le(Loc, SA - P);
1074 break;
1075 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001076 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001077 write32le(Loc, SA - P);
1078 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001079 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001080 write64le(Loc, SA - P);
1081 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001082 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001083 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001084 }
1085}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001086
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001087template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001088 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001089 GotHeaderEntriesNum = 2;
1090}
1091
1092template <class ELFT>
1093void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001094 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001095 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1096 // Module pointer
1097 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001098}
1099
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001100template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001101void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1102template <class ELFT>
1103void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1104 uint64_t PltEntryAddr) const {}
1105template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001106void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1107 uint64_t GotEntryAddr,
1108 uint64_t PltEntryAddr, int32_t Index,
1109 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001110
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001111template <class ELFT>
1112bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1113 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001114 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001115}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001116
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001117template <class ELFT>
1118bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1119 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001120 return false;
1121}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001122
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001123template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001124void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001125 uint32_t Type, uint64_t P, uint64_t SA,
1126 uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001127 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001128 switch (Type) {
1129 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001130 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001131 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001132 case R_MIPS_CALL16:
1133 case R_MIPS_GOT16: {
1134 int64_t V = SA - getMipsGpAddr<ELFT>();
1135 if (Type == R_MIPS_GOT16)
1136 checkInt<16>(V, Type);
1137 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1138 break;
1139 }
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001140 case R_MIPS_HI16: {
1141 uint32_t Instr = read32<E>(Loc);
1142 if (PairedLoc) {
1143 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001144 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001145 write32<E>(Loc, (Instr & 0xffff0000) | (((SA + AHL) >> 16) & 0xffff));
1146 } else {
1147 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
1148 write32<E>(Loc, (Instr & 0xffff0000) | ((SA >> 16) & 0xffff));
1149 }
1150 break;
1151 }
1152 case R_MIPS_LO16: {
1153 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001154 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001155 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1156 break;
1157 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001158 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001159 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001160 }
1161}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001162
Rui Ueyama24e39522015-12-03 20:57:45 +00001163template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001164 const unsigned GPOffset = 0x7ff0;
1165 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1166}
1167
1168template uint32_t getMipsGpAddr<ELF32LE>();
1169template uint32_t getMipsGpAddr<ELF32BE>();
1170template uint64_t getMipsGpAddr<ELF64LE>();
1171template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001172}
1173}