blob: 5b05dfeb8e9fcb8a457f62f4c9ee9ee76bd63922 [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
Rui Ueyamac1c282a2016-02-11 21:18:01 +000036TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000037
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;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +000086 bool isTlsLocalDynamicRel(unsigned Type) const override;
87 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000088 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
89 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000090 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000091 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
92 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000093 bool needsCopyRelImpl(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000094 bool needsDynRelative(unsigned Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000095 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +000096 bool needsPltImpl(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000097 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000098 uint64_t SA, uint64_t ZA = 0,
99 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000100 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000101 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
102 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000103 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000104 bool refersToGotEntry(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000105
106private:
107 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
108 uint64_t SA) const;
109 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
110 uint64_t SA) const;
111 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
112 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000113 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
114 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000115};
116
117class X86_64TargetInfo final : public TargetInfo {
118public:
119 X86_64TargetInfo();
George Rimar2960c982016-02-11 11:14:46 +0000120 unsigned getTlsGotRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000121 bool isTlsLocalDynamicRel(unsigned Type) const override;
122 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000123 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
124 void writeGotPltHeader(uint8_t *Buf) const override;
125 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000126 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000127 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
128 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000129 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000130 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000131 bool refersToGotEntry(uint32_t Type) const override;
132 bool needsPltImpl(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000133 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000134 uint64_t SA, uint64_t ZA = 0,
135 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000137 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000138 bool isSizeRel(uint32_t Type) const override;
139 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
140 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000141
142private:
143 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
145 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000147 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000149 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
150 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000151};
152
Davide Italiano8c3444362016-01-11 19:45:33 +0000153class PPCTargetInfo final : public TargetInfo {
154public:
155 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000156 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
157 uint64_t SA, uint64_t ZA = 0,
158 uint8_t *PairedLoc = nullptr) const override;
159 bool isRelRelative(uint32_t Type) const override;
160};
161
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000162class PPC64TargetInfo final : public TargetInfo {
163public:
164 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000165 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
166 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000167 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000168 bool needsPltImpl(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000169 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000170 uint64_t SA, uint64_t ZA = 0,
171 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172 bool isRelRelative(uint32_t Type) const override;
173};
174
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000175class AArch64TargetInfo final : public TargetInfo {
176public:
177 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000178 unsigned getDynRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000179 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000180 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000181 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000182 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
183 int32_t Index, unsigned RelOff) const override;
George Rimar2960c982016-02-11 11:14:46 +0000184 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000185 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000186 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000187 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000188 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000189 bool needsPltImpl(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000190 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000191 uint64_t SA, uint64_t ZA = 0,
192 uint8_t *PairedLoc = nullptr) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000193 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
194 uint64_t SA, const SymbolBody *S) const override;
195 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
196
197private:
198 void relocateTlsGdToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
199 uint64_t P, uint64_t SA) const;
200 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
201 uint64_t P, uint64_t SA) const;
202
203 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000204};
205
Tom Stellard80efb162016-01-07 03:59:08 +0000206class AMDGPUTargetInfo final : public TargetInfo {
207public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000208 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000209 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
210 uint64_t SA, uint64_t ZA = 0,
211 uint8_t *PairedLoc = nullptr) const override;
212};
213
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000214template <class ELFT> class MipsTargetInfo final : public TargetInfo {
215public:
216 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000217 unsigned getDynRel(unsigned Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000218 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
219 void writePltZero(uint8_t *Buf) const override;
220 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
221 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000222 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000223 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000224 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000225 bool needsPltImpl(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000226 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000227 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000228 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000229 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000230 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000231};
232} // anonymous namespace
233
Rui Ueyama91004392015-10-13 16:08:15 +0000234TargetInfo *createTarget() {
235 switch (Config->EMachine) {
236 case EM_386:
237 return new X86TargetInfo();
238 case EM_AARCH64:
239 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000240 case EM_AMDGPU:
241 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000242 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000243 switch (Config->EKind) {
244 case ELF32LEKind:
245 return new MipsTargetInfo<ELF32LE>();
246 case ELF32BEKind:
247 return new MipsTargetInfo<ELF32BE>();
248 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000249 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000250 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000251 case EM_PPC:
252 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000253 case EM_PPC64:
254 return new PPC64TargetInfo();
255 case EM_X86_64:
256 return new X86_64TargetInfo();
257 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000258 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000259}
260
Rafael Espindola01205f72015-09-22 18:19:46 +0000261TargetInfo::~TargetInfo() {}
262
Rui Ueyamabaf16512016-01-29 00:20:12 +0000263bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000264 return false;
265}
266
Igor Kudrinf6f45472015-11-10 08:39:27 +0000267uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
268
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000269bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
270
271template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
272 if (Config->Shared)
273 return false;
274 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
275 if (!SS)
276 return false;
277 return SS->Sym.getType() == STT_OBJECT;
278}
279
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000280template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000281bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000282 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000283}
284
Rui Ueyama012eb782016-01-29 04:05:09 +0000285bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
286 return false;
287}
288
George Rimarbfb7bf72015-12-21 10:00:12 +0000289bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000290bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000291bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000292bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000293
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000294bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000295
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000296bool TargetInfo::needsPltImpl(uint32_t Type, const SymbolBody &S) const {
297 return false;
298}
299
300bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
301
302template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000303TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
304 const SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000305 if (needsPltImpl(Type, S))
306 return Plt_Explicit;
307
308 // This handles a non PIC program call to function in a shared library.
309 // In an ideal world, we could just report an error saying the relocation
310 // can overflow at runtime.
311 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
312 // libc.so.
313 //
314 // The general idea on how to handle such cases is to create a PLT entry
315 // and use that as the function value.
316 //
317 // For the static linking part, we just return true and everything else
318 // will use the the PLT entry as the address.
319 //
320 // The remaining problem is making sure pointer equality still works. We
321 // need the help of the dynamic linker for that. We let it know that we have
322 // a direct reference to a so symbol by creating an undefined symbol with a
323 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
324 // the value of the symbol we created. This is true even for got entries, so
325 // pointer equality is maintained. To avoid an infinite loop, the only entry
326 // that points to the real function is a dedicated got entry used by the
327 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
328 // R_386_JMP_SLOT, etc).
329 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
330 if (SS->Sym.getType() == STT_FUNC && !refersToGotEntry(Type))
331 return Plt_Implicit;
332
Rafael Espindola852860e2016-02-12 15:47:37 +0000333 return Plt_No;
334}
Rui Ueyama012eb782016-01-29 04:05:09 +0000335
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000336bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
337 return false;
338}
339
340bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
341 return false;
342}
343
Rui Ueyamac516ae12016-01-29 02:33:45 +0000344unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
345 uint64_t P, uint64_t SA,
346 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000347 return 0;
348}
George Rimar77d1cb12015-11-24 09:00:06 +0000349
Rafael Espindola7f074422015-09-22 21:35:51 +0000350X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000351 CopyRel = R_386_COPY;
352 GotRel = R_386_GLOB_DAT;
353 PltRel = R_386_JUMP_SLOT;
354 IRelativeRel = R_386_IRELATIVE;
355 RelativeRel = R_386_RELATIVE;
356 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000357 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
358 TlsOffsetRel = R_386_TLS_DTPOFF32;
359 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000360 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000361 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000362}
363
Rui Ueyamac516ae12016-01-29 02:33:45 +0000364void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000365 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
366}
367
Rui Ueyamac516ae12016-01-29 02:33:45 +0000368void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000369 // Entries in .got.plt initially points back to the corresponding
370 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000371 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000372}
Rafael Espindola01205f72015-09-22 18:19:46 +0000373
Rui Ueyamac516ae12016-01-29 02:33:45 +0000374unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000375 if (Type == R_386_TLS_LE)
376 return R_386_TLS_TPOFF;
377 if (Type == R_386_TLS_LE_32)
378 return R_386_TLS_TPOFF32;
379 return Type;
380}
381
Rui Ueyama724d6252016-01-29 01:49:32 +0000382unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000383 if (Type == R_386_TLS_IE)
384 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000385 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000386}
387
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000388bool X86TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
389 return Type == R_386_TLS_GD;
390}
391
392bool X86TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
393 return Type == R_386_TLS_LDM;
394}
395
Rui Ueyamac516ae12016-01-29 02:33:45 +0000396bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000397 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
398 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000399 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000400 if (Type == R_386_TLS_IE)
401 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000402 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000403}
404
Rui Ueyama900e2d22016-01-29 03:51:49 +0000405void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000406 // Executable files and shared object files have
407 // separate procedure linkage tables.
408 if (Config->Shared) {
409 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000410 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000411 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
412 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000413 };
414 memcpy(Buf, V, sizeof(V));
415 return;
416 }
George Rimar648a2c32015-10-20 08:54:27 +0000417
George Rimar77b77792015-11-25 22:15:01 +0000418 const uint8_t PltData[] = {
419 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000420 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
421 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000422 };
423 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000424 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000425 write32le(Buf + 2, Got + 4);
426 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000427}
428
Rui Ueyama9398f862016-01-29 04:15:02 +0000429void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
430 uint64_t PltEntryAddr, int32_t Index,
431 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000432 const uint8_t Inst[] = {
433 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
434 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
435 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
436 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000437 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000438
George Rimar77b77792015-11-25 22:15:01 +0000439 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
440 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000441 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
442 : Out<ELF32LE>::Got->getVA();
443 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000444 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000445 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000446}
447
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000448bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
449 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000450}
451
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000452bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000453 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000454 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000455 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000456 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000457 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000458}
459
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000460bool X86TargetInfo::needsPltImpl(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola852860e2016-02-12 15:47:37 +0000461 if (isGnuIFunc<ELF32LE>(S) ||
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000462 (Type == R_386_PLT32 && canBePreempted(&S, true)))
463 return true;
464 return false;
Rafael Espindola01205f72015-09-22 18:19:46 +0000465}
466
George Rimarbfb7bf72015-12-21 10:00:12 +0000467bool X86TargetInfo::isGotRelative(uint32_t Type) const {
468 // This relocation does not require got entry,
469 // but it is relative to got and needs it to be created.
470 // Here we request for that.
471 return Type == R_386_GOTOFF;
472}
473
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000474bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
475 return Type == R_386_GOT32;
476}
477
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000478void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000479 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000480 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000481 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000482 case R_386_32:
483 add32le(Loc, SA);
484 break;
George Rimarffb67352016-01-19 11:00:48 +0000485 case R_386_GOT32: {
486 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
487 Out<ELF32LE>::Got->getNumEntries() * 4;
488 checkInt<32>(V, Type);
489 add32le(Loc, V);
490 break;
491 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000492 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000493 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000494 break;
George Rimar13934772015-11-25 20:20:31 +0000495 case R_386_GOTPC:
496 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
497 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000498 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000499 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000500 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000501 break;
George Rimar9db204a2015-12-02 09:58:20 +0000502 case R_386_TLS_GD:
503 case R_386_TLS_LDM:
504 case R_386_TLS_TPOFF: {
505 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
506 Out<ELF32LE>::Got->getNumEntries() * 4;
507 checkInt<32>(V, Type);
508 write32le(Loc, V);
509 break;
510 }
George Rimar6f17e092015-12-17 09:32:21 +0000511 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000512 case R_386_TLS_LDO_32:
513 write32le(Loc, SA);
514 break;
George Rimard23970f2015-11-25 20:41:53 +0000515 case R_386_TLS_LE:
516 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
517 break;
518 case R_386_TLS_LE_32:
519 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
520 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000521 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000522 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000523 }
524}
525
Rui Ueyamabaf16512016-01-29 00:20:12 +0000526bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000527 if (Config->Shared || (S && !S->IsTls))
George Rimar2558e122015-12-09 09:55:54 +0000528 return false;
529 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
530 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000531 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000532 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
533}
534
Rui Ueyamac516ae12016-01-29 02:33:45 +0000535bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000536 return Config->Shared && Type == R_386_TLS_IE;
537}
538
Rui Ueyamac516ae12016-01-29 02:33:45 +0000539unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
540 uint64_t P, uint64_t SA,
541 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000542 switch (Type) {
543 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000544 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000545 relocateTlsGdToIe(Loc, BufEnd, P, SA);
546 else
547 relocateTlsGdToLe(Loc, BufEnd, P, SA);
548 // The next relocation should be against __tls_get_addr, so skip it
549 return 1;
550 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000551 case R_386_TLS_IE:
552 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000553 return 0;
554 case R_386_TLS_LDM:
555 relocateTlsLdToLe(Loc, BufEnd, P, SA);
556 // The next relocation should be against __tls_get_addr, so skip it
557 return 1;
558 case R_386_TLS_LDO_32:
559 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
560 return 0;
561 }
562 llvm_unreachable("Unknown TLS optimization");
563}
564
565// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
566// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
567// how GD can be optimized to IE:
568// leal x@tlsgd(, %ebx, 1),
569// call __tls_get_addr@plt
570// Is converted to:
571// movl %gs:0, %eax
572// addl x@gotntpoff(%ebx), %eax
573void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
574 uint64_t SA) const {
575 const uint8_t Inst[] = {
576 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
577 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
578 };
579 memcpy(Loc - 3, Inst, sizeof(Inst));
580 relocateOne(Loc + 5, BufEnd, R_386_32, P,
581 SA - Out<ELF32LE>::Got->getVA() -
582 Out<ELF32LE>::Got->getNumEntries() * 4);
583}
584
585// GD can be optimized to LE:
586// leal x@tlsgd(, %ebx, 1),
587// call __tls_get_addr@plt
588// Can be converted to:
589// movl %gs:0,%eax
590// addl $x@ntpoff,%eax
591// But gold emits subl $foo@tpoff,%eax instead of addl.
592// These instructions are completely equal in behavior.
593// This method generates subl to be consistent with gold.
594void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
595 uint64_t SA) const {
596 const uint8_t Inst[] = {
597 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
598 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
599 };
600 memcpy(Loc - 3, Inst, sizeof(Inst));
601 relocateOne(Loc + 5, BufEnd, R_386_32, P,
602 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
603}
604
605// LD can be optimized to LE:
606// leal foo(%reg),%eax
607// call ___tls_get_addr
608// Is converted to:
609// movl %gs:0,%eax
610// nop
611// leal 0(%esi,1),%esi
612void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
613 uint64_t SA) const {
614 const uint8_t Inst[] = {
615 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
616 0x90, // nop
617 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
618 };
619 memcpy(Loc - 2, Inst, sizeof(Inst));
620}
621
George Rimar6f17e092015-12-17 09:32:21 +0000622// In some conditions, relocations can be optimized to avoid using GOT.
623// This function does that for Initial Exec to Local Exec case.
624// Read "ELF Handling For Thread-Local Storage, 5.1
625// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000626// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000627void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
628 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000629 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000630 // Ulrich's document section 6.2 says that @gotntpoff can
631 // be used with MOVL or ADDL instructions.
632 // @indntpoff is similar to @gotntpoff, but for use in
633 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000634 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000635 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000636 uint8_t Reg = (Loc[-1] >> 3) & 7;
637 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000638 if (Type == R_386_TLS_IE) {
639 // For R_386_TLS_IE relocation we perform the next transformations:
640 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
641 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
642 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
643 // First one is special because when EAX is used the sequence is 5 bytes
644 // long, otherwise it is 6 bytes.
645 if (*Op == 0xa1) {
646 *Op = 0xb8;
647 } else {
648 *Inst = IsMov ? 0xc7 : 0x81;
649 *Op = 0xc0 | ((*Op >> 3) & 7);
650 }
651 } else {
652 // R_386_TLS_GOTIE relocation can be optimized to
653 // R_386_TLS_LE so that it does not use GOT.
654 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
655 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
656 // Note: gold converts to ADDL instead of LEAL.
657 *Inst = IsMov ? 0xc7 : 0x8d;
658 if (IsMov)
659 *Op = 0xc0 | ((*Op >> 3) & 7);
660 else
661 *Op = 0x80 | Reg | (Reg << 3);
662 }
George Rimar2558e122015-12-09 09:55:54 +0000663 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
664}
665
Rafael Espindola7f074422015-09-22 21:35:51 +0000666X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000667 CopyRel = R_X86_64_COPY;
668 GotRel = R_X86_64_GLOB_DAT;
669 PltRel = R_X86_64_JUMP_SLOT;
670 RelativeRel = R_X86_64_RELATIVE;
671 IRelativeRel = R_X86_64_IRELATIVE;
672 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000673 TlsModuleIndexRel = R_X86_64_DTPMOD64;
674 TlsOffsetRel = R_X86_64_DTPOFF64;
675 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000676 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000677 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000678}
679
Rui Ueyamac516ae12016-01-29 02:33:45 +0000680void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000681 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
682}
683
Rui Ueyamac516ae12016-01-29 02:33:45 +0000684void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000685 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000686 write32le(Buf, Plt + 6);
687}
688
Rui Ueyama900e2d22016-01-29 03:51:49 +0000689void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000690 const uint8_t PltData[] = {
691 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
692 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
693 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
694 };
695 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000696 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
697 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
698 write32le(Buf + 2, Got - Plt + 2); // GOT+8
699 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000700}
Rafael Espindola01205f72015-09-22 18:19:46 +0000701
Rui Ueyama9398f862016-01-29 04:15:02 +0000702void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
703 uint64_t PltEntryAddr, int32_t Index,
704 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000705 const uint8_t Inst[] = {
706 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
707 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
708 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
709 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000710 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000711
George Rimar648a2c32015-10-20 08:54:27 +0000712 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
713 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000714 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000715}
716
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000717bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
718 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
719 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000720}
721
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000722bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
723 return Type == R_X86_64_GOTPCREL;
724}
725
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000726bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000727 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000728 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000729 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000730 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000731 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000732}
733
George Rimar2960c982016-02-11 11:14:46 +0000734unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const {
735 // No other types of TLS relocations requiring GOT should
736 // reach here.
737 assert(Type == R_X86_64_GOTTPOFF);
738 return R_X86_64_PC32;
739}
740
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000741bool X86_64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
742 return Type == R_X86_64_TLSGD;
743}
744
745bool X86_64TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
746 return Type == R_X86_64_TLSLD;
747}
748
Rui Ueyamac516ae12016-01-29 02:33:45 +0000749bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000750 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000751}
752
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000753bool X86_64TargetInfo::needsPltImpl(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000754 if (needsCopyRel<ELF64LE>(Type, S))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000755 return false;
George Rimara07ff662015-12-21 10:12:06 +0000756 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000757 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000758
Rafael Espindola01205f72015-09-22 18:19:46 +0000759 switch (Type) {
760 default:
Rafael Espindola852860e2016-02-12 15:47:37 +0000761 return Plt_No;
Rafael Espindola01205f72015-09-22 18:19:46 +0000762 case R_X86_64_PLT32:
Rafael Espindola852860e2016-02-12 15:47:37 +0000763 if (canBePreempted(&S, true))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000764 return true;
765 return false;
Rafael Espindola01205f72015-09-22 18:19:46 +0000766 }
767}
Rafael Espindolac4010882015-09-22 20:54:08 +0000768
Rafael Espindolaae244002015-10-05 19:30:12 +0000769bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
770 switch (Type) {
771 default:
772 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000773 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000774 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000775 case R_X86_64_PC8:
776 case R_X86_64_PC16:
777 case R_X86_64_PC32:
778 case R_X86_64_PC64:
779 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000780 return true;
781 }
782}
783
Rui Ueyamac516ae12016-01-29 02:33:45 +0000784bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000785 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000786}
787
Rui Ueyamabaf16512016-01-29 00:20:12 +0000788bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000789 if (Config->Shared || (S && !S->IsTls))
George Rimar77d1cb12015-11-24 09:00:06 +0000790 return false;
George Rimar25411f252015-12-04 11:20:13 +0000791 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
792 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000793 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
794}
795
796// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
797// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
798// how LD can be optimized to LE:
799// leaq bar@tlsld(%rip), %rdi
800// callq __tls_get_addr@PLT
801// leaq bar@dtpoff(%rax), %rcx
802// Is converted to:
803// .word 0x6666
804// .byte 0x66
805// mov %fs:0,%rax
806// leaq bar@tpoff(%rax), %rcx
807void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
808 uint64_t P, uint64_t SA) const {
809 const uint8_t Inst[] = {
810 0x66, 0x66, //.word 0x6666
811 0x66, //.byte 0x66
812 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
813 };
814 memcpy(Loc - 3, Inst, sizeof(Inst));
815}
816
817// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
818// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
819// how GD can be optimized to LE:
820// .byte 0x66
821// leaq x@tlsgd(%rip), %rdi
822// .word 0x6666
823// rex64
824// call __tls_get_addr@plt
825// Is converted to:
826// mov %fs:0x0,%rax
827// lea x@tpoff,%rax
828void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
829 uint64_t P, uint64_t SA) const {
830 const uint8_t Inst[] = {
831 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
832 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
833 };
834 memcpy(Loc - 4, Inst, sizeof(Inst));
835 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000836}
837
George Rimar25411f252015-12-04 11:20:13 +0000838// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
839// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
840// how GD can be optimized to IE:
841// .byte 0x66
842// leaq x@tlsgd(%rip), %rdi
843// .word 0x6666
844// rex64
845// call __tls_get_addr@plt
846// Is converted to:
847// mov %fs:0x0,%rax
848// addq x@tpoff,%rax
849void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
850 uint64_t P, uint64_t SA) const {
851 const uint8_t Inst[] = {
852 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
853 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
854 };
855 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000856 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000857}
858
George Rimar77d1cb12015-11-24 09:00:06 +0000859// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000860// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000861// This function does that. Read "ELF Handling For Thread-Local Storage,
862// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
863// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000864void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
865 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000866 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
867 // used in MOVQ or ADDQ instructions only.
868 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
869 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
870 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
871 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
872 uint8_t *Prefix = Loc - 3;
873 uint8_t *Inst = Loc - 2;
874 uint8_t *RegSlot = Loc - 1;
875 uint8_t Reg = Loc[-1] >> 3;
876 bool IsMov = *Inst == 0x8b;
877 bool RspAdd = !IsMov && Reg == 4;
878 // r12 and rsp registers requires special handling.
879 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
880 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
881 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
882 // The same true for rsp. So we convert to addq for them, saving 1 byte that
883 // we dont have.
884 if (RspAdd)
885 *Inst = 0x81;
886 else
887 *Inst = IsMov ? 0xc7 : 0x8d;
888 if (*Prefix == 0x4c)
889 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
890 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
891 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
892}
893
George Rimar6713cf82015-11-25 21:46:05 +0000894// This function applies a TLS relocation with an optimization as described
895// in the Ulrich's document. As a result of rewriting instructions at the
896// relocation target, relocations immediately follow the TLS relocation (which
897// would be applied to rewritten instructions) may have to be skipped.
898// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000899unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
900 uint32_t Type, uint64_t P, uint64_t SA,
901 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000902 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000903 case R_X86_64_DTPOFF32:
904 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
905 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000906 case R_X86_64_GOTTPOFF:
907 relocateTlsIeToLe(Loc, BufEnd, P, SA);
908 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000909 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000910 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000911 relocateTlsGdToIe(Loc, BufEnd, P, SA);
912 else
913 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000914 // The next relocation should be against __tls_get_addr, so skip it
915 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000916 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000917 case R_X86_64_TLSLD:
918 relocateTlsLdToLe(Loc, BufEnd, P, SA);
919 // The next relocation should be against __tls_get_addr, so skip it
920 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000921 }
922 llvm_unreachable("Unknown TLS optimization");
923}
924
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000925void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000926 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000927 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000928 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000929 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000930 checkUInt<32>(SA, Type);
931 write32le(Loc, SA);
932 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000933 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000934 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000935 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000936 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000937 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000938 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000939 write64le(Loc, SA);
940 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000941 case R_X86_64_DTPOFF32:
942 write32le(Loc, SA);
943 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000944 case R_X86_64_GOTPCREL:
945 case R_X86_64_PC32:
946 case R_X86_64_PLT32:
947 case R_X86_64_TLSGD:
948 case R_X86_64_TLSLD:
949 write32le(Loc, SA - P);
950 break;
George Rimar48651482015-12-11 08:59:37 +0000951 case R_X86_64_SIZE32:
952 write32le(Loc, ZA);
953 break;
954 case R_X86_64_SIZE64:
955 write64le(Loc, ZA);
956 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000957 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000958 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000959 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000960 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000961 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000962 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000963 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000964 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000965 }
966}
967
Hal Finkel3c8cc672015-10-12 20:56:18 +0000968// Relocation masks following the #lo(value), #hi(value), #ha(value),
969// #higher(value), #highera(value), #highest(value), and #highesta(value)
970// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
971// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000972static uint16_t applyPPCLo(uint64_t V) { return V; }
973static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
974static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
975static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
976static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000977static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000978static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
979
Davide Italiano8c3444362016-01-11 19:45:33 +0000980PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000981bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
982
983void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
984 uint64_t P, uint64_t SA, uint64_t ZA,
985 uint8_t *PairedLoc) const {
986 switch (Type) {
987 case R_PPC_ADDR16_HA:
988 write16be(Loc, applyPPCHa(SA));
989 break;
990 case R_PPC_ADDR16_LO:
991 write16be(Loc, applyPPCLo(SA));
992 break;
993 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000994 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000995 }
996}
997
Rafael Espindolac4010882015-09-22 20:54:08 +0000998PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000999 GotRel = R_PPC64_GLOB_DAT;
1000 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001001 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001002
1003 // We need 64K pages (at least under glibc/Linux, the loader won't
1004 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001005 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001006
1007 // The PPC64 ELF ABI v1 spec, says:
1008 //
1009 // It is normally desirable to put segments with different characteristics
1010 // in separate 256 Mbyte portions of the address space, to give the
1011 // operating system full paging flexibility in the 64-bit address space.
1012 //
1013 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1014 // use 0x10000000 as the starting address.
1015 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001016}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001017
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001018uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1020 // order. The TOC starts where the first of these sections starts.
1021
1022 // FIXME: This obviously does not do the right thing when there is no .got
1023 // section, but there is a .toc or .tocbss section.
1024 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1025 if (!TocVA)
1026 TocVA = Out<ELF64BE>::Plt->getVA();
1027
1028 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1029 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1030 // code (crt1.o) assumes that you can get from the TOC base to the
1031 // start of the .toc section with only a single (signed) 16-bit relocation.
1032 return TocVA + 0x8000;
1033}
1034
Rui Ueyama9398f862016-01-29 04:15:02 +00001035void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1036 uint64_t PltEntryAddr, int32_t Index,
1037 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1039
1040 // FIXME: What we should do, in theory, is get the offset of the function
1041 // descriptor in the .opd section, and use that as the offset from %r2 (the
1042 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1043 // be a pointer to the function descriptor in the .opd section. Using
1044 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1045
Hal Finkelfa92f682015-10-13 21:47:34 +00001046 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1048 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1049 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1050 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1051 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1052 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1053 write32be(Buf + 28, 0x4e800420); // bctr
1054}
1055
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001056bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001057 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 return true;
1059
1060 switch (Type) {
1061 default: return false;
1062 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001064 case R_PPC64_GOT16_HA:
1065 case R_PPC64_GOT16_HI:
1066 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001067 case R_PPC64_GOT16_LO_DS:
1068 return true;
1069 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001070}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001072bool PPC64TargetInfo::needsPltImpl(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola852860e2016-02-12 15:47:37 +00001074 if (Type == R_PPC64_REL24 && canBePreempted(&S, false))
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001075 return true;
1076 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001077}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078
Hal Finkelbe0823d2015-10-12 20:58:52 +00001079bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1080 switch (Type) {
1081 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001082 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001083 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001084 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001085 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001086 }
1087}
1088
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001089void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001090 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001091 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001092 uint64_t TB = getPPC64TocBase();
1093
Hal Finkel3c8cc672015-10-12 20:56:18 +00001094 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1095 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001096 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001097 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1098 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001099 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1100 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001101 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1102 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 default: break;
1104 }
1105
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001107 case R_PPC64_ADDR14: {
1108 checkAlignment<4>(SA, Type);
1109 // Preserve the AA/LK bits in the branch instruction
1110 uint8_t AALK = Loc[3];
1111 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1112 break;
1113 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001115 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001117 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001118 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001119 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001120 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001121 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001122 case R_PPC64_ADDR16_HA:
1123 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 break;
1125 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001126 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001127 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001128 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
1131 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001132 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
1137 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001140 case R_PPC64_ADDR16_LO:
1141 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001143 case R_PPC64_ADDR16_LO_DS:
1144 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
1146 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001147 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001148 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001149 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001150 case R_PPC64_ADDR64:
1151 write64be(Loc, SA);
1152 break;
1153 case R_PPC64_REL16_HA:
1154 write16be(Loc, applyPPCHa(SA - P));
1155 break;
1156 case R_PPC64_REL16_HI:
1157 write16be(Loc, applyPPCHi(SA - P));
1158 break;
1159 case R_PPC64_REL16_LO:
1160 write16be(Loc, applyPPCLo(SA - P));
1161 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001162 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001163 // If we have an undefined weak symbol, we might get here with a symbol
1164 // address of zero. That could overflow, but the code must be unreachable,
1165 // so don't bother doing anything at all.
1166 if (!SA)
1167 break;
1168
Hal Finkeldaedc122015-10-12 23:16:53 +00001169 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1170 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001171 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001172
1173 if (!InPlt && Out<ELF64BE>::Opd) {
1174 // If this is a local call, and we currently have the address of a
1175 // function-descriptor, get the underlying code address instead.
1176 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1177 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001178 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001179
1180 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001181 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001182 }
1183
Hal Finkel3c8cc672015-10-12 20:56:18 +00001184 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001185 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001186 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001187
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 uint32_t Nop = 0x60000000;
1189 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1190 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001191 break;
1192 }
1193 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001194 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001195 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001196 break;
1197 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001198 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001199 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001200 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001201 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001202 break;
1203 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001204 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001205 }
1206}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001207
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001208AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001209 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001210 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001211 IRelativeRel = R_AARCH64_IRELATIVE;
1212 GotRel = R_AARCH64_GLOB_DAT;
1213 PltRel = R_AARCH64_JUMP_SLOT;
1214 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001215 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1216 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001217 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001218 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001219 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001220}
George Rimar648a2c32015-10-20 08:54:27 +00001221
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001222bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001223 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001224 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
1225 Type == R_AARCH64_LDST32_ABS_LO12_NC;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001226}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001227
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001228bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
1229 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1230 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1231 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1232 Type == R_AARCH64_TLSDESC_CALL;
1233}
1234
Rui Ueyamac516ae12016-01-29 02:33:45 +00001235unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001236 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1237 return Type;
1238 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001239 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001240 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001241 // Keep it going with a dummy value so that we can find more reloc errors.
1242 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001243}
1244
Rui Ueyamac516ae12016-01-29 02:33:45 +00001245void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001246 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1247}
1248
Rui Ueyama900e2d22016-01-29 03:51:49 +00001249void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001250 const uint8_t PltData[] = {
1251 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1252 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1253 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1254 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1255 0x20, 0x02, 0x1f, 0xd6, // br x17
1256 0x1f, 0x20, 0x03, 0xd5, // nop
1257 0x1f, 0x20, 0x03, 0xd5, // nop
1258 0x1f, 0x20, 0x03, 0xd5 // nop
1259 };
1260 memcpy(Buf, PltData, sizeof(PltData));
1261
Rui Ueyama900e2d22016-01-29 03:51:49 +00001262 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1263 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1264 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1265 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1266 Got + 16);
1267 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1268 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001269}
1270
Rui Ueyama9398f862016-01-29 04:15:02 +00001271void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1272 uint64_t PltEntryAddr, int32_t Index,
1273 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001274 const uint8_t Inst[] = {
1275 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1276 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1277 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1278 0x20, 0x02, 0x1f, 0xd6 // br x17
1279 };
1280 memcpy(Buf, Inst, sizeof(Inst));
1281
1282 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1283 GotEntryAddr);
1284 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1285 GotEntryAddr);
1286 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1287 GotEntryAddr);
1288}
1289
Rui Ueyama724d6252016-01-29 01:49:32 +00001290unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001291 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1292 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001293 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001294}
1295
Rui Ueyamac516ae12016-01-29 02:33:45 +00001296bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001297 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1298 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1299 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1300 Type == R_AARCH64_TLSDESC_CALL ||
1301 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001302 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1303}
1304
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001305bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001306 switch (Type) {
1307 default:
1308 return false;
1309 case R_AARCH64_ABS16:
1310 case R_AARCH64_ABS32:
1311 case R_AARCH64_ABS64:
1312 case R_AARCH64_ADD_ABS_LO12_NC:
1313 case R_AARCH64_ADR_PREL_LO21:
1314 case R_AARCH64_ADR_PREL_PG_HI21:
1315 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001316 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001317 case R_AARCH64_LDST32_ABS_LO12_NC:
1318 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001319 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001320 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001321 }
1322}
1323
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001324bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001325 switch (Type) {
1326 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1327 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1328 case R_AARCH64_ADR_GOT_PAGE:
1329 case R_AARCH64_LD64_GOT_LO12_NC:
1330 return true;
1331 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001332 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001333 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001334}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001335
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001336bool AArch64TargetInfo::needsPltImpl(uint32_t Type, const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001337 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001338 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001339 switch (Type) {
1340 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001341 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001342 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001343 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001344 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001345 case R_AARCH64_TSTBR14:
Rafael Espindola852860e2016-02-12 15:47:37 +00001346 if (canBePreempted(&S, true))
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001347 return true;
1348 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001349 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001350}
Davide Italiano1d750a62015-09-27 08:45:38 +00001351
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001352static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001353 uint32_t ImmLo = (Imm & 0x3) << 29;
1354 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1355 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001356 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001357}
1358
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001359static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1360 or32le(L, (Imm & 0xFFF) << 10);
1361}
1362
Davide Italiano318ca222015-10-02 22:13:51 +00001363// Page(Expr) is the page address of the expression Expr, defined
1364// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001365// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001366static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001367 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001368}
1369
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001370void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001371 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001372 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001373 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001374 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001375 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001376 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001377 break;
1378 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001379 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001380 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001381 break;
1382 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001383 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001384 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001385 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001386 // This relocation stores 12 bits and there's no instruction
1387 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001388 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1389 // bits in Loc are zero.
1390 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001391 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001392 case R_AARCH64_ADR_GOT_PAGE: {
1393 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1394 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001395 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001396 break;
1397 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001398 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001399 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001400 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001401 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001402 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001403 }
George Rimar3d737e42016-01-13 13:04:46 +00001404 case R_AARCH64_ADR_PREL_PG_HI21:
1405 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001406 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001407 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001408 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001409 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001410 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001411 case R_AARCH64_CALL26:
1412 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001413 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001414 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001415 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1416 break;
1417 }
George Rimar4102bfb2016-01-11 14:22:00 +00001418 case R_AARCH64_CONDBR19: {
1419 uint64_t X = SA - P;
1420 checkInt<21>(X, Type);
1421 or32le(Loc, (X & 0x1FFFFC) << 3);
1422 break;
1423 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001424 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001425 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001426 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001427 or32le(Loc, (SA & 0xFF8) << 7);
1428 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001429 case R_AARCH64_LDST128_ABS_LO12_NC:
1430 or32le(Loc, (SA & 0x0FF8) << 6);
1431 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001432 case R_AARCH64_LDST16_ABS_LO12_NC:
1433 or32le(Loc, (SA & 0x0FFC) << 9);
1434 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001435 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001436 or32le(Loc, (SA & 0xFFF) << 10);
1437 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001438 case R_AARCH64_LDST32_ABS_LO12_NC:
1439 or32le(Loc, (SA & 0xFFC) << 8);
1440 break;
1441 case R_AARCH64_LDST64_ABS_LO12_NC:
1442 or32le(Loc, (SA & 0xFF8) << 7);
1443 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001444 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001445 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001446 write16le(Loc, SA - P);
1447 break;
1448 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001449 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001450 write32le(Loc, SA - P);
1451 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001452 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001453 write64le(Loc, SA - P);
1454 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001455 case R_AARCH64_TSTBR14: {
1456 uint64_t X = SA - P;
1457 checkInt<16>(X, Type);
1458 or32le(Loc, (X & 0xFFFC) << 3);
1459 break;
1460 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001461 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1462 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1463 checkInt<24>(V, Type);
1464 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1465 break;
1466 }
1467 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1468 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1469 updateAArch64Add(Loc, V & 0xFFF);
1470 break;
1471 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001472 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001473 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001474 }
1475}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001476
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001477bool AArch64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +00001478 if (Config->Shared || (S && !S->IsTls))
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001479 return false;
1480
1481 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is
1482 // an executable. And if the target is local it can also be fully relaxed to
1483 // Local-Exec.
1484 if (isTlsGlobalDynamicRel(Type))
1485 return !canBePreempted(S, true);
1486
1487 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local
1488 // symbol.
1489 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1490 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1491 return !canBePreempted(S, true);
1492
1493 return false;
1494}
1495
1496unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1497 uint32_t Type, uint64_t P, uint64_t SA,
1498 const SymbolBody *S) const {
1499 switch (Type) {
1500 case R_AARCH64_TLSDESC_ADR_PAGE21:
1501 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1502 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1503 case R_AARCH64_TLSDESC_CALL: {
1504 if (canBePreempted(S, true))
1505 fatal("Unsupported TLS optimization");
1506 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1507 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1508 return 0;
1509 }
1510 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1511 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1512 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1513 return 0;
1514 }
1515 llvm_unreachable("Unknown TLS optimization");
1516}
1517
1518// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1519// an executable and target is final (can notbe preempted).
1520void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc,
1521 uint8_t *BufEnd, uint64_t P,
1522 uint64_t SA) const {
1523 // TLSDESC Global-Dynamic relocation are in the form:
1524 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1525 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1526 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1527 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1528 // And it can optimized to:
1529 // movz x0, #0x0, lsl #16
1530 // movk x0, #0x10
1531 // nop
1532 // nop
1533
1534 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1535 uint64_t X = SA + TPOff;
1536 checkUInt<32>(X, Type);
1537
1538 uint32_t NewInst;
1539 switch (Type) {
1540 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1541 case R_AARCH64_TLSDESC_CALL:
1542 // nop
1543 NewInst = 0xd503201f;
1544 break;
1545 case R_AARCH64_TLSDESC_ADR_PAGE21:
1546 // movz
1547 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1548 break;
1549 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1550 // movk
1551 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1552 break;
1553 default:
1554 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1555 }
1556 write32le(Loc, NewInst);
1557}
1558
1559// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1560// (can not be preempted).
1561void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
1562 uint8_t *BufEnd, uint64_t P,
1563 uint64_t SA) const {
1564 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1565 uint64_t X = SA + TPOff;
1566 checkUInt<32>(X, Type);
1567
1568 uint32_t Inst = read32le (Loc);
1569 uint32_t NewInst;
1570 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1571 // Generate movz.
1572 unsigned RegNo = (Inst & 0x1f);
1573 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1574 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1575 // Generate movk
1576 unsigned RegNo = (Inst & 0x1f);
1577 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1578 } else {
1579 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1580 }
1581 write32le(Loc, NewInst);
1582}
1583
1584
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001585// Implementing relocations for AMDGPU is low priority since most
1586// programs don't use relocations now. Thus, this function is not
1587// actually called (relocateOne is called for each relocation).
1588// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001589void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1590 uint64_t P, uint64_t SA, uint64_t ZA,
1591 uint8_t *PairedLoc) const {
1592 llvm_unreachable("not implemented");
1593}
1594
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001595template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001596 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001597 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001598 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001599 PltEntrySize = 16;
1600 PltZeroSize = 32;
1601 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001602 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001603 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001604 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001605}
1606
1607template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001608unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001609 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1610 return R_MIPS_REL32;
1611 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001612 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001613 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001614 // Keep it going with a dummy value so that we can find more reloc errors.
1615 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001616}
1617
1618template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001619void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001620 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001621 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1622
1623 // Set the MSB of the second GOT slot. This is not required by any
1624 // MIPS ABI documentation, though.
1625 //
1626 // There is a comment in glibc saying that "The MSB of got[1] of a
1627 // gnu object is set to identify gnu objects," and in GNU gold it
1628 // says "the second entry will be used by some runtime loaders".
1629 // But how this field is being used is unclear.
1630 //
1631 // We are not really willing to mimic other linkers behaviors
1632 // without understanding why they do that, but because all files
1633 // generated by GNU tools have this special GOT value, and because
1634 // we've been doing this for years, it is probably a safe bet to
1635 // keep doing this for now. We really need to revisit this to see
1636 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001637 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001638 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001639}
1640
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001641template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001642void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1643 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001644}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001645
Simon Atanasyan35031192015-12-15 06:06:34 +00001646static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001647
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001648template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001649static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001650 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001651 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001652 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001653 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1654 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001655 checkAlignment<(1 << SHIFT)>(S + A, Type);
1656 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001657 checkInt<BSIZE + SHIFT>(V, Type);
1658 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001659}
1660
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001661template <endianness E>
1662static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1663 uint32_t Instr = read32<E>(Loc);
1664 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1665}
1666
1667template <class ELFT>
1668void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1669 const endianness E = ELFT::TargetEndianness;
1670 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1671 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1672 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1673 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1674 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1675 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1676 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1677 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1678 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1679 uint64_t Plt = Out<ELFT>::Plt->getVA();
1680 applyMipsHi16Reloc<E>(Buf, Got, 0);
1681 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1682 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1683}
1684
1685template <class ELFT>
1686void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1687 uint64_t PltEntryAddr, int32_t Index,
1688 unsigned RelOff) const {
1689 const endianness E = ELFT::TargetEndianness;
1690 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1691 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1692 write32<E>(Buf + 8, 0x03200008); // jr $25
1693 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1694 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1695 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1696 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1697}
1698
1699template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001700bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
1701 return Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001702}
1703
1704template <class ELFT>
1705bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001706 return needsPlt<ELFT>(Type, S) || Type == R_MIPS_GOT16 ||
1707 Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001708}
1709
1710template <class ELFT>
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001711bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type,
1712 const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001713 if (needsCopyRel<ELFT>(Type, S))
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001714 return false;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001715 if (Type == R_MIPS_26 && canBePreempted(&S, false))
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001716 return true;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001717 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
Rafael Espindola852860e2016-02-12 15:47:37 +00001718 if (S.isShared())
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001719 return true;
1720 return false;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001721}
1722
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001723template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001724void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001725 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001726 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001727 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001728 switch (Type) {
1729 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001730 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001731 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001732 case R_MIPS_26: {
1733 uint32_t Instr = read32<E>(Loc);
1734 // FIXME (simon): If the relocation target symbol is not a PLT entry
1735 // we should use another expression for calculation:
1736 // ((A << 2) | (P & 0xf0000000)) >> 2
1737 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1738 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1739 break;
1740 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001741 case R_MIPS_CALL16:
1742 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001743 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001744 if (Type == R_MIPS_GOT16)
1745 checkInt<16>(V, Type);
1746 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1747 break;
1748 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001749 case R_MIPS_GPREL16: {
1750 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001751 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001752 checkInt<16>(V, Type);
1753 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1754 break;
1755 }
1756 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001757 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001758 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001759 case R_MIPS_HI16: {
1760 uint32_t Instr = read32<E>(Loc);
1761 if (PairedLoc) {
1762 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001763 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001764 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001765 } else {
1766 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001767 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001768 }
1769 break;
1770 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001771 case R_MIPS_JALR:
1772 // Ignore this optimization relocation for now
1773 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001774 case R_MIPS_LO16: {
1775 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001776 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001777 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001778 break;
1779 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001781 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001782 break;
1783 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001784 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001785 break;
1786 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001787 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001788 break;
1789 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001790 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001791 break;
1792 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001793 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001794 break;
1795 case R_MIPS_PCHI16: {
1796 uint32_t Instr = read32<E>(Loc);
1797 if (PairedLoc) {
1798 uint64_t AHL = ((Instr & 0xffff) << 16) +
1799 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001800 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001801 } else {
1802 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001803 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001804 }
1805 break;
1806 }
1807 case R_MIPS_PCLO16: {
1808 uint32_t Instr = read32<E>(Loc);
1809 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001810 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001811 break;
1812 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001813 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001814 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001815 }
1816}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001817
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001818template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001819bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001820 return Type == R_MIPS_JALR;
1821}
1822
1823template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001824bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1825 switch (Type) {
1826 default:
1827 return false;
1828 case R_MIPS_PC16:
1829 case R_MIPS_PC19_S2:
1830 case R_MIPS_PC21_S2:
1831 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001832 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001833 case R_MIPS_PCHI16:
1834 case R_MIPS_PCLO16:
1835 return true;
1836 }
1837}
1838
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001839// _gp is a MIPS-specific ABI-defined symbol which points to
1840// a location that is relative to GOT. This function returns
1841// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001842template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001843 unsigned GPOffset = 0x7ff0;
1844 if (uint64_t V = Out<ELFT>::Got->getVA())
1845 return V + GPOffset;
1846 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001847}
1848
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001849template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1850template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1851template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1852template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1853
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001854template uint32_t getMipsGpAddr<ELF32LE>();
1855template uint32_t getMipsGpAddr<ELF32BE>();
1856template uint64_t getMipsGpAddr<ELF64LE>();
1857template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001858
1859template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1860 const SymbolBody &) const;
1861template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1862 const SymbolBody &) const;
1863template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1864 const SymbolBody &) const;
1865template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1866 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001867
1868template TargetInfo::PltNeed
1869TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1870template TargetInfo::PltNeed
1871TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1872template TargetInfo::PltNeed
1873TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1874template TargetInfo::PltNeed
1875TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001876}
1877}