blob: 86333db026bfbc4964f17d38556bde268e1bd702 [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);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000049 fatal("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050}
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);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000056 fatal("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000057}
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);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000063 fatal("Relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064}
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);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000070 fatal("Improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
George Rimara07ff662015-12-21 10:12:06 +000073template <class ELFT> bool isGnuIFunc(const SymbolBody &S) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000074 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S))
George Rimara07ff662015-12-21 10:12:06 +000075 return SS->Sym.getType() == STT_GNU_IFUNC;
76 return false;
77}
78
79template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
80template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
81template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
82template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
83
Rui Ueyamaefc23de2015-10-14 21:30:32 +000084namespace {
85class X86TargetInfo final : public TargetInfo {
86public:
87 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000088 void writeGotPltHeader(uint8_t *Buf) const override;
89 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000090 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000091 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
92 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
93 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
94 uint64_t PltEntryAddr) const override;
95 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
96 uint64_t PltEntryAddr, int32_t Index,
97 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000098 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000099 bool needsDynRelative(unsigned Type) const override;
100 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
101 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000102 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000103 uint64_t SA, uint64_t ZA = 0,
104 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000105 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000106 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
107 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000108 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000109
110private:
111 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
112 uint64_t SA) const;
113 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
114 uint64_t SA) const;
115 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
116 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000117 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
118 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000119};
120
121class X86_64TargetInfo final : public TargetInfo {
122public:
123 X86_64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000124 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
125 void writeGotPltHeader(uint8_t *Buf) const override;
126 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
127 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
128 uint64_t PltEntryAddr) const override;
129 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
130 uint64_t PltEntryAddr, int32_t Index,
131 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000132 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000133 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
134 bool needsPlt(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,
George Rimar48651482015-12-11 08:59:37 +0000136 uint64_t SA, uint64_t ZA = 0,
137 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000138 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000139 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000140 bool isSizeRel(uint32_t Type) const override;
141 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
142 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000143
144private:
145 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
147 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000149 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
150 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000151 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
152 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000153};
154
Davide Italiano8c3444362016-01-11 19:45:33 +0000155class PPCTargetInfo final : public TargetInfo {
156public:
157 PPCTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000158 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
159 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
160 uint64_t PltEntryAddr) const override;
161 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
162 uint64_t PltEntryAddr, int32_t Index,
163 unsigned RelOff) const override;
164 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
165 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000166 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
167 uint64_t SA, uint64_t ZA = 0,
168 uint8_t *PairedLoc = nullptr) const override;
169 bool isRelRelative(uint32_t Type) const override;
170};
171
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172class PPC64TargetInfo final : public TargetInfo {
173public:
174 PPC64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000175 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
176 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
177 uint64_t PltEntryAddr) const override;
178 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
179 uint64_t PltEntryAddr, int32_t Index,
180 unsigned RelOff) const override;
181 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
182 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000183 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000184 uint64_t SA, uint64_t ZA = 0,
185 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000186 bool isRelRelative(uint32_t Type) const override;
187};
188
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000189class AArch64TargetInfo final : public TargetInfo {
190public:
191 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000192 unsigned getDynRel(unsigned Type) const override;
193 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
194 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
195 uint64_t PltEntryAddr) const override;
196 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
197 uint64_t PltEntryAddr, int32_t Index,
198 unsigned RelOff) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +0000199 unsigned getTlsGotRel(unsigned Type = -1) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000200 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000201 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000202 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
203 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000204 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000205 uint64_t SA, uint64_t ZA = 0,
206 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000207};
208
Tom Stellard80efb162016-01-07 03:59:08 +0000209class AMDGPUTargetInfo final : public TargetInfo {
210public:
211 AMDGPUTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000212 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
213 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
214 uint64_t PltEntryAddr) const override;
215 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
216 uint64_t PltEntryAddr, int32_t Index,
217 unsigned RelOff) const override;
218 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
219 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000220 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
221 uint64_t SA, uint64_t ZA = 0,
222 uint8_t *PairedLoc = nullptr) const override;
223};
224
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000225template <class ELFT> class MipsTargetInfo final : public TargetInfo {
226public:
227 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000228 unsigned getDynRel(unsigned Type) const override;
229 void writeGotHeader(uint8_t *Buf) const override;
230 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
231 void writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
232 uint64_t PltEntryAddr) const override;
233 void writePlt(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
234 uint64_t PltEntryAddr, int32_t Index,
235 unsigned RelOff) const override;
236 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
237 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000238 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000239 uint64_t SA, uint64_t ZA = 0,
240 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000241 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000242 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000243};
244} // anonymous namespace
245
Rui Ueyama91004392015-10-13 16:08:15 +0000246TargetInfo *createTarget() {
247 switch (Config->EMachine) {
248 case EM_386:
249 return new X86TargetInfo();
250 case EM_AARCH64:
251 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000252 case EM_AMDGPU:
253 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000254 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000255 switch (Config->EKind) {
256 case ELF32LEKind:
257 return new MipsTargetInfo<ELF32LE>();
258 case ELF32BEKind:
259 return new MipsTargetInfo<ELF32BE>();
260 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000261 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000262 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000263 case EM_PPC:
264 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000265 case EM_PPC64:
266 return new PPC64TargetInfo();
267 case EM_X86_64:
268 return new X86_64TargetInfo();
269 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000270 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000271}
272
Rafael Espindola01205f72015-09-22 18:19:46 +0000273TargetInfo::~TargetInfo() {}
274
Rui Ueyamabaf16512016-01-29 00:20:12 +0000275bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000276 return false;
277}
278
Igor Kudrinf6f45472015-11-10 08:39:27 +0000279uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
280
Rui Ueyama02dfd492015-12-17 01:18:40 +0000281bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000282 return false;
283}
284
George Rimarbfb7bf72015-12-21 10:00:12 +0000285bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
286
Rui Ueyamac516ae12016-01-29 02:33:45 +0000287bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Simon Atanasyan682aeea2016-01-14 20:42:09 +0000288
Rafael Espindolaae244002015-10-05 19:30:12 +0000289bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
290
Rui Ueyamac516ae12016-01-29 02:33:45 +0000291bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000292
Rui Ueyamac516ae12016-01-29 02:33:45 +0000293unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
294 uint64_t P, uint64_t SA,
295 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000296 return 0;
297}
George Rimar77d1cb12015-11-24 09:00:06 +0000298
Rui Ueyamac516ae12016-01-29 02:33:45 +0000299void TargetInfo::writeGotHeader(uint8_t *Buf) const {}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000300
Rui Ueyamac516ae12016-01-29 02:33:45 +0000301void TargetInfo::writeGotPltHeader(uint8_t *Buf) const {}
Igor Kudrin351b41d2015-11-16 17:44:08 +0000302
Rafael Espindola7f074422015-09-22 21:35:51 +0000303X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000304 CopyRel = R_386_COPY;
305 GotRel = R_386_GLOB_DAT;
306 PltRel = R_386_JUMP_SLOT;
307 IRelativeRel = R_386_IRELATIVE;
308 RelativeRel = R_386_RELATIVE;
309 TlsGotRel = R_386_TLS_TPOFF;
310 TlsGlobalDynamicRel = R_386_TLS_GD;
311 TlsLocalDynamicRel = R_386_TLS_LDM;
312 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
313 TlsOffsetRel = R_386_TLS_DTPOFF32;
314 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000315 PltEntrySize = 16;
316 PltZeroEntrySize = 16;
317}
318
Rui Ueyamac516ae12016-01-29 02:33:45 +0000319void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000320 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
321}
322
Rui Ueyamac516ae12016-01-29 02:33:45 +0000323void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
George Rimar77b77792015-11-25 22:15:01 +0000324 // Skip 6 bytes of "pushl (GOT+4)"
325 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000326}
Rafael Espindola01205f72015-09-22 18:19:46 +0000327
Rui Ueyamac516ae12016-01-29 02:33:45 +0000328unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000329 if (Type == R_386_TLS_LE)
330 return R_386_TLS_TPOFF;
331 if (Type == R_386_TLS_LE_32)
332 return R_386_TLS_TPOFF32;
333 return Type;
334}
335
Rui Ueyama724d6252016-01-29 01:49:32 +0000336unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000337 if (Type == R_386_TLS_IE)
338 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000339 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000340}
341
Rui Ueyamac516ae12016-01-29 02:33:45 +0000342bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000343 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
344 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000345 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000346 if (Type == R_386_TLS_IE)
347 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000348 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000349}
350
Rui Ueyamac516ae12016-01-29 02:33:45 +0000351void X86TargetInfo::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
352 uint64_t PltEntryAddr) const {
George Rimar77b77792015-11-25 22:15:01 +0000353 // Executable files and shared object files have
354 // separate procedure linkage tables.
355 if (Config->Shared) {
356 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000357 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
George Rimar77b77792015-11-25 22:15:01 +0000358 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
359 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
360 };
361 memcpy(Buf, V, sizeof(V));
362 return;
363 }
George Rimar648a2c32015-10-20 08:54:27 +0000364
George Rimar77b77792015-11-25 22:15:01 +0000365 const uint8_t PltData[] = {
366 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
367 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
368 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
369 };
370 memcpy(Buf, PltData, sizeof(PltData));
371 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
372 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
373}
374
Rui Ueyamac516ae12016-01-29 02:33:45 +0000375void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotAddr,
376 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
377 int32_t Index, unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000378 const uint8_t Inst[] = {
379 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
380 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
381 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
382 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000383 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000384 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
385 Buf[1] = Config->Shared ? 0xa3 : 0x25;
386 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
387 write32le(Buf + 7, RelOff);
388 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000389}
390
Rui Ueyama02dfd492015-12-17 01:18:40 +0000391bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000392 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
393 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
394 return SS->Sym.getType() == STT_OBJECT;
395 return false;
396}
397
Rui Ueyamac516ae12016-01-29 02:33:45 +0000398bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000399 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000400 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000401 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000402 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000403 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000404}
405
Rui Ueyamac516ae12016-01-29 02:33:45 +0000406bool X86TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000407 return isGnuIFunc<ELF32LE>(S) ||
408 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000409 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000410}
411
George Rimarbfb7bf72015-12-21 10:00:12 +0000412bool X86TargetInfo::isGotRelative(uint32_t Type) const {
413 // This relocation does not require got entry,
414 // but it is relative to got and needs it to be created.
415 // Here we request for that.
416 return Type == R_386_GOTOFF;
417}
418
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000419void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000420 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000421 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000422 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000423 case R_386_32:
424 add32le(Loc, SA);
425 break;
George Rimarffb67352016-01-19 11:00:48 +0000426 case R_386_GOT32: {
427 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
428 Out<ELF32LE>::Got->getNumEntries() * 4;
429 checkInt<32>(V, Type);
430 add32le(Loc, V);
431 break;
432 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000433 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000434 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000435 break;
George Rimar13934772015-11-25 20:20:31 +0000436 case R_386_GOTPC:
437 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
438 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000439 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000440 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000441 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000442 break;
George Rimar9db204a2015-12-02 09:58:20 +0000443 case R_386_TLS_GD:
444 case R_386_TLS_LDM:
445 case R_386_TLS_TPOFF: {
446 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
447 Out<ELF32LE>::Got->getNumEntries() * 4;
448 checkInt<32>(V, Type);
449 write32le(Loc, V);
450 break;
451 }
George Rimar6f17e092015-12-17 09:32:21 +0000452 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000453 case R_386_TLS_LDO_32:
454 write32le(Loc, SA);
455 break;
George Rimard23970f2015-11-25 20:41:53 +0000456 case R_386_TLS_LE:
457 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
458 break;
459 case R_386_TLS_LE_32:
460 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
461 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000462 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000463 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000464 }
465}
466
Rui Ueyamabaf16512016-01-29 00:20:12 +0000467bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000468 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000469 return false;
470 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
471 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000472 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000473 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
474}
475
Rui Ueyamac516ae12016-01-29 02:33:45 +0000476bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000477 return Config->Shared && Type == R_386_TLS_IE;
478}
479
Rui Ueyamac516ae12016-01-29 02:33:45 +0000480unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
481 uint64_t P, uint64_t SA,
482 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000483 switch (Type) {
484 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000485 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000486 relocateTlsGdToIe(Loc, BufEnd, P, SA);
487 else
488 relocateTlsGdToLe(Loc, BufEnd, P, SA);
489 // The next relocation should be against __tls_get_addr, so skip it
490 return 1;
491 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000492 case R_386_TLS_IE:
493 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000494 return 0;
495 case R_386_TLS_LDM:
496 relocateTlsLdToLe(Loc, BufEnd, P, SA);
497 // The next relocation should be against __tls_get_addr, so skip it
498 return 1;
499 case R_386_TLS_LDO_32:
500 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
501 return 0;
502 }
503 llvm_unreachable("Unknown TLS optimization");
504}
505
506// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
507// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
508// how GD can be optimized to IE:
509// leal x@tlsgd(, %ebx, 1),
510// call __tls_get_addr@plt
511// Is converted to:
512// movl %gs:0, %eax
513// addl x@gotntpoff(%ebx), %eax
514void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
515 uint64_t SA) const {
516 const uint8_t Inst[] = {
517 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
518 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
519 };
520 memcpy(Loc - 3, Inst, sizeof(Inst));
521 relocateOne(Loc + 5, BufEnd, R_386_32, P,
522 SA - Out<ELF32LE>::Got->getVA() -
523 Out<ELF32LE>::Got->getNumEntries() * 4);
524}
525
526// GD can be optimized to LE:
527// leal x@tlsgd(, %ebx, 1),
528// call __tls_get_addr@plt
529// Can be converted to:
530// movl %gs:0,%eax
531// addl $x@ntpoff,%eax
532// But gold emits subl $foo@tpoff,%eax instead of addl.
533// These instructions are completely equal in behavior.
534// This method generates subl to be consistent with gold.
535void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
536 uint64_t SA) const {
537 const uint8_t Inst[] = {
538 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
539 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
540 };
541 memcpy(Loc - 3, Inst, sizeof(Inst));
542 relocateOne(Loc + 5, BufEnd, R_386_32, P,
543 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
544}
545
546// LD can be optimized to LE:
547// leal foo(%reg),%eax
548// call ___tls_get_addr
549// Is converted to:
550// movl %gs:0,%eax
551// nop
552// leal 0(%esi,1),%esi
553void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
554 uint64_t SA) const {
555 const uint8_t Inst[] = {
556 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
557 0x90, // nop
558 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
559 };
560 memcpy(Loc - 2, Inst, sizeof(Inst));
561}
562
George Rimar6f17e092015-12-17 09:32:21 +0000563// In some conditions, relocations can be optimized to avoid using GOT.
564// This function does that for Initial Exec to Local Exec case.
565// Read "ELF Handling For Thread-Local Storage, 5.1
566// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000567// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000568void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
569 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000570 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000571 // Ulrich's document section 6.2 says that @gotntpoff can
572 // be used with MOVL or ADDL instructions.
573 // @indntpoff is similar to @gotntpoff, but for use in
574 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000575 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000576 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000577 uint8_t Reg = (Loc[-1] >> 3) & 7;
578 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000579 if (Type == R_386_TLS_IE) {
580 // For R_386_TLS_IE relocation we perform the next transformations:
581 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
582 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
583 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
584 // First one is special because when EAX is used the sequence is 5 bytes
585 // long, otherwise it is 6 bytes.
586 if (*Op == 0xa1) {
587 *Op = 0xb8;
588 } else {
589 *Inst = IsMov ? 0xc7 : 0x81;
590 *Op = 0xc0 | ((*Op >> 3) & 7);
591 }
592 } else {
593 // R_386_TLS_GOTIE relocation can be optimized to
594 // R_386_TLS_LE so that it does not use GOT.
595 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
596 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
597 // Note: gold converts to ADDL instead of LEAL.
598 *Inst = IsMov ? 0xc7 : 0x8d;
599 if (IsMov)
600 *Op = 0xc0 | ((*Op >> 3) & 7);
601 else
602 *Op = 0x80 | Reg | (Reg << 3);
603 }
George Rimar2558e122015-12-09 09:55:54 +0000604 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
605}
606
Rafael Espindola7f074422015-09-22 21:35:51 +0000607X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000608 CopyRel = R_X86_64_COPY;
609 GotRel = R_X86_64_GLOB_DAT;
610 PltRel = R_X86_64_JUMP_SLOT;
611 RelativeRel = R_X86_64_RELATIVE;
612 IRelativeRel = R_X86_64_IRELATIVE;
613 TlsGotRel = R_X86_64_TPOFF64;
614 TlsLocalDynamicRel = R_X86_64_TLSLD;
615 TlsGlobalDynamicRel = R_X86_64_TLSGD;
616 TlsModuleIndexRel = R_X86_64_DTPMOD64;
617 TlsOffsetRel = R_X86_64_DTPOFF64;
618 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000619 PltEntrySize = 16;
620 PltZeroEntrySize = 16;
621}
622
Rui Ueyamac516ae12016-01-29 02:33:45 +0000623void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000624 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
625}
626
Rui Ueyamac516ae12016-01-29 02:33:45 +0000627void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
George Rimar648a2c32015-10-20 08:54:27 +0000628 // Skip 6 bytes of "jmpq *got(%rip)"
629 write32le(Buf, Plt + 6);
630}
631
Rui Ueyamac516ae12016-01-29 02:33:45 +0000632void X86_64TargetInfo::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
633 uint64_t PltEntryAddr) const {
George Rimar648a2c32015-10-20 08:54:27 +0000634 const uint8_t PltData[] = {
635 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
636 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
637 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
638 };
639 memcpy(Buf, PltData, sizeof(PltData));
640 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
641 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000642}
Rafael Espindola01205f72015-09-22 18:19:46 +0000643
Rui Ueyamac516ae12016-01-29 02:33:45 +0000644void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotAddr,
645 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
646 int32_t Index, unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000647 const uint8_t Inst[] = {
648 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
649 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
650 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
651 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000652 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000653
George Rimar648a2c32015-10-20 08:54:27 +0000654 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
655 write32le(Buf + 7, Index);
656 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000657}
658
Rui Ueyama02dfd492015-12-17 01:18:40 +0000659bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000660 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
661 Type == R_X86_64_64)
662 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
663 return SS->Sym.getType() == STT_OBJECT;
664 return false;
665}
666
Rui Ueyamac516ae12016-01-29 02:33:45 +0000667bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000668 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000669 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000670 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000671 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000672 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000673}
674
Rui Ueyamac516ae12016-01-29 02:33:45 +0000675bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000676 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000677}
678
Rui Ueyamac516ae12016-01-29 02:33:45 +0000679bool X86_64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000680 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000681 return false;
George Rimara07ff662015-12-21 10:12:06 +0000682 if (isGnuIFunc<ELF64LE>(S))
683 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000684
Rafael Espindola01205f72015-09-22 18:19:46 +0000685 switch (Type) {
686 default:
687 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000688 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000689 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000690 case R_X86_64_PC32:
691 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000692 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000693 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000694 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000695 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000696 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
697 // libc.so.
698 //
699 // The general idea on how to handle such cases is to create a PLT entry
700 // and use that as the function value.
701 //
702 // For the static linking part, we just return true and everything else
703 // will use the the PLT entry as the address.
704 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000705 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000706 // still works. We need the help of the dynamic linker for that. We
707 // let it know that we have a direct reference to a so symbol by creating
708 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000709 // dynamic linker resolves the symbol to the value of the symbol we created.
710 // This is true even for got entries, so pointer equality is maintained.
711 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000712 // real function is a dedicated got entry used by the plt. That is
713 // identified by special relocation types (R_X86_64_JUMP_SLOT,
714 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000715 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000716 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000717 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000718 }
719}
Rafael Espindolac4010882015-09-22 20:54:08 +0000720
Rafael Espindolaae244002015-10-05 19:30:12 +0000721bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
722 switch (Type) {
723 default:
724 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000725 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000726 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000727 case R_X86_64_PC8:
728 case R_X86_64_PC16:
729 case R_X86_64_PC32:
730 case R_X86_64_PC64:
731 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000732 return true;
733 }
734}
735
Rui Ueyamac516ae12016-01-29 02:33:45 +0000736bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000737 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000738}
739
Rui Ueyamabaf16512016-01-29 00:20:12 +0000740bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000741 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000742 return false;
George Rimar25411f252015-12-04 11:20:13 +0000743 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
744 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000745 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
746}
747
748// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
749// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
750// how LD can be optimized to LE:
751// leaq bar@tlsld(%rip), %rdi
752// callq __tls_get_addr@PLT
753// leaq bar@dtpoff(%rax), %rcx
754// Is converted to:
755// .word 0x6666
756// .byte 0x66
757// mov %fs:0,%rax
758// leaq bar@tpoff(%rax), %rcx
759void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
760 uint64_t P, uint64_t SA) const {
761 const uint8_t Inst[] = {
762 0x66, 0x66, //.word 0x6666
763 0x66, //.byte 0x66
764 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
765 };
766 memcpy(Loc - 3, Inst, sizeof(Inst));
767}
768
769// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
770// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
771// how GD can be optimized to LE:
772// .byte 0x66
773// leaq x@tlsgd(%rip), %rdi
774// .word 0x6666
775// rex64
776// call __tls_get_addr@plt
777// Is converted to:
778// mov %fs:0x0,%rax
779// lea x@tpoff,%rax
780void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
781 uint64_t P, uint64_t SA) const {
782 const uint8_t Inst[] = {
783 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
784 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
785 };
786 memcpy(Loc - 4, Inst, sizeof(Inst));
787 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000788}
789
George Rimar25411f252015-12-04 11:20:13 +0000790// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
791// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
792// how GD can be optimized to IE:
793// .byte 0x66
794// leaq x@tlsgd(%rip), %rdi
795// .word 0x6666
796// rex64
797// call __tls_get_addr@plt
798// Is converted to:
799// mov %fs:0x0,%rax
800// addq x@tpoff,%rax
801void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
802 uint64_t P, uint64_t SA) const {
803 const uint8_t Inst[] = {
804 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
805 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
806 };
807 memcpy(Loc - 4, Inst, sizeof(Inst));
808 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
809}
810
George Rimar77d1cb12015-11-24 09:00:06 +0000811// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000812// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000813// This function does that. Read "ELF Handling For Thread-Local Storage,
814// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
815// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000816void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
817 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000818 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
819 // used in MOVQ or ADDQ instructions only.
820 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
821 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
822 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
823 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
824 uint8_t *Prefix = Loc - 3;
825 uint8_t *Inst = Loc - 2;
826 uint8_t *RegSlot = Loc - 1;
827 uint8_t Reg = Loc[-1] >> 3;
828 bool IsMov = *Inst == 0x8b;
829 bool RspAdd = !IsMov && Reg == 4;
830 // r12 and rsp registers requires special handling.
831 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
832 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
833 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
834 // The same true for rsp. So we convert to addq for them, saving 1 byte that
835 // we dont have.
836 if (RspAdd)
837 *Inst = 0x81;
838 else
839 *Inst = IsMov ? 0xc7 : 0x8d;
840 if (*Prefix == 0x4c)
841 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
842 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
843 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
844}
845
George Rimar6713cf82015-11-25 21:46:05 +0000846// This function applies a TLS relocation with an optimization as described
847// in the Ulrich's document. As a result of rewriting instructions at the
848// relocation target, relocations immediately follow the TLS relocation (which
849// would be applied to rewritten instructions) may have to be skipped.
850// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000851unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
852 uint32_t Type, uint64_t P, uint64_t SA,
853 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000854 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000855 case R_X86_64_DTPOFF32:
856 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
857 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000858 case R_X86_64_GOTTPOFF:
859 relocateTlsIeToLe(Loc, BufEnd, P, SA);
860 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000861 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000862 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000863 relocateTlsGdToIe(Loc, BufEnd, P, SA);
864 else
865 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000866 // The next relocation should be against __tls_get_addr, so skip it
867 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000868 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000869 case R_X86_64_TLSLD:
870 relocateTlsLdToLe(Loc, BufEnd, P, SA);
871 // The next relocation should be against __tls_get_addr, so skip it
872 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000873 }
874 llvm_unreachable("Unknown TLS optimization");
875}
876
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000877void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000878 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000879 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000880 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000881 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000882 checkUInt<32>(SA, Type);
883 write32le(Loc, SA);
884 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000885 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000886 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000887 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000888 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000889 case R_X86_64_64:
890 write64le(Loc, SA);
891 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000892 case R_X86_64_DTPOFF32:
893 write32le(Loc, SA);
894 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000895 case R_X86_64_DTPOFF64:
896 write64le(Loc, SA);
897 break;
898 case R_X86_64_GOTPCREL:
899 case R_X86_64_PC32:
900 case R_X86_64_PLT32:
901 case R_X86_64_TLSGD:
902 case R_X86_64_TLSLD:
903 write32le(Loc, SA - P);
904 break;
George Rimar48651482015-12-11 08:59:37 +0000905 case R_X86_64_SIZE32:
906 write32le(Loc, ZA);
907 break;
908 case R_X86_64_SIZE64:
909 write64le(Loc, ZA);
910 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000911 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000912 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000913 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000914 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000915 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000916 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000917 case R_X86_64_TPOFF64:
918 write32le(Loc, SA - P);
919 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000920 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000921 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000922 }
923}
924
Hal Finkel3c8cc672015-10-12 20:56:18 +0000925// Relocation masks following the #lo(value), #hi(value), #ha(value),
926// #higher(value), #highera(value), #highest(value), and #highesta(value)
927// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
928// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000929static uint16_t applyPPCLo(uint64_t V) { return V; }
930static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
931static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
932static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
933static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000934static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000935static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
936
Davide Italiano8c3444362016-01-11 19:45:33 +0000937PPCTargetInfo::PPCTargetInfo() {}
Rui Ueyamac516ae12016-01-29 02:33:45 +0000938void PPCTargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {}
939void PPCTargetInfo::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
940 uint64_t PltEntryAddr) const {}
941void PPCTargetInfo::writePlt(uint8_t *Buf, uint64_t GotAddr,
942 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
943 int32_t Index, unsigned RelOff) const {}
944bool PPCTargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000945 return false;
946}
Rui Ueyamac516ae12016-01-29 02:33:45 +0000947bool PPCTargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000948 return false;
949}
950bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
951
952void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
953 uint64_t P, uint64_t SA, uint64_t ZA,
954 uint8_t *PairedLoc) const {
955 switch (Type) {
956 case R_PPC_ADDR16_HA:
957 write16be(Loc, applyPPCHa(SA));
958 break;
959 case R_PPC_ADDR16_LO:
960 write16be(Loc, applyPPCLo(SA));
961 break;
962 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000963 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000964 }
965}
966
Rafael Espindolac4010882015-09-22 20:54:08 +0000967PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000968 GotRel = R_PPC64_GLOB_DAT;
969 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000970 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000971
972 // We need 64K pages (at least under glibc/Linux, the loader won't
973 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000974 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000975
976 // The PPC64 ELF ABI v1 spec, says:
977 //
978 // It is normally desirable to put segments with different characteristics
979 // in separate 256 Mbyte portions of the address space, to give the
980 // operating system full paging flexibility in the 64-bit address space.
981 //
982 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
983 // use 0x10000000 as the starting address.
984 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000985}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000986
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000987uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000988 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
989 // order. The TOC starts where the first of these sections starts.
990
991 // FIXME: This obviously does not do the right thing when there is no .got
992 // section, but there is a .toc or .tocbss section.
993 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
994 if (!TocVA)
995 TocVA = Out<ELF64BE>::Plt->getVA();
996
997 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
998 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
999 // code (crt1.o) assumes that you can get from the TOC base to the
1000 // start of the .toc section with only a single (signed) 16-bit relocation.
1001 return TocVA + 0x8000;
1002}
1003
Rui Ueyamac516ae12016-01-29 02:33:45 +00001004void PPC64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {}
1005void PPC64TargetInfo::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
1006 uint64_t PltEntryAddr) const {}
1007void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotAddr,
1008 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
1009 int32_t Index, unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1011
1012 // FIXME: What we should do, in theory, is get the offset of the function
1013 // descriptor in the .opd section, and use that as the offset from %r2 (the
1014 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1015 // be a pointer to the function descriptor in the .opd section. Using
1016 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1017
Hal Finkelfa92f682015-10-13 21:47:34 +00001018 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1020 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1021 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1022 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1023 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1024 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1025 write32be(Buf + 28, 0x4e800420); // bctr
1026}
1027
Rui Ueyamac516ae12016-01-29 02:33:45 +00001028bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
1029 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 return true;
1031
1032 switch (Type) {
1033 default: return false;
1034 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001035 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001036 case R_PPC64_GOT16_HA:
1037 case R_PPC64_GOT16_HI:
1038 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001039 case R_PPC64_GOT16_LO_DS:
1040 return true;
1041 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001042}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043
Rui Ueyamac516ae12016-01-29 02:33:45 +00001044bool PPC64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001045 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001046 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001047}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048
Hal Finkelbe0823d2015-10-12 20:58:52 +00001049bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1050 switch (Type) {
1051 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001052 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001053 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001054 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001055 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001056 }
1057}
1058
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001059void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001060 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001061 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 uint64_t TB = getPPC64TocBase();
1063
Hal Finkel3c8cc672015-10-12 20:56:18 +00001064 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1065 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001066 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001067 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1068 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001069 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1070 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001071 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1072 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 default: break;
1074 }
1075
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001077 case R_PPC64_ADDR14: {
1078 checkAlignment<4>(SA, Type);
1079 // Preserve the AA/LK bits in the branch instruction
1080 uint8_t AALK = Loc[3];
1081 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1082 break;
1083 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001085 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001086 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001087 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001088 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001089 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001090 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001091 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001092 case R_PPC64_ADDR16_HA:
1093 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001094 break;
1095 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001096 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001097 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001098 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001099 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001100 break;
1101 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001102 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 break;
1104 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001105 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 break;
1107 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001108 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001110 case R_PPC64_ADDR16_LO:
1111 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001112 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001113 case R_PPC64_ADDR16_LO_DS:
1114 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 break;
1116 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001117 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001118 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001120 case R_PPC64_ADDR64:
1121 write64be(Loc, SA);
1122 break;
1123 case R_PPC64_REL16_HA:
1124 write16be(Loc, applyPPCHa(SA - P));
1125 break;
1126 case R_PPC64_REL16_HI:
1127 write16be(Loc, applyPPCHi(SA - P));
1128 break;
1129 case R_PPC64_REL16_LO:
1130 write16be(Loc, applyPPCLo(SA - P));
1131 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001133 // If we have an undefined weak symbol, we might get here with a symbol
1134 // address of zero. That could overflow, but the code must be unreachable,
1135 // so don't bother doing anything at all.
1136 if (!SA)
1137 break;
1138
Hal Finkeldaedc122015-10-12 23:16:53 +00001139 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1140 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001141 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001142
1143 if (!InPlt && Out<ELF64BE>::Opd) {
1144 // If this is a local call, and we currently have the address of a
1145 // function-descriptor, get the underlying code address instead.
1146 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1147 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001148 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001149
1150 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001151 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001152 }
1153
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001155 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001156 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001157
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001158 uint32_t Nop = 0x60000000;
1159 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1160 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001161 break;
1162 }
1163 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001164 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001165 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001166 break;
1167 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001168 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001169 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001170 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001171 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001172 break;
1173 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001174 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001175 }
1176}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001177
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001178AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001179 CopyRel = R_AARCH64_COPY;
1180 IRelativeRel = R_AARCH64_IRELATIVE;
1181 GotRel = R_AARCH64_GLOB_DAT;
1182 PltRel = R_AARCH64_JUMP_SLOT;
1183 TlsGotRel = R_AARCH64_TLS_TPREL64;
1184 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001185 PltEntrySize = 16;
1186 PltZeroEntrySize = 32;
1187}
George Rimar648a2c32015-10-20 08:54:27 +00001188
Rui Ueyamac516ae12016-01-29 02:33:45 +00001189unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001190 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1191 return Type;
1192 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001193 fatal("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001194 "recompile with -fPIC.");
1195}
1196
Rui Ueyamac516ae12016-01-29 02:33:45 +00001197void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001198 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1199}
1200
Rui Ueyamac516ae12016-01-29 02:33:45 +00001201void AArch64TargetInfo::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
1202 uint64_t PltEntryAddr) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001203 const uint8_t PltData[] = {
1204 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1205 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1206 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1207 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1208 0x20, 0x02, 0x1f, 0xd6, // br x17
1209 0x1f, 0x20, 0x03, 0xd5, // nop
1210 0x1f, 0x20, 0x03, 0xd5, // nop
1211 0x1f, 0x20, 0x03, 0xd5 // nop
1212 };
1213 memcpy(Buf, PltData, sizeof(PltData));
1214
1215 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
1216 GotEntryAddr + 16);
1217 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
1218 GotEntryAddr + 16);
1219 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
1220 GotEntryAddr + 16);
1221}
1222
Rui Ueyamac516ae12016-01-29 02:33:45 +00001223void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotAddr,
1224 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
1225 int32_t Index, unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001226 const uint8_t Inst[] = {
1227 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1228 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1229 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1230 0x20, 0x02, 0x1f, 0xd6 // br x17
1231 };
1232 memcpy(Buf, Inst, sizeof(Inst));
1233
1234 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1235 GotEntryAddr);
1236 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1237 GotEntryAddr);
1238 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1239 GotEntryAddr);
1240}
1241
Rui Ueyama724d6252016-01-29 01:49:32 +00001242unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar3d737e42016-01-13 13:04:46 +00001243 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1244 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1245 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +00001246 return TlsGotRel;
George Rimar3d737e42016-01-13 13:04:46 +00001247}
1248
Rui Ueyamac516ae12016-01-29 02:33:45 +00001249bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001250 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1251 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1252}
1253
Rui Ueyama02dfd492015-12-17 01:18:40 +00001254bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001255 if (Config->Shared)
1256 return false;
1257 switch (Type) {
1258 default:
1259 return false;
1260 case R_AARCH64_ABS16:
1261 case R_AARCH64_ABS32:
1262 case R_AARCH64_ABS64:
1263 case R_AARCH64_ADD_ABS_LO12_NC:
1264 case R_AARCH64_ADR_PREL_LO21:
1265 case R_AARCH64_ADR_PREL_PG_HI21:
1266 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001267 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001268 case R_AARCH64_LDST32_ABS_LO12_NC:
1269 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001270 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001271 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1272 return SS->Sym.getType() == STT_OBJECT;
1273 return false;
1274 }
1275}
1276
Rui Ueyamac516ae12016-01-29 02:33:45 +00001277bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001278 switch (Type) {
1279 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1280 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1281 case R_AARCH64_ADR_GOT_PAGE:
1282 case R_AARCH64_LD64_GOT_LO12_NC:
1283 return true;
1284 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001285 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001286 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001287}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001288
Rui Ueyamac516ae12016-01-29 02:33:45 +00001289bool AArch64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001290 if (isGnuIFunc<ELF64LE>(S))
1291 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001292 switch (Type) {
1293 default:
1294 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001295 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001296 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001297 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001298 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001299 return canBePreempted(&S, true);
1300 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001301}
Davide Italiano1d750a62015-09-27 08:45:38 +00001302
Davide Italianoef4be6b2015-10-06 19:01:32 +00001303static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001304 uint32_t ImmLo = (Imm & 0x3) << 29;
1305 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1306 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001307 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001308}
1309
Davide Italiano318ca222015-10-02 22:13:51 +00001310// Page(Expr) is the page address of the expression Expr, defined
1311// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001312// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001313static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001314 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001315}
1316
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001317void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001318 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001319 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001320 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001321 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001322 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001323 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001324 break;
1325 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001326 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001327 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001328 break;
1329 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001330 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001331 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001332 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001333 // This relocation stores 12 bits and there's no instruction
1334 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001335 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1336 // bits in Loc are zero.
1337 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001338 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001339 case R_AARCH64_ADR_GOT_PAGE: {
1340 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1341 checkInt<33>(X, Type);
1342 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1343 break;
1344 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001345 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001346 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001347 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001348 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001349 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001350 }
George Rimar3d737e42016-01-13 13:04:46 +00001351 case R_AARCH64_ADR_PREL_PG_HI21:
1352 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001353 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001354 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001355 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001356 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001357 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001358 case R_AARCH64_CALL26:
1359 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001360 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001361 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001362 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1363 break;
1364 }
George Rimar4102bfb2016-01-11 14:22:00 +00001365 case R_AARCH64_CONDBR19: {
1366 uint64_t X = SA - P;
1367 checkInt<21>(X, Type);
1368 or32le(Loc, (X & 0x1FFFFC) << 3);
1369 break;
1370 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001371 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001372 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001373 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001374 or32le(Loc, (SA & 0xFF8) << 7);
1375 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001376 case R_AARCH64_LDST128_ABS_LO12_NC:
1377 or32le(Loc, (SA & 0x0FF8) << 6);
1378 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001379 case R_AARCH64_LDST16_ABS_LO12_NC:
1380 or32le(Loc, (SA & 0x0FFC) << 9);
1381 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001382 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001383 or32le(Loc, (SA & 0xFFF) << 10);
1384 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001385 case R_AARCH64_LDST32_ABS_LO12_NC:
1386 or32le(Loc, (SA & 0xFFC) << 8);
1387 break;
1388 case R_AARCH64_LDST64_ABS_LO12_NC:
1389 or32le(Loc, (SA & 0xFF8) << 7);
1390 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001391 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001392 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001393 write16le(Loc, SA - P);
1394 break;
1395 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001396 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001397 write32le(Loc, SA - P);
1398 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001399 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001400 write64le(Loc, SA - P);
1401 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001402 case R_AARCH64_TSTBR14: {
1403 uint64_t X = SA - P;
1404 checkInt<16>(X, Type);
1405 or32le(Loc, (X & 0xFFFC) << 3);
1406 break;
1407 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001408 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001409 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001410 }
1411}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001412
Tom Stellard80efb162016-01-07 03:59:08 +00001413AMDGPUTargetInfo::AMDGPUTargetInfo() {}
1414
Rui Ueyamac516ae12016-01-29 02:33:45 +00001415void AMDGPUTargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Tom Stellard80efb162016-01-07 03:59:08 +00001416 llvm_unreachable("not implemented");
1417}
1418
Rui Ueyamac516ae12016-01-29 02:33:45 +00001419void AMDGPUTargetInfo::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
1420 uint64_t PltEntryAddr) const {
Tom Stellard80efb162016-01-07 03:59:08 +00001421 llvm_unreachable("not implemented");
1422}
1423
Rui Ueyamac516ae12016-01-29 02:33:45 +00001424void AMDGPUTargetInfo::writePlt(uint8_t *Buf, uint64_t GotAddr,
1425 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
1426 int32_t Index, unsigned RelOff) const {
Tom Stellard80efb162016-01-07 03:59:08 +00001427 llvm_unreachable("not implemented");
1428}
1429
Rui Ueyamac516ae12016-01-29 02:33:45 +00001430bool AMDGPUTargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Tom Stellard80efb162016-01-07 03:59:08 +00001431 return false;
1432}
1433
Rui Ueyamac516ae12016-01-29 02:33:45 +00001434bool AMDGPUTargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Tom Stellard80efb162016-01-07 03:59:08 +00001435 return false;
1436}
1437
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001438// Implementing relocations for AMDGPU is low priority since most
1439// programs don't use relocations now. Thus, this function is not
1440// actually called (relocateOne is called for each relocation).
1441// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001442void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1443 uint64_t P, uint64_t SA, uint64_t ZA,
1444 uint8_t *PairedLoc) const {
1445 llvm_unreachable("not implemented");
1446}
1447
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001448template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001449 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001450 GotHeaderEntriesNum = 2;
Rui Ueyama724d6252016-01-29 01:49:32 +00001451 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001452}
1453
1454template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001455unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001456 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1457 return R_MIPS_REL32;
1458 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001459 fatal("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001460 "recompile with -fPIC.");
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001461}
1462
1463template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001464void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001465 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001466 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1467 // Module pointer
1468 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001469}
1470
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001471template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001472void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {}
George Rimar648a2c32015-10-20 08:54:27 +00001473template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001474void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf, uint64_t GotEntryAddr,
1475 uint64_t PltEntryAddr) const {}
George Rimar648a2c32015-10-20 08:54:27 +00001476template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001477void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotAddr,
1478 uint64_t GotEntryAddr,
1479 uint64_t PltEntryAddr, int32_t Index,
1480 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001481
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001482template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001483bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001484 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001485}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001486
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001487template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001488bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001489 return false;
1490}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001491
Simon Atanasyan35031192015-12-15 06:06:34 +00001492static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001493
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001494template <endianness E, uint8_t BSIZE>
1495static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1496 uint64_t SA) {
1497 uint32_t Mask = ~(0xffffffff << BSIZE);
1498 uint32_t Instr = read32<E>(Loc);
1499 int64_t A = SignExtend64<BSIZE + 2>((Instr & Mask) << 2);
1500 checkAlignment<4>(SA + A, Type);
1501 int64_t V = SA + A - P;
1502 checkInt<BSIZE + 2>(V, Type);
1503 write32<E>(Loc, (Instr & ~Mask) | ((V >> 2) & Mask));
1504}
1505
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001506template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001507void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001508 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001509 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001510 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001511 switch (Type) {
1512 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001513 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001514 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001515 case R_MIPS_CALL16:
1516 case R_MIPS_GOT16: {
1517 int64_t V = SA - getMipsGpAddr<ELFT>();
1518 if (Type == R_MIPS_GOT16)
1519 checkInt<16>(V, Type);
1520 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1521 break;
1522 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001523 case R_MIPS_GPREL16: {
1524 uint32_t Instr = read32<E>(Loc);
1525 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1526 checkInt<16>(V, Type);
1527 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1528 break;
1529 }
1530 case R_MIPS_GPREL32:
1531 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1532 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001533 case R_MIPS_HI16: {
1534 uint32_t Instr = read32<E>(Loc);
1535 if (PairedLoc) {
1536 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001537 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001538 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001539 } else {
1540 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001541 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001542 }
1543 break;
1544 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001545 case R_MIPS_JALR:
1546 // Ignore this optimization relocation for now
1547 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001548 case R_MIPS_LO16: {
1549 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001550 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001551 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1552 break;
1553 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001554 case R_MIPS_PC16:
1555 applyMipsPcReloc<E, 16>(Loc, Type, P, SA);
1556 break;
1557 case R_MIPS_PC19_S2:
1558 applyMipsPcReloc<E, 19>(Loc, Type, P, SA);
1559 break;
1560 case R_MIPS_PC21_S2:
1561 applyMipsPcReloc<E, 21>(Loc, Type, P, SA);
1562 break;
1563 case R_MIPS_PC26_S2:
1564 applyMipsPcReloc<E, 26>(Loc, Type, P, SA);
1565 break;
1566 case R_MIPS_PCHI16: {
1567 uint32_t Instr = read32<E>(Loc);
1568 if (PairedLoc) {
1569 uint64_t AHL = ((Instr & 0xffff) << 16) +
1570 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1571 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1572 } else {
1573 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1574 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1575 }
1576 break;
1577 }
1578 case R_MIPS_PCLO16: {
1579 uint32_t Instr = read32<E>(Loc);
1580 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1581 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1582 break;
1583 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001584 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001585 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001586 }
1587}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001588
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001589template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001590bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001591 return Type == R_MIPS_JALR;
1592}
1593
1594template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001595bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1596 switch (Type) {
1597 default:
1598 return false;
1599 case R_MIPS_PC16:
1600 case R_MIPS_PC19_S2:
1601 case R_MIPS_PC21_S2:
1602 case R_MIPS_PC26_S2:
1603 case R_MIPS_PCHI16:
1604 case R_MIPS_PCLO16:
1605 return true;
1606 }
1607}
1608
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001609// _gp is a MIPS-specific ABI-defined symbol which points to
1610// a location that is relative to GOT. This function returns
1611// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001612template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001613 unsigned GPOffset = 0x7ff0;
1614 if (uint64_t V = Out<ELFT>::Got->getVA())
1615 return V + GPOffset;
1616 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001617}
1618
Simon Atanasyan56ab5f02016-01-21 05:33:23 +00001619bool needsMipsLocalGot(uint32_t Type, SymbolBody *Body) {
1620 // The R_MIPS_GOT16 relocation requires creation of entry in the local part
1621 // of GOT if its target is a local symbol or non-local symbol with 'local'
1622 // visibility.
1623 if (Type != R_MIPS_GOT16)
1624 return false;
1625 if (!Body)
1626 return true;
1627 uint8_t V = Body->getVisibility();
1628 if (V != STV_DEFAULT && V != STV_PROTECTED)
1629 return true;
1630 return !Config->Shared;
1631}
1632
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001633template uint32_t getMipsGpAddr<ELF32LE>();
1634template uint32_t getMipsGpAddr<ELF32BE>();
1635template uint64_t getMipsGpAddr<ELF64LE>();
1636template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001637}
1638}