blob: e99b37e711c8691640ccc67fa58449cf06ff7d5a [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) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000318 case R_386_32:
319 add32le(Loc, SA);
320 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000321 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000322 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000323 break;
George Rimar13934772015-11-25 20:20:31 +0000324 case R_386_GOTPC:
325 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
326 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000327 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000328 add32le(Loc, SA - P);
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;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000465 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000466 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000467 case R_X86_64_PC8:
468 case R_X86_64_PC16:
469 case R_X86_64_PC32:
470 case R_X86_64_PC64:
471 case R_X86_64_PLT32:
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) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000571 case R_X86_64_DTPOFF32:
572 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
573 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000574 case R_X86_64_GOTTPOFF:
575 relocateTlsIeToLe(Loc, BufEnd, P, SA);
576 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000577 case R_X86_64_TLSGD:
578 relocateTlsGdToLe(Loc, BufEnd, P, SA);
579 // The next relocation should be against __tls_get_addr, so skip it
580 return 1;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000581 case R_X86_64_TLSLD:
582 relocateTlsLdToLe(Loc, BufEnd, P, SA);
583 // The next relocation should be against __tls_get_addr, so skip it
584 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000585 }
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) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000592 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000593 checkUInt<32>(SA, Type);
594 write32le(Loc, SA);
595 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000596 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000597 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000598 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000599 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000600 case R_X86_64_64:
601 write64le(Loc, SA);
602 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000603 case R_X86_64_DTPOFF32:
604 write32le(Loc, SA);
605 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000606 case R_X86_64_DTPOFF64:
607 write64le(Loc, SA);
608 break;
609 case R_X86_64_GOTPCREL:
610 case R_X86_64_PC32:
611 case R_X86_64_PLT32:
612 case R_X86_64_TLSGD:
613 case R_X86_64_TLSLD:
614 write32le(Loc, SA - P);
615 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000616 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000617 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000618 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000619 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000620 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000621 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000622 case R_X86_64_TPOFF64:
623 write32le(Loc, SA - P);
624 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000625 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000626 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000627 }
628}
629
Hal Finkel3c8cc672015-10-12 20:56:18 +0000630// Relocation masks following the #lo(value), #hi(value), #ha(value),
631// #higher(value), #highera(value), #highest(value), and #highesta(value)
632// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
633// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000634static uint16_t applyPPCLo(uint64_t V) { return V; }
635static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
636static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
637static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
638static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000639static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000640static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
641
Rafael Espindolac4010882015-09-22 20:54:08 +0000642PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000643 PCRelReloc = R_PPC64_REL24;
644 GotReloc = R_PPC64_GLOB_DAT;
645 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000646 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000647 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000648
649 // We need 64K pages (at least under glibc/Linux, the loader won't
650 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000651 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000652
653 // The PPC64 ELF ABI v1 spec, says:
654 //
655 // It is normally desirable to put segments with different characteristics
656 // in separate 256 Mbyte portions of the address space, to give the
657 // operating system full paging flexibility in the 64-bit address space.
658 //
659 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
660 // use 0x10000000 as the starting address.
661 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000662}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000663
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000664uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000665 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
666 // order. The TOC starts where the first of these sections starts.
667
668 // FIXME: This obviously does not do the right thing when there is no .got
669 // section, but there is a .toc or .tocbss section.
670 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
671 if (!TocVA)
672 TocVA = Out<ELF64BE>::Plt->getVA();
673
674 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
675 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
676 // code (crt1.o) assumes that you can get from the TOC base to the
677 // start of the .toc section with only a single (signed) 16-bit relocation.
678 return TocVA + 0x8000;
679}
680
George Rimar648a2c32015-10-20 08:54:27 +0000681void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
682void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
683 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000684void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
685 uint64_t GotEntryAddr,
686 uint64_t PltEntryAddr, int32_t Index,
687 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000688 uint64_t Off = GotEntryAddr - getPPC64TocBase();
689
690 // FIXME: What we should do, in theory, is get the offset of the function
691 // descriptor in the .opd section, and use that as the offset from %r2 (the
692 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
693 // be a pointer to the function descriptor in the .opd section. Using
694 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
695
Hal Finkelfa92f682015-10-13 21:47:34 +0000696 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000697 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
698 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
699 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
700 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
701 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
702 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
703 write32be(Buf + 28, 0x4e800420); // bctr
704}
705
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000706bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000707 if (relocNeedsPlt(Type, S))
708 return true;
709
710 switch (Type) {
711 default: return false;
712 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000713 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000714 case R_PPC64_GOT16_HA:
715 case R_PPC64_GOT16_HI:
716 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000717 case R_PPC64_GOT16_LO_DS:
718 return true;
719 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000720}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000721
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000722bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000723 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000724 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000725}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000726
Hal Finkelbe0823d2015-10-12 20:58:52 +0000727bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
728 switch (Type) {
729 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000730 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000731 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000732 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000733 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000734 }
735}
736
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000737void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
738 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000739 uint64_t TB = getPPC64TocBase();
740
Hal Finkel3c8cc672015-10-12 20:56:18 +0000741 // For a TOC-relative relocation, adjust the addend and proceed in terms of
742 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000743 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000744 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
745 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000746 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
747 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +0000748 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
749 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000750 default: break;
751 }
752
Hal Finkel3c8cc672015-10-12 20:56:18 +0000753 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000754 case R_PPC64_ADDR14: {
755 checkAlignment<4>(SA, Type);
756 // Preserve the AA/LK bits in the branch instruction
757 uint8_t AALK = Loc[3];
758 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
759 break;
760 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000761 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000762 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000763 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000764 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000765 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000766 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000767 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000768 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000769 case R_PPC64_ADDR16_HA:
770 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000771 break;
772 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000773 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000774 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000775 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000776 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000777 break;
778 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000779 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000780 break;
781 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000782 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000783 break;
784 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000785 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000786 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000787 case R_PPC64_ADDR16_LO:
788 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000789 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000790 case R_PPC64_ADDR16_LO_DS:
791 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000792 break;
793 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000794 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000795 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000796 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000797 case R_PPC64_ADDR64:
798 write64be(Loc, SA);
799 break;
800 case R_PPC64_REL16_HA:
801 write16be(Loc, applyPPCHa(SA - P));
802 break;
803 case R_PPC64_REL16_HI:
804 write16be(Loc, applyPPCHi(SA - P));
805 break;
806 case R_PPC64_REL16_LO:
807 write16be(Loc, applyPPCLo(SA - P));
808 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000809 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000810 // If we have an undefined weak symbol, we might get here with a symbol
811 // address of zero. That could overflow, but the code must be unreachable,
812 // so don't bother doing anything at all.
813 if (!SA)
814 break;
815
Hal Finkeldaedc122015-10-12 23:16:53 +0000816 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
817 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000818 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000819
820 if (!InPlt && Out<ELF64BE>::Opd) {
821 // If this is a local call, and we currently have the address of a
822 // function-descriptor, get the underlying code address instead.
823 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
824 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000825 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000826
827 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000828 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000829 }
830
Hal Finkel3c8cc672015-10-12 20:56:18 +0000831 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000832 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000833 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000834
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000835 uint32_t Nop = 0x60000000;
836 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
837 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000838 break;
839 }
840 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000841 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000842 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000843 break;
844 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000845 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000846 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000847 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000848 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000849 break;
850 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000851 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000852 }
853}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000854
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000855AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000856 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000857 PltReloc = R_AARCH64_JUMP_SLOT;
858 LazyRelocations = true;
859 PltEntrySize = 16;
860 PltZeroEntrySize = 32;
861}
George Rimar648a2c32015-10-20 08:54:27 +0000862
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000863unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; }
864
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000865unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
866
867void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
868 write64le(Buf, Out<ELF64LE>::Plt->getVA());
869}
870
George Rimar648a2c32015-10-20 08:54:27 +0000871void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000872 uint64_t PltEntryAddr) const {
873 const uint8_t PltData[] = {
874 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
875 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
876 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
877 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
878 0x20, 0x02, 0x1f, 0xd6, // br x17
879 0x1f, 0x20, 0x03, 0xd5, // nop
880 0x1f, 0x20, 0x03, 0xd5, // nop
881 0x1f, 0x20, 0x03, 0xd5 // nop
882 };
883 memcpy(Buf, PltData, sizeof(PltData));
884
885 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
886 GotEntryAddr + 16);
887 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
888 GotEntryAddr + 16);
889 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
890 GotEntryAddr + 16);
891}
892
George Rimar77b77792015-11-25 22:15:01 +0000893void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
894 uint64_t GotEntryAddr,
895 uint64_t PltEntryAddr, int32_t Index,
896 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000897 const uint8_t Inst[] = {
898 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
899 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
900 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
901 0x20, 0x02, 0x1f, 0xd6 // br x17
902 };
903 memcpy(Buf, Inst, sizeof(Inst));
904
905 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
906 GotEntryAddr);
907 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
908 GotEntryAddr);
909 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
910 GotEntryAddr);
911}
912
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000913bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
914 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000915 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
916 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000917}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000918
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000919bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
920 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000921 switch (Type) {
922 default:
923 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000924 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000925 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000926 return canBePreempted(&S, true);
927 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000928}
Davide Italiano1d750a62015-09-27 08:45:38 +0000929
Davide Italianoef4be6b2015-10-06 19:01:32 +0000930static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000931 uint32_t ImmLo = (Imm & 0x3) << 29;
932 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
933 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000934 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000935}
936
Davide Italiano318ca222015-10-02 22:13:51 +0000937// Page(Expr) is the page address of the expression Expr, defined
938// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000939// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000940static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000941 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000942}
943
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000944void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
945 uint32_t Type, uint64_t P,
946 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000947 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000948 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000949 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000950 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000951 break;
952 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +0000953 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000954 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000955 break;
956 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000957 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000958 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000959 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +0000960 // This relocation stores 12 bits and there's no instruction
961 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000962 // of r_addend bitwise-or'ed Loc. This assumes that the addend
963 // bits in Loc are zero.
964 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000965 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000966 case R_AARCH64_ADR_GOT_PAGE: {
967 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
968 checkInt<33>(X, Type);
969 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
970 break;
971 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000972 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000973 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000974 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000975 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000976 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000977 }
978 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000979 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000980 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000981 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000982 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000983 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000984 case R_AARCH64_CALL26:
985 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +0000986 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000987 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +0000988 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
989 break;
990 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000991 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000992 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000993 or32le(Loc, (SA & 0xFF8) << 7);
994 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000995 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +0000996 or32le(Loc, (SA & 0xFFF) << 10);
997 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000998 case R_AARCH64_LDST32_ABS_LO12_NC:
999 or32le(Loc, (SA & 0xFFC) << 8);
1000 break;
1001 case R_AARCH64_LDST64_ABS_LO12_NC:
1002 or32le(Loc, (SA & 0xFF8) << 7);
1003 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001004 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001005 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001006 write16le(Loc, SA - P);
1007 break;
1008 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001009 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001010 write32le(Loc, SA - P);
1011 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001012 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001013 write64le(Loc, SA - P);
1014 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001015 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001016 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001017 }
1018}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001019
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001020template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001021 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001022 GotRefReloc = R_MIPS_GOT16;
1023 GotHeaderEntriesNum = 2;
1024}
1025
1026template <class ELFT>
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001027unsigned MipsTargetInfo<ELFT>::getGotRefReloc(unsigned Type) const {
1028 return Type;
1029}
1030
1031template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001032void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
1033 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
1034 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1035 // Module pointer
1036 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001037}
1038
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001039template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001040void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1041template <class ELFT>
1042void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1043 uint64_t PltEntryAddr) const {}
1044template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001045void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1046 uint64_t GotEntryAddr,
1047 uint64_t PltEntryAddr, int32_t Index,
1048 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001049
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001050template <class ELFT>
1051bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1052 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001053 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001054}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001055
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001056template <class ELFT>
1057bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1058 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001059 return false;
1060}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001061
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001062template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001063void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
1064 uint32_t Type, uint64_t P,
1065 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001066 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001067 switch (Type) {
1068 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001069 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001070 break;
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001071 case R_MIPS_CALL16:
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001072 case R_MIPS_GOT16: {
1073 int64_t V = SA - getMipsGpAddr<ELFT>();
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001074 if (Type == R_MIPS_GOT16)
1075 checkInt<16>(V, Type);
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001076 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001077 break;
1078 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001079 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001080 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001081 }
1082}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001083
1084template <class ELFT>
1085typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
1086 const unsigned GPOffset = 0x7ff0;
1087 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1088}
1089
1090template uint32_t getMipsGpAddr<ELF32LE>();
1091template uint32_t getMipsGpAddr<ELF32BE>();
1092template uint64_t getMipsGpAddr<ELF64LE>();
1093template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001094}
1095}