blob: 107a7f384be25a1a806a4b5be7c6e08a06854349 [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 {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000035
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 Espindola993f0272016-02-26 14:27:47 +000096 bool needsPltImpl(uint32_t Type) 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;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000100 bool canRelaxTlsImpl(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;
Rafael Espindola993f0272016-02-26 14:27:47 +0000132 bool needsPltImpl(uint32_t Type) 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;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000137 bool canRelaxTlsImpl(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 Espindola993f0272016-02-26 14:27:47 +0000168 bool needsPltImpl(uint32_t Type) 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 Espindola993f0272016-02-26 14:27:47 +0000189 bool needsPltImpl(uint32_t Type) 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;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000195 bool canRelaxTlsImpl(unsigned Type, const SymbolBody *S) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000196
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 Espindola993f0272016-02-26 14:27:47 +0000225 bool needsPltImpl(uint32_t Type) 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;
Simon Atanasyand040a582016-02-25 16:19:15 +0000231 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000232};
233} // anonymous namespace
234
Rui Ueyama91004392015-10-13 16:08:15 +0000235TargetInfo *createTarget() {
236 switch (Config->EMachine) {
237 case EM_386:
238 return new X86TargetInfo();
239 case EM_AARCH64:
240 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000241 case EM_AMDGPU:
242 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000243 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000244 switch (Config->EKind) {
245 case ELF32LEKind:
246 return new MipsTargetInfo<ELF32LE>();
247 case ELF32BEKind:
248 return new MipsTargetInfo<ELF32BE>();
249 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000250 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000251 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000252 case EM_PPC:
253 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000254 case EM_PPC64:
255 return new PPC64TargetInfo();
256 case EM_X86_64:
257 return new X86_64TargetInfo();
258 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000259 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000260}
261
Rafael Espindola01205f72015-09-22 18:19:46 +0000262TargetInfo::~TargetInfo() {}
263
Rui Ueyamabaf16512016-01-29 00:20:12 +0000264bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000265 if (Config->Shared || (S && !S->isTls()))
266 return false;
267 return canRelaxTlsImpl(Type, S);
268}
269
270bool TargetInfo::canRelaxTlsImpl(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000271 return false;
272}
273
Igor Kudrinf6f45472015-11-10 08:39:27 +0000274uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
275
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000276bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
277
278template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
279 if (Config->Shared)
280 return false;
281 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
282 if (!SS)
283 return false;
284 return SS->Sym.getType() == STT_OBJECT;
285}
286
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000287template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000288bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000289 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000290}
291
Rui Ueyama012eb782016-01-29 04:05:09 +0000292bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
293 return false;
294}
295
George Rimarbfb7bf72015-12-21 10:00:12 +0000296bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000297bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000298bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000299bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000300
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000301bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000302
Rafael Espindola993f0272016-02-26 14:27:47 +0000303bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000304
305bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
306
307template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000308TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
309 const SymbolBody &S) const {
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000310 if (isGnuIFunc<ELFT>(S))
311 return Plt_Explicit;
Rafael Espindola993f0272016-02-26 14:27:47 +0000312 if (canBePreempted(&S) && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000313 return Plt_Explicit;
314
315 // This handles a non PIC program call to function in a shared library.
316 // In an ideal world, we could just report an error saying the relocation
317 // can overflow at runtime.
318 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
319 // libc.so.
320 //
321 // The general idea on how to handle such cases is to create a PLT entry
322 // and use that as the function value.
323 //
324 // For the static linking part, we just return true and everything else
325 // will use the the PLT entry as the address.
326 //
327 // The remaining problem is making sure pointer equality still works. We
328 // need the help of the dynamic linker for that. We let it know that we have
329 // a direct reference to a so symbol by creating an undefined symbol with a
330 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
331 // the value of the symbol we created. This is true even for got entries, so
332 // pointer equality is maintained. To avoid an infinite loop, the only entry
333 // that points to the real function is a dedicated got entry used by the
334 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
335 // R_386_JMP_SLOT, etc).
336 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
Rafael Espindola005d8442016-03-03 18:44:38 +0000337 if (!Config->Shared && SS->Sym.getType() == STT_FUNC &&
338 !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000339 return Plt_Implicit;
340
Rafael Espindola852860e2016-02-12 15:47:37 +0000341 return Plt_No;
342}
Rui Ueyama012eb782016-01-29 04:05:09 +0000343
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000344bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
345 return false;
346}
347
348bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
349 return false;
350}
351
Rui Ueyamac516ae12016-01-29 02:33:45 +0000352unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
353 uint64_t P, uint64_t SA,
354 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000355 return 0;
356}
George Rimar77d1cb12015-11-24 09:00:06 +0000357
Rafael Espindola7f074422015-09-22 21:35:51 +0000358X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000359 CopyRel = R_386_COPY;
360 GotRel = R_386_GLOB_DAT;
361 PltRel = R_386_JUMP_SLOT;
362 IRelativeRel = R_386_IRELATIVE;
363 RelativeRel = R_386_RELATIVE;
364 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000365 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
366 TlsOffsetRel = R_386_TLS_DTPOFF32;
367 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000368 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000369 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000370}
371
Rui Ueyamac516ae12016-01-29 02:33:45 +0000372void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000373 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
374}
375
Rui Ueyamac516ae12016-01-29 02:33:45 +0000376void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000377 // Entries in .got.plt initially points back to the corresponding
378 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000379 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000380}
Rafael Espindola01205f72015-09-22 18:19:46 +0000381
Rui Ueyamac516ae12016-01-29 02:33:45 +0000382unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000383 if (Type == R_386_TLS_LE)
384 return R_386_TLS_TPOFF;
385 if (Type == R_386_TLS_LE_32)
386 return R_386_TLS_TPOFF32;
387 return Type;
388}
389
Rui Ueyama724d6252016-01-29 01:49:32 +0000390unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000391 if (Type == R_386_TLS_IE)
392 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000393 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000394}
395
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000396bool X86TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
397 return Type == R_386_TLS_GD;
398}
399
400bool X86TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
401 return Type == R_386_TLS_LDM;
402}
403
Rui Ueyamac516ae12016-01-29 02:33:45 +0000404bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000405 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
406 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000407 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000408 if (Type == R_386_TLS_IE)
Rafael Espindola993f0272016-02-26 14:27:47 +0000409 return canBePreempted(&S);
George Rimar2558e122015-12-09 09:55:54 +0000410 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000411}
412
Rui Ueyama900e2d22016-01-29 03:51:49 +0000413void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000414 // Executable files and shared object files have
415 // separate procedure linkage tables.
416 if (Config->Shared) {
417 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000418 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000419 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
420 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000421 };
422 memcpy(Buf, V, sizeof(V));
423 return;
424 }
George Rimar648a2c32015-10-20 08:54:27 +0000425
George Rimar77b77792015-11-25 22:15:01 +0000426 const uint8_t PltData[] = {
427 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000428 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
429 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000430 };
431 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000432 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000433 write32le(Buf + 2, Got + 4);
434 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000435}
436
Rui Ueyama9398f862016-01-29 04:15:02 +0000437void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
438 uint64_t PltEntryAddr, int32_t Index,
439 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000440 const uint8_t Inst[] = {
441 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
442 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
443 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
444 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000445 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000446
George Rimar77b77792015-11-25 22:15:01 +0000447 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
448 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000449 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
450 : Out<ELF32LE>::Got->getVA();
451 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000452 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000453 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000454}
455
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000456bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
457 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000458}
459
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000460bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Davide Italiano255730c2016-03-04 01:55:28 +0000461 if (S.isTls() && Type == R_386_TLS_GD)
Rafael Espindola993f0272016-02-26 14:27:47 +0000462 return Target->canRelaxTls(Type, &S) && canBePreempted(&S);
George Rimar6f17e092015-12-17 09:32:21 +0000463 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000464 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000465 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000466}
467
Rafael Espindola993f0272016-02-26 14:27:47 +0000468bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
469 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000470}
471
George Rimarbfb7bf72015-12-21 10:00:12 +0000472bool X86TargetInfo::isGotRelative(uint32_t Type) const {
473 // This relocation does not require got entry,
474 // but it is relative to got and needs it to be created.
475 // Here we request for that.
476 return Type == R_386_GOTOFF;
477}
478
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000479bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
480 return Type == R_386_GOT32;
481}
482
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000483void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000484 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000485 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000486 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000487 case R_386_32:
488 add32le(Loc, SA);
489 break;
George Rimarffb67352016-01-19 11:00:48 +0000490 case R_386_GOT32: {
491 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
492 Out<ELF32LE>::Got->getNumEntries() * 4;
493 checkInt<32>(V, Type);
494 add32le(Loc, V);
495 break;
496 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000497 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000498 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000499 break;
George Rimar13934772015-11-25 20:20:31 +0000500 case R_386_GOTPC:
501 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
502 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000503 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000504 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000505 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000506 break;
George Rimar9db204a2015-12-02 09:58:20 +0000507 case R_386_TLS_GD:
508 case R_386_TLS_LDM:
509 case R_386_TLS_TPOFF: {
510 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
511 Out<ELF32LE>::Got->getNumEntries() * 4;
512 checkInt<32>(V, Type);
513 write32le(Loc, V);
514 break;
515 }
George Rimar6f17e092015-12-17 09:32:21 +0000516 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000517 case R_386_TLS_LDO_32:
518 write32le(Loc, SA);
519 break;
George Rimard23970f2015-11-25 20:41:53 +0000520 case R_386_TLS_LE:
521 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
522 break;
523 case R_386_TLS_LE_32:
524 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
525 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000526 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000527 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000528 }
529}
530
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000531bool X86TargetInfo::canRelaxTlsImpl(unsigned Type, const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000532 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
Rafael Espindola993f0272016-02-26 14:27:47 +0000533 Type == R_386_TLS_GD || (Type == R_386_TLS_IE && !canBePreempted(S)) ||
534 (Type == R_386_TLS_GOTIE && !canBePreempted(S));
George Rimar2558e122015-12-09 09:55:54 +0000535}
536
Rui Ueyamac516ae12016-01-29 02:33:45 +0000537bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000538 return Config->Shared && Type == R_386_TLS_IE;
539}
540
Rui Ueyamac516ae12016-01-29 02:33:45 +0000541unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
542 uint64_t P, uint64_t SA,
543 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000544 switch (Type) {
545 case R_386_TLS_GD:
Rafael Espindola993f0272016-02-26 14:27:47 +0000546 if (canBePreempted(S))
George Rimar2558e122015-12-09 09:55:54 +0000547 relocateTlsGdToIe(Loc, BufEnd, P, SA);
548 else
549 relocateTlsGdToLe(Loc, BufEnd, P, SA);
550 // The next relocation should be against __tls_get_addr, so skip it
551 return 1;
552 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000553 case R_386_TLS_IE:
554 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000555 return 0;
556 case R_386_TLS_LDM:
557 relocateTlsLdToLe(Loc, BufEnd, P, SA);
558 // The next relocation should be against __tls_get_addr, so skip it
559 return 1;
560 case R_386_TLS_LDO_32:
561 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
562 return 0;
563 }
564 llvm_unreachable("Unknown TLS optimization");
565}
566
567// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
568// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
569// how GD can be optimized to IE:
570// leal x@tlsgd(, %ebx, 1),
571// call __tls_get_addr@plt
572// Is converted to:
573// movl %gs:0, %eax
574// addl x@gotntpoff(%ebx), %eax
575void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
576 uint64_t SA) const {
577 const uint8_t Inst[] = {
578 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
579 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
580 };
581 memcpy(Loc - 3, Inst, sizeof(Inst));
582 relocateOne(Loc + 5, BufEnd, R_386_32, P,
583 SA - Out<ELF32LE>::Got->getVA() -
584 Out<ELF32LE>::Got->getNumEntries() * 4);
585}
586
587// GD can be optimized to LE:
588// leal x@tlsgd(, %ebx, 1),
589// call __tls_get_addr@plt
590// Can be converted to:
591// movl %gs:0,%eax
592// addl $x@ntpoff,%eax
593// But gold emits subl $foo@tpoff,%eax instead of addl.
594// These instructions are completely equal in behavior.
595// This method generates subl to be consistent with gold.
596void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
597 uint64_t SA) const {
598 const uint8_t Inst[] = {
599 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
600 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
601 };
602 memcpy(Loc - 3, Inst, sizeof(Inst));
603 relocateOne(Loc + 5, BufEnd, R_386_32, P,
604 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
605}
606
607// LD can be optimized to LE:
608// leal foo(%reg),%eax
609// call ___tls_get_addr
610// Is converted to:
611// movl %gs:0,%eax
612// nop
613// leal 0(%esi,1),%esi
614void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
615 uint64_t SA) const {
616 const uint8_t Inst[] = {
617 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
618 0x90, // nop
619 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
620 };
621 memcpy(Loc - 2, Inst, sizeof(Inst));
622}
623
George Rimar6f17e092015-12-17 09:32:21 +0000624// In some conditions, relocations can be optimized to avoid using GOT.
625// This function does that for Initial Exec to Local Exec case.
626// Read "ELF Handling For Thread-Local Storage, 5.1
627// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000628// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000629void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
630 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000631 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000632 // Ulrich's document section 6.2 says that @gotntpoff can
633 // be used with MOVL or ADDL instructions.
634 // @indntpoff is similar to @gotntpoff, but for use in
635 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000636 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000637 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000638 uint8_t Reg = (Loc[-1] >> 3) & 7;
639 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000640 if (Type == R_386_TLS_IE) {
641 // For R_386_TLS_IE relocation we perform the next transformations:
642 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
643 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
644 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
645 // First one is special because when EAX is used the sequence is 5 bytes
646 // long, otherwise it is 6 bytes.
647 if (*Op == 0xa1) {
648 *Op = 0xb8;
649 } else {
650 *Inst = IsMov ? 0xc7 : 0x81;
651 *Op = 0xc0 | ((*Op >> 3) & 7);
652 }
653 } else {
654 // R_386_TLS_GOTIE relocation can be optimized to
655 // R_386_TLS_LE so that it does not use GOT.
656 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
657 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
658 // Note: gold converts to ADDL instead of LEAL.
659 *Inst = IsMov ? 0xc7 : 0x8d;
660 if (IsMov)
661 *Op = 0xc0 | ((*Op >> 3) & 7);
662 else
663 *Op = 0x80 | Reg | (Reg << 3);
664 }
George Rimar2558e122015-12-09 09:55:54 +0000665 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
666}
667
Rafael Espindola7f074422015-09-22 21:35:51 +0000668X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000669 CopyRel = R_X86_64_COPY;
670 GotRel = R_X86_64_GLOB_DAT;
671 PltRel = R_X86_64_JUMP_SLOT;
672 RelativeRel = R_X86_64_RELATIVE;
673 IRelativeRel = R_X86_64_IRELATIVE;
674 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000675 TlsModuleIndexRel = R_X86_64_DTPMOD64;
676 TlsOffsetRel = R_X86_64_DTPOFF64;
677 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000678 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000679 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000680}
681
Rui Ueyamac516ae12016-01-29 02:33:45 +0000682void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000683 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
684}
685
Rui Ueyamac516ae12016-01-29 02:33:45 +0000686void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000687 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000688 write32le(Buf, Plt + 6);
689}
690
Rui Ueyama900e2d22016-01-29 03:51:49 +0000691void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000692 const uint8_t PltData[] = {
693 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
694 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
695 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
696 };
697 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000698 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
699 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
700 write32le(Buf + 2, Got - Plt + 2); // GOT+8
701 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000702}
Rafael Espindola01205f72015-09-22 18:19:46 +0000703
Rui Ueyama9398f862016-01-29 04:15:02 +0000704void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
705 uint64_t PltEntryAddr, int32_t Index,
706 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000707 const uint8_t Inst[] = {
708 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
709 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
710 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
711 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000712 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000713
George Rimar648a2c32015-10-20 08:54:27 +0000714 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
715 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000716 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000717}
718
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000719bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
720 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
721 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000722}
723
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000724bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
725 return Type == R_X86_64_GOTPCREL;
726}
727
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000728bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000729 if (Type == R_X86_64_TLSGD)
Rafael Espindola993f0272016-02-26 14:27:47 +0000730 return Target->canRelaxTls(Type, &S) && canBePreempted(&S);
George Rimar77d1cb12015-11-24 09:00:06 +0000731 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000732 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000733 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000734}
735
George Rimar2960c982016-02-11 11:14:46 +0000736unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const {
737 // No other types of TLS relocations requiring GOT should
738 // reach here.
739 assert(Type == R_X86_64_GOTTPOFF);
740 return R_X86_64_PC32;
741}
742
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000743bool X86_64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
744 return Type == R_X86_64_TLSGD;
745}
746
747bool X86_64TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
748 return Type == R_X86_64_TLSLD;
749}
750
Rui Ueyamac516ae12016-01-29 02:33:45 +0000751bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000752 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000753}
754
Rafael Espindola993f0272016-02-26 14:27:47 +0000755bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
756 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000757}
Rafael Espindolac4010882015-09-22 20:54:08 +0000758
Rafael Espindolaae244002015-10-05 19:30:12 +0000759bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
760 switch (Type) {
761 default:
762 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000763 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000764 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000765 case R_X86_64_PC8:
766 case R_X86_64_PC16:
767 case R_X86_64_PC32:
768 case R_X86_64_PC64:
769 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000770 return true;
771 }
772}
773
Rui Ueyamac516ae12016-01-29 02:33:45 +0000774bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000775 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000776}
777
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000778bool X86_64TargetInfo::canRelaxTlsImpl(unsigned Type,
779 const SymbolBody *S) const {
George Rimar25411f252015-12-04 11:20:13 +0000780 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
781 Type == R_X86_64_DTPOFF32 ||
Rafael Espindola993f0272016-02-26 14:27:47 +0000782 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S));
George Rimar6713cf82015-11-25 21:46:05 +0000783}
784
785// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
786// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
787// how LD can be optimized to LE:
788// leaq bar@tlsld(%rip), %rdi
789// callq __tls_get_addr@PLT
790// leaq bar@dtpoff(%rax), %rcx
791// Is converted to:
792// .word 0x6666
793// .byte 0x66
794// mov %fs:0,%rax
795// leaq bar@tpoff(%rax), %rcx
796void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
797 uint64_t P, uint64_t SA) const {
798 const uint8_t Inst[] = {
799 0x66, 0x66, //.word 0x6666
800 0x66, //.byte 0x66
801 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
802 };
803 memcpy(Loc - 3, Inst, sizeof(Inst));
804}
805
806// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
807// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
808// how GD can be optimized to LE:
809// .byte 0x66
810// leaq x@tlsgd(%rip), %rdi
811// .word 0x6666
812// rex64
813// call __tls_get_addr@plt
814// Is converted to:
815// mov %fs:0x0,%rax
816// lea x@tpoff,%rax
817void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
818 uint64_t P, uint64_t SA) const {
819 const uint8_t Inst[] = {
820 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
821 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
822 };
823 memcpy(Loc - 4, Inst, sizeof(Inst));
824 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000825}
826
George Rimar25411f252015-12-04 11:20:13 +0000827// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
828// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
829// how GD can be optimized to IE:
830// .byte 0x66
831// leaq x@tlsgd(%rip), %rdi
832// .word 0x6666
833// rex64
834// call __tls_get_addr@plt
835// Is converted to:
836// mov %fs:0x0,%rax
837// addq x@tpoff,%rax
838void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
839 uint64_t P, uint64_t SA) const {
840 const uint8_t Inst[] = {
841 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
842 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
843 };
844 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000845 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000846}
847
George Rimar77d1cb12015-11-24 09:00:06 +0000848// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000849// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000850// This function does that. Read "ELF Handling For Thread-Local Storage,
851// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
852// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000853void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
854 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000855 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
856 // used in MOVQ or ADDQ instructions only.
857 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
858 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
859 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
860 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
861 uint8_t *Prefix = Loc - 3;
862 uint8_t *Inst = Loc - 2;
863 uint8_t *RegSlot = Loc - 1;
864 uint8_t Reg = Loc[-1] >> 3;
865 bool IsMov = *Inst == 0x8b;
866 bool RspAdd = !IsMov && Reg == 4;
867 // r12 and rsp registers requires special handling.
868 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
869 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
870 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
871 // The same true for rsp. So we convert to addq for them, saving 1 byte that
872 // we dont have.
873 if (RspAdd)
874 *Inst = 0x81;
875 else
876 *Inst = IsMov ? 0xc7 : 0x8d;
877 if (*Prefix == 0x4c)
878 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
879 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
880 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
881}
882
George Rimar6713cf82015-11-25 21:46:05 +0000883// This function applies a TLS relocation with an optimization as described
884// in the Ulrich's document. As a result of rewriting instructions at the
885// relocation target, relocations immediately follow the TLS relocation (which
886// would be applied to rewritten instructions) may have to be skipped.
887// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000888unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
889 uint32_t Type, uint64_t P, uint64_t SA,
890 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000891 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000892 case R_X86_64_DTPOFF32:
893 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
894 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000895 case R_X86_64_GOTTPOFF:
896 relocateTlsIeToLe(Loc, BufEnd, P, SA);
897 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000898 case R_X86_64_TLSGD: {
Rafael Espindola993f0272016-02-26 14:27:47 +0000899 if (canBePreempted(S))
George Rimar25411f252015-12-04 11:20:13 +0000900 relocateTlsGdToIe(Loc, BufEnd, P, SA);
901 else
902 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000903 // The next relocation should be against __tls_get_addr, so skip it
904 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000905 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000906 case R_X86_64_TLSLD:
907 relocateTlsLdToLe(Loc, BufEnd, P, SA);
908 // The next relocation should be against __tls_get_addr, so skip it
909 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000910 }
911 llvm_unreachable("Unknown TLS optimization");
912}
913
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000914void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000915 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000916 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000917 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000918 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000919 checkUInt<32>(SA, Type);
920 write32le(Loc, SA);
921 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000922 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000923 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000924 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000925 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000926 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000927 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000928 write64le(Loc, SA);
929 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000930 case R_X86_64_DTPOFF32:
931 write32le(Loc, SA);
932 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000933 case R_X86_64_GOTPCREL:
934 case R_X86_64_PC32:
935 case R_X86_64_PLT32:
936 case R_X86_64_TLSGD:
937 case R_X86_64_TLSLD:
938 write32le(Loc, SA - P);
939 break;
George Rimar48651482015-12-11 08:59:37 +0000940 case R_X86_64_SIZE32:
941 write32le(Loc, ZA);
942 break;
943 case R_X86_64_SIZE64:
944 write64le(Loc, ZA);
945 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000946 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000947 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000948 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000949 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000950 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000951 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000952 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000953 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000954 }
955}
956
Hal Finkel3c8cc672015-10-12 20:56:18 +0000957// Relocation masks following the #lo(value), #hi(value), #ha(value),
958// #higher(value), #highera(value), #highest(value), and #highesta(value)
959// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
960// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000961static uint16_t applyPPCLo(uint64_t V) { return V; }
962static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
963static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
964static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
965static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000966static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000967static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
968
Davide Italiano8c3444362016-01-11 19:45:33 +0000969PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000970bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
971
972void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
973 uint64_t P, uint64_t SA, uint64_t ZA,
974 uint8_t *PairedLoc) const {
975 switch (Type) {
976 case R_PPC_ADDR16_HA:
977 write16be(Loc, applyPPCHa(SA));
978 break;
979 case R_PPC_ADDR16_LO:
980 write16be(Loc, applyPPCLo(SA));
981 break;
982 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000983 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000984 }
985}
986
Rafael Espindolac4010882015-09-22 20:54:08 +0000987PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000988 GotRel = R_PPC64_GLOB_DAT;
989 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000990 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000991
992 // We need 64K pages (at least under glibc/Linux, the loader won't
993 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000994 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000995
996 // The PPC64 ELF ABI v1 spec, says:
997 //
998 // It is normally desirable to put segments with different characteristics
999 // in separate 256 Mbyte portions of the address space, to give the
1000 // operating system full paging flexibility in the 64-bit address space.
1001 //
1002 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1003 // use 0x10000000 as the starting address.
1004 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001005}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001006
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001007uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001008 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1009 // order. The TOC starts where the first of these sections starts.
1010
1011 // FIXME: This obviously does not do the right thing when there is no .got
1012 // section, but there is a .toc or .tocbss section.
1013 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1014 if (!TocVA)
1015 TocVA = Out<ELF64BE>::Plt->getVA();
1016
1017 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1018 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1019 // code (crt1.o) assumes that you can get from the TOC base to the
1020 // start of the .toc section with only a single (signed) 16-bit relocation.
1021 return TocVA + 0x8000;
1022}
1023
Rui Ueyama9398f862016-01-29 04:15:02 +00001024void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1025 uint64_t PltEntryAddr, int32_t Index,
1026 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1028
1029 // FIXME: What we should do, in theory, is get the offset of the function
1030 // descriptor in the .opd section, and use that as the offset from %r2 (the
1031 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1032 // be a pointer to the function descriptor in the .opd section. Using
1033 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1034
Hal Finkelfa92f682015-10-13 21:47:34 +00001035 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001036 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1037 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1038 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1039 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1040 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1041 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1042 write32be(Buf + 28, 0x4e800420); // bctr
1043}
1044
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001045bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001046 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 return true;
1048
1049 switch (Type) {
1050 default: return false;
1051 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001053 case R_PPC64_GOT16_HA:
1054 case R_PPC64_GOT16_HI:
1055 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001056 case R_PPC64_GOT16_LO_DS:
1057 return true;
1058 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001059}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060
Rafael Espindola993f0272016-02-26 14:27:47 +00001061bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001063 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001064}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065
Hal Finkelbe0823d2015-10-12 20:58:52 +00001066bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1067 switch (Type) {
1068 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001069 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001070 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001071 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001072 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001073 }
1074}
1075
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001076void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001077 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001078 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001079 uint64_t TB = getPPC64TocBase();
1080
Hal Finkel3c8cc672015-10-12 20:56:18 +00001081 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1082 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001083 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001084 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1085 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001086 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1087 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001088 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1089 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001090 default: break;
1091 }
1092
Hal Finkel3c8cc672015-10-12 20:56:18 +00001093 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001094 case R_PPC64_ADDR14: {
1095 checkAlignment<4>(SA, Type);
1096 // Preserve the AA/LK bits in the branch instruction
1097 uint8_t AALK = Loc[3];
1098 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1099 break;
1100 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001101 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001102 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001103 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001104 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001105 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001106 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001107 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001108 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001109 case R_PPC64_ADDR16_HA:
1110 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 break;
1112 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001113 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 break;
1118 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
1121 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001122 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 break;
1124 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001127 case R_PPC64_ADDR16_LO:
1128 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001129 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001130 case R_PPC64_ADDR16_LO_DS:
1131 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
1133 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001134 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001137 case R_PPC64_ADDR64:
1138 write64be(Loc, SA);
1139 break;
1140 case R_PPC64_REL16_HA:
1141 write16be(Loc, applyPPCHa(SA - P));
1142 break;
1143 case R_PPC64_REL16_HI:
1144 write16be(Loc, applyPPCHi(SA - P));
1145 break;
1146 case R_PPC64_REL16_LO:
1147 write16be(Loc, applyPPCLo(SA - P));
1148 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001149 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001150 // If we have an undefined weak symbol, we might get here with a symbol
1151 // address of zero. That could overflow, but the code must be unreachable,
1152 // so don't bother doing anything at all.
1153 if (!SA)
1154 break;
1155
Hal Finkeldaedc122015-10-12 23:16:53 +00001156 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1157 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001158 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001159
1160 if (!InPlt && Out<ELF64BE>::Opd) {
1161 // If this is a local call, and we currently have the address of a
1162 // function-descriptor, get the underlying code address instead.
1163 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1164 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001165 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001166
1167 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001168 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001169 }
1170
Hal Finkel3c8cc672015-10-12 20:56:18 +00001171 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001172 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001173 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001174
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001175 uint32_t Nop = 0x60000000;
1176 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1177 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001178 break;
1179 }
1180 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001181 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001182 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001183 break;
1184 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001185 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001186 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001187 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001189 break;
1190 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001191 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001192 }
1193}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001194
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001195AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001196 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001197 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001198 IRelativeRel = R_AARCH64_IRELATIVE;
1199 GotRel = R_AARCH64_GLOB_DAT;
1200 PltRel = R_AARCH64_JUMP_SLOT;
1201 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001202 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1203 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001204 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001205 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001206 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001207}
George Rimar648a2c32015-10-20 08:54:27 +00001208
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001209bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001210 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001211 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
Rafael Espindola57ca2702016-02-24 20:52:58 +00001212 Type == R_AARCH64_LDST32_ABS_LO12_NC ||
Rafael Espindola47ed5422016-02-24 21:48:06 +00001213 Type == R_AARCH64_LDST64_ABS_LO12_NC ||
Rafael Espindolaa598a3a2016-02-24 22:07:12 +00001214 Type == R_AARCH64_ADD_ABS_LO12_NC || Type == R_AARCH64_CALL26;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001215}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001216
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001217bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
1218 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1219 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1220 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1221 Type == R_AARCH64_TLSDESC_CALL;
1222}
1223
Rui Ueyamac516ae12016-01-29 02:33:45 +00001224unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001225 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1226 return Type;
1227 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001228 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001229 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001230 // Keep it going with a dummy value so that we can find more reloc errors.
1231 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001232}
1233
Rui Ueyamac516ae12016-01-29 02:33:45 +00001234void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001235 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1236}
1237
Rui Ueyama900e2d22016-01-29 03:51:49 +00001238void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001239 const uint8_t PltData[] = {
1240 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1241 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1242 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1243 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1244 0x20, 0x02, 0x1f, 0xd6, // br x17
1245 0x1f, 0x20, 0x03, 0xd5, // nop
1246 0x1f, 0x20, 0x03, 0xd5, // nop
1247 0x1f, 0x20, 0x03, 0xd5 // nop
1248 };
1249 memcpy(Buf, PltData, sizeof(PltData));
1250
Rui Ueyama900e2d22016-01-29 03:51:49 +00001251 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1252 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1253 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1254 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1255 Got + 16);
1256 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1257 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001258}
1259
Rui Ueyama9398f862016-01-29 04:15:02 +00001260void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1261 uint64_t PltEntryAddr, int32_t Index,
1262 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001263 const uint8_t Inst[] = {
1264 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1265 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1266 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1267 0x20, 0x02, 0x1f, 0xd6 // br x17
1268 };
1269 memcpy(Buf, Inst, sizeof(Inst));
1270
1271 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1272 GotEntryAddr);
1273 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1274 GotEntryAddr);
1275 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1276 GotEntryAddr);
1277}
1278
Rui Ueyama724d6252016-01-29 01:49:32 +00001279unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001280 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1281 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001282 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001283}
1284
Rui Ueyamac516ae12016-01-29 02:33:45 +00001285bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001286 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1287 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1288 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1289 Type == R_AARCH64_TLSDESC_CALL ||
1290 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001291 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1292}
1293
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001294bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001295 switch (Type) {
1296 default:
1297 return false;
1298 case R_AARCH64_ABS16:
1299 case R_AARCH64_ABS32:
1300 case R_AARCH64_ABS64:
1301 case R_AARCH64_ADD_ABS_LO12_NC:
1302 case R_AARCH64_ADR_PREL_LO21:
1303 case R_AARCH64_ADR_PREL_PG_HI21:
1304 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001305 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001306 case R_AARCH64_LDST32_ABS_LO12_NC:
1307 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001308 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001309 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001310 }
1311}
1312
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001313bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001314 switch (Type) {
1315 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1316 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1317 case R_AARCH64_ADR_GOT_PAGE:
1318 case R_AARCH64_LD64_GOT_LO12_NC:
1319 return true;
1320 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001321 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001322 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001323}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001324
Rafael Espindola993f0272016-02-26 14:27:47 +00001325bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001326 switch (Type) {
1327 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001328 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001329 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001330 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001331 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001332 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001333 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001334 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001335}
Davide Italiano1d750a62015-09-27 08:45:38 +00001336
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001337static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001338 uint32_t ImmLo = (Imm & 0x3) << 29;
1339 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1340 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001341 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001342}
1343
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001344static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1345 or32le(L, (Imm & 0xFFF) << 10);
1346}
1347
Davide Italiano318ca222015-10-02 22:13:51 +00001348// Page(Expr) is the page address of the expression Expr, defined
1349// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001350// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001351static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001352 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001353}
1354
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001355void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001356 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001357 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001358 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001359 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001360 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001361 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001362 break;
1363 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001364 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001365 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001366 break;
1367 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001368 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001369 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001370 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001371 // This relocation stores 12 bits and there's no instruction
1372 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001373 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1374 // bits in Loc are zero.
1375 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001376 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001377 case R_AARCH64_ADR_GOT_PAGE: {
1378 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1379 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001380 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001381 break;
1382 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001383 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001384 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001385 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001386 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001387 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001388 }
George Rimar3d737e42016-01-13 13:04:46 +00001389 case R_AARCH64_ADR_PREL_PG_HI21:
1390 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001391 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001392 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001393 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001394 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001395 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001396 case R_AARCH64_CALL26:
1397 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001398 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001399 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001400 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1401 break;
1402 }
George Rimar4102bfb2016-01-11 14:22:00 +00001403 case R_AARCH64_CONDBR19: {
1404 uint64_t X = SA - P;
1405 checkInt<21>(X, Type);
1406 or32le(Loc, (X & 0x1FFFFC) << 3);
1407 break;
1408 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001409 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001410 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001411 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001412 or32le(Loc, (SA & 0xFF8) << 7);
1413 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001414 case R_AARCH64_LDST128_ABS_LO12_NC:
1415 or32le(Loc, (SA & 0x0FF8) << 6);
1416 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001417 case R_AARCH64_LDST16_ABS_LO12_NC:
1418 or32le(Loc, (SA & 0x0FFC) << 9);
1419 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001420 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001421 or32le(Loc, (SA & 0xFFF) << 10);
1422 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001423 case R_AARCH64_LDST32_ABS_LO12_NC:
1424 or32le(Loc, (SA & 0xFFC) << 8);
1425 break;
1426 case R_AARCH64_LDST64_ABS_LO12_NC:
1427 or32le(Loc, (SA & 0xFF8) << 7);
1428 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001429 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001430 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001431 write16le(Loc, SA - P);
1432 break;
1433 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001434 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001435 write32le(Loc, SA - P);
1436 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001437 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001438 write64le(Loc, SA - P);
1439 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001440 case R_AARCH64_TSTBR14: {
1441 uint64_t X = SA - P;
1442 checkInt<16>(X, Type);
1443 or32le(Loc, (X & 0xFFFC) << 3);
1444 break;
1445 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001446 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1447 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1448 checkInt<24>(V, Type);
1449 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1450 break;
1451 }
1452 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1453 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1454 updateAArch64Add(Loc, V & 0xFFF);
1455 break;
1456 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001457 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001458 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001459 }
1460}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001461
Rafael Espindola1ea51d22016-03-04 16:14:19 +00001462bool AArch64TargetInfo::canRelaxTlsImpl(unsigned Type,
1463 const SymbolBody *S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001464 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is
1465 // an executable. And if the target is local it can also be fully relaxed to
1466 // Local-Exec.
1467 if (isTlsGlobalDynamicRel(Type))
Rafael Espindola993f0272016-02-26 14:27:47 +00001468 return !canBePreempted(S);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001469
1470 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local
1471 // symbol.
1472 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1473 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
Rafael Espindola993f0272016-02-26 14:27:47 +00001474 return !canBePreempted(S);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001475
1476 return false;
1477}
1478
1479unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1480 uint32_t Type, uint64_t P, uint64_t SA,
1481 const SymbolBody *S) const {
1482 switch (Type) {
1483 case R_AARCH64_TLSDESC_ADR_PAGE21:
1484 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1485 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1486 case R_AARCH64_TLSDESC_CALL: {
Rafael Espindola993f0272016-02-26 14:27:47 +00001487 if (canBePreempted(S))
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001488 fatal("Unsupported TLS optimization");
1489 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1490 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1491 return 0;
1492 }
1493 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1494 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1495 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1496 return 0;
1497 }
1498 llvm_unreachable("Unknown TLS optimization");
1499}
1500
1501// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1502// an executable and target is final (can notbe preempted).
1503void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc,
1504 uint8_t *BufEnd, uint64_t P,
1505 uint64_t SA) const {
1506 // TLSDESC Global-Dynamic relocation are in the form:
1507 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1508 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1509 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1510 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1511 // And it can optimized to:
1512 // movz x0, #0x0, lsl #16
1513 // movk x0, #0x10
1514 // nop
1515 // nop
1516
1517 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1518 uint64_t X = SA + TPOff;
1519 checkUInt<32>(X, Type);
1520
1521 uint32_t NewInst;
1522 switch (Type) {
1523 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1524 case R_AARCH64_TLSDESC_CALL:
1525 // nop
1526 NewInst = 0xd503201f;
1527 break;
1528 case R_AARCH64_TLSDESC_ADR_PAGE21:
1529 // movz
1530 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1531 break;
1532 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1533 // movk
1534 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1535 break;
1536 default:
1537 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1538 }
1539 write32le(Loc, NewInst);
1540}
1541
1542// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1543// (can not be preempted).
1544void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
1545 uint8_t *BufEnd, uint64_t P,
1546 uint64_t SA) const {
1547 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1548 uint64_t X = SA + TPOff;
1549 checkUInt<32>(X, Type);
1550
1551 uint32_t Inst = read32le (Loc);
1552 uint32_t NewInst;
1553 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1554 // Generate movz.
1555 unsigned RegNo = (Inst & 0x1f);
1556 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1557 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1558 // Generate movk
1559 unsigned RegNo = (Inst & 0x1f);
1560 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1561 } else {
1562 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1563 }
1564 write32le(Loc, NewInst);
1565}
1566
1567
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001568// Implementing relocations for AMDGPU is low priority since most
1569// programs don't use relocations now. Thus, this function is not
1570// actually called (relocateOne is called for each relocation).
1571// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001572void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1573 uint64_t P, uint64_t SA, uint64_t ZA,
1574 uint8_t *PairedLoc) const {
1575 llvm_unreachable("not implemented");
1576}
1577
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001578template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001579 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001580 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001581 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001582 PltEntrySize = 16;
1583 PltZeroSize = 32;
1584 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001585 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001586 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001587 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001588}
1589
1590template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001591unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001592 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1593 return R_MIPS_REL32;
1594 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001595 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001596 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001597 // Keep it going with a dummy value so that we can find more reloc errors.
1598 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001599}
1600
1601template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001602void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001603 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001604 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1605
1606 // Set the MSB of the second GOT slot. This is not required by any
1607 // MIPS ABI documentation, though.
1608 //
1609 // There is a comment in glibc saying that "The MSB of got[1] of a
1610 // gnu object is set to identify gnu objects," and in GNU gold it
1611 // says "the second entry will be used by some runtime loaders".
1612 // But how this field is being used is unclear.
1613 //
1614 // We are not really willing to mimic other linkers behaviors
1615 // without understanding why they do that, but because all files
1616 // generated by GNU tools have this special GOT value, and because
1617 // we've been doing this for years, it is probably a safe bet to
1618 // keep doing this for now. We really need to revisit this to see
1619 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001620 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001621 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001622}
1623
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001624template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001625void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1626 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001627}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001628
Simon Atanasyan35031192015-12-15 06:06:34 +00001629static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001630
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001631template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001632static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001633 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001634 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001635 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001636 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1637 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001638 checkAlignment<(1 << SHIFT)>(S + A, Type);
1639 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001640 checkInt<BSIZE + SHIFT>(V, Type);
1641 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001642}
1643
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001644template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001645static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001646 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001647 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001648}
1649
Simon Atanasyan3b377852016-03-04 10:55:20 +00001650template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001651static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1652 uint32_t Instr = read32<E>(Loc);
1653 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1654}
1655
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001656template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1657 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1658}
1659
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001660template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001661static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001662 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001663}
1664
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001665template <class ELFT>
1666void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1667 const endianness E = ELFT::TargetEndianness;
1668 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1669 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1670 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1671 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1672 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1673 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1674 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1675 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1676 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001677 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001678 writeMipsLo16<E>(Buf + 4, Got);
1679 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001680}
1681
1682template <class ELFT>
1683void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1684 uint64_t PltEntryAddr, int32_t Index,
1685 unsigned RelOff) const {
1686 const endianness E = ELFT::TargetEndianness;
1687 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1688 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1689 write32<E>(Buf + 8, 0x03200008); // jr $25
1690 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001691 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001692 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1693 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001694}
1695
1696template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001697bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001698 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001699}
1700
1701template <class ELFT>
1702bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyand040a582016-02-25 16:19:15 +00001703 return needsPlt<ELFT>(Type, S) || refersToGotEntry(Type);
1704}
1705
1706template <class ELFT>
1707bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1708 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001709}
1710
1711template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001712bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1713 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001714}
1715
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001716template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001717void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001718 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001719 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001720 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001721 switch (Type) {
1722 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001723 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001724 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001725 case R_MIPS_26: {
1726 uint32_t Instr = read32<E>(Loc);
1727 // FIXME (simon): If the relocation target symbol is not a PLT entry
1728 // we should use another expression for calculation:
1729 // ((A << 2) | (P & 0xf0000000)) >> 2
1730 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1731 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1732 break;
1733 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001734 case R_MIPS_CALL16:
1735 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001736 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001737 if (Type == R_MIPS_GOT16)
1738 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001739 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001740 break;
1741 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001742 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001743 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001744 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001745 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001746 break;
1747 }
1748 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001749 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001750 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001751 case R_MIPS_HI16:
1752 if (PairedLoc)
1753 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1754 else {
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001755 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001756 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001757 }
1758 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001759 case R_MIPS_JALR:
1760 // Ignore this optimization relocation for now
1761 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001762 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001763 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001764 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001765 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001766 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001767 break;
1768 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001769 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001770 break;
1771 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001772 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001773 break;
1774 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001775 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001776 break;
1777 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001778 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001779 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001780 case R_MIPS_PCHI16:
1781 if (PairedLoc)
1782 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1783 else {
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001784 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001785 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001786 }
1787 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001788 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001789 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001790 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001791 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001792 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001793 }
1794}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001795
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001796template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001797bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001798 return Type == R_MIPS_JALR;
1799}
1800
1801template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001802bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1803 switch (Type) {
1804 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001805 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001806 case R_MIPS_26:
1807 case R_MIPS_32:
1808 case R_MIPS_64:
1809 case R_MIPS_HI16:
1810 case R_MIPS_LO16:
1811 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001812 }
1813}
1814
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001815// _gp is a MIPS-specific ABI-defined symbol which points to
1816// a location that is relative to GOT. This function returns
1817// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001818template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001819 unsigned GPOffset = 0x7ff0;
1820 if (uint64_t V = Out<ELFT>::Got->getVA())
1821 return V + GPOffset;
1822 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001823}
1824
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001825template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1826template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1827template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1828template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1829
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001830template uint32_t getMipsGpAddr<ELF32LE>();
1831template uint32_t getMipsGpAddr<ELF32BE>();
1832template uint64_t getMipsGpAddr<ELF64LE>();
1833template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001834
1835template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1836 const SymbolBody &) const;
1837template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1838 const SymbolBody &) const;
1839template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1840 const SymbolBody &) const;
1841template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1842 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001843
1844template TargetInfo::PltNeed
1845TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1846template TargetInfo::PltNeed
1847TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1848template TargetInfo::PltNeed
1849TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1850template TargetInfo::PltNeed
1851TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001852}
1853}