blob: fde140c25e15f61a2ff939f9c4e4afcacd2f4355 [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,
Rui Ueyama66072272015-10-15 19:52:27 +000090 uint64_t SA) 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,
Rui Ueyama66072272015-10-15 19:52:27 +0000109 uint64_t SA) 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,
Rui Ueyama66072272015-10-15 19:52:27 +0000136 uint64_t SA) 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;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000150 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
151 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000152 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000153 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000154};
155
156template <class ELFT> class MipsTargetInfo final : public TargetInfo {
157public:
158 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000159 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000160 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
161 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
162 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000163 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
164 uint64_t PltEntryAddr, int32_t Index,
165 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000166 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
167 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000168 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000169 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170};
171} // anonymous namespace
172
Rui Ueyama91004392015-10-13 16:08:15 +0000173TargetInfo *createTarget() {
174 switch (Config->EMachine) {
175 case EM_386:
176 return new X86TargetInfo();
177 case EM_AARCH64:
178 return new AArch64TargetInfo();
179 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000180 switch (Config->EKind) {
181 case ELF32LEKind:
182 return new MipsTargetInfo<ELF32LE>();
183 case ELF32BEKind:
184 return new MipsTargetInfo<ELF32BE>();
185 default:
186 error("Unsupported MIPS target");
187 }
Rui Ueyama91004392015-10-13 16:08:15 +0000188 case EM_PPC64:
189 return new PPC64TargetInfo();
190 case EM_X86_64:
191 return new X86_64TargetInfo();
192 }
193 error("Unknown target machine");
194}
195
Rafael Espindola01205f72015-09-22 18:19:46 +0000196TargetInfo::~TargetInfo() {}
197
George Rimar6713cf82015-11-25 21:46:05 +0000198bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000199 return false;
200}
201
Igor Kudrinf6f45472015-11-10 08:39:27 +0000202uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
203
George Rimarbc590fe2015-10-28 16:48:58 +0000204bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
205 return false;
206}
207
Igor Kudrine7ad0932015-11-17 17:47:53 +0000208unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000209
Rafael Espindolaae244002015-10-05 19:30:12 +0000210bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
211
George Rimar6713cf82015-11-25 21:46:05 +0000212unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
213 uint32_t Type, uint64_t P,
214 uint64_t SA) const {
215 return 0;
216}
George Rimar77d1cb12015-11-24 09:00:06 +0000217
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000218void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
219
Igor Kudrin351b41d2015-11-16 17:44:08 +0000220void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
221
Rafael Espindola7f074422015-09-22 21:35:51 +0000222X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000223 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000224 PCRelReloc = R_386_PC32;
225 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000226 PltReloc = R_386_JUMP_SLOT;
George Rimar77b77792015-11-25 22:15:01 +0000227 LazyRelocations = true;
228 PltEntrySize = 16;
229 PltZeroEntrySize = 16;
230}
231
232void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
233 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
234}
235
236void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
237 // Skip 6 bytes of "pushl (GOT+4)"
238 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000239}
Rafael Espindola01205f72015-09-22 18:19:46 +0000240
George Rimard23970f2015-11-25 20:41:53 +0000241unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
242 if (Type == R_386_TLS_LE)
243 return R_386_TLS_TPOFF;
244 if (Type == R_386_TLS_LE_32)
245 return R_386_TLS_TPOFF32;
246 return Type;
247}
248
249bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
250 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32)
251 return Config->Shared;
252 return false;
253}
254
George Rimar648a2c32015-10-20 08:54:27 +0000255void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000256 uint64_t PltEntryAddr) const {
257 // Executable files and shared object files have
258 // separate procedure linkage tables.
259 if (Config->Shared) {
260 const uint8_t V[] = {
261 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
262 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
263 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
264 };
265 memcpy(Buf, V, sizeof(V));
266 return;
267 }
George Rimar648a2c32015-10-20 08:54:27 +0000268
George Rimar77b77792015-11-25 22:15:01 +0000269 const uint8_t PltData[] = {
270 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
271 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
272 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
273 };
274 memcpy(Buf, PltData, sizeof(PltData));
275 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
276 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
277}
278
279void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
280 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
281 int32_t Index, unsigned RelOff) const {
282 const uint8_t Inst[] = {
283 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
284 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
285 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
286 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000287 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000288 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
289 Buf[1] = Config->Shared ? 0xa3 : 0x25;
290 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
291 write32le(Buf + 7, RelOff);
292 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000293}
294
George Rimar70e25082015-11-25 11:27:40 +0000295bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
296 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
297 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
298 return SS->Sym.getType() == STT_OBJECT;
299 return false;
300}
301
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000302bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000303 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000304}
305
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000306bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000307 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000308}
309
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000310void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
311 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000312 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000313 case R_386_32:
314 add32le(Loc, SA);
315 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000316 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000317 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000318 break;
George Rimar13934772015-11-25 20:20:31 +0000319 case R_386_GOTPC:
320 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
321 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000322 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000323 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000324 break;
George Rimard23970f2015-11-25 20:41:53 +0000325 case R_386_TLS_LE:
326 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
327 break;
328 case R_386_TLS_LE_32:
329 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
330 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000331 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000332 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000333 }
334}
335
Rafael Espindola7f074422015-09-22 21:35:51 +0000336X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000337 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000338 PCRelReloc = R_X86_64_PC32;
339 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000340 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000341 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000342 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000343 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000344 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000345 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000346 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000347 LazyRelocations = true;
348 PltEntrySize = 16;
349 PltZeroEntrySize = 16;
350}
351
Igor Kudrin351b41d2015-11-16 17:44:08 +0000352void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
353 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
354}
355
George Rimar648a2c32015-10-20 08:54:27 +0000356void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
357 // Skip 6 bytes of "jmpq *got(%rip)"
358 write32le(Buf, Plt + 6);
359}
360
361void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
362 uint64_t PltEntryAddr) const {
363 const uint8_t PltData[] = {
364 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
365 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
366 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
367 };
368 memcpy(Buf, PltData, sizeof(PltData));
369 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
370 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000371}
Rafael Espindola01205f72015-09-22 18:19:46 +0000372
George Rimar77b77792015-11-25 22:15:01 +0000373void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
374 uint64_t GotEntryAddr,
375 uint64_t PltEntryAddr, int32_t Index,
376 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000377 const uint8_t Inst[] = {
378 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
379 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
380 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
381 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000382 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000383
George Rimar648a2c32015-10-20 08:54:27 +0000384 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
385 write32le(Buf + 7, Index);
386 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000387}
388
George Rimarbc590fe2015-10-28 16:48:58 +0000389bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
390 const SymbolBody &S) const {
391 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
392 Type == R_X86_64_64)
393 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
394 return SS->Sym.getType() == STT_OBJECT;
395 return false;
396}
397
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000398bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000399 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000400 return !isTlsOptimized(Type, &S);
George Rimar687138c2015-11-13 16:28:53 +0000401 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
402 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000403}
404
George Rimard23970f2015-11-25 20:41:53 +0000405bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
406 return Type == R_X86_64_GOTTPOFF;
407}
408
Igor Kudrine7ad0932015-11-17 17:47:53 +0000409unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000410 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000411 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000412 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000413}
414
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000415bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000416 if (relocNeedsCopy(Type, S))
417 return false;
418
Rafael Espindola01205f72015-09-22 18:19:46 +0000419 switch (Type) {
420 default:
421 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000422 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000423 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000424 case R_X86_64_PC32:
425 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000426 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000427 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000428 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000429 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000430 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
431 // libc.so.
432 //
433 // The general idea on how to handle such cases is to create a PLT entry
434 // and use that as the function value.
435 //
436 // For the static linking part, we just return true and everything else
437 // will use the the PLT entry as the address.
438 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000439 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000440 // still works. We need the help of the dynamic linker for that. We
441 // let it know that we have a direct reference to a so symbol by creating
442 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000443 // dynamic linker resolves the symbol to the value of the symbol we created.
444 // This is true even for got entries, so pointer equality is maintained.
445 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000446 // real function is a dedicated got entry used by the plt. That is
447 // identified by special relocation types (R_X86_64_JUMP_SLOT,
448 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000449 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000450 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000451 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000452 }
453}
Rafael Espindolac4010882015-09-22 20:54:08 +0000454
Rafael Espindolaae244002015-10-05 19:30:12 +0000455bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
456 switch (Type) {
457 default:
458 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000459 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000460 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000461 case R_X86_64_PC8:
462 case R_X86_64_PC16:
463 case R_X86_64_PC32:
464 case R_X86_64_PC64:
465 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000466 return true;
467 }
468}
469
George Rimar77d1cb12015-11-24 09:00:06 +0000470bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000471 const SymbolBody *S) const {
472 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000473 return false;
George Rimar6713cf82015-11-25 21:46:05 +0000474 return Type == R_X86_64_TLSLD || Type == R_X86_64_DTPOFF32 ||
475 (Type == R_X86_64_TLSGD && !canBePreempted(S, true)) ||
476 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
477}
478
479// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
480// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
481// how LD can be optimized to LE:
482// leaq bar@tlsld(%rip), %rdi
483// callq __tls_get_addr@PLT
484// leaq bar@dtpoff(%rax), %rcx
485// Is converted to:
486// .word 0x6666
487// .byte 0x66
488// mov %fs:0,%rax
489// leaq bar@tpoff(%rax), %rcx
490void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
491 uint64_t P, uint64_t SA) const {
492 const uint8_t Inst[] = {
493 0x66, 0x66, //.word 0x6666
494 0x66, //.byte 0x66
495 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
496 };
497 memcpy(Loc - 3, Inst, sizeof(Inst));
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 GD can be optimized to LE:
503// .byte 0x66
504// leaq x@tlsgd(%rip), %rdi
505// .word 0x6666
506// rex64
507// call __tls_get_addr@plt
508// Is converted to:
509// mov %fs:0x0,%rax
510// lea x@tpoff,%rax
511void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
512 uint64_t P, uint64_t SA) const {
513 const uint8_t Inst[] = {
514 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
515 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
516 };
517 memcpy(Loc - 4, Inst, sizeof(Inst));
518 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000519}
520
521// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
522// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
523// This function does that. Read "ELF Handling For Thread-Local Storage,
524// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
525// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000526void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
527 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000528 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
529 // used in MOVQ or ADDQ instructions only.
530 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
531 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
532 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
533 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
534 uint8_t *Prefix = Loc - 3;
535 uint8_t *Inst = Loc - 2;
536 uint8_t *RegSlot = Loc - 1;
537 uint8_t Reg = Loc[-1] >> 3;
538 bool IsMov = *Inst == 0x8b;
539 bool RspAdd = !IsMov && Reg == 4;
540 // r12 and rsp registers requires special handling.
541 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
542 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
543 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
544 // The same true for rsp. So we convert to addq for them, saving 1 byte that
545 // we dont have.
546 if (RspAdd)
547 *Inst = 0x81;
548 else
549 *Inst = IsMov ? 0xc7 : 0x8d;
550 if (*Prefix == 0x4c)
551 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
552 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
553 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
554}
555
George Rimar6713cf82015-11-25 21:46:05 +0000556// This function applies a TLS relocation with an optimization as described
557// in the Ulrich's document. As a result of rewriting instructions at the
558// relocation target, relocations immediately follow the TLS relocation (which
559// would be applied to rewritten instructions) may have to be skipped.
560// This function returns a number of relocations that need to be skipped.
561unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
562 uint32_t Type, uint64_t P,
563 uint64_t SA) const {
564 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000565 case R_X86_64_DTPOFF32:
566 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
567 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000568 case R_X86_64_GOTTPOFF:
569 relocateTlsIeToLe(Loc, BufEnd, P, SA);
570 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000571 case R_X86_64_TLSGD:
572 relocateTlsGdToLe(Loc, BufEnd, P, SA);
573 // The next relocation should be against __tls_get_addr, so skip it
574 return 1;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000575 case R_X86_64_TLSLD:
576 relocateTlsLdToLe(Loc, BufEnd, P, SA);
577 // The next relocation should be against __tls_get_addr, so skip it
578 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000579 }
580 llvm_unreachable("Unknown TLS optimization");
581}
582
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000583void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
584 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000585 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000586 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000587 checkUInt<32>(SA, Type);
588 write32le(Loc, SA);
589 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000590 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000591 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000592 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000593 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000594 case R_X86_64_64:
595 write64le(Loc, SA);
596 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000597 case R_X86_64_DTPOFF32:
598 write32le(Loc, SA);
599 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000600 case R_X86_64_DTPOFF64:
601 write64le(Loc, SA);
602 break;
603 case R_X86_64_GOTPCREL:
604 case R_X86_64_PC32:
605 case R_X86_64_PLT32:
606 case R_X86_64_TLSGD:
607 case R_X86_64_TLSLD:
608 write32le(Loc, SA - P);
609 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000610 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000611 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000612 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000613 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000614 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000615 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000616 case R_X86_64_TPOFF64:
617 write32le(Loc, SA - P);
618 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000619 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000620 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000621 }
622}
623
Hal Finkel3c8cc672015-10-12 20:56:18 +0000624// Relocation masks following the #lo(value), #hi(value), #ha(value),
625// #higher(value), #highera(value), #highest(value), and #highesta(value)
626// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
627// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000628static uint16_t applyPPCLo(uint64_t V) { return V; }
629static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
630static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
631static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
632static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000633static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000634static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
635
Rafael Espindolac4010882015-09-22 20:54:08 +0000636PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000637 PCRelReloc = R_PPC64_REL24;
638 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000639 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000640 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000641
642 // We need 64K pages (at least under glibc/Linux, the loader won't
643 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000644 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000645
646 // The PPC64 ELF ABI v1 spec, says:
647 //
648 // It is normally desirable to put segments with different characteristics
649 // in separate 256 Mbyte portions of the address space, to give the
650 // operating system full paging flexibility in the 64-bit address space.
651 //
652 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
653 // use 0x10000000 as the starting address.
654 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000655}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000656
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000657uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000658 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
659 // order. The TOC starts where the first of these sections starts.
660
661 // FIXME: This obviously does not do the right thing when there is no .got
662 // section, but there is a .toc or .tocbss section.
663 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
664 if (!TocVA)
665 TocVA = Out<ELF64BE>::Plt->getVA();
666
667 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
668 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
669 // code (crt1.o) assumes that you can get from the TOC base to the
670 // start of the .toc section with only a single (signed) 16-bit relocation.
671 return TocVA + 0x8000;
672}
673
George Rimar648a2c32015-10-20 08:54:27 +0000674void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
675void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
676 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000677void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
678 uint64_t GotEntryAddr,
679 uint64_t PltEntryAddr, int32_t Index,
680 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000681 uint64_t Off = GotEntryAddr - getPPC64TocBase();
682
683 // FIXME: What we should do, in theory, is get the offset of the function
684 // descriptor in the .opd section, and use that as the offset from %r2 (the
685 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
686 // be a pointer to the function descriptor in the .opd section. Using
687 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
688
Hal Finkelfa92f682015-10-13 21:47:34 +0000689 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000690 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
691 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
692 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
693 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
694 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
695 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
696 write32be(Buf + 28, 0x4e800420); // bctr
697}
698
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000699bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000700 if (relocNeedsPlt(Type, S))
701 return true;
702
703 switch (Type) {
704 default: return false;
705 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000706 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000707 case R_PPC64_GOT16_HA:
708 case R_PPC64_GOT16_HI:
709 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000710 case R_PPC64_GOT16_LO_DS:
711 return true;
712 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000713}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000714
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000715bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000716 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000717 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000718}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000719
Hal Finkelbe0823d2015-10-12 20:58:52 +0000720bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
721 switch (Type) {
722 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000723 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000724 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000725 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000726 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000727 }
728}
729
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000730void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
731 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000732 uint64_t TB = getPPC64TocBase();
733
Hal Finkel3c8cc672015-10-12 20:56:18 +0000734 // For a TOC-relative relocation, adjust the addend and proceed in terms of
735 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000736 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000737 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
738 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000739 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
740 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000741 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
742 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000743 default: break;
744 }
745
Hal Finkel3c8cc672015-10-12 20:56:18 +0000746 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000747 case R_PPC64_ADDR14: {
748 checkAlignment<4>(SA, Type);
749 // Preserve the AA/LK bits in the branch instruction
750 uint8_t AALK = Loc[3];
751 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
752 break;
753 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000754 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000755 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000756 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000757 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000758 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000759 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000760 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000761 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000762 case R_PPC64_ADDR16_HA:
763 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000764 break;
765 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000766 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000767 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000768 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000769 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000770 break;
771 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000772 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000773 break;
774 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000775 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000776 break;
777 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000778 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000779 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000780 case R_PPC64_ADDR16_LO:
781 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000782 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000783 case R_PPC64_ADDR16_LO_DS:
784 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000785 break;
786 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000787 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000788 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000789 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000790 case R_PPC64_ADDR64:
791 write64be(Loc, SA);
792 break;
793 case R_PPC64_REL16_HA:
794 write16be(Loc, applyPPCHa(SA - P));
795 break;
796 case R_PPC64_REL16_HI:
797 write16be(Loc, applyPPCHi(SA - P));
798 break;
799 case R_PPC64_REL16_LO:
800 write16be(Loc, applyPPCLo(SA - P));
801 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000802 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000803 // If we have an undefined weak symbol, we might get here with a symbol
804 // address of zero. That could overflow, but the code must be unreachable,
805 // so don't bother doing anything at all.
806 if (!SA)
807 break;
808
Hal Finkeldaedc122015-10-12 23:16:53 +0000809 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
810 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000811 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000812
813 if (!InPlt && Out<ELF64BE>::Opd) {
814 // If this is a local call, and we currently have the address of a
815 // function-descriptor, get the underlying code address instead.
816 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
817 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000818 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000819
820 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000821 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000822 }
823
Hal Finkel3c8cc672015-10-12 20:56:18 +0000824 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000825 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000826 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000827
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000828 uint32_t Nop = 0x60000000;
829 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
830 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000831 break;
832 }
833 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000834 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000835 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000836 break;
837 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000838 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000839 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000840 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000841 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000842 break;
843 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000844 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000845 }
846}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000847
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000848AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000849 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000850 PltReloc = R_AARCH64_JUMP_SLOT;
851 LazyRelocations = true;
852 PltEntrySize = 16;
853 PltZeroEntrySize = 32;
854}
George Rimar648a2c32015-10-20 08:54:27 +0000855
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000856unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
857
858void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
859 write64le(Buf, Out<ELF64LE>::Plt->getVA());
860}
861
George Rimar648a2c32015-10-20 08:54:27 +0000862void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000863 uint64_t PltEntryAddr) const {
864 const uint8_t PltData[] = {
865 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
866 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
867 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
868 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
869 0x20, 0x02, 0x1f, 0xd6, // br x17
870 0x1f, 0x20, 0x03, 0xd5, // nop
871 0x1f, 0x20, 0x03, 0xd5, // nop
872 0x1f, 0x20, 0x03, 0xd5 // nop
873 };
874 memcpy(Buf, PltData, sizeof(PltData));
875
876 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
877 GotEntryAddr + 16);
878 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
879 GotEntryAddr + 16);
880 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
881 GotEntryAddr + 16);
882}
883
George Rimar77b77792015-11-25 22:15:01 +0000884void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
885 uint64_t GotEntryAddr,
886 uint64_t PltEntryAddr, int32_t Index,
887 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000888 const uint8_t Inst[] = {
889 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
890 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
891 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
892 0x20, 0x02, 0x1f, 0xd6 // br x17
893 };
894 memcpy(Buf, Inst, sizeof(Inst));
895
896 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
897 GotEntryAddr);
898 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
899 GotEntryAddr);
900 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
901 GotEntryAddr);
902}
903
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000904bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
905 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000906 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
907 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000908}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000909
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000910bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
911 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000912 switch (Type) {
913 default:
914 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000915 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000916 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000917 return canBePreempted(&S, true);
918 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000919}
Davide Italiano1d750a62015-09-27 08:45:38 +0000920
Davide Italianoef4be6b2015-10-06 19:01:32 +0000921static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000922 uint32_t ImmLo = (Imm & 0x3) << 29;
923 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
924 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000925 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000926}
927
Davide Italiano318ca222015-10-02 22:13:51 +0000928// Page(Expr) is the page address of the expression Expr, defined
929// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000930// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000931static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000932 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000933}
934
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000935void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
936 uint32_t Type, uint64_t P,
937 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000938 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000939 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000940 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000941 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000942 break;
943 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000944 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000945 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000946 break;
947 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000948 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000949 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000950 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +0000951 // This relocation stores 12 bits and there's no instruction
952 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000953 // of r_addend bitwise-or'ed Loc. This assumes that the addend
954 // bits in Loc are zero.
955 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000956 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000957 case R_AARCH64_ADR_GOT_PAGE: {
958 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
959 checkInt<33>(X, Type);
960 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
961 break;
962 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000963 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000964 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000965 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000966 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000967 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000968 }
969 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000970 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000971 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000972 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000973 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000974 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000975 case R_AARCH64_CALL26:
976 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +0000977 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000978 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +0000979 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
980 break;
981 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000982 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000983 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000984 or32le(Loc, (SA & 0xFF8) << 7);
985 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000986 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +0000987 or32le(Loc, (SA & 0xFFF) << 10);
988 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000989 case R_AARCH64_LDST32_ABS_LO12_NC:
990 or32le(Loc, (SA & 0xFFC) << 8);
991 break;
992 case R_AARCH64_LDST64_ABS_LO12_NC:
993 or32le(Loc, (SA & 0xFF8) << 7);
994 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000995 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000996 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000997 write16le(Loc, SA - P);
998 break;
999 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001000 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001001 write32le(Loc, SA - P);
1002 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001003 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001004 write64le(Loc, SA - P);
1005 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001006 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001007 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001008 }
1009}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001010
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001011template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001012 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001013 GotHeaderEntriesNum = 2;
1014}
1015
1016template <class ELFT>
1017void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
1018 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
1019 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1020 // Module pointer
1021 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001022}
1023
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001024template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001025void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1026template <class ELFT>
1027void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1028 uint64_t PltEntryAddr) const {}
1029template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001030void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1031 uint64_t GotEntryAddr,
1032 uint64_t PltEntryAddr, int32_t Index,
1033 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001034
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001035template <class ELFT>
1036bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1037 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001038 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001039}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001040
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001041template <class ELFT>
1042bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1043 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001044 return false;
1045}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001046
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001047template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001048void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
1049 uint32_t Type, uint64_t P,
1050 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001051 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001052 switch (Type) {
1053 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001054 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001055 break;
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001056 case R_MIPS_CALL16:
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001057 case R_MIPS_GOT16: {
1058 int64_t V = SA - getMipsGpAddr<ELFT>();
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001059 if (Type == R_MIPS_GOT16)
1060 checkInt<16>(V, Type);
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001061 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001062 break;
1063 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001064 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001065 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001066 }
1067}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001068
1069template <class ELFT>
1070typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
1071 const unsigned GPOffset = 0x7ff0;
1072 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1073}
1074
1075template uint32_t getMipsGpAddr<ELF32LE>();
1076template uint32_t getMipsGpAddr<ELF32BE>();
1077template uint64_t getMipsGpAddr<ELF64LE>();
1078template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001079}
1080}