blob: 2b1e9f7308d39bbfa3d96124309f3a43c8a8238f [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 Ueyama21923992016-02-01 23:28:21 +000049 error("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 Ueyama21923992016-02-01 23:28:21 +000056 error("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 Ueyama21923992016-02-01 23:28:21 +000063 error("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 Ueyama21923992016-02-01 23:28:21 +000070 error("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
Rui Ueyamaefc23de2015-10-14 21:30:32 +000079namespace {
80class X86TargetInfo final : public TargetInfo {
81public:
82 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000083 void writeGotPltHeader(uint8_t *Buf) const override;
84 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000085 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000086 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
87 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000088 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000089 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
90 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000091 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000092 bool needsDynRelative(unsigned Type) const override;
93 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
94 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000095 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000096 uint64_t SA, uint64_t ZA = 0,
97 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +000098 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000099 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
100 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000101 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000102
103private:
104 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
105 uint64_t SA) const;
106 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
107 uint64_t SA) const;
108 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
109 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000110 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
111 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000112};
113
114class X86_64TargetInfo final : public TargetInfo {
115public:
116 X86_64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000117 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
118 void writeGotPltHeader(uint8_t *Buf) const override;
119 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000120 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000121 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
122 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000123 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000124 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
125 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000126 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000127 uint64_t SA, uint64_t ZA = 0,
128 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000130 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000131 bool isSizeRel(uint32_t Type) const override;
132 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
133 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000134
135private:
136 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
137 uint64_t SA) const;
138 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
139 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000140 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
141 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000142 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
143 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000144};
145
Davide Italiano8c3444362016-01-11 19:45:33 +0000146class PPCTargetInfo final : public TargetInfo {
147public:
148 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000149 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
150 uint64_t SA, uint64_t ZA = 0,
151 uint8_t *PairedLoc = nullptr) const override;
152 bool isRelRelative(uint32_t Type) const override;
153};
154
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155class PPC64TargetInfo final : public TargetInfo {
156public:
157 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000158 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
159 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000160 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
161 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000162 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000163 uint64_t SA, uint64_t ZA = 0,
164 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000165 bool isRelRelative(uint32_t Type) const override;
166};
167
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000168class AArch64TargetInfo final : public TargetInfo {
169public:
170 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000171 unsigned getDynRel(unsigned Type) const override;
172 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000173 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000174 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
175 int32_t Index, unsigned RelOff) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +0000176 unsigned getTlsGotRel(unsigned Type = -1) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000177 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000178 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000179 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
180 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000181 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000182 uint64_t SA, uint64_t ZA = 0,
183 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000184};
185
Tom Stellard80efb162016-01-07 03:59:08 +0000186class AMDGPUTargetInfo final : public TargetInfo {
187public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000188 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000189 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
190 uint64_t SA, uint64_t ZA = 0,
191 uint8_t *PairedLoc = nullptr) const override;
192};
193
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000194template <class ELFT> class MipsTargetInfo final : public TargetInfo {
195public:
196 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000197 unsigned getDynRel(unsigned Type) const override;
198 void writeGotHeader(uint8_t *Buf) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000199 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
200 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000201 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000202 uint64_t SA, uint64_t ZA = 0,
203 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000204 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000205 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000206};
207} // anonymous namespace
208
Rui Ueyama91004392015-10-13 16:08:15 +0000209TargetInfo *createTarget() {
210 switch (Config->EMachine) {
211 case EM_386:
212 return new X86TargetInfo();
213 case EM_AARCH64:
214 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000215 case EM_AMDGPU:
216 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000217 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000218 switch (Config->EKind) {
219 case ELF32LEKind:
220 return new MipsTargetInfo<ELF32LE>();
221 case ELF32BEKind:
222 return new MipsTargetInfo<ELF32BE>();
223 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000224 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000225 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000226 case EM_PPC:
227 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000228 case EM_PPC64:
229 return new PPC64TargetInfo();
230 case EM_X86_64:
231 return new X86_64TargetInfo();
232 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000233 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000234}
235
Rafael Espindola01205f72015-09-22 18:19:46 +0000236TargetInfo::~TargetInfo() {}
237
Rui Ueyamabaf16512016-01-29 00:20:12 +0000238bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000239 return false;
240}
241
Igor Kudrinf6f45472015-11-10 08:39:27 +0000242uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
243
Rui Ueyama02dfd492015-12-17 01:18:40 +0000244bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000245 return false;
246}
247
Rui Ueyama012eb782016-01-29 04:05:09 +0000248bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
249 return Type == TlsLocalDynamicRel;
250}
251
252bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
253 return Type == TlsGlobalDynamicRel;
254}
255
256bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
257 return false;
258}
259
George Rimarbfb7bf72015-12-21 10:00:12 +0000260bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000261bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000262bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000263bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000264
Rui Ueyama012eb782016-01-29 04:05:09 +0000265bool TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
266 return false;
267}
268
269bool TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
270 return false;
271}
272
Rui Ueyamac516ae12016-01-29 02:33:45 +0000273unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
274 uint64_t P, uint64_t SA,
275 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000276 return 0;
277}
George Rimar77d1cb12015-11-24 09:00:06 +0000278
Rafael Espindola7f074422015-09-22 21:35:51 +0000279X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000280 CopyRel = R_386_COPY;
281 GotRel = R_386_GLOB_DAT;
282 PltRel = R_386_JUMP_SLOT;
283 IRelativeRel = R_386_IRELATIVE;
284 RelativeRel = R_386_RELATIVE;
285 TlsGotRel = R_386_TLS_TPOFF;
286 TlsGlobalDynamicRel = R_386_TLS_GD;
287 TlsLocalDynamicRel = R_386_TLS_LDM;
288 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
289 TlsOffsetRel = R_386_TLS_DTPOFF32;
290 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000291 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000292 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000293}
294
Rui Ueyamac516ae12016-01-29 02:33:45 +0000295void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000296 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
297}
298
Rui Ueyamac516ae12016-01-29 02:33:45 +0000299void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000300 // Entries in .got.plt initially points back to the corresponding
301 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000302 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000303}
Rafael Espindola01205f72015-09-22 18:19:46 +0000304
Rui Ueyamac516ae12016-01-29 02:33:45 +0000305unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000306 if (Type == R_386_TLS_LE)
307 return R_386_TLS_TPOFF;
308 if (Type == R_386_TLS_LE_32)
309 return R_386_TLS_TPOFF32;
310 return Type;
311}
312
Rui Ueyama724d6252016-01-29 01:49:32 +0000313unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000314 if (Type == R_386_TLS_IE)
315 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000316 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000317}
318
Rui Ueyamac516ae12016-01-29 02:33:45 +0000319bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000320 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
321 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000322 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000323 if (Type == R_386_TLS_IE)
324 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000325 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000326}
327
Rui Ueyama900e2d22016-01-29 03:51:49 +0000328void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000329 // Executable files and shared object files have
330 // separate procedure linkage tables.
331 if (Config->Shared) {
332 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000333 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000334 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
335 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000336 };
337 memcpy(Buf, V, sizeof(V));
338 return;
339 }
George Rimar648a2c32015-10-20 08:54:27 +0000340
George Rimar77b77792015-11-25 22:15:01 +0000341 const uint8_t PltData[] = {
342 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000343 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
344 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000345 };
346 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000347 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000348 write32le(Buf + 2, Got + 4);
349 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000350}
351
Rui Ueyama9398f862016-01-29 04:15:02 +0000352void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
353 uint64_t PltEntryAddr, int32_t Index,
354 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000355 const uint8_t Inst[] = {
356 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
357 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
358 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
359 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000360 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000361
George Rimar77b77792015-11-25 22:15:01 +0000362 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
363 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000364 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
365 : Out<ELF32LE>::Got->getVA();
366 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000367 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000368 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000369}
370
Rui Ueyama02dfd492015-12-17 01:18:40 +0000371bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000372 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
373 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
374 return SS->Sym.getType() == STT_OBJECT;
375 return false;
376}
377
Rui Ueyamac516ae12016-01-29 02:33:45 +0000378bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000379 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000380 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000381 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000382 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000383 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000384}
385
Rui Ueyamac516ae12016-01-29 02:33:45 +0000386bool X86TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000387 return isGnuIFunc<ELF32LE>(S) ||
388 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000389 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000390}
391
George Rimarbfb7bf72015-12-21 10:00:12 +0000392bool X86TargetInfo::isGotRelative(uint32_t Type) const {
393 // This relocation does not require got entry,
394 // but it is relative to got and needs it to be created.
395 // Here we request for that.
396 return Type == R_386_GOTOFF;
397}
398
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000399void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000400 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000401 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000402 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000403 case R_386_32:
404 add32le(Loc, SA);
405 break;
George Rimarffb67352016-01-19 11:00:48 +0000406 case R_386_GOT32: {
407 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
408 Out<ELF32LE>::Got->getNumEntries() * 4;
409 checkInt<32>(V, Type);
410 add32le(Loc, V);
411 break;
412 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000413 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000414 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000415 break;
George Rimar13934772015-11-25 20:20:31 +0000416 case R_386_GOTPC:
417 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
418 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000419 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000420 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000421 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000422 break;
George Rimar9db204a2015-12-02 09:58:20 +0000423 case R_386_TLS_GD:
424 case R_386_TLS_LDM:
425 case R_386_TLS_TPOFF: {
426 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
427 Out<ELF32LE>::Got->getNumEntries() * 4;
428 checkInt<32>(V, Type);
429 write32le(Loc, V);
430 break;
431 }
George Rimar6f17e092015-12-17 09:32:21 +0000432 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000433 case R_386_TLS_LDO_32:
434 write32le(Loc, SA);
435 break;
George Rimard23970f2015-11-25 20:41:53 +0000436 case R_386_TLS_LE:
437 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
438 break;
439 case R_386_TLS_LE_32:
440 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
441 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000442 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000443 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000444 }
445}
446
Rui Ueyamabaf16512016-01-29 00:20:12 +0000447bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000448 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000449 return false;
450 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
451 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000452 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000453 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
454}
455
Rui Ueyamac516ae12016-01-29 02:33:45 +0000456bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000457 return Config->Shared && Type == R_386_TLS_IE;
458}
459
Rui Ueyamac516ae12016-01-29 02:33:45 +0000460unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
461 uint64_t P, uint64_t SA,
462 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000463 switch (Type) {
464 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000465 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000466 relocateTlsGdToIe(Loc, BufEnd, P, SA);
467 else
468 relocateTlsGdToLe(Loc, BufEnd, P, SA);
469 // The next relocation should be against __tls_get_addr, so skip it
470 return 1;
471 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000472 case R_386_TLS_IE:
473 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000474 return 0;
475 case R_386_TLS_LDM:
476 relocateTlsLdToLe(Loc, BufEnd, P, SA);
477 // The next relocation should be against __tls_get_addr, so skip it
478 return 1;
479 case R_386_TLS_LDO_32:
480 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
481 return 0;
482 }
483 llvm_unreachable("Unknown TLS optimization");
484}
485
486// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
487// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
488// how GD can be optimized to IE:
489// leal x@tlsgd(, %ebx, 1),
490// call __tls_get_addr@plt
491// Is converted to:
492// movl %gs:0, %eax
493// addl x@gotntpoff(%ebx), %eax
494void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
495 uint64_t SA) const {
496 const uint8_t Inst[] = {
497 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
498 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
499 };
500 memcpy(Loc - 3, Inst, sizeof(Inst));
501 relocateOne(Loc + 5, BufEnd, R_386_32, P,
502 SA - Out<ELF32LE>::Got->getVA() -
503 Out<ELF32LE>::Got->getNumEntries() * 4);
504}
505
506// GD can be optimized to LE:
507// leal x@tlsgd(, %ebx, 1),
508// call __tls_get_addr@plt
509// Can be converted to:
510// movl %gs:0,%eax
511// addl $x@ntpoff,%eax
512// But gold emits subl $foo@tpoff,%eax instead of addl.
513// These instructions are completely equal in behavior.
514// This method generates subl to be consistent with gold.
515void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
516 uint64_t SA) const {
517 const uint8_t Inst[] = {
518 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
519 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
520 };
521 memcpy(Loc - 3, Inst, sizeof(Inst));
522 relocateOne(Loc + 5, BufEnd, R_386_32, P,
523 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
524}
525
526// LD can be optimized to LE:
527// leal foo(%reg),%eax
528// call ___tls_get_addr
529// Is converted to:
530// movl %gs:0,%eax
531// nop
532// leal 0(%esi,1),%esi
533void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
534 uint64_t SA) const {
535 const uint8_t Inst[] = {
536 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
537 0x90, // nop
538 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
539 };
540 memcpy(Loc - 2, Inst, sizeof(Inst));
541}
542
George Rimar6f17e092015-12-17 09:32:21 +0000543// In some conditions, relocations can be optimized to avoid using GOT.
544// This function does that for Initial Exec to Local Exec case.
545// Read "ELF Handling For Thread-Local Storage, 5.1
546// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000547// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000548void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
549 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000550 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000551 // Ulrich's document section 6.2 says that @gotntpoff can
552 // be used with MOVL or ADDL instructions.
553 // @indntpoff is similar to @gotntpoff, but for use in
554 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000555 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000556 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000557 uint8_t Reg = (Loc[-1] >> 3) & 7;
558 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000559 if (Type == R_386_TLS_IE) {
560 // For R_386_TLS_IE relocation we perform the next transformations:
561 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
562 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
563 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
564 // First one is special because when EAX is used the sequence is 5 bytes
565 // long, otherwise it is 6 bytes.
566 if (*Op == 0xa1) {
567 *Op = 0xb8;
568 } else {
569 *Inst = IsMov ? 0xc7 : 0x81;
570 *Op = 0xc0 | ((*Op >> 3) & 7);
571 }
572 } else {
573 // R_386_TLS_GOTIE relocation can be optimized to
574 // R_386_TLS_LE so that it does not use GOT.
575 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
576 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
577 // Note: gold converts to ADDL instead of LEAL.
578 *Inst = IsMov ? 0xc7 : 0x8d;
579 if (IsMov)
580 *Op = 0xc0 | ((*Op >> 3) & 7);
581 else
582 *Op = 0x80 | Reg | (Reg << 3);
583 }
George Rimar2558e122015-12-09 09:55:54 +0000584 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
585}
586
Rafael Espindola7f074422015-09-22 21:35:51 +0000587X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000588 CopyRel = R_X86_64_COPY;
589 GotRel = R_X86_64_GLOB_DAT;
590 PltRel = R_X86_64_JUMP_SLOT;
591 RelativeRel = R_X86_64_RELATIVE;
592 IRelativeRel = R_X86_64_IRELATIVE;
593 TlsGotRel = R_X86_64_TPOFF64;
594 TlsLocalDynamicRel = R_X86_64_TLSLD;
595 TlsGlobalDynamicRel = R_X86_64_TLSGD;
596 TlsModuleIndexRel = R_X86_64_DTPMOD64;
597 TlsOffsetRel = R_X86_64_DTPOFF64;
598 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000599 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000600 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000601}
602
Rui Ueyamac516ae12016-01-29 02:33:45 +0000603void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000604 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
605}
606
Rui Ueyamac516ae12016-01-29 02:33:45 +0000607void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000608 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000609 write32le(Buf, Plt + 6);
610}
611
Rui Ueyama900e2d22016-01-29 03:51:49 +0000612void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000613 const uint8_t PltData[] = {
614 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
615 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
616 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
617 };
618 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000619 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
620 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
621 write32le(Buf + 2, Got - Plt + 2); // GOT+8
622 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000623}
Rafael Espindola01205f72015-09-22 18:19:46 +0000624
Rui Ueyama9398f862016-01-29 04:15:02 +0000625void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
626 uint64_t PltEntryAddr, int32_t Index,
627 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000628 const uint8_t Inst[] = {
629 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
630 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
631 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
632 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000633 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000634
George Rimar648a2c32015-10-20 08:54:27 +0000635 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
636 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000637 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000638}
639
Rui Ueyama02dfd492015-12-17 01:18:40 +0000640bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000641 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
642 Type == R_X86_64_64)
643 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
644 return SS->Sym.getType() == STT_OBJECT;
645 return false;
646}
647
Rui Ueyamac516ae12016-01-29 02:33:45 +0000648bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000649 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000650 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000651 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000652 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000653 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000654}
655
Rui Ueyamac516ae12016-01-29 02:33:45 +0000656bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000657 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000658}
659
Rui Ueyamac516ae12016-01-29 02:33:45 +0000660bool X86_64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000661 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000662 return false;
George Rimara07ff662015-12-21 10:12:06 +0000663 if (isGnuIFunc<ELF64LE>(S))
664 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000665
Rafael Espindola01205f72015-09-22 18:19:46 +0000666 switch (Type) {
667 default:
668 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000669 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000670 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000671 case R_X86_64_PC32:
672 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000673 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000674 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000675 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000676 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000677 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
678 // libc.so.
679 //
680 // The general idea on how to handle such cases is to create a PLT entry
681 // and use that as the function value.
682 //
683 // For the static linking part, we just return true and everything else
684 // will use the the PLT entry as the address.
685 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000686 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000687 // still works. We need the help of the dynamic linker for that. We
688 // let it know that we have a direct reference to a so symbol by creating
689 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000690 // dynamic linker resolves the symbol to the value of the symbol we created.
691 // This is true even for got entries, so pointer equality is maintained.
692 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000693 // real function is a dedicated got entry used by the plt. That is
694 // identified by special relocation types (R_X86_64_JUMP_SLOT,
695 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000696 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000697 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000698 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000699 }
700}
Rafael Espindolac4010882015-09-22 20:54:08 +0000701
Rafael Espindolaae244002015-10-05 19:30:12 +0000702bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
703 switch (Type) {
704 default:
705 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000706 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000707 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000708 case R_X86_64_PC8:
709 case R_X86_64_PC16:
710 case R_X86_64_PC32:
711 case R_X86_64_PC64:
712 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000713 return true;
714 }
715}
716
Rui Ueyamac516ae12016-01-29 02:33:45 +0000717bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000718 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000719}
720
Rui Ueyamabaf16512016-01-29 00:20:12 +0000721bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000722 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000723 return false;
George Rimar25411f252015-12-04 11:20:13 +0000724 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
725 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000726 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
727}
728
729// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
730// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
731// how LD can be optimized to LE:
732// leaq bar@tlsld(%rip), %rdi
733// callq __tls_get_addr@PLT
734// leaq bar@dtpoff(%rax), %rcx
735// Is converted to:
736// .word 0x6666
737// .byte 0x66
738// mov %fs:0,%rax
739// leaq bar@tpoff(%rax), %rcx
740void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
741 uint64_t P, uint64_t SA) const {
742 const uint8_t Inst[] = {
743 0x66, 0x66, //.word 0x6666
744 0x66, //.byte 0x66
745 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
746 };
747 memcpy(Loc - 3, Inst, sizeof(Inst));
748}
749
750// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
751// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
752// how GD can be optimized to LE:
753// .byte 0x66
754// leaq x@tlsgd(%rip), %rdi
755// .word 0x6666
756// rex64
757// call __tls_get_addr@plt
758// Is converted to:
759// mov %fs:0x0,%rax
760// lea x@tpoff,%rax
761void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
762 uint64_t P, uint64_t SA) const {
763 const uint8_t Inst[] = {
764 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
765 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
766 };
767 memcpy(Loc - 4, Inst, sizeof(Inst));
768 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000769}
770
George Rimar25411f252015-12-04 11:20:13 +0000771// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
772// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
773// how GD can be optimized to IE:
774// .byte 0x66
775// leaq x@tlsgd(%rip), %rdi
776// .word 0x6666
777// rex64
778// call __tls_get_addr@plt
779// Is converted to:
780// mov %fs:0x0,%rax
781// addq x@tpoff,%rax
782void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
783 uint64_t P, uint64_t SA) const {
784 const uint8_t Inst[] = {
785 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
786 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
787 };
788 memcpy(Loc - 4, Inst, sizeof(Inst));
789 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
790}
791
George Rimar77d1cb12015-11-24 09:00:06 +0000792// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000793// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000794// This function does that. Read "ELF Handling For Thread-Local Storage,
795// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
796// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000797void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
798 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000799 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
800 // used in MOVQ or ADDQ instructions only.
801 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
802 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
803 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
804 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
805 uint8_t *Prefix = Loc - 3;
806 uint8_t *Inst = Loc - 2;
807 uint8_t *RegSlot = Loc - 1;
808 uint8_t Reg = Loc[-1] >> 3;
809 bool IsMov = *Inst == 0x8b;
810 bool RspAdd = !IsMov && Reg == 4;
811 // r12 and rsp registers requires special handling.
812 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
813 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
814 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
815 // The same true for rsp. So we convert to addq for them, saving 1 byte that
816 // we dont have.
817 if (RspAdd)
818 *Inst = 0x81;
819 else
820 *Inst = IsMov ? 0xc7 : 0x8d;
821 if (*Prefix == 0x4c)
822 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
823 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
824 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
825}
826
George Rimar6713cf82015-11-25 21:46:05 +0000827// This function applies a TLS relocation with an optimization as described
828// in the Ulrich's document. As a result of rewriting instructions at the
829// relocation target, relocations immediately follow the TLS relocation (which
830// would be applied to rewritten instructions) may have to be skipped.
831// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000832unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
833 uint32_t Type, uint64_t P, uint64_t SA,
834 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000835 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000836 case R_X86_64_DTPOFF32:
837 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
838 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000839 case R_X86_64_GOTTPOFF:
840 relocateTlsIeToLe(Loc, BufEnd, P, SA);
841 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000842 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000843 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000844 relocateTlsGdToIe(Loc, BufEnd, P, SA);
845 else
846 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000847 // The next relocation should be against __tls_get_addr, so skip it
848 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000849 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000850 case R_X86_64_TLSLD:
851 relocateTlsLdToLe(Loc, BufEnd, P, SA);
852 // The next relocation should be against __tls_get_addr, so skip it
853 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000854 }
855 llvm_unreachable("Unknown TLS optimization");
856}
857
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000858void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000859 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000860 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000861 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000862 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000863 checkUInt<32>(SA, Type);
864 write32le(Loc, SA);
865 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000866 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000867 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000868 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000869 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000870 case R_X86_64_64:
871 write64le(Loc, SA);
872 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000873 case R_X86_64_DTPOFF32:
874 write32le(Loc, SA);
875 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000876 case R_X86_64_GOTPCREL:
877 case R_X86_64_PC32:
878 case R_X86_64_PLT32:
879 case R_X86_64_TLSGD:
880 case R_X86_64_TLSLD:
881 write32le(Loc, SA - P);
882 break;
George Rimar48651482015-12-11 08:59:37 +0000883 case R_X86_64_SIZE32:
884 write32le(Loc, ZA);
885 break;
886 case R_X86_64_SIZE64:
887 write64le(Loc, ZA);
888 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000889 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000890 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000891 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000892 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000893 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000894 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000895 case R_X86_64_TPOFF64:
896 write32le(Loc, SA - P);
897 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000898 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000899 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000900 }
901}
902
Hal Finkel3c8cc672015-10-12 20:56:18 +0000903// Relocation masks following the #lo(value), #hi(value), #ha(value),
904// #higher(value), #highera(value), #highest(value), and #highesta(value)
905// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
906// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000907static uint16_t applyPPCLo(uint64_t V) { return V; }
908static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
909static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
910static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
911static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000912static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000913static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
914
Davide Italiano8c3444362016-01-11 19:45:33 +0000915PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000916bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
917
918void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
919 uint64_t P, uint64_t SA, uint64_t ZA,
920 uint8_t *PairedLoc) const {
921 switch (Type) {
922 case R_PPC_ADDR16_HA:
923 write16be(Loc, applyPPCHa(SA));
924 break;
925 case R_PPC_ADDR16_LO:
926 write16be(Loc, applyPPCLo(SA));
927 break;
928 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000929 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000930 }
931}
932
Rafael Espindolac4010882015-09-22 20:54:08 +0000933PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000934 GotRel = R_PPC64_GLOB_DAT;
935 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000936 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000937
938 // We need 64K pages (at least under glibc/Linux, the loader won't
939 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000940 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000941
942 // The PPC64 ELF ABI v1 spec, says:
943 //
944 // It is normally desirable to put segments with different characteristics
945 // in separate 256 Mbyte portions of the address space, to give the
946 // operating system full paging flexibility in the 64-bit address space.
947 //
948 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
949 // use 0x10000000 as the starting address.
950 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000951}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000952
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000953uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000954 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
955 // order. The TOC starts where the first of these sections starts.
956
957 // FIXME: This obviously does not do the right thing when there is no .got
958 // section, but there is a .toc or .tocbss section.
959 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
960 if (!TocVA)
961 TocVA = Out<ELF64BE>::Plt->getVA();
962
963 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
964 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
965 // code (crt1.o) assumes that you can get from the TOC base to the
966 // start of the .toc section with only a single (signed) 16-bit relocation.
967 return TocVA + 0x8000;
968}
969
Rui Ueyama9398f862016-01-29 04:15:02 +0000970void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
971 uint64_t PltEntryAddr, int32_t Index,
972 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000973 uint64_t Off = GotEntryAddr - getPPC64TocBase();
974
975 // FIXME: What we should do, in theory, is get the offset of the function
976 // descriptor in the .opd section, and use that as the offset from %r2 (the
977 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
978 // be a pointer to the function descriptor in the .opd section. Using
979 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
980
Hal Finkelfa92f682015-10-13 21:47:34 +0000981 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
983 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
984 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
985 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
986 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
987 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
988 write32be(Buf + 28, 0x4e800420); // bctr
989}
990
Rui Ueyamac516ae12016-01-29 02:33:45 +0000991bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
992 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000993 return true;
994
995 switch (Type) {
996 default: return false;
997 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000998 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000999 case R_PPC64_GOT16_HA:
1000 case R_PPC64_GOT16_HI:
1001 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001002 case R_PPC64_GOT16_LO_DS:
1003 return true;
1004 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001005}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001006
Rui Ueyamac516ae12016-01-29 02:33:45 +00001007bool PPC64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001008 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001009 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001010}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001011
Hal Finkelbe0823d2015-10-12 20:58:52 +00001012bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1013 switch (Type) {
1014 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001015 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001016 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001017 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001018 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001019 }
1020}
1021
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001022void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001023 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001024 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001025 uint64_t TB = getPPC64TocBase();
1026
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1028 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001029 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001030 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1031 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001032 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1033 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001034 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1035 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001036 default: break;
1037 }
1038
Hal Finkel3c8cc672015-10-12 20:56:18 +00001039 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001040 case R_PPC64_ADDR14: {
1041 checkAlignment<4>(SA, Type);
1042 // Preserve the AA/LK bits in the branch instruction
1043 uint8_t AALK = Loc[3];
1044 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1045 break;
1046 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001048 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001049 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001050 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001052 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001053 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001054 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001055 case R_PPC64_ADDR16_HA:
1056 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001057 break;
1058 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001059 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001062 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 break;
1064 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001065 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001066 break;
1067 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001068 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001069 break;
1070 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001071 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001072 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001073 case R_PPC64_ADDR16_LO:
1074 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001075 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001076 case R_PPC64_ADDR16_LO_DS:
1077 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 break;
1079 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001080 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001081 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001083 case R_PPC64_ADDR64:
1084 write64be(Loc, SA);
1085 break;
1086 case R_PPC64_REL16_HA:
1087 write16be(Loc, applyPPCHa(SA - P));
1088 break;
1089 case R_PPC64_REL16_HI:
1090 write16be(Loc, applyPPCHi(SA - P));
1091 break;
1092 case R_PPC64_REL16_LO:
1093 write16be(Loc, applyPPCLo(SA - P));
1094 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001095 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001096 // If we have an undefined weak symbol, we might get here with a symbol
1097 // address of zero. That could overflow, but the code must be unreachable,
1098 // so don't bother doing anything at all.
1099 if (!SA)
1100 break;
1101
Hal Finkeldaedc122015-10-12 23:16:53 +00001102 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1103 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001104 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001105
1106 if (!InPlt && Out<ELF64BE>::Opd) {
1107 // If this is a local call, and we currently have the address of a
1108 // function-descriptor, get the underlying code address instead.
1109 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1110 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001111 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001112
1113 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001114 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001115 }
1116
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001118 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001120
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001121 uint32_t Nop = 0x60000000;
1122 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1123 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 break;
1125 }
1126 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001127 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001129 break;
1130 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001131 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001133 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001134 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001135 break;
1136 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001137 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001138 }
1139}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001140
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001141AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001142 CopyRel = R_AARCH64_COPY;
1143 IRelativeRel = R_AARCH64_IRELATIVE;
1144 GotRel = R_AARCH64_GLOB_DAT;
1145 PltRel = R_AARCH64_JUMP_SLOT;
1146 TlsGotRel = R_AARCH64_TLS_TPREL64;
1147 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001148 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001149 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001150}
George Rimar648a2c32015-10-20 08:54:27 +00001151
Rui Ueyamac516ae12016-01-29 02:33:45 +00001152unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001153 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1154 return Type;
1155 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001156 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001157 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001158 // Keep it going with a dummy value so that we can find more reloc errors.
1159 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001160}
1161
Rui Ueyamac516ae12016-01-29 02:33:45 +00001162void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001163 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1164}
1165
Rui Ueyama900e2d22016-01-29 03:51:49 +00001166void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001167 const uint8_t PltData[] = {
1168 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1169 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1170 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1171 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1172 0x20, 0x02, 0x1f, 0xd6, // br x17
1173 0x1f, 0x20, 0x03, 0xd5, // nop
1174 0x1f, 0x20, 0x03, 0xd5, // nop
1175 0x1f, 0x20, 0x03, 0xd5 // nop
1176 };
1177 memcpy(Buf, PltData, sizeof(PltData));
1178
Rui Ueyama900e2d22016-01-29 03:51:49 +00001179 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1180 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1181 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1182 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1183 Got + 16);
1184 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1185 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001186}
1187
Rui Ueyama9398f862016-01-29 04:15:02 +00001188void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1189 uint64_t PltEntryAddr, int32_t Index,
1190 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001191 const uint8_t Inst[] = {
1192 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1193 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1194 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1195 0x20, 0x02, 0x1f, 0xd6 // br x17
1196 };
1197 memcpy(Buf, Inst, sizeof(Inst));
1198
1199 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1200 GotEntryAddr);
1201 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1202 GotEntryAddr);
1203 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1204 GotEntryAddr);
1205}
1206
Rui Ueyama724d6252016-01-29 01:49:32 +00001207unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar3d737e42016-01-13 13:04:46 +00001208 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1209 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1210 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +00001211 return TlsGotRel;
George Rimar3d737e42016-01-13 13:04:46 +00001212}
1213
Rui Ueyamac516ae12016-01-29 02:33:45 +00001214bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001215 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1216 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1217}
1218
Rui Ueyama02dfd492015-12-17 01:18:40 +00001219bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001220 if (Config->Shared)
1221 return false;
1222 switch (Type) {
1223 default:
1224 return false;
1225 case R_AARCH64_ABS16:
1226 case R_AARCH64_ABS32:
1227 case R_AARCH64_ABS64:
1228 case R_AARCH64_ADD_ABS_LO12_NC:
1229 case R_AARCH64_ADR_PREL_LO21:
1230 case R_AARCH64_ADR_PREL_PG_HI21:
1231 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001232 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001233 case R_AARCH64_LDST32_ABS_LO12_NC:
1234 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001235 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001236 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1237 return SS->Sym.getType() == STT_OBJECT;
1238 return false;
1239 }
1240}
1241
Rui Ueyamac516ae12016-01-29 02:33:45 +00001242bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001243 switch (Type) {
1244 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1245 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1246 case R_AARCH64_ADR_GOT_PAGE:
1247 case R_AARCH64_LD64_GOT_LO12_NC:
1248 return true;
1249 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001250 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001251 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001252}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001253
Rui Ueyamac516ae12016-01-29 02:33:45 +00001254bool AArch64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001255 if (isGnuIFunc<ELF64LE>(S))
1256 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001257 switch (Type) {
1258 default:
1259 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001260 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001261 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001262 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001263 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001264 return canBePreempted(&S, true);
1265 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001266}
Davide Italiano1d750a62015-09-27 08:45:38 +00001267
Davide Italianoef4be6b2015-10-06 19:01:32 +00001268static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001269 uint32_t ImmLo = (Imm & 0x3) << 29;
1270 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1271 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001272 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001273}
1274
Davide Italiano318ca222015-10-02 22:13:51 +00001275// Page(Expr) is the page address of the expression Expr, defined
1276// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001277// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001278static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001279 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001280}
1281
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001282void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001283 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001284 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001285 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001286 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001287 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001288 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001289 break;
1290 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001291 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001292 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001293 break;
1294 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001295 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001296 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001297 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001298 // This relocation stores 12 bits and there's no instruction
1299 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001300 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1301 // bits in Loc are zero.
1302 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001303 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001304 case R_AARCH64_ADR_GOT_PAGE: {
1305 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1306 checkInt<33>(X, Type);
1307 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1308 break;
1309 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001310 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001311 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001312 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001313 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001314 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001315 }
George Rimar3d737e42016-01-13 13:04:46 +00001316 case R_AARCH64_ADR_PREL_PG_HI21:
1317 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001318 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001319 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001320 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001321 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001322 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001323 case R_AARCH64_CALL26:
1324 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001325 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001326 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001327 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1328 break;
1329 }
George Rimar4102bfb2016-01-11 14:22:00 +00001330 case R_AARCH64_CONDBR19: {
1331 uint64_t X = SA - P;
1332 checkInt<21>(X, Type);
1333 or32le(Loc, (X & 0x1FFFFC) << 3);
1334 break;
1335 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001336 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001337 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001338 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001339 or32le(Loc, (SA & 0xFF8) << 7);
1340 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001341 case R_AARCH64_LDST128_ABS_LO12_NC:
1342 or32le(Loc, (SA & 0x0FF8) << 6);
1343 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001344 case R_AARCH64_LDST16_ABS_LO12_NC:
1345 or32le(Loc, (SA & 0x0FFC) << 9);
1346 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001347 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001348 or32le(Loc, (SA & 0xFFF) << 10);
1349 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001350 case R_AARCH64_LDST32_ABS_LO12_NC:
1351 or32le(Loc, (SA & 0xFFC) << 8);
1352 break;
1353 case R_AARCH64_LDST64_ABS_LO12_NC:
1354 or32le(Loc, (SA & 0xFF8) << 7);
1355 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001356 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001357 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001358 write16le(Loc, SA - P);
1359 break;
1360 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001361 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001362 write32le(Loc, SA - P);
1363 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001364 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001365 write64le(Loc, SA - P);
1366 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001367 case R_AARCH64_TSTBR14: {
1368 uint64_t X = SA - P;
1369 checkInt<16>(X, Type);
1370 or32le(Loc, (X & 0xFFFC) << 3);
1371 break;
1372 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001373 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001374 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001375 }
1376}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001377
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001378// Implementing relocations for AMDGPU is low priority since most
1379// programs don't use relocations now. Thus, this function is not
1380// actually called (relocateOne is called for each relocation).
1381// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001382void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1383 uint64_t P, uint64_t SA, uint64_t ZA,
1384 uint8_t *PairedLoc) const {
1385 llvm_unreachable("not implemented");
1386}
1387
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001388template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001389 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001390 GotHeaderEntriesNum = 2;
Rui Ueyama724d6252016-01-29 01:49:32 +00001391 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001392}
1393
1394template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001395unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001396 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1397 return R_MIPS_REL32;
1398 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001399 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001400 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001401 // Keep it going with a dummy value so that we can find more reloc errors.
1402 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001403}
1404
1405template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001406void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001407 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001408 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1409
1410 // Set the MSB of the second GOT slot. This is not required by any
1411 // MIPS ABI documentation, though.
1412 //
1413 // There is a comment in glibc saying that "The MSB of got[1] of a
1414 // gnu object is set to identify gnu objects," and in GNU gold it
1415 // says "the second entry will be used by some runtime loaders".
1416 // But how this field is being used is unclear.
1417 //
1418 // We are not really willing to mimic other linkers behaviors
1419 // without understanding why they do that, but because all files
1420 // generated by GNU tools have this special GOT value, and because
1421 // we've been doing this for years, it is probably a safe bet to
1422 // keep doing this for now. We really need to revisit this to see
1423 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001424 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001425 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001426}
1427
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001428template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001429bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001430 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001431}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001432
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001433template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001434bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001435 return false;
1436}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001437
Simon Atanasyan35031192015-12-15 06:06:34 +00001438static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001439
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001440template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001441static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1442 uint64_t SA) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001443 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001444 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001445 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1446 if (SHIFT > 0)
1447 checkAlignment<(1 << SHIFT)>(SA + A, Type);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001448 int64_t V = SA + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001449 checkInt<BSIZE + SHIFT>(V, Type);
1450 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001451}
1452
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001453template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001454void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001455 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001456 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001457 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001458 switch (Type) {
1459 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001460 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001461 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001462 case R_MIPS_CALL16:
1463 case R_MIPS_GOT16: {
1464 int64_t V = SA - getMipsGpAddr<ELFT>();
1465 if (Type == R_MIPS_GOT16)
1466 checkInt<16>(V, Type);
1467 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1468 break;
1469 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001470 case R_MIPS_GPREL16: {
1471 uint32_t Instr = read32<E>(Loc);
1472 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1473 checkInt<16>(V, Type);
1474 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1475 break;
1476 }
1477 case R_MIPS_GPREL32:
1478 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1479 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001480 case R_MIPS_HI16: {
1481 uint32_t Instr = read32<E>(Loc);
1482 if (PairedLoc) {
1483 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001484 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001485 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001486 } else {
1487 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001488 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001489 }
1490 break;
1491 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001492 case R_MIPS_JALR:
1493 // Ignore this optimization relocation for now
1494 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001495 case R_MIPS_LO16: {
1496 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001497 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001498 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1499 break;
1500 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001501 case R_MIPS_PC16:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001502 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001503 break;
1504 case R_MIPS_PC19_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001505 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001506 break;
1507 case R_MIPS_PC21_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001508 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001509 break;
1510 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001511 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, SA);
1512 break;
1513 case R_MIPS_PC32:
1514 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001515 break;
1516 case R_MIPS_PCHI16: {
1517 uint32_t Instr = read32<E>(Loc);
1518 if (PairedLoc) {
1519 uint64_t AHL = ((Instr & 0xffff) << 16) +
1520 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1521 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1522 } else {
1523 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1524 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1525 }
1526 break;
1527 }
1528 case R_MIPS_PCLO16: {
1529 uint32_t Instr = read32<E>(Loc);
1530 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1531 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1532 break;
1533 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001534 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001535 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001536 }
1537}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001538
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001539template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001540bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001541 return Type == R_MIPS_JALR;
1542}
1543
1544template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001545bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1546 switch (Type) {
1547 default:
1548 return false;
1549 case R_MIPS_PC16:
1550 case R_MIPS_PC19_S2:
1551 case R_MIPS_PC21_S2:
1552 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001553 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001554 case R_MIPS_PCHI16:
1555 case R_MIPS_PCLO16:
1556 return true;
1557 }
1558}
1559
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001560// _gp is a MIPS-specific ABI-defined symbol which points to
1561// a location that is relative to GOT. This function returns
1562// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001563template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001564 unsigned GPOffset = 0x7ff0;
1565 if (uint64_t V = Out<ELFT>::Got->getVA())
1566 return V + GPOffset;
1567 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001568}
1569
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001570template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1571template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1572template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1573template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1574
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001575template uint32_t getMipsGpAddr<ELF32LE>();
1576template uint32_t getMipsGpAddr<ELF32BE>();
1577template uint64_t getMipsGpAddr<ELF64LE>();
1578template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001579}
1580}