blob: df2e7e6fcea935af0a8f1b7ddc8d6caa168e33fd [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
45namespace {
46class X86TargetInfo final : public TargetInfo {
47public:
48 X86TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000049 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
50 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
51 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000052 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000053 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000054 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
55 bool relocPointsToGot(uint32_t Type) const override;
56 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000057 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000058 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000059};
60
61class X86_64TargetInfo final : public TargetInfo {
62public:
63 X86_64TargetInfo();
Igor Kudrine7ad0932015-11-17 17:47:53 +000064 unsigned getPltRefReloc(unsigned Type) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +000065 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +000066 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
67 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
68 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000069 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000070 uint64_t PltEntryAddr, int32_t Index) const override;
George Rimarbc590fe2015-10-28 16:48:58 +000071 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000072 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
73 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000074 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000075 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000076 bool isRelRelative(uint32_t Type) const override;
George Rimar77d1cb12015-11-24 09:00:06 +000077 bool isTlsOptimized(unsigned Type, const SymbolBody &S) const override;
78 void relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
79 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000080};
81
82class PPC64TargetInfo final : public TargetInfo {
83public:
84 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000085 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
86 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
87 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000088 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000089 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000090 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
91 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000092 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000093 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000094 bool isRelRelative(uint32_t Type) const override;
95};
96
Rui Ueyamaefc23de2015-10-14 21:30:32 +000097class AArch64TargetInfo final : public TargetInfo {
98public:
99 AArch64TargetInfo();
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000100 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000101 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000102 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
103 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
104 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000105 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000106 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000107 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
108 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000109 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000110 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000111};
112
113template <class ELFT> class MipsTargetInfo final : public TargetInfo {
114public:
115 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000116 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000117 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
118 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
119 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000120 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000121 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000122 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
123 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000124 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000125 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000126};
127} // anonymous namespace
128
Rui Ueyama91004392015-10-13 16:08:15 +0000129TargetInfo *createTarget() {
130 switch (Config->EMachine) {
131 case EM_386:
132 return new X86TargetInfo();
133 case EM_AARCH64:
134 return new AArch64TargetInfo();
135 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000136 switch (Config->EKind) {
137 case ELF32LEKind:
138 return new MipsTargetInfo<ELF32LE>();
139 case ELF32BEKind:
140 return new MipsTargetInfo<ELF32BE>();
141 default:
142 error("Unsupported MIPS target");
143 }
Rui Ueyama91004392015-10-13 16:08:15 +0000144 case EM_PPC64:
145 return new PPC64TargetInfo();
146 case EM_X86_64:
147 return new X86_64TargetInfo();
148 }
149 error("Unknown target machine");
150}
151
Rafael Espindola01205f72015-09-22 18:19:46 +0000152TargetInfo::~TargetInfo() {}
153
George Rimar77d1cb12015-11-24 09:00:06 +0000154bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody &S) const {
155 return false;
156}
157
Igor Kudrinf6f45472015-11-10 08:39:27 +0000158uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
159
George Rimarbc590fe2015-10-28 16:48:58 +0000160bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
161 return false;
162}
163
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000164unsigned TargetInfo::getGotRefReloc(unsigned Type) const { return GotRefReloc; }
165
Igor Kudrine7ad0932015-11-17 17:47:53 +0000166unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000167
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000168bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
169
Rafael Espindolaae244002015-10-05 19:30:12 +0000170bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
171
George Rimar77d1cb12015-11-24 09:00:06 +0000172void TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
173 uint64_t SA) const {}
174
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000175void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
176
Igor Kudrin351b41d2015-11-16 17:44:08 +0000177void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
178
Rafael Espindola7f074422015-09-22 21:35:51 +0000179X86TargetInfo::X86TargetInfo() {
180 PCRelReloc = R_386_PC32;
181 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000182 GotRefReloc = R_386_GOT32;
George Rimar648a2c32015-10-20 08:54:27 +0000183 PltReloc = R_386_JUMP_SLOT;
Rafael Espindola7f074422015-09-22 21:35:51 +0000184}
Rafael Espindola01205f72015-09-22 18:19:46 +0000185
George Rimar648a2c32015-10-20 08:54:27 +0000186void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
187void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
188 uint64_t PltEntryAddr) const {}
189
Rafael Espindola01205f72015-09-22 18:19:46 +0000190void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000191 uint64_t PltEntryAddr, int32_t Index) const {
Rui Ueyamac58656c2015-10-13 16:59:30 +0000192 // jmpl *val; nop; nop
193 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000194 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +0000195 assert(isUInt<32>(GotEntryAddr));
196 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +0000197}
198
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000199bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000200 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000201}
202
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000203bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
204 return Type == R_386_GOTPC;
205}
206
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000207bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000208 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000209}
210
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000211void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
212 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000213 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000214 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000215 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000216 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000217 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000218 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000219 break;
220 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000221 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000222 break;
223 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000224 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000225 }
226}
227
Rafael Espindola7f074422015-09-22 21:35:51 +0000228X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000229 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000230 PCRelReloc = R_X86_64_PC32;
231 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000232 GotRefReloc = R_X86_64_PC32;
George Rimar648a2c32015-10-20 08:54:27 +0000233 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000234 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000235 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000236 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000237 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000238 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000239 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar687138c2015-11-13 16:28:53 +0000240 TlsPcRelGotReloc = R_X86_64_GOTTPOFF;
George Rimar648a2c32015-10-20 08:54:27 +0000241 LazyRelocations = true;
242 PltEntrySize = 16;
243 PltZeroEntrySize = 16;
244}
245
Igor Kudrin351b41d2015-11-16 17:44:08 +0000246void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
247 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
248}
249
George Rimar648a2c32015-10-20 08:54:27 +0000250void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
251 // Skip 6 bytes of "jmpq *got(%rip)"
252 write32le(Buf, Plt + 6);
253}
254
255void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
256 uint64_t PltEntryAddr) const {
257 const uint8_t PltData[] = {
258 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
259 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
260 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
261 };
262 memcpy(Buf, PltData, sizeof(PltData));
263 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
264 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000265}
Rafael Espindola01205f72015-09-22 18:19:46 +0000266
267void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000268 uint64_t PltEntryAddr,
269 int32_t Index) const {
270 const uint8_t Inst[] = {
271 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
272 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
273 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
274 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000275 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000276
George Rimar648a2c32015-10-20 08:54:27 +0000277 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
278 write32le(Buf + 7, Index);
279 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000280}
281
George Rimarbc590fe2015-10-28 16:48:58 +0000282bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
283 const SymbolBody &S) const {
284 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
285 Type == R_X86_64_64)
286 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
287 return SS->Sym.getType() == STT_OBJECT;
288 return false;
289}
290
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000291bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000292 if (Type == R_X86_64_GOTTPOFF)
293 return !isTlsOptimized(Type, S);
George Rimar687138c2015-11-13 16:28:53 +0000294 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
295 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000296}
297
Igor Kudrine7ad0932015-11-17 17:47:53 +0000298unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000299 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000300 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000301 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000302}
303
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000304bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000305 if (relocNeedsCopy(Type, S))
306 return false;
307
Rafael Espindola01205f72015-09-22 18:19:46 +0000308 switch (Type) {
309 default:
310 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000311 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000312 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000313 case R_X86_64_PC32:
314 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000315 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000316 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000317 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000318 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000319 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
320 // libc.so.
321 //
322 // The general idea on how to handle such cases is to create a PLT entry
323 // and use that as the function value.
324 //
325 // For the static linking part, we just return true and everything else
326 // will use the the PLT entry as the address.
327 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000328 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000329 // still works. We need the help of the dynamic linker for that. We
330 // let it know that we have a direct reference to a so symbol by creating
331 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000332 // dynamic linker resolves the symbol to the value of the symbol we created.
333 // This is true even for got entries, so pointer equality is maintained.
334 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000335 // real function is a dedicated got entry used by the plt. That is
336 // identified by special relocation types (R_X86_64_JUMP_SLOT,
337 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000338 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000339 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000340 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000341 }
342}
Rafael Espindolac4010882015-09-22 20:54:08 +0000343
Rafael Espindolaae244002015-10-05 19:30:12 +0000344bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
345 switch (Type) {
346 default:
347 return false;
348 case R_X86_64_PC64:
349 case R_X86_64_PC32:
350 case R_X86_64_PC16:
351 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000352 case R_X86_64_PLT32:
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000353 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000354 case R_X86_64_DTPOFF64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000355 return true;
356 }
357}
358
George Rimar77d1cb12015-11-24 09:00:06 +0000359bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
360 const SymbolBody &S) const {
361 if (Config->Shared || !S.isTLS())
362 return false;
363 return Type == R_X86_64_GOTTPOFF && !canBePreempted(&S, true);
364}
365
366// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
367// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
368// This function does that. Read "ELF Handling For Thread-Local Storage,
369// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
370// by Ulrich Drepper for details.
371void X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
372 uint64_t P, uint64_t SA) const {
373 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
374 // used in MOVQ or ADDQ instructions only.
375 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
376 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
377 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
378 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
379 uint8_t *Prefix = Loc - 3;
380 uint8_t *Inst = Loc - 2;
381 uint8_t *RegSlot = Loc - 1;
382 uint8_t Reg = Loc[-1] >> 3;
383 bool IsMov = *Inst == 0x8b;
384 bool RspAdd = !IsMov && Reg == 4;
385 // r12 and rsp registers requires special handling.
386 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
387 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
388 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
389 // The same true for rsp. So we convert to addq for them, saving 1 byte that
390 // we dont have.
391 if (RspAdd)
392 *Inst = 0x81;
393 else
394 *Inst = IsMov ? 0xc7 : 0x8d;
395 if (*Prefix == 0x4c)
396 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
397 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
398 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
399}
400
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000401void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
402 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000403 switch (Type) {
404 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000405 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000406 case R_X86_64_PLT32:
Michael J. Spencer1e225612015-11-11 01:00:24 +0000407 case R_X86_64_TLSLD:
Michael J. Spencer627ae702015-11-13 00:28:34 +0000408 case R_X86_64_TLSGD:
George Rimar687138c2015-11-13 16:28:53 +0000409 case R_X86_64_TPOFF64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000410 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000411 break;
412 case R_X86_64_64:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000413 case R_X86_64_DTPOFF64:
Rui Ueyama66072272015-10-15 19:52:27 +0000414 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000415 break;
Rui Ueyama3835b492015-10-23 16:13:27 +0000416 case R_X86_64_32:
Rafael Espindolac4010882015-09-22 20:54:08 +0000417 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000418 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000419 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000420 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000421 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000422 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000423 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000424 case R_X86_64_DTPOFF32:
425 write32le(Loc, SA);
426 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000427 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000428 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000429 if (!isInt<32>(Val))
430 error("R_X86_64_TPOFF32 out of range");
431 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000432 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000433 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000434 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000435 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000436 }
437}
438
Hal Finkel3c8cc672015-10-12 20:56:18 +0000439// Relocation masks following the #lo(value), #hi(value), #ha(value),
440// #higher(value), #highera(value), #highest(value), and #highesta(value)
441// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
442// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000443static uint16_t applyPPCLo(uint64_t V) { return V; }
444static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
445static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
446static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
447static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000448static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000449static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
450
Rafael Espindolac4010882015-09-22 20:54:08 +0000451PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000452 PCRelReloc = R_PPC64_REL24;
453 GotReloc = R_PPC64_GLOB_DAT;
454 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000455 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000456 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000457
458 // We need 64K pages (at least under glibc/Linux, the loader won't
459 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000460 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000461
462 // The PPC64 ELF ABI v1 spec, says:
463 //
464 // It is normally desirable to put segments with different characteristics
465 // in separate 256 Mbyte portions of the address space, to give the
466 // operating system full paging flexibility in the 64-bit address space.
467 //
468 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
469 // use 0x10000000 as the starting address.
470 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000471}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000472
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000473uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000474 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
475 // order. The TOC starts where the first of these sections starts.
476
477 // FIXME: This obviously does not do the right thing when there is no .got
478 // section, but there is a .toc or .tocbss section.
479 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
480 if (!TocVA)
481 TocVA = Out<ELF64BE>::Plt->getVA();
482
483 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
484 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
485 // code (crt1.o) assumes that you can get from the TOC base to the
486 // start of the .toc section with only a single (signed) 16-bit relocation.
487 return TocVA + 0x8000;
488}
489
George Rimar648a2c32015-10-20 08:54:27 +0000490void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
491void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
492 uint64_t PltEntryAddr) const {}
Rafael Espindolac4010882015-09-22 20:54:08 +0000493void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000494 uint64_t PltEntryAddr, int32_t Index) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000495 uint64_t Off = GotEntryAddr - getPPC64TocBase();
496
497 // FIXME: What we should do, in theory, is get the offset of the function
498 // descriptor in the .opd section, and use that as the offset from %r2 (the
499 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
500 // be a pointer to the function descriptor in the .opd section. Using
501 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
502
Hal Finkelfa92f682015-10-13 21:47:34 +0000503 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000504 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
505 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
506 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
507 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
508 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
509 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
510 write32be(Buf + 28, 0x4e800420); // bctr
511}
512
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000513bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000514 if (relocNeedsPlt(Type, S))
515 return true;
516
517 switch (Type) {
518 default: return false;
519 case R_PPC64_GOT16:
520 case R_PPC64_GOT16_LO:
521 case R_PPC64_GOT16_HI:
522 case R_PPC64_GOT16_HA:
523 case R_PPC64_GOT16_DS:
524 case R_PPC64_GOT16_LO_DS:
525 return true;
526 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000527}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000528
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000529bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000530 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000531 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000532}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000533
Hal Finkelbe0823d2015-10-12 20:58:52 +0000534bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
535 switch (Type) {
536 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000537 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000538 case R_PPC64_TOC:
539 case R_PPC64_ADDR64:
540 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000541 }
542}
543
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000544void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
545 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000546 uint64_t TB = getPPC64TocBase();
547
Hal Finkel3c8cc672015-10-12 20:56:18 +0000548 // For a TOC-relative relocation, adjust the addend and proceed in terms of
549 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000550 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000551 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
552 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
553 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
554 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
555 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
556 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000557 default: break;
558 }
559
Hal Finkel3c8cc672015-10-12 20:56:18 +0000560 switch (Type) {
561 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000562 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000563 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000564 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000565 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000566 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000567 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000568 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000569 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000570 break;
571 case R_PPC64_ADDR16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000572 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000573 break;
574 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000575 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000576 break;
577 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000578 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000579 break;
580 case R_PPC64_ADDR16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000581 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000582 break;
583 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000584 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000585 break;
586 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000587 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000588 break;
589 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000590 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000591 break;
592 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000593 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000594 break;
595 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000596 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000597 error("Improper alignment for relocation R_PPC64_ADDR14");
598
599 // Preserve the AA/LK bits in the branch instruction
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000600 uint8_t AALK = Loc[3];
601 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000602 break;
603 }
604 case R_PPC64_REL16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000605 write16be(Loc, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000606 break;
607 case R_PPC64_REL16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000608 write16be(Loc, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000609 break;
610 case R_PPC64_REL16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000611 write16be(Loc, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000612 break;
613 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000614 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000615 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000616 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000617 break;
618 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000619 // If we have an undefined weak symbol, we might get here with a symbol
620 // address of zero. That could overflow, but the code must be unreachable,
621 // so don't bother doing anything at all.
622 if (!SA)
623 break;
624
Hal Finkeldaedc122015-10-12 23:16:53 +0000625 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
626 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000627 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000628
629 if (!InPlt && Out<ELF64BE>::Opd) {
630 // If this is a local call, and we currently have the address of a
631 // function-descriptor, get the underlying code address instead.
632 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
633 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000634 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000635
636 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000637 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000638 }
639
Hal Finkel3c8cc672015-10-12 20:56:18 +0000640 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000641 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000642 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000643 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000644
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000645 uint32_t Nop = 0x60000000;
646 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
647 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000648 break;
649 }
650 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000651 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000652 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000653 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000654 break;
655 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000656 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000657 break;
658 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000659 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000660 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000661 break;
662 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000663 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000664 }
665}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000666
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000667AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000668 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000669 PltReloc = R_AARCH64_JUMP_SLOT;
670 LazyRelocations = true;
671 PltEntrySize = 16;
672 PltZeroEntrySize = 32;
673}
George Rimar648a2c32015-10-20 08:54:27 +0000674
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000675unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; }
676
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000677unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
678
679void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
680 write64le(Buf, Out<ELF64LE>::Plt->getVA());
681}
682
George Rimar648a2c32015-10-20 08:54:27 +0000683void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000684 uint64_t PltEntryAddr) const {
685 const uint8_t PltData[] = {
686 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
687 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
688 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
689 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
690 0x20, 0x02, 0x1f, 0xd6, // br x17
691 0x1f, 0x20, 0x03, 0xd5, // nop
692 0x1f, 0x20, 0x03, 0xd5, // nop
693 0x1f, 0x20, 0x03, 0xd5 // nop
694 };
695 memcpy(Buf, PltData, sizeof(PltData));
696
697 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
698 GotEntryAddr + 16);
699 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
700 GotEntryAddr + 16);
701 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
702 GotEntryAddr + 16);
703}
704
Davide Italianocde93362015-09-26 00:32:04 +0000705void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000706 uint64_t PltEntryAddr,
707 int32_t Index) const {
708 const uint8_t Inst[] = {
709 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
710 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
711 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
712 0x20, 0x02, 0x1f, 0xd6 // br x17
713 };
714 memcpy(Buf, Inst, sizeof(Inst));
715
716 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
717 GotEntryAddr);
718 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
719 GotEntryAddr);
720 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
721 GotEntryAddr);
722}
723
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000724bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
725 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000726 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
727 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000728}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000729
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000730bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
731 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000732 switch (Type) {
733 default:
734 return false;
735 case R_AARCH64_JUMP26:
736 case R_AARCH64_CALL26:
737 return canBePreempted(&S, true);
738 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000739}
Davide Italiano1d750a62015-09-27 08:45:38 +0000740
Davide Italianoef4be6b2015-10-06 19:01:32 +0000741static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000742 uint32_t ImmLo = (Imm & 0x3) << 29;
743 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
744 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000745 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000746}
747
Davide Italiano318ca222015-10-02 22:13:51 +0000748// Page(Expr) is the page address of the expression Expr, defined
749// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000750// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000751static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000752 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000753}
754
Igor Kudrinee252de2015-11-23 17:16:09 +0000755template <unsigned N>
756static void checkAArch64OutOfRange(int64_t X, uint32_t Type) {
757 if (!isInt<N>(X))
758 error("Relocation " + getELFRelocationTypeName(EM_AARCH64, Type) +
759 " out of range");
760}
761
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000762void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
763 uint32_t Type, uint64_t P,
764 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000765 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000766 case R_AARCH64_ABS16:
Igor Kudrinee252de2015-11-23 17:16:09 +0000767 checkAArch64OutOfRange<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000768 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000769 break;
770 case R_AARCH64_ABS32:
Igor Kudrinee252de2015-11-23 17:16:09 +0000771 checkAArch64OutOfRange<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000772 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000773 break;
774 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000775 // No overflow check needed.
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000776 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000777 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000778 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000779 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000780 // This relocation stores 12 bits and there's no instruction
781 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000782 // of r_addend bitwise-or'ed Loc. This assumes that the addend
783 // bits in Loc are zero.
784 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000785 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000786 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000787 uint64_t X = SA - P;
Igor Kudrinee252de2015-11-23 17:16:09 +0000788 checkAArch64OutOfRange<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000789 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000790 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000791 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000792 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000793 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000794 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrinee252de2015-11-23 17:16:09 +0000795 checkAArch64OutOfRange<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000796 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000797 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000798 }
Igor Kudrinb34115b2015-11-13 03:26:59 +0000799 case R_AARCH64_JUMP26:
800 case R_AARCH64_CALL26: {
801 uint64_t X = SA - P;
Igor Kudrinee252de2015-11-23 17:16:09 +0000802 checkAArch64OutOfRange<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +0000803 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
804 break;
805 }
Davide Italianodc67f9b2015-11-20 21:35:38 +0000806 case R_AARCH64_LDST32_ABS_LO12_NC:
807 // No overflow check needed.
808 or32le(Loc, (SA & 0xFFC) << 8);
809 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000810 case R_AARCH64_LD64_GOT_LO12_NC:
811 if (SA & 0x7)
812 error("Relocation R_AARCH64_LD64_GOT_LO12_NC not aligned");
813 // No overflow check needed.
814 or32le(Loc, (SA & 0xFF8) << 7);
815 break;
Davide Italianobbcc7f62015-11-08 04:45:26 +0000816 case R_AARCH64_LDST64_ABS_LO12_NC:
817 // No overflow check needed.
818 or32le(Loc, (SA & 0xFF8) << 7);
819 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000820 case R_AARCH64_LDST8_ABS_LO12_NC:
821 // No overflow check needed.
822 or32le(Loc, (SA & 0xFFF) << 10);
823 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000824 case R_AARCH64_PREL16:
Igor Kudrinee252de2015-11-23 17:16:09 +0000825 checkAArch64OutOfRange<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000826 write16le(Loc, SA - P);
827 break;
828 case R_AARCH64_PREL32:
Igor Kudrinee252de2015-11-23 17:16:09 +0000829 checkAArch64OutOfRange<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000830 write32le(Loc, SA - P);
831 break;
Davide Italianob12d6682015-10-28 16:14:18 +0000832 case R_AARCH64_PREL64:
833 // No overflow check needed.
834 write64le(Loc, SA - P);
835 break;
Davide Italiano1d750a62015-09-27 08:45:38 +0000836 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000837 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000838 }
839}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000840
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000841template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +0000842 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000843 GotRefReloc = R_MIPS_GOT16;
844 GotHeaderEntriesNum = 2;
845}
846
847template <class ELFT>
848void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
849 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
850 auto *P = reinterpret_cast<Elf_Off *>(Buf);
851 // Module pointer
852 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000853}
854
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000855template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000856void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
857template <class ELFT>
858void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
859 uint64_t PltEntryAddr) const {}
860template <class ELFT>
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000861void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000862 uint64_t PltEntryAddr, int32_t Index) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000863
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000864template <class ELFT>
865bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
866 const SymbolBody &S) const {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000867 return Type == R_MIPS_GOT16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000868}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000869
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000870template <class ELFT>
871bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
872 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000873 return false;
874}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000875
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000876template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000877void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
878 uint32_t Type, uint64_t P,
879 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000880 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000881 switch (Type) {
882 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000883 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000884 break;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000885 case R_MIPS_GOT16: {
886 int64_t V = SA - getMipsGpAddr<ELFT>();
887 if (!isInt<16>(V))
888 error("Relocation R_MIPS_GOT16 out of range");
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000889 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000890 break;
891 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000892 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000893 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000894 }
895}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000896
897template <class ELFT>
898typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
899 const unsigned GPOffset = 0x7ff0;
900 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
901}
902
903template uint32_t getMipsGpAddr<ELF32LE>();
904template uint32_t getMipsGpAddr<ELF32BE>();
905template uint64_t getMipsGpAddr<ELF64LE>();
906template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +0000907}
908}