blob: e32c6901b12121375d2c880c54fb0198a0494d6b [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;
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,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000153 uint64_t SA, uint8_t *PairedLoc = nullptr) 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,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000169 uint64_t SA, uint8_t *PairedLoc = nullptr) 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,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000311 uint64_t P, uint64_t SA,
312 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000313 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000314 case R_386_32:
315 add32le(Loc, SA);
316 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000317 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000318 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000319 break;
George Rimar13934772015-11-25 20:20:31 +0000320 case R_386_GOTPC:
321 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
322 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000323 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000324 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000325 break;
George Rimard23970f2015-11-25 20:41:53 +0000326 case R_386_TLS_LE:
327 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
328 break;
329 case R_386_TLS_LE_32:
330 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
331 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000332 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000333 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000334 }
335}
336
Rafael Espindola7f074422015-09-22 21:35:51 +0000337X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000338 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000339 PCRelReloc = R_X86_64_PC32;
340 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000341 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000342 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000343 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000344 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000345 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000346 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000347 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000348 LazyRelocations = true;
349 PltEntrySize = 16;
350 PltZeroEntrySize = 16;
351}
352
Igor Kudrin351b41d2015-11-16 17:44:08 +0000353void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
354 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
355}
356
George Rimar648a2c32015-10-20 08:54:27 +0000357void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
358 // Skip 6 bytes of "jmpq *got(%rip)"
359 write32le(Buf, Plt + 6);
360}
361
362void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
363 uint64_t PltEntryAddr) const {
364 const uint8_t PltData[] = {
365 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
366 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
367 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
368 };
369 memcpy(Buf, PltData, sizeof(PltData));
370 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
371 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000372}
Rafael Espindola01205f72015-09-22 18:19:46 +0000373
George Rimar77b77792015-11-25 22:15:01 +0000374void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
375 uint64_t GotEntryAddr,
376 uint64_t PltEntryAddr, int32_t Index,
377 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000378 const uint8_t Inst[] = {
379 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
380 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
381 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
382 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000383 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000384
George Rimar648a2c32015-10-20 08:54:27 +0000385 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
386 write32le(Buf + 7, Index);
387 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000388}
389
George Rimarbc590fe2015-10-28 16:48:58 +0000390bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
391 const SymbolBody &S) const {
392 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
393 Type == R_X86_64_64)
394 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
395 return SS->Sym.getType() == STT_OBJECT;
396 return false;
397}
398
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000399bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000400 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000401 return !isTlsOptimized(Type, &S);
George Rimar687138c2015-11-13 16:28:53 +0000402 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
403 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000404}
405
George Rimard23970f2015-11-25 20:41:53 +0000406bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
407 return Type == R_X86_64_GOTTPOFF;
408}
409
Igor Kudrine7ad0932015-11-17 17:47:53 +0000410unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000411 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000412 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000413 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000414}
415
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000416bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000417 if (relocNeedsCopy(Type, S))
418 return false;
419
Rafael Espindola01205f72015-09-22 18:19:46 +0000420 switch (Type) {
421 default:
422 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000423 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000424 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000425 case R_X86_64_PC32:
426 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000427 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000428 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000429 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000430 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000431 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
432 // libc.so.
433 //
434 // The general idea on how to handle such cases is to create a PLT entry
435 // and use that as the function value.
436 //
437 // For the static linking part, we just return true and everything else
438 // will use the the PLT entry as the address.
439 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000440 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000441 // still works. We need the help of the dynamic linker for that. We
442 // let it know that we have a direct reference to a so symbol by creating
443 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000444 // dynamic linker resolves the symbol to the value of the symbol we created.
445 // This is true even for got entries, so pointer equality is maintained.
446 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000447 // real function is a dedicated got entry used by the plt. That is
448 // identified by special relocation types (R_X86_64_JUMP_SLOT,
449 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000450 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000451 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000452 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000453 }
454}
Rafael Espindolac4010882015-09-22 20:54:08 +0000455
Rafael Espindolaae244002015-10-05 19:30:12 +0000456bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
457 switch (Type) {
458 default:
459 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000460 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000461 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000462 case R_X86_64_PC8:
463 case R_X86_64_PC16:
464 case R_X86_64_PC32:
465 case R_X86_64_PC64:
466 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000467 return true;
468 }
469}
470
George Rimar77d1cb12015-11-24 09:00:06 +0000471bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000472 const SymbolBody *S) const {
473 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000474 return false;
George Rimar6713cf82015-11-25 21:46:05 +0000475 return Type == R_X86_64_TLSLD || Type == R_X86_64_DTPOFF32 ||
476 (Type == R_X86_64_TLSGD && !canBePreempted(S, true)) ||
477 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
478}
479
480// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
481// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
482// how LD can be optimized to LE:
483// leaq bar@tlsld(%rip), %rdi
484// callq __tls_get_addr@PLT
485// leaq bar@dtpoff(%rax), %rcx
486// Is converted to:
487// .word 0x6666
488// .byte 0x66
489// mov %fs:0,%rax
490// leaq bar@tpoff(%rax), %rcx
491void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
492 uint64_t P, uint64_t SA) const {
493 const uint8_t Inst[] = {
494 0x66, 0x66, //.word 0x6666
495 0x66, //.byte 0x66
496 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
497 };
498 memcpy(Loc - 3, Inst, sizeof(Inst));
499}
500
501// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
502// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
503// how GD can be optimized to LE:
504// .byte 0x66
505// leaq x@tlsgd(%rip), %rdi
506// .word 0x6666
507// rex64
508// call __tls_get_addr@plt
509// Is converted to:
510// mov %fs:0x0,%rax
511// lea x@tpoff,%rax
512void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
513 uint64_t P, uint64_t SA) const {
514 const uint8_t Inst[] = {
515 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
516 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
517 };
518 memcpy(Loc - 4, Inst, sizeof(Inst));
519 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000520}
521
522// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
523// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
524// This function does that. Read "ELF Handling For Thread-Local Storage,
525// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
526// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000527void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
528 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000529 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
530 // used in MOVQ or ADDQ instructions only.
531 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
532 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
533 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
534 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
535 uint8_t *Prefix = Loc - 3;
536 uint8_t *Inst = Loc - 2;
537 uint8_t *RegSlot = Loc - 1;
538 uint8_t Reg = Loc[-1] >> 3;
539 bool IsMov = *Inst == 0x8b;
540 bool RspAdd = !IsMov && Reg == 4;
541 // r12 and rsp registers requires special handling.
542 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
543 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
544 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
545 // The same true for rsp. So we convert to addq for them, saving 1 byte that
546 // we dont have.
547 if (RspAdd)
548 *Inst = 0x81;
549 else
550 *Inst = IsMov ? 0xc7 : 0x8d;
551 if (*Prefix == 0x4c)
552 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
553 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
554 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
555}
556
George Rimar6713cf82015-11-25 21:46:05 +0000557// This function applies a TLS relocation with an optimization as described
558// in the Ulrich's document. As a result of rewriting instructions at the
559// relocation target, relocations immediately follow the TLS relocation (which
560// would be applied to rewritten instructions) may have to be skipped.
561// This function returns a number of relocations that need to be skipped.
562unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
563 uint32_t Type, uint64_t P,
564 uint64_t SA) const {
565 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000566 case R_X86_64_DTPOFF32:
567 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
568 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000569 case R_X86_64_GOTTPOFF:
570 relocateTlsIeToLe(Loc, BufEnd, P, SA);
571 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000572 case R_X86_64_TLSGD:
573 relocateTlsGdToLe(Loc, BufEnd, P, SA);
574 // The next relocation should be against __tls_get_addr, so skip it
575 return 1;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000576 case R_X86_64_TLSLD:
577 relocateTlsLdToLe(Loc, BufEnd, P, SA);
578 // The next relocation should be against __tls_get_addr, so skip it
579 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000580 }
581 llvm_unreachable("Unknown TLS optimization");
582}
583
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000584void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000585 uint64_t P, uint64_t SA,
586 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000587 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000588 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000589 checkUInt<32>(SA, Type);
590 write32le(Loc, SA);
591 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000592 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000593 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000594 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000595 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000596 case R_X86_64_64:
597 write64le(Loc, SA);
598 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000599 case R_X86_64_DTPOFF32:
600 write32le(Loc, SA);
601 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000602 case R_X86_64_DTPOFF64:
603 write64le(Loc, SA);
604 break;
605 case R_X86_64_GOTPCREL:
606 case R_X86_64_PC32:
607 case R_X86_64_PLT32:
608 case R_X86_64_TLSGD:
609 case R_X86_64_TLSLD:
610 write32le(Loc, SA - P);
611 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000612 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000613 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000614 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000615 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000616 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000617 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000618 case R_X86_64_TPOFF64:
619 write32le(Loc, SA - P);
620 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000621 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000622 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000623 }
624}
625
Hal Finkel3c8cc672015-10-12 20:56:18 +0000626// Relocation masks following the #lo(value), #hi(value), #ha(value),
627// #higher(value), #highera(value), #highest(value), and #highesta(value)
628// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
629// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000630static uint16_t applyPPCLo(uint64_t V) { return V; }
631static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
632static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
633static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
634static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000635static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000636static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
637
Rafael Espindolac4010882015-09-22 20:54:08 +0000638PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000639 PCRelReloc = R_PPC64_REL24;
640 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000641 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000642 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000643
644 // We need 64K pages (at least under glibc/Linux, the loader won't
645 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000646 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000647
648 // The PPC64 ELF ABI v1 spec, says:
649 //
650 // It is normally desirable to put segments with different characteristics
651 // in separate 256 Mbyte portions of the address space, to give the
652 // operating system full paging flexibility in the 64-bit address space.
653 //
654 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
655 // use 0x10000000 as the starting address.
656 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000657}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000658
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000659uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000660 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
661 // order. The TOC starts where the first of these sections starts.
662
663 // FIXME: This obviously does not do the right thing when there is no .got
664 // section, but there is a .toc or .tocbss section.
665 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
666 if (!TocVA)
667 TocVA = Out<ELF64BE>::Plt->getVA();
668
669 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
670 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
671 // code (crt1.o) assumes that you can get from the TOC base to the
672 // start of the .toc section with only a single (signed) 16-bit relocation.
673 return TocVA + 0x8000;
674}
675
George Rimar648a2c32015-10-20 08:54:27 +0000676void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
677void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
678 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000679void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
680 uint64_t GotEntryAddr,
681 uint64_t PltEntryAddr, int32_t Index,
682 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000683 uint64_t Off = GotEntryAddr - getPPC64TocBase();
684
685 // FIXME: What we should do, in theory, is get the offset of the function
686 // descriptor in the .opd section, and use that as the offset from %r2 (the
687 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
688 // be a pointer to the function descriptor in the .opd section. Using
689 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
690
Hal Finkelfa92f682015-10-13 21:47:34 +0000691 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000692 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
693 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
694 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
695 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
696 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
697 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
698 write32be(Buf + 28, 0x4e800420); // bctr
699}
700
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000701bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000702 if (relocNeedsPlt(Type, S))
703 return true;
704
705 switch (Type) {
706 default: return false;
707 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000708 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000709 case R_PPC64_GOT16_HA:
710 case R_PPC64_GOT16_HI:
711 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000712 case R_PPC64_GOT16_LO_DS:
713 return true;
714 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000715}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000716
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000717bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000718 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000719 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000720}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000721
Hal Finkelbe0823d2015-10-12 20:58:52 +0000722bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
723 switch (Type) {
724 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000725 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000726 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000727 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000728 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000729 }
730}
731
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000732void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000733 uint64_t P, uint64_t SA,
734 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000735 uint64_t TB = getPPC64TocBase();
736
Hal Finkel3c8cc672015-10-12 20:56:18 +0000737 // For a TOC-relative relocation, adjust the addend and proceed in terms of
738 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000739 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000740 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
741 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000742 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
743 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000744 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
745 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000746 default: break;
747 }
748
Hal Finkel3c8cc672015-10-12 20:56:18 +0000749 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000750 case R_PPC64_ADDR14: {
751 checkAlignment<4>(SA, Type);
752 // Preserve the AA/LK bits in the branch instruction
753 uint8_t AALK = Loc[3];
754 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
755 break;
756 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000757 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000758 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000759 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000760 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000761 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000762 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000763 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000764 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000765 case R_PPC64_ADDR16_HA:
766 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000767 break;
768 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000769 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000770 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000771 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000772 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000773 break;
774 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000775 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000776 break;
777 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000778 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000779 break;
780 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000781 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000782 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000783 case R_PPC64_ADDR16_LO:
784 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000785 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000786 case R_PPC64_ADDR16_LO_DS:
787 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000788 break;
789 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000790 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000791 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000792 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000793 case R_PPC64_ADDR64:
794 write64be(Loc, SA);
795 break;
796 case R_PPC64_REL16_HA:
797 write16be(Loc, applyPPCHa(SA - P));
798 break;
799 case R_PPC64_REL16_HI:
800 write16be(Loc, applyPPCHi(SA - P));
801 break;
802 case R_PPC64_REL16_LO:
803 write16be(Loc, applyPPCLo(SA - P));
804 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000805 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000806 // If we have an undefined weak symbol, we might get here with a symbol
807 // address of zero. That could overflow, but the code must be unreachable,
808 // so don't bother doing anything at all.
809 if (!SA)
810 break;
811
Hal Finkeldaedc122015-10-12 23:16:53 +0000812 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
813 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000814 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000815
816 if (!InPlt && Out<ELF64BE>::Opd) {
817 // If this is a local call, and we currently have the address of a
818 // function-descriptor, get the underlying code address instead.
819 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
820 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000821 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000822
823 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000824 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000825 }
826
Hal Finkel3c8cc672015-10-12 20:56:18 +0000827 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000828 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000829 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000830
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000831 uint32_t Nop = 0x60000000;
832 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
833 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000834 break;
835 }
836 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000837 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000838 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000839 break;
840 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000841 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000842 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000843 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000844 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000845 break;
846 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000847 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000848 }
849}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000850
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000851AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000852 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000853 PltReloc = R_AARCH64_JUMP_SLOT;
854 LazyRelocations = true;
855 PltEntrySize = 16;
856 PltZeroEntrySize = 32;
857}
George Rimar648a2c32015-10-20 08:54:27 +0000858
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000859unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
860
861void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
862 write64le(Buf, Out<ELF64LE>::Plt->getVA());
863}
864
George Rimar648a2c32015-10-20 08:54:27 +0000865void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000866 uint64_t PltEntryAddr) const {
867 const uint8_t PltData[] = {
868 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
869 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
870 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
871 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
872 0x20, 0x02, 0x1f, 0xd6, // br x17
873 0x1f, 0x20, 0x03, 0xd5, // nop
874 0x1f, 0x20, 0x03, 0xd5, // nop
875 0x1f, 0x20, 0x03, 0xd5 // nop
876 };
877 memcpy(Buf, PltData, sizeof(PltData));
878
879 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
880 GotEntryAddr + 16);
881 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
882 GotEntryAddr + 16);
883 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
884 GotEntryAddr + 16);
885}
886
George Rimar77b77792015-11-25 22:15:01 +0000887void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
888 uint64_t GotEntryAddr,
889 uint64_t PltEntryAddr, int32_t Index,
890 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000891 const uint8_t Inst[] = {
892 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
893 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
894 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
895 0x20, 0x02, 0x1f, 0xd6 // br x17
896 };
897 memcpy(Buf, Inst, sizeof(Inst));
898
899 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
900 GotEntryAddr);
901 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
902 GotEntryAddr);
903 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
904 GotEntryAddr);
905}
906
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000907bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
908 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000909 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
910 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000911}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000912
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000913bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
914 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000915 switch (Type) {
916 default:
917 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000918 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000919 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000920 return canBePreempted(&S, true);
921 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000922}
Davide Italiano1d750a62015-09-27 08:45:38 +0000923
Davide Italianoef4be6b2015-10-06 19:01:32 +0000924static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000925 uint32_t ImmLo = (Imm & 0x3) << 29;
926 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
927 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000928 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000929}
930
Davide Italiano318ca222015-10-02 22:13:51 +0000931// Page(Expr) is the page address of the expression Expr, defined
932// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000933// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000934static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000935 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000936}
937
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000938void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000939 uint32_t Type, uint64_t P, uint64_t SA,
940 uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000941 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000942 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000943 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000944 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000945 break;
946 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000947 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000948 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000949 break;
950 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000951 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000952 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000953 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +0000954 // This relocation stores 12 bits and there's no instruction
955 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000956 // of r_addend bitwise-or'ed Loc. This assumes that the addend
957 // bits in Loc are zero.
958 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000959 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000960 case R_AARCH64_ADR_GOT_PAGE: {
961 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
962 checkInt<33>(X, Type);
963 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
964 break;
965 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000966 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000967 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000968 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000969 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000970 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000971 }
972 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000973 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000974 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000975 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000976 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000977 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000978 case R_AARCH64_CALL26:
979 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +0000980 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000981 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +0000982 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
983 break;
984 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000985 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000986 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000987 or32le(Loc, (SA & 0xFF8) << 7);
988 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000989 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +0000990 or32le(Loc, (SA & 0xFFF) << 10);
991 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000992 case R_AARCH64_LDST32_ABS_LO12_NC:
993 or32le(Loc, (SA & 0xFFC) << 8);
994 break;
995 case R_AARCH64_LDST64_ABS_LO12_NC:
996 or32le(Loc, (SA & 0xFF8) << 7);
997 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000998 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000999 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001000 write16le(Loc, SA - P);
1001 break;
1002 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001003 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001004 write32le(Loc, SA - P);
1005 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001006 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001007 write64le(Loc, SA - P);
1008 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001009 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001010 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001011 }
1012}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001013
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001014template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001015 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001016 GotHeaderEntriesNum = 2;
1017}
1018
1019template <class ELFT>
1020void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
1021 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
1022 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1023 // Module pointer
1024 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001025}
1026
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001027template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001028void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1029template <class ELFT>
1030void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1031 uint64_t PltEntryAddr) const {}
1032template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001033void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1034 uint64_t GotEntryAddr,
1035 uint64_t PltEntryAddr, int32_t Index,
1036 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001037
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001038template <class ELFT>
1039bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1040 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001041 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001042}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001043
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001044template <class ELFT>
1045bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1046 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001047 return false;
1048}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001049
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001050template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001051void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001052 uint32_t Type, uint64_t P, uint64_t SA,
1053 uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001054 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001055 switch (Type) {
1056 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001057 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001058 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001059 case R_MIPS_HI16: {
1060 uint32_t Instr = read32<E>(Loc);
1061 if (PairedLoc) {
1062 uint64_t AHL = ((Instr & 0xffff) << 16) +
1063 llvm::SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1064 write32<E>(Loc, (Instr & 0xffff0000) | (((SA + AHL) >> 16) & 0xffff));
1065 } else {
1066 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
1067 write32<E>(Loc, (Instr & 0xffff0000) | ((SA >> 16) & 0xffff));
1068 }
1069 break;
1070 }
1071 case R_MIPS_LO16: {
1072 uint32_t Instr = read32<E>(Loc);
1073 int64_t AHL = llvm::SignExtend64<16>(Instr & 0xffff);
1074 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1075 break;
1076 }
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001077 case R_MIPS_CALL16:
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001078 case R_MIPS_GOT16: {
1079 int64_t V = SA - getMipsGpAddr<ELFT>();
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001080 if (Type == R_MIPS_GOT16)
1081 checkInt<16>(V, Type);
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001082 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001083 break;
1084 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001085 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001086 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001087 }
1088}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001089
1090template <class ELFT>
1091typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
1092 const unsigned GPOffset = 0x7ff0;
1093 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1094}
1095
1096template uint32_t getMipsGpAddr<ELF32LE>();
1097template uint32_t getMipsGpAddr<ELF32BE>();
1098template uint64_t getMipsGpAddr<ELF64LE>();
1099template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001100}
1101}