blob: 3a649029eebb6fb6bb114aceeda6b23f95bf43c6 [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 Kudrin5d2bffd2015-11-24 06:48:31 +0000143 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000144 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000145 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
146 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
147 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000148 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
149 uint64_t PltEntryAddr, int32_t Index,
150 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000151 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
152 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000153 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000154 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155};
156
157template <class ELFT> class MipsTargetInfo final : public TargetInfo {
158public:
159 MipsTargetInfo();
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000160 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000161 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000162 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
163 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
164 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000165 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
166 uint64_t PltEntryAddr, int32_t Index,
167 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000168 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
169 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000170 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000171 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172};
173} // anonymous namespace
174
Rui Ueyama91004392015-10-13 16:08:15 +0000175TargetInfo *createTarget() {
176 switch (Config->EMachine) {
177 case EM_386:
178 return new X86TargetInfo();
179 case EM_AARCH64:
180 return new AArch64TargetInfo();
181 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000182 switch (Config->EKind) {
183 case ELF32LEKind:
184 return new MipsTargetInfo<ELF32LE>();
185 case ELF32BEKind:
186 return new MipsTargetInfo<ELF32BE>();
187 default:
188 error("Unsupported MIPS target");
189 }
Rui Ueyama91004392015-10-13 16:08:15 +0000190 case EM_PPC64:
191 return new PPC64TargetInfo();
192 case EM_X86_64:
193 return new X86_64TargetInfo();
194 }
195 error("Unknown target machine");
196}
197
Rafael Espindola01205f72015-09-22 18:19:46 +0000198TargetInfo::~TargetInfo() {}
199
George Rimar6713cf82015-11-25 21:46:05 +0000200bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000201 return false;
202}
203
Igor Kudrinf6f45472015-11-10 08:39:27 +0000204uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
205
George Rimarbc590fe2015-10-28 16:48:58 +0000206bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
207 return false;
208}
209
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000210unsigned TargetInfo::getGotRefReloc(unsigned Type) const { return GotRefReloc; }
211
Igor Kudrine7ad0932015-11-17 17:47:53 +0000212unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000213
Rafael Espindolaae244002015-10-05 19:30:12 +0000214bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
215
George Rimar6713cf82015-11-25 21:46:05 +0000216unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
217 uint32_t Type, uint64_t P,
218 uint64_t SA) const {
219 return 0;
220}
George Rimar77d1cb12015-11-24 09:00:06 +0000221
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000222void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
223
Igor Kudrin351b41d2015-11-16 17:44:08 +0000224void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
225
Rafael Espindola7f074422015-09-22 21:35:51 +0000226X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000227 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000228 PCRelReloc = R_386_PC32;
229 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000230 GotRefReloc = R_386_GOT32;
George Rimar648a2c32015-10-20 08:54:27 +0000231 PltReloc = R_386_JUMP_SLOT;
George Rimar77b77792015-11-25 22:15:01 +0000232 LazyRelocations = true;
233 PltEntrySize = 16;
234 PltZeroEntrySize = 16;
235}
236
237void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
238 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
239}
240
241void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
242 // Skip 6 bytes of "pushl (GOT+4)"
243 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000244}
Rafael Espindola01205f72015-09-22 18:19:46 +0000245
George Rimard23970f2015-11-25 20:41:53 +0000246unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
247 if (Type == R_386_TLS_LE)
248 return R_386_TLS_TPOFF;
249 if (Type == R_386_TLS_LE_32)
250 return R_386_TLS_TPOFF32;
251 return Type;
252}
253
254bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
255 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32)
256 return Config->Shared;
257 return false;
258}
259
George Rimar648a2c32015-10-20 08:54:27 +0000260void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000261 uint64_t PltEntryAddr) const {
262 // Executable files and shared object files have
263 // separate procedure linkage tables.
264 if (Config->Shared) {
265 const uint8_t V[] = {
266 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
267 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
268 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
269 };
270 memcpy(Buf, V, sizeof(V));
271 return;
272 }
George Rimar648a2c32015-10-20 08:54:27 +0000273
George Rimar77b77792015-11-25 22:15:01 +0000274 const uint8_t PltData[] = {
275 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
276 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
277 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
278 };
279 memcpy(Buf, PltData, sizeof(PltData));
280 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
281 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
282}
283
284void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
285 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
286 int32_t Index, unsigned RelOff) const {
287 const uint8_t Inst[] = {
288 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
289 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
290 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
291 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000292 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000293 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
294 Buf[1] = Config->Shared ? 0xa3 : 0x25;
295 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
296 write32le(Buf + 7, RelOff);
297 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000298}
299
George Rimar70e25082015-11-25 11:27:40 +0000300bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
301 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
302 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
303 return SS->Sym.getType() == STT_OBJECT;
304 return false;
305}
306
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000307bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000308 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000309}
310
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000311bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000312 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000313}
314
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000315void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
316 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000317 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000318 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000319 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000320 break;
George Rimar13934772015-11-25 20:20:31 +0000321 case R_386_GOTPC:
322 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
323 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000324 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000325 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000326 break;
327 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000328 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000329 break;
George Rimard23970f2015-11-25 20:41:53 +0000330 case R_386_TLS_LE:
331 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
332 break;
333 case R_386_TLS_LE_32:
334 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
335 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000336 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000337 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000338 }
339}
340
Rafael Espindola7f074422015-09-22 21:35:51 +0000341X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000342 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000343 PCRelReloc = R_X86_64_PC32;
344 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000345 GotRefReloc = R_X86_64_PC32;
George Rimar648a2c32015-10-20 08:54:27 +0000346 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000347 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000348 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000349 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000350 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000351 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000352 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000353 LazyRelocations = true;
354 PltEntrySize = 16;
355 PltZeroEntrySize = 16;
356}
357
Igor Kudrin351b41d2015-11-16 17:44:08 +0000358void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
359 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
360}
361
George Rimar648a2c32015-10-20 08:54:27 +0000362void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
363 // Skip 6 bytes of "jmpq *got(%rip)"
364 write32le(Buf, Plt + 6);
365}
366
367void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
368 uint64_t PltEntryAddr) const {
369 const uint8_t PltData[] = {
370 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
371 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
372 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
373 };
374 memcpy(Buf, PltData, sizeof(PltData));
375 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
376 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000377}
Rafael Espindola01205f72015-09-22 18:19:46 +0000378
George Rimar77b77792015-11-25 22:15:01 +0000379void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
380 uint64_t GotEntryAddr,
381 uint64_t PltEntryAddr, int32_t Index,
382 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000383 const uint8_t Inst[] = {
384 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
385 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
386 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
387 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000388 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000389
George Rimar648a2c32015-10-20 08:54:27 +0000390 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
391 write32le(Buf + 7, Index);
392 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000393}
394
George Rimarbc590fe2015-10-28 16:48:58 +0000395bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
396 const SymbolBody &S) const {
397 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
398 Type == R_X86_64_64)
399 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
400 return SS->Sym.getType() == STT_OBJECT;
401 return false;
402}
403
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000404bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000405 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000406 return !isTlsOptimized(Type, &S);
George Rimar687138c2015-11-13 16:28:53 +0000407 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
408 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000409}
410
George Rimard23970f2015-11-25 20:41:53 +0000411bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
412 return Type == R_X86_64_GOTTPOFF;
413}
414
Igor Kudrine7ad0932015-11-17 17:47:53 +0000415unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000416 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000417 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000418 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000419}
420
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000421bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000422 if (relocNeedsCopy(Type, S))
423 return false;
424
Rafael Espindola01205f72015-09-22 18:19:46 +0000425 switch (Type) {
426 default:
427 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000428 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000429 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000430 case R_X86_64_PC32:
431 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000432 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000433 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000434 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000435 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000436 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
437 // libc.so.
438 //
439 // The general idea on how to handle such cases is to create a PLT entry
440 // and use that as the function value.
441 //
442 // For the static linking part, we just return true and everything else
443 // will use the the PLT entry as the address.
444 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000445 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000446 // still works. We need the help of the dynamic linker for that. We
447 // let it know that we have a direct reference to a so symbol by creating
448 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000449 // dynamic linker resolves the symbol to the value of the symbol we created.
450 // This is true even for got entries, so pointer equality is maintained.
451 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000452 // real function is a dedicated got entry used by the plt. That is
453 // identified by special relocation types (R_X86_64_JUMP_SLOT,
454 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000455 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000456 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000457 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000458 }
459}
Rafael Espindolac4010882015-09-22 20:54:08 +0000460
Rafael Espindolaae244002015-10-05 19:30:12 +0000461bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
462 switch (Type) {
463 default:
464 return false;
465 case R_X86_64_PC64:
466 case R_X86_64_PC32:
467 case R_X86_64_PC16:
468 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000469 case R_X86_64_PLT32:
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000470 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000471 case R_X86_64_DTPOFF64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000472 return true;
473 }
474}
475
George Rimar77d1cb12015-11-24 09:00:06 +0000476bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000477 const SymbolBody *S) const {
478 if (Config->Shared || (S && !S->isTLS()))
George Rimar77d1cb12015-11-24 09:00:06 +0000479 return false;
George Rimar6713cf82015-11-25 21:46:05 +0000480 return Type == R_X86_64_TLSLD || Type == R_X86_64_DTPOFF32 ||
481 (Type == R_X86_64_TLSGD && !canBePreempted(S, true)) ||
482 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
483}
484
485// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
486// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
487// how LD can be optimized to LE:
488// leaq bar@tlsld(%rip), %rdi
489// callq __tls_get_addr@PLT
490// leaq bar@dtpoff(%rax), %rcx
491// Is converted to:
492// .word 0x6666
493// .byte 0x66
494// mov %fs:0,%rax
495// leaq bar@tpoff(%rax), %rcx
496void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
497 uint64_t P, uint64_t SA) const {
498 const uint8_t Inst[] = {
499 0x66, 0x66, //.word 0x6666
500 0x66, //.byte 0x66
501 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
502 };
503 memcpy(Loc - 3, Inst, sizeof(Inst));
504}
505
506// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
507// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
508// how GD can be optimized to LE:
509// .byte 0x66
510// leaq x@tlsgd(%rip), %rdi
511// .word 0x6666
512// rex64
513// call __tls_get_addr@plt
514// Is converted to:
515// mov %fs:0x0,%rax
516// lea x@tpoff,%rax
517void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
518 uint64_t P, uint64_t SA) const {
519 const uint8_t Inst[] = {
520 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
521 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
522 };
523 memcpy(Loc - 4, Inst, sizeof(Inst));
524 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000525}
526
527// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
528// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
529// This function does that. Read "ELF Handling For Thread-Local Storage,
530// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
531// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000532void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
533 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000534 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
535 // used in MOVQ or ADDQ instructions only.
536 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
537 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
538 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
539 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
540 uint8_t *Prefix = Loc - 3;
541 uint8_t *Inst = Loc - 2;
542 uint8_t *RegSlot = Loc - 1;
543 uint8_t Reg = Loc[-1] >> 3;
544 bool IsMov = *Inst == 0x8b;
545 bool RspAdd = !IsMov && Reg == 4;
546 // r12 and rsp registers requires special handling.
547 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
548 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
549 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
550 // The same true for rsp. So we convert to addq for them, saving 1 byte that
551 // we dont have.
552 if (RspAdd)
553 *Inst = 0x81;
554 else
555 *Inst = IsMov ? 0xc7 : 0x8d;
556 if (*Prefix == 0x4c)
557 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
558 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
559 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
560}
561
George Rimar6713cf82015-11-25 21:46:05 +0000562// This function applies a TLS relocation with an optimization as described
563// in the Ulrich's document. As a result of rewriting instructions at the
564// relocation target, relocations immediately follow the TLS relocation (which
565// would be applied to rewritten instructions) may have to be skipped.
566// This function returns a number of relocations that need to be skipped.
567unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
568 uint32_t Type, uint64_t P,
569 uint64_t SA) const {
570 switch (Type) {
571 case R_X86_64_GOTTPOFF:
572 relocateTlsIeToLe(Loc, BufEnd, P, SA);
573 return 0;
574 case R_X86_64_TLSLD:
575 relocateTlsLdToLe(Loc, BufEnd, P, SA);
576 // The next relocation should be against __tls_get_addr, so skip it
577 return 1;
578 case R_X86_64_TLSGD:
579 relocateTlsGdToLe(Loc, BufEnd, P, SA);
580 // The next relocation should be against __tls_get_addr, so skip it
581 return 1;
582 case R_X86_64_DTPOFF32:
583 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
584 return 0;
585 }
586 llvm_unreachable("Unknown TLS optimization");
587}
588
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000589void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
590 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000591 switch (Type) {
592 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000593 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000594 case R_X86_64_PLT32:
Michael J. Spencer1e225612015-11-11 01:00:24 +0000595 case R_X86_64_TLSLD:
Michael J. Spencer627ae702015-11-13 00:28:34 +0000596 case R_X86_64_TLSGD:
George Rimar687138c2015-11-13 16:28:53 +0000597 case R_X86_64_TPOFF64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000598 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000599 break;
600 case R_X86_64_64:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000601 case R_X86_64_DTPOFF64:
Rui Ueyama66072272015-10-15 19:52:27 +0000602 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000603 break;
Rui Ueyama3835b492015-10-23 16:13:27 +0000604 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000605 checkUInt<32>(SA, Type);
606 write32le(Loc, SA);
607 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000608 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000609 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000610 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000611 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000612 case R_X86_64_DTPOFF32:
613 write32le(Loc, SA);
614 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000615 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000616 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000617 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000618 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000619 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000620 }
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;
641 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000642 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000643 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000644
645 // We need 64K pages (at least under glibc/Linux, the loader won't
646 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000647 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000648
649 // The PPC64 ELF ABI v1 spec, says:
650 //
651 // It is normally desirable to put segments with different characteristics
652 // in separate 256 Mbyte portions of the address space, to give the
653 // operating system full paging flexibility in the 64-bit address space.
654 //
655 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
656 // use 0x10000000 as the starting address.
657 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000658}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000659
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000660uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000661 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
662 // order. The TOC starts where the first of these sections starts.
663
664 // FIXME: This obviously does not do the right thing when there is no .got
665 // section, but there is a .toc or .tocbss section.
666 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
667 if (!TocVA)
668 TocVA = Out<ELF64BE>::Plt->getVA();
669
670 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
671 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
672 // code (crt1.o) assumes that you can get from the TOC base to the
673 // start of the .toc section with only a single (signed) 16-bit relocation.
674 return TocVA + 0x8000;
675}
676
George Rimar648a2c32015-10-20 08:54:27 +0000677void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
678void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
679 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000680void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
681 uint64_t GotEntryAddr,
682 uint64_t PltEntryAddr, int32_t Index,
683 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000684 uint64_t Off = GotEntryAddr - getPPC64TocBase();
685
686 // FIXME: What we should do, in theory, is get the offset of the function
687 // descriptor in the .opd section, and use that as the offset from %r2 (the
688 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
689 // be a pointer to the function descriptor in the .opd section. Using
690 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
691
Hal Finkelfa92f682015-10-13 21:47:34 +0000692 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000693 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
694 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
695 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
696 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
697 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
698 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
699 write32be(Buf + 28, 0x4e800420); // bctr
700}
701
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000702bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000703 if (relocNeedsPlt(Type, S))
704 return true;
705
706 switch (Type) {
707 default: return false;
708 case R_PPC64_GOT16:
709 case R_PPC64_GOT16_LO:
710 case R_PPC64_GOT16_HI:
711 case R_PPC64_GOT16_HA:
712 case R_PPC64_GOT16_DS:
713 case R_PPC64_GOT16_LO_DS:
714 return true;
715 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000716}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000717
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000718bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000719 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000720 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000721}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000722
Hal Finkelbe0823d2015-10-12 20:58:52 +0000723bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
724 switch (Type) {
725 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000726 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000727 case R_PPC64_TOC:
728 case R_PPC64_ADDR64:
729 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000730 }
731}
732
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000733void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
734 uint64_t P, uint64_t SA) 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;
742 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
743 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
744 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
745 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; 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) {
750 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000751 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000752 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000753 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000754 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000755 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000756 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000757 break;
758 case R_PPC64_ADDR16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000759 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000760 break;
761 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000762 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000763 break;
764 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000765 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000766 break;
767 case R_PPC64_ADDR16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000768 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000769 break;
770 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000771 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000772 break;
773 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000774 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000775 break;
776 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000777 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000778 break;
779 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000780 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000781 break;
782 case R_PPC64_ADDR14: {
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000783 checkAlignment<4>(SA, Type);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000784 // Preserve the AA/LK bits in the branch instruction
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000785 uint8_t AALK = Loc[3];
786 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000787 break;
788 }
789 case R_PPC64_REL16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000790 write16be(Loc, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000791 break;
792 case R_PPC64_REL16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000793 write16be(Loc, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000794 break;
795 case R_PPC64_REL16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000796 write16be(Loc, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000797 break;
798 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000799 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000800 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000801 break;
802 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;
840 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000841 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000842 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000843 break;
844 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000845 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000846 }
847}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000848
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000849AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000850 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000851 PltReloc = R_AARCH64_JUMP_SLOT;
852 LazyRelocations = true;
853 PltEntrySize = 16;
854 PltZeroEntrySize = 32;
855}
George Rimar648a2c32015-10-20 08:54:27 +0000856
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000857unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; }
858
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;
918 case R_AARCH64_JUMP26:
919 case R_AARCH64_CALL26:
920 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,
939 uint32_t Type, uint64_t P,
940 uint64_t SA) 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 Ueyamaee8c53b2015-10-06 19:57:01 +0000951 // No overflow check needed.
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000952 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000953 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000954 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000955 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000956 // This relocation stores 12 bits and there's no instruction
957 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000958 // of r_addend bitwise-or'ed Loc. This assumes that the addend
959 // bits in Loc are zero.
960 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000961 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000962 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000963 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000964 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000965 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000966 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000967 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000968 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000969 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 Kudrinb34115b2015-11-13 03:26:59 +0000975 case R_AARCH64_JUMP26:
976 case R_AARCH64_CALL26: {
977 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 }
Davide Italianodc67f9b2015-11-20 21:35:38 +0000982 case R_AARCH64_LDST32_ABS_LO12_NC:
983 // No overflow check needed.
984 or32le(Loc, (SA & 0xFFC) << 8);
985 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000986 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000987 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000988 // No overflow check needed.
989 or32le(Loc, (SA & 0xFF8) << 7);
990 break;
Davide Italianobbcc7f62015-11-08 04:45:26 +0000991 case R_AARCH64_LDST64_ABS_LO12_NC:
992 // No overflow check needed.
993 or32le(Loc, (SA & 0xFF8) << 7);
994 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000995 case R_AARCH64_LDST8_ABS_LO12_NC:
996 // No overflow check needed.
997 or32le(Loc, (SA & 0xFFF) << 10);
998 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000999 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001000 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001001 write16le(Loc, SA - P);
1002 break;
1003 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001004 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001005 write32le(Loc, SA - P);
1006 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001007 case R_AARCH64_PREL64:
1008 // No overflow check needed.
1009 write64le(Loc, SA - P);
1010 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001011 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001012 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001013 }
1014}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001015
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001016template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001017 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001018 GotRefReloc = R_MIPS_GOT16;
1019 GotHeaderEntriesNum = 2;
1020}
1021
1022template <class ELFT>
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001023unsigned MipsTargetInfo<ELFT>::getGotRefReloc(unsigned Type) const {
1024 return Type;
1025}
1026
1027template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001028void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
1029 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
1030 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1031 // Module pointer
1032 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001033}
1034
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001035template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001036void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1037template <class ELFT>
1038void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1039 uint64_t PltEntryAddr) const {}
1040template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001041void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1042 uint64_t GotEntryAddr,
1043 uint64_t PltEntryAddr, int32_t Index,
1044 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001045
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001046template <class ELFT>
1047bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1048 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001049 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001050}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001051
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001052template <class ELFT>
1053bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1054 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001055 return false;
1056}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001057
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001058template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001059void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
1060 uint32_t Type, uint64_t P,
1061 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001062 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001063 switch (Type) {
1064 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001065 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001066 break;
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001067 case R_MIPS_CALL16:
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001068 case R_MIPS_GOT16: {
1069 int64_t V = SA - getMipsGpAddr<ELFT>();
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001070 if (Type == R_MIPS_GOT16)
1071 checkInt<16>(V, Type);
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001072 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001073 break;
1074 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001075 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001076 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001077 }
1078}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001079
1080template <class ELFT>
1081typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
1082 const unsigned GPOffset = 0x7ff0;
1083 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1084}
1085
1086template uint32_t getMipsGpAddr<ELF32LE>();
1087template uint32_t getMipsGpAddr<ELF32BE>();
1088template uint64_t getMipsGpAddr<ELF64LE>();
1089template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001090}
1091}