blob: 088ba5c35bc65d4683f184c9c9fad2822143f7ba [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,
113 uint64_t P, uint64_t SA) const override;
114
115private:
116 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
117 uint64_t SA) const;
118 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
119 uint64_t SA) const;
120 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
121 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000122};
123
124class PPC64TargetInfo final : public TargetInfo {
125public:
126 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +0000127 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
128 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
129 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000130 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
131 uint64_t PltEntryAddr, int32_t Index,
132 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000133 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
134 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000135 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000136 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000137 bool isRelRelative(uint32_t Type) const override;
138};
139
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000140class AArch64TargetInfo final : public TargetInfo {
141public:
142 AArch64TargetInfo();
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000143 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000144 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
145 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
146 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000147 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
148 uint64_t PltEntryAddr, int32_t Index,
149 unsigned RelOff) const override;
Igor Kudrin9606d192015-12-03 08:05:35 +0000150 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000151 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
152 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000153 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000154 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155};
156
157template <class ELFT> class MipsTargetInfo final : public TargetInfo {
158public:
159 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000160 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000161 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
162 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
163 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000164 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
165 uint64_t PltEntryAddr, int32_t Index,
166 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000167 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
168 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000169 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000170 uint64_t SA, uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000171};
172} // anonymous namespace
173
Rui Ueyama91004392015-10-13 16:08:15 +0000174TargetInfo *createTarget() {
175 switch (Config->EMachine) {
176 case EM_386:
177 return new X86TargetInfo();
178 case EM_AARCH64:
179 return new AArch64TargetInfo();
180 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000181 switch (Config->EKind) {
182 case ELF32LEKind:
183 return new MipsTargetInfo<ELF32LE>();
184 case ELF32BEKind:
185 return new MipsTargetInfo<ELF32BE>();
186 default:
187 error("Unsupported MIPS target");
188 }
Rui Ueyama91004392015-10-13 16:08:15 +0000189 case EM_PPC64:
190 return new PPC64TargetInfo();
191 case EM_X86_64:
192 return new X86_64TargetInfo();
193 }
194 error("Unknown target machine");
195}
196
Rafael Espindola01205f72015-09-22 18:19:46 +0000197TargetInfo::~TargetInfo() {}
198
George Rimar6713cf82015-11-25 21:46:05 +0000199bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000200 return false;
201}
202
Igor Kudrinf6f45472015-11-10 08:39:27 +0000203uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
204
George Rimarbc590fe2015-10-28 16:48:58 +0000205bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
206 return false;
207}
208
Igor Kudrine7ad0932015-11-17 17:47:53 +0000209unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000210
Rafael Espindolaae244002015-10-05 19:30:12 +0000211bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
212
George Rimar6713cf82015-11-25 21:46:05 +0000213unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
214 uint32_t Type, uint64_t P,
215 uint64_t SA) const {
216 return 0;
217}
George Rimar77d1cb12015-11-24 09:00:06 +0000218
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000219void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
220
Igor Kudrin351b41d2015-11-16 17:44:08 +0000221void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
222
Rafael Espindola7f074422015-09-22 21:35:51 +0000223X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000224 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000225 PCRelReloc = R_386_PC32;
226 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000227 PltReloc = R_386_JUMP_SLOT;
George Rimar9db204a2015-12-02 09:58:20 +0000228 TlsGotReloc = R_386_TLS_TPOFF;
229 TlsGlobalDynamicReloc = R_386_TLS_GD;
230 TlsLocalDynamicReloc = R_386_TLS_LDM;
231 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
232 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000233 LazyRelocations = true;
234 PltEntrySize = 16;
235 PltZeroEntrySize = 16;
236}
237
238void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
239 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
240}
241
242void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
243 // Skip 6 bytes of "pushl (GOT+4)"
244 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000245}
Rafael Espindola01205f72015-09-22 18:19:46 +0000246
George Rimard23970f2015-11-25 20:41:53 +0000247unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
248 if (Type == R_386_TLS_LE)
249 return R_386_TLS_TPOFF;
250 if (Type == R_386_TLS_LE_32)
251 return R_386_TLS_TPOFF32;
252 return Type;
253}
254
255bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
George Rimar9db204a2015-12-02 09:58:20 +0000256 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
257 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000258 return Config->Shared;
259 return false;
260}
261
George Rimar648a2c32015-10-20 08:54:27 +0000262void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000263 uint64_t PltEntryAddr) const {
264 // Executable files and shared object files have
265 // separate procedure linkage tables.
266 if (Config->Shared) {
267 const uint8_t V[] = {
268 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
269 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
270 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
271 };
272 memcpy(Buf, V, sizeof(V));
273 return;
274 }
George Rimar648a2c32015-10-20 08:54:27 +0000275
George Rimar77b77792015-11-25 22:15:01 +0000276 const uint8_t PltData[] = {
277 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
278 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
279 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
280 };
281 memcpy(Buf, PltData, sizeof(PltData));
282 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
283 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
284}
285
286void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
287 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
288 int32_t Index, unsigned RelOff) const {
289 const uint8_t Inst[] = {
290 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
291 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
292 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
293 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000294 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000295 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
296 Buf[1] = Config->Shared ? 0xa3 : 0x25;
297 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
298 write32le(Buf + 7, RelOff);
299 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000300}
301
George Rimar70e25082015-11-25 11:27:40 +0000302bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
303 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
304 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
305 return SS->Sym.getType() == STT_OBJECT;
306 return false;
307}
308
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000309bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000310 return Type == R_386_TLS_GOTIE || Type == R_386_GOT32 ||
311 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000312}
313
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000314bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000315 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000316}
317
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000318void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000319 uint64_t P, uint64_t SA,
320 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000321 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000322 case R_386_32:
323 add32le(Loc, SA);
324 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000325 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000326 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000327 break;
George Rimar13934772015-11-25 20:20:31 +0000328 case R_386_GOTPC:
329 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
330 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000331 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000332 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000333 break;
George Rimar9db204a2015-12-02 09:58:20 +0000334 case R_386_TLS_GD:
335 case R_386_TLS_LDM:
336 case R_386_TLS_TPOFF: {
337 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
338 Out<ELF32LE>::Got->getNumEntries() * 4;
339 checkInt<32>(V, Type);
340 write32le(Loc, V);
341 break;
342 }
343 case R_386_TLS_LDO_32:
344 write32le(Loc, SA);
345 break;
George Rimard23970f2015-11-25 20:41:53 +0000346 case R_386_TLS_LE:
347 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
348 break;
349 case R_386_TLS_LE_32:
350 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
351 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000352 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000353 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000354 }
355}
356
Rafael Espindola7f074422015-09-22 21:35:51 +0000357X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000358 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000359 PCRelReloc = R_X86_64_PC32;
360 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000361 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000362 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000363 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000364 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000365 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000366 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000367 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000368 LazyRelocations = true;
369 PltEntrySize = 16;
370 PltZeroEntrySize = 16;
371}
372
Igor Kudrin351b41d2015-11-16 17:44:08 +0000373void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
374 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
375}
376
George Rimar648a2c32015-10-20 08:54:27 +0000377void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
378 // Skip 6 bytes of "jmpq *got(%rip)"
379 write32le(Buf, Plt + 6);
380}
381
382void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
383 uint64_t PltEntryAddr) const {
384 const uint8_t PltData[] = {
385 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
386 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
387 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
388 };
389 memcpy(Buf, PltData, sizeof(PltData));
390 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
391 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000392}
Rafael Espindola01205f72015-09-22 18:19:46 +0000393
George Rimar77b77792015-11-25 22:15:01 +0000394void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
395 uint64_t GotEntryAddr,
396 uint64_t PltEntryAddr, int32_t Index,
397 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000398 const uint8_t Inst[] = {
399 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
400 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
401 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
402 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000403 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000404
George Rimar648a2c32015-10-20 08:54:27 +0000405 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
406 write32le(Buf + 7, Index);
407 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000408}
409
George Rimarbc590fe2015-10-28 16:48:58 +0000410bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
411 const SymbolBody &S) const {
412 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
413 Type == R_X86_64_64)
414 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
415 return SS->Sym.getType() == STT_OBJECT;
416 return false;
417}
418
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000419bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000420 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000421 return !isTlsOptimized(Type, &S);
George Rimar687138c2015-11-13 16:28:53 +0000422 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
423 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000424}
425
George Rimard23970f2015-11-25 20:41:53 +0000426bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
427 return Type == R_X86_64_GOTTPOFF;
428}
429
Igor Kudrine7ad0932015-11-17 17:47:53 +0000430unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000431 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000432 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000433 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000434}
435
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000436bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000437 if (relocNeedsCopy(Type, S))
438 return false;
439
Rafael Espindola01205f72015-09-22 18:19:46 +0000440 switch (Type) {
441 default:
442 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000443 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000444 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000445 case R_X86_64_PC32:
446 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000447 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000448 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000449 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000450 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000451 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
452 // libc.so.
453 //
454 // The general idea on how to handle such cases is to create a PLT entry
455 // and use that as the function value.
456 //
457 // For the static linking part, we just return true and everything else
458 // will use the the PLT entry as the address.
459 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000460 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000461 // still works. We need the help of the dynamic linker for that. We
462 // let it know that we have a direct reference to a so symbol by creating
463 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000464 // dynamic linker resolves the symbol to the value of the symbol we created.
465 // This is true even for got entries, so pointer equality is maintained.
466 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000467 // real function is a dedicated got entry used by the plt. That is
468 // identified by special relocation types (R_X86_64_JUMP_SLOT,
469 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000470 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000471 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000472 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000473 }
474}
Rafael Espindolac4010882015-09-22 20:54:08 +0000475
Rafael Espindolaae244002015-10-05 19:30:12 +0000476bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
477 switch (Type) {
478 default:
479 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000480 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000481 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000482 case R_X86_64_PC8:
483 case R_X86_64_PC16:
484 case R_X86_64_PC32:
485 case R_X86_64_PC64:
486 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000487 return true;
488 }
489}
490
George Rimar77d1cb12015-11-24 09:00:06 +0000491bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000492 const SymbolBody *S) const {
493 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000494 return false;
George Rimar6713cf82015-11-25 21:46:05 +0000495 return Type == R_X86_64_TLSLD || Type == R_X86_64_DTPOFF32 ||
496 (Type == R_X86_64_TLSGD && !canBePreempted(S, true)) ||
497 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
498}
499
500// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
501// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
502// how LD can be optimized to LE:
503// leaq bar@tlsld(%rip), %rdi
504// callq __tls_get_addr@PLT
505// leaq bar@dtpoff(%rax), %rcx
506// Is converted to:
507// .word 0x6666
508// .byte 0x66
509// mov %fs:0,%rax
510// leaq bar@tpoff(%rax), %rcx
511void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
512 uint64_t P, uint64_t SA) const {
513 const uint8_t Inst[] = {
514 0x66, 0x66, //.word 0x6666
515 0x66, //.byte 0x66
516 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
517 };
518 memcpy(Loc - 3, Inst, sizeof(Inst));
519}
520
521// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
522// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
523// how GD can be optimized to LE:
524// .byte 0x66
525// leaq x@tlsgd(%rip), %rdi
526// .word 0x6666
527// rex64
528// call __tls_get_addr@plt
529// Is converted to:
530// mov %fs:0x0,%rax
531// lea x@tpoff,%rax
532void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
533 uint64_t P, uint64_t SA) const {
534 const uint8_t Inst[] = {
535 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
536 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
537 };
538 memcpy(Loc - 4, Inst, sizeof(Inst));
539 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000540}
541
542// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
543// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
544// This function does that. Read "ELF Handling For Thread-Local Storage,
545// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
546// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000547void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
548 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000549 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
550 // used in MOVQ or ADDQ instructions only.
551 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
552 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
553 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
554 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
555 uint8_t *Prefix = Loc - 3;
556 uint8_t *Inst = Loc - 2;
557 uint8_t *RegSlot = Loc - 1;
558 uint8_t Reg = Loc[-1] >> 3;
559 bool IsMov = *Inst == 0x8b;
560 bool RspAdd = !IsMov && Reg == 4;
561 // r12 and rsp registers requires special handling.
562 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
563 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
564 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
565 // The same true for rsp. So we convert to addq for them, saving 1 byte that
566 // we dont have.
567 if (RspAdd)
568 *Inst = 0x81;
569 else
570 *Inst = IsMov ? 0xc7 : 0x8d;
571 if (*Prefix == 0x4c)
572 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
573 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
574 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
575}
576
George Rimar6713cf82015-11-25 21:46:05 +0000577// This function applies a TLS relocation with an optimization as described
578// in the Ulrich's document. As a result of rewriting instructions at the
579// relocation target, relocations immediately follow the TLS relocation (which
580// would be applied to rewritten instructions) may have to be skipped.
581// This function returns a number of relocations that need to be skipped.
582unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
583 uint32_t Type, uint64_t P,
584 uint64_t SA) const {
585 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000586 case R_X86_64_DTPOFF32:
587 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
588 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000589 case R_X86_64_GOTTPOFF:
590 relocateTlsIeToLe(Loc, BufEnd, P, SA);
591 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000592 case R_X86_64_TLSGD:
593 relocateTlsGdToLe(Loc, BufEnd, P, SA);
594 // The next relocation should be against __tls_get_addr, so skip it
595 return 1;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000596 case R_X86_64_TLSLD:
597 relocateTlsLdToLe(Loc, BufEnd, P, SA);
598 // The next relocation should be against __tls_get_addr, so skip it
599 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000600 }
601 llvm_unreachable("Unknown TLS optimization");
602}
603
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000604void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000605 uint64_t P, uint64_t SA,
606 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000607 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000608 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000609 checkUInt<32>(SA, Type);
610 write32le(Loc, SA);
611 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000612 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000613 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000614 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000615 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000616 case R_X86_64_64:
617 write64le(Loc, SA);
618 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000619 case R_X86_64_DTPOFF32:
620 write32le(Loc, SA);
621 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000622 case R_X86_64_DTPOFF64:
623 write64le(Loc, SA);
624 break;
625 case R_X86_64_GOTPCREL:
626 case R_X86_64_PC32:
627 case R_X86_64_PLT32:
628 case R_X86_64_TLSGD:
629 case R_X86_64_TLSLD:
630 write32le(Loc, SA - P);
631 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000632 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000633 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000634 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000635 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000636 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000637 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000638 case R_X86_64_TPOFF64:
639 write32le(Loc, SA - P);
640 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000641 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000642 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000643 }
644}
645
Hal Finkel3c8cc672015-10-12 20:56:18 +0000646// Relocation masks following the #lo(value), #hi(value), #ha(value),
647// #higher(value), #highera(value), #highest(value), and #highesta(value)
648// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
649// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000650static uint16_t applyPPCLo(uint64_t V) { return V; }
651static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
652static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
653static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
654static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000655static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000656static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
657
Rafael Espindolac4010882015-09-22 20:54:08 +0000658PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000659 PCRelReloc = R_PPC64_REL24;
660 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000661 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000662 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000663
664 // We need 64K pages (at least under glibc/Linux, the loader won't
665 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000666 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000667
668 // The PPC64 ELF ABI v1 spec, says:
669 //
670 // It is normally desirable to put segments with different characteristics
671 // in separate 256 Mbyte portions of the address space, to give the
672 // operating system full paging flexibility in the 64-bit address space.
673 //
674 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
675 // use 0x10000000 as the starting address.
676 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000677}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000678
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000679uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000680 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
681 // order. The TOC starts where the first of these sections starts.
682
683 // FIXME: This obviously does not do the right thing when there is no .got
684 // section, but there is a .toc or .tocbss section.
685 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
686 if (!TocVA)
687 TocVA = Out<ELF64BE>::Plt->getVA();
688
689 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
690 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
691 // code (crt1.o) assumes that you can get from the TOC base to the
692 // start of the .toc section with only a single (signed) 16-bit relocation.
693 return TocVA + 0x8000;
694}
695
George Rimar648a2c32015-10-20 08:54:27 +0000696void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
697void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
698 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000699void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
700 uint64_t GotEntryAddr,
701 uint64_t PltEntryAddr, int32_t Index,
702 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000703 uint64_t Off = GotEntryAddr - getPPC64TocBase();
704
705 // FIXME: What we should do, in theory, is get the offset of the function
706 // descriptor in the .opd section, and use that as the offset from %r2 (the
707 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
708 // be a pointer to the function descriptor in the .opd section. Using
709 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
710
Hal Finkelfa92f682015-10-13 21:47:34 +0000711 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000712 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
713 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
714 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
715 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
716 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
717 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
718 write32be(Buf + 28, 0x4e800420); // bctr
719}
720
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000721bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000722 if (relocNeedsPlt(Type, S))
723 return true;
724
725 switch (Type) {
726 default: return false;
727 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000728 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000729 case R_PPC64_GOT16_HA:
730 case R_PPC64_GOT16_HI:
731 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000732 case R_PPC64_GOT16_LO_DS:
733 return true;
734 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000735}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000736
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000737bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000738 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000739 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000740}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000741
Hal Finkelbe0823d2015-10-12 20:58:52 +0000742bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
743 switch (Type) {
744 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000745 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000746 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000747 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000748 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000749 }
750}
751
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000752void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000753 uint64_t P, uint64_t SA,
754 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000755 uint64_t TB = getPPC64TocBase();
756
Hal Finkel3c8cc672015-10-12 20:56:18 +0000757 // For a TOC-relative relocation, adjust the addend and proceed in terms of
758 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000759 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000760 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
761 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000762 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
763 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000764 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
765 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000766 default: break;
767 }
768
Hal Finkel3c8cc672015-10-12 20:56:18 +0000769 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000770 case R_PPC64_ADDR14: {
771 checkAlignment<4>(SA, Type);
772 // Preserve the AA/LK bits in the branch instruction
773 uint8_t AALK = Loc[3];
774 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
775 break;
776 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000777 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000778 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000779 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000780 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000781 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000782 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000783 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000784 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000785 case R_PPC64_ADDR16_HA:
786 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000787 break;
788 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000789 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000790 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000791 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000792 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000793 break;
794 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000795 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000796 break;
797 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000798 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000799 break;
800 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000801 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000802 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000803 case R_PPC64_ADDR16_LO:
804 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000805 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000806 case R_PPC64_ADDR16_LO_DS:
807 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000808 break;
809 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000810 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000811 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000812 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000813 case R_PPC64_ADDR64:
814 write64be(Loc, SA);
815 break;
816 case R_PPC64_REL16_HA:
817 write16be(Loc, applyPPCHa(SA - P));
818 break;
819 case R_PPC64_REL16_HI:
820 write16be(Loc, applyPPCHi(SA - P));
821 break;
822 case R_PPC64_REL16_LO:
823 write16be(Loc, applyPPCLo(SA - P));
824 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000825 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000826 // If we have an undefined weak symbol, we might get here with a symbol
827 // address of zero. That could overflow, but the code must be unreachable,
828 // so don't bother doing anything at all.
829 if (!SA)
830 break;
831
Hal Finkeldaedc122015-10-12 23:16:53 +0000832 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
833 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000834 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000835
836 if (!InPlt && Out<ELF64BE>::Opd) {
837 // If this is a local call, and we currently have the address of a
838 // function-descriptor, get the underlying code address instead.
839 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
840 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000841 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000842
843 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000844 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000845 }
846
Hal Finkel3c8cc672015-10-12 20:56:18 +0000847 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000848 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000849 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000850
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000851 uint32_t Nop = 0x60000000;
852 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
853 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000854 break;
855 }
856 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000857 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000858 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000859 break;
860 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000861 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000862 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000863 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000864 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000865 break;
866 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000867 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000868 }
869}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000870
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000871AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +0000872 CopyReloc = R_AARCH64_COPY;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000873 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000874 PltReloc = R_AARCH64_JUMP_SLOT;
875 LazyRelocations = true;
876 PltEntrySize = 16;
877 PltZeroEntrySize = 32;
878}
George Rimar648a2c32015-10-20 08:54:27 +0000879
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000880unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
881
882void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
883 write64le(Buf, Out<ELF64LE>::Plt->getVA());
884}
885
George Rimar648a2c32015-10-20 08:54:27 +0000886void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000887 uint64_t PltEntryAddr) const {
888 const uint8_t PltData[] = {
889 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
890 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
891 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
892 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
893 0x20, 0x02, 0x1f, 0xd6, // br x17
894 0x1f, 0x20, 0x03, 0xd5, // nop
895 0x1f, 0x20, 0x03, 0xd5, // nop
896 0x1f, 0x20, 0x03, 0xd5 // nop
897 };
898 memcpy(Buf, PltData, sizeof(PltData));
899
900 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
901 GotEntryAddr + 16);
902 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
903 GotEntryAddr + 16);
904 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
905 GotEntryAddr + 16);
906}
907
George Rimar77b77792015-11-25 22:15:01 +0000908void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
909 uint64_t GotEntryAddr,
910 uint64_t PltEntryAddr, int32_t Index,
911 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000912 const uint8_t Inst[] = {
913 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
914 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
915 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
916 0x20, 0x02, 0x1f, 0xd6 // br x17
917 };
918 memcpy(Buf, Inst, sizeof(Inst));
919
920 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
921 GotEntryAddr);
922 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
923 GotEntryAddr);
924 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
925 GotEntryAddr);
926}
927
Igor Kudrin9606d192015-12-03 08:05:35 +0000928bool AArch64TargetInfo::relocNeedsCopy(uint32_t Type,
929 const SymbolBody &S) const {
930 if (Config->Shared)
931 return false;
932 switch (Type) {
933 default:
934 return false;
935 case R_AARCH64_ABS16:
936 case R_AARCH64_ABS32:
937 case R_AARCH64_ABS64:
938 case R_AARCH64_ADD_ABS_LO12_NC:
939 case R_AARCH64_ADR_PREL_LO21:
940 case R_AARCH64_ADR_PREL_PG_HI21:
941 case R_AARCH64_LDST8_ABS_LO12_NC:
942 case R_AARCH64_LDST32_ABS_LO12_NC:
943 case R_AARCH64_LDST64_ABS_LO12_NC:
944 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
945 return SS->Sym.getType() == STT_OBJECT;
946 return false;
947 }
948}
949
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000950bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
951 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000952 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
953 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000954}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000955
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000956bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
957 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000958 switch (Type) {
959 default:
960 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000961 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000962 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000963 return canBePreempted(&S, true);
964 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000965}
Davide Italiano1d750a62015-09-27 08:45:38 +0000966
Davide Italianoef4be6b2015-10-06 19:01:32 +0000967static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000968 uint32_t ImmLo = (Imm & 0x3) << 29;
969 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
970 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000971 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000972}
973
Davide Italiano318ca222015-10-02 22:13:51 +0000974// Page(Expr) is the page address of the expression Expr, defined
975// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000976// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000977static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000978 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000979}
980
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000981void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000982 uint32_t Type, uint64_t P, uint64_t SA,
983 uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000984 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000985 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000986 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000987 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000988 break;
989 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000990 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000991 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000992 break;
993 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000994 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000995 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000996 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +0000997 // This relocation stores 12 bits and there's no instruction
998 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000999 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1000 // bits in Loc are zero.
1001 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001002 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001003 case R_AARCH64_ADR_GOT_PAGE: {
1004 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1005 checkInt<33>(X, Type);
1006 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1007 break;
1008 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001009 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001010 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001011 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001012 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001013 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001014 }
1015 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001016 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001017 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001018 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001019 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001020 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001021 case R_AARCH64_CALL26:
1022 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001023 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001024 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001025 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1026 break;
1027 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001028 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001029 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001030 or32le(Loc, (SA & 0xFF8) << 7);
1031 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001032 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001033 or32le(Loc, (SA & 0xFFF) << 10);
1034 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001035 case R_AARCH64_LDST32_ABS_LO12_NC:
1036 or32le(Loc, (SA & 0xFFC) << 8);
1037 break;
1038 case R_AARCH64_LDST64_ABS_LO12_NC:
1039 or32le(Loc, (SA & 0xFF8) << 7);
1040 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001041 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001042 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001043 write16le(Loc, SA - P);
1044 break;
1045 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001046 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001047 write32le(Loc, SA - P);
1048 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001049 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001050 write64le(Loc, SA - P);
1051 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001052 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001053 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001054 }
1055}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001056
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001057template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001058 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001059 GotHeaderEntriesNum = 2;
1060}
1061
1062template <class ELFT>
1063void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001064 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001065 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1066 // Module pointer
1067 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001068}
1069
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001070template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001071void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1072template <class ELFT>
1073void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1074 uint64_t PltEntryAddr) const {}
1075template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001076void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1077 uint64_t GotEntryAddr,
1078 uint64_t PltEntryAddr, int32_t Index,
1079 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001080
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001081template <class ELFT>
1082bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1083 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001084 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001085}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001086
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001087template <class ELFT>
1088bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1089 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001090 return false;
1091}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001092
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001093template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001094void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001095 uint32_t Type, uint64_t P, uint64_t SA,
1096 uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001097 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001098 switch (Type) {
1099 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001100 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001101 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001102 case R_MIPS_CALL16:
1103 case R_MIPS_GOT16: {
1104 int64_t V = SA - getMipsGpAddr<ELFT>();
1105 if (Type == R_MIPS_GOT16)
1106 checkInt<16>(V, Type);
1107 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1108 break;
1109 }
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001110 case R_MIPS_HI16: {
1111 uint32_t Instr = read32<E>(Loc);
1112 if (PairedLoc) {
1113 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001114 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001115 write32<E>(Loc, (Instr & 0xffff0000) | (((SA + AHL) >> 16) & 0xffff));
1116 } else {
1117 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
1118 write32<E>(Loc, (Instr & 0xffff0000) | ((SA >> 16) & 0xffff));
1119 }
1120 break;
1121 }
1122 case R_MIPS_LO16: {
1123 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001124 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001125 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1126 break;
1127 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001128 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001129 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001130 }
1131}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001132
Rui Ueyama24e39522015-12-03 20:57:45 +00001133template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001134 const unsigned GPOffset = 0x7ff0;
1135 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1136}
1137
1138template uint32_t getMipsGpAddr<ELF32LE>();
1139template uint32_t getMipsGpAddr<ELF32BE>();
1140template uint64_t getMipsGpAddr<ELF64LE>();
1141template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001142}
1143}