blob: a2a609cdd58e5555259b06c7b22683b501784999 [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
34namespace elf2 {
35
Rui Ueyamac1c282a2016-02-11 21:18:01 +000036TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000037
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000049 error("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050}
51
52template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
53 if (isUInt<N>(V))
54 return;
55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000056 error("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000057}
58
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
60 if (isInt<N>(V) || isUInt<N>(V))
61 return;
62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000063 error("Relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064}
65
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
67 if ((V & (N - 1)) == 0)
68 return;
69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000070 error("Improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
George Rimara07ff662015-12-21 10:12:06 +000073template <class ELFT> bool isGnuIFunc(const SymbolBody &S) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000074 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S))
George Rimara07ff662015-12-21 10:12:06 +000075 return SS->Sym.getType() == STT_GNU_IFUNC;
76 return false;
77}
78
Rui Ueyamaefc23de2015-10-14 21:30:32 +000079namespace {
80class X86TargetInfo final : public TargetInfo {
81public:
82 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000083 void writeGotPltHeader(uint8_t *Buf) const override;
84 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000085 unsigned getTlsGotRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +000086 bool isTlsLocalDynamicRel(unsigned Type) const override;
87 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000088 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
89 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000090 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000091 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
92 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000093 bool needsCopyRelImpl(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000094 bool needsDynRelative(unsigned Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000095 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola852860e2016-02-12 15:47:37 +000096 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000097 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000098 uint64_t SA, uint64_t ZA = 0,
99 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000100 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000101 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
102 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000103 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000104
105private:
106 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
107 uint64_t SA) const;
108 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
109 uint64_t SA) const;
110 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
111 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000112 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
113 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000114};
115
116class X86_64TargetInfo final : public TargetInfo {
117public:
118 X86_64TargetInfo();
George Rimar2960c982016-02-11 11:14:46 +0000119 unsigned getTlsGotRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000120 bool isTlsLocalDynamicRel(unsigned Type) const override;
121 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000122 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
123 void writeGotPltHeader(uint8_t *Buf) const override;
124 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000125 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000126 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
127 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000128 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000129 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola852860e2016-02-12 15:47:37 +0000130 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000131 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000132 uint64_t SA, uint64_t ZA = 0,
133 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000135 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000136 bool isSizeRel(uint32_t Type) const override;
137 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
138 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000139
140private:
141 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
142 uint64_t SA) const;
143 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000145 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000147 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000149};
150
Davide Italiano8c3444362016-01-11 19:45:33 +0000151class PPCTargetInfo final : public TargetInfo {
152public:
153 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
155 uint64_t SA, uint64_t ZA = 0,
156 uint8_t *PairedLoc = nullptr) const override;
157 bool isRelRelative(uint32_t Type) const override;
158};
159
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000160class PPC64TargetInfo final : public TargetInfo {
161public:
162 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
164 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000165 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola852860e2016-02-12 15:47:37 +0000166 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000167 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000168 uint64_t SA, uint64_t ZA = 0,
169 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170 bool isRelRelative(uint32_t Type) const override;
171};
172
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173class AArch64TargetInfo final : public TargetInfo {
174public:
175 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000176 unsigned getDynRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000177 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000178 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000179 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000180 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
181 int32_t Index, unsigned RelOff) const override;
George Rimar2960c982016-02-11 11:14:46 +0000182 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000183 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000184 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000185 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000186 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola852860e2016-02-12 15:47:37 +0000187 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000188 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000189 uint64_t SA, uint64_t ZA = 0,
190 uint8_t *PairedLoc = nullptr) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000191 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
192 uint64_t SA, const SymbolBody *S) const override;
193 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
194
195private:
196 void relocateTlsGdToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
197 uint64_t P, uint64_t SA) const;
198 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
199 uint64_t P, uint64_t SA) const;
200
201 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000202};
203
Tom Stellard80efb162016-01-07 03:59:08 +0000204class AMDGPUTargetInfo final : public TargetInfo {
205public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000206 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000207 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
208 uint64_t SA, uint64_t ZA = 0,
209 uint8_t *PairedLoc = nullptr) const override;
210};
211
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000212template <class ELFT> class MipsTargetInfo final : public TargetInfo {
213public:
214 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000215 unsigned getDynRel(unsigned Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000216 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
217 void writePltZero(uint8_t *Buf) const override;
218 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
219 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000220 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000221 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000222 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola852860e2016-02-12 15:47:37 +0000223 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000224 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000225 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000226 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000227 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000228 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000229};
230} // anonymous namespace
231
Rui Ueyama91004392015-10-13 16:08:15 +0000232TargetInfo *createTarget() {
233 switch (Config->EMachine) {
234 case EM_386:
235 return new X86TargetInfo();
236 case EM_AARCH64:
237 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000238 case EM_AMDGPU:
239 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000240 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000241 switch (Config->EKind) {
242 case ELF32LEKind:
243 return new MipsTargetInfo<ELF32LE>();
244 case ELF32BEKind:
245 return new MipsTargetInfo<ELF32BE>();
246 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000247 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000248 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000249 case EM_PPC:
250 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000251 case EM_PPC64:
252 return new PPC64TargetInfo();
253 case EM_X86_64:
254 return new X86_64TargetInfo();
255 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000256 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000257}
258
Rafael Espindola01205f72015-09-22 18:19:46 +0000259TargetInfo::~TargetInfo() {}
260
Rui Ueyamabaf16512016-01-29 00:20:12 +0000261bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000262 return false;
263}
264
Igor Kudrinf6f45472015-11-10 08:39:27 +0000265uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
266
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000267bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
268
269template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
270 if (Config->Shared)
271 return false;
272 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
273 if (!SS)
274 return false;
275 return SS->Sym.getType() == STT_OBJECT;
276}
277
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000278template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000279bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000280 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000281}
282
Rui Ueyama012eb782016-01-29 04:05:09 +0000283bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
284 return false;
285}
286
George Rimarbfb7bf72015-12-21 10:00:12 +0000287bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000288bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000289bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000290bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000291
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000292bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000293
Rafael Espindola852860e2016-02-12 15:47:37 +0000294TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
295 const SymbolBody &S) const {
296 return Plt_No;
297}
Rui Ueyama012eb782016-01-29 04:05:09 +0000298
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000299bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
300 return false;
301}
302
303bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
304 return false;
305}
306
Rui Ueyamac516ae12016-01-29 02:33:45 +0000307unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
308 uint64_t P, uint64_t SA,
309 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000310 return 0;
311}
George Rimar77d1cb12015-11-24 09:00:06 +0000312
Rafael Espindola7f074422015-09-22 21:35:51 +0000313X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000314 CopyRel = R_386_COPY;
315 GotRel = R_386_GLOB_DAT;
316 PltRel = R_386_JUMP_SLOT;
317 IRelativeRel = R_386_IRELATIVE;
318 RelativeRel = R_386_RELATIVE;
319 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000320 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
321 TlsOffsetRel = R_386_TLS_DTPOFF32;
322 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000323 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000324 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000325}
326
Rui Ueyamac516ae12016-01-29 02:33:45 +0000327void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000328 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
329}
330
Rui Ueyamac516ae12016-01-29 02:33:45 +0000331void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000332 // Entries in .got.plt initially points back to the corresponding
333 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000334 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000335}
Rafael Espindola01205f72015-09-22 18:19:46 +0000336
Rui Ueyamac516ae12016-01-29 02:33:45 +0000337unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000338 if (Type == R_386_TLS_LE)
339 return R_386_TLS_TPOFF;
340 if (Type == R_386_TLS_LE_32)
341 return R_386_TLS_TPOFF32;
342 return Type;
343}
344
Rui Ueyama724d6252016-01-29 01:49:32 +0000345unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000346 if (Type == R_386_TLS_IE)
347 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000348 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000349}
350
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000351bool X86TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
352 return Type == R_386_TLS_GD;
353}
354
355bool X86TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
356 return Type == R_386_TLS_LDM;
357}
358
Rui Ueyamac516ae12016-01-29 02:33:45 +0000359bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000360 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
361 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000362 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000363 if (Type == R_386_TLS_IE)
364 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000365 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000366}
367
Rui Ueyama900e2d22016-01-29 03:51:49 +0000368void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000369 // Executable files and shared object files have
370 // separate procedure linkage tables.
371 if (Config->Shared) {
372 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000373 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000374 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
375 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000376 };
377 memcpy(Buf, V, sizeof(V));
378 return;
379 }
George Rimar648a2c32015-10-20 08:54:27 +0000380
George Rimar77b77792015-11-25 22:15:01 +0000381 const uint8_t PltData[] = {
382 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000383 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
384 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000385 };
386 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000387 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000388 write32le(Buf + 2, Got + 4);
389 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000390}
391
Rui Ueyama9398f862016-01-29 04:15:02 +0000392void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
393 uint64_t PltEntryAddr, int32_t Index,
394 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000395 const uint8_t Inst[] = {
396 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
397 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
398 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
399 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000400 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000401
George Rimar77b77792015-11-25 22:15:01 +0000402 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
403 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000404 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
405 : Out<ELF32LE>::Got->getVA();
406 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000407 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000408 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000409}
410
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000411bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
412 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000413}
414
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000415bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000416 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000417 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000418 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000419 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000420 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000421}
422
Rafael Espindola852860e2016-02-12 15:47:37 +0000423TargetInfo::PltNeed X86TargetInfo::needsPlt(uint32_t Type,
424 const SymbolBody &S) const {
425 if (isGnuIFunc<ELF32LE>(S) ||
426 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
427 (Type == R_386_PC32 && S.isShared()))
428 return Plt_Explicit;
429 return Plt_No;
Rafael Espindola01205f72015-09-22 18:19:46 +0000430}
431
George Rimarbfb7bf72015-12-21 10:00:12 +0000432bool X86TargetInfo::isGotRelative(uint32_t Type) const {
433 // This relocation does not require got entry,
434 // but it is relative to got and needs it to be created.
435 // Here we request for that.
436 return Type == R_386_GOTOFF;
437}
438
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000439void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000440 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000441 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000442 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000443 case R_386_32:
444 add32le(Loc, SA);
445 break;
George Rimarffb67352016-01-19 11:00:48 +0000446 case R_386_GOT32: {
447 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
448 Out<ELF32LE>::Got->getNumEntries() * 4;
449 checkInt<32>(V, Type);
450 add32le(Loc, V);
451 break;
452 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000453 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000454 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000455 break;
George Rimar13934772015-11-25 20:20:31 +0000456 case R_386_GOTPC:
457 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
458 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000459 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000460 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000461 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000462 break;
George Rimar9db204a2015-12-02 09:58:20 +0000463 case R_386_TLS_GD:
464 case R_386_TLS_LDM:
465 case R_386_TLS_TPOFF: {
466 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
467 Out<ELF32LE>::Got->getNumEntries() * 4;
468 checkInt<32>(V, Type);
469 write32le(Loc, V);
470 break;
471 }
George Rimar6f17e092015-12-17 09:32:21 +0000472 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000473 case R_386_TLS_LDO_32:
474 write32le(Loc, SA);
475 break;
George Rimard23970f2015-11-25 20:41:53 +0000476 case R_386_TLS_LE:
477 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
478 break;
479 case R_386_TLS_LE_32:
480 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
481 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000482 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000483 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000484 }
485}
486
Rui Ueyamabaf16512016-01-29 00:20:12 +0000487bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000488 if (Config->Shared || (S && !S->IsTls))
George Rimar2558e122015-12-09 09:55:54 +0000489 return false;
490 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
491 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000492 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000493 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
494}
495
Rui Ueyamac516ae12016-01-29 02:33:45 +0000496bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000497 return Config->Shared && Type == R_386_TLS_IE;
498}
499
Rui Ueyamac516ae12016-01-29 02:33:45 +0000500unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
501 uint64_t P, uint64_t SA,
502 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000503 switch (Type) {
504 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000505 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000506 relocateTlsGdToIe(Loc, BufEnd, P, SA);
507 else
508 relocateTlsGdToLe(Loc, BufEnd, P, SA);
509 // The next relocation should be against __tls_get_addr, so skip it
510 return 1;
511 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000512 case R_386_TLS_IE:
513 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000514 return 0;
515 case R_386_TLS_LDM:
516 relocateTlsLdToLe(Loc, BufEnd, P, SA);
517 // The next relocation should be against __tls_get_addr, so skip it
518 return 1;
519 case R_386_TLS_LDO_32:
520 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
521 return 0;
522 }
523 llvm_unreachable("Unknown TLS optimization");
524}
525
526// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
527// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
528// how GD can be optimized to IE:
529// leal x@tlsgd(, %ebx, 1),
530// call __tls_get_addr@plt
531// Is converted to:
532// movl %gs:0, %eax
533// addl x@gotntpoff(%ebx), %eax
534void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
535 uint64_t SA) const {
536 const uint8_t Inst[] = {
537 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
538 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
539 };
540 memcpy(Loc - 3, Inst, sizeof(Inst));
541 relocateOne(Loc + 5, BufEnd, R_386_32, P,
542 SA - Out<ELF32LE>::Got->getVA() -
543 Out<ELF32LE>::Got->getNumEntries() * 4);
544}
545
546// GD can be optimized to LE:
547// leal x@tlsgd(, %ebx, 1),
548// call __tls_get_addr@plt
549// Can be converted to:
550// movl %gs:0,%eax
551// addl $x@ntpoff,%eax
552// But gold emits subl $foo@tpoff,%eax instead of addl.
553// These instructions are completely equal in behavior.
554// This method generates subl to be consistent with gold.
555void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
556 uint64_t SA) const {
557 const uint8_t Inst[] = {
558 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
559 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
560 };
561 memcpy(Loc - 3, Inst, sizeof(Inst));
562 relocateOne(Loc + 5, BufEnd, R_386_32, P,
563 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
564}
565
566// LD can be optimized to LE:
567// leal foo(%reg),%eax
568// call ___tls_get_addr
569// Is converted to:
570// movl %gs:0,%eax
571// nop
572// leal 0(%esi,1),%esi
573void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
574 uint64_t SA) const {
575 const uint8_t Inst[] = {
576 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
577 0x90, // nop
578 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
579 };
580 memcpy(Loc - 2, Inst, sizeof(Inst));
581}
582
George Rimar6f17e092015-12-17 09:32:21 +0000583// In some conditions, relocations can be optimized to avoid using GOT.
584// This function does that for Initial Exec to Local Exec case.
585// Read "ELF Handling For Thread-Local Storage, 5.1
586// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000587// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000588void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
589 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000590 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000591 // Ulrich's document section 6.2 says that @gotntpoff can
592 // be used with MOVL or ADDL instructions.
593 // @indntpoff is similar to @gotntpoff, but for use in
594 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000595 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000596 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000597 uint8_t Reg = (Loc[-1] >> 3) & 7;
598 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000599 if (Type == R_386_TLS_IE) {
600 // For R_386_TLS_IE relocation we perform the next transformations:
601 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
602 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
603 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
604 // First one is special because when EAX is used the sequence is 5 bytes
605 // long, otherwise it is 6 bytes.
606 if (*Op == 0xa1) {
607 *Op = 0xb8;
608 } else {
609 *Inst = IsMov ? 0xc7 : 0x81;
610 *Op = 0xc0 | ((*Op >> 3) & 7);
611 }
612 } else {
613 // R_386_TLS_GOTIE relocation can be optimized to
614 // R_386_TLS_LE so that it does not use GOT.
615 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
616 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
617 // Note: gold converts to ADDL instead of LEAL.
618 *Inst = IsMov ? 0xc7 : 0x8d;
619 if (IsMov)
620 *Op = 0xc0 | ((*Op >> 3) & 7);
621 else
622 *Op = 0x80 | Reg | (Reg << 3);
623 }
George Rimar2558e122015-12-09 09:55:54 +0000624 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
625}
626
Rafael Espindola7f074422015-09-22 21:35:51 +0000627X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000628 CopyRel = R_X86_64_COPY;
629 GotRel = R_X86_64_GLOB_DAT;
630 PltRel = R_X86_64_JUMP_SLOT;
631 RelativeRel = R_X86_64_RELATIVE;
632 IRelativeRel = R_X86_64_IRELATIVE;
633 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000634 TlsModuleIndexRel = R_X86_64_DTPMOD64;
635 TlsOffsetRel = R_X86_64_DTPOFF64;
636 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000637 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000638 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000639}
640
Rui Ueyamac516ae12016-01-29 02:33:45 +0000641void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000642 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
643}
644
Rui Ueyamac516ae12016-01-29 02:33:45 +0000645void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000646 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000647 write32le(Buf, Plt + 6);
648}
649
Rui Ueyama900e2d22016-01-29 03:51:49 +0000650void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000651 const uint8_t PltData[] = {
652 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
653 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
654 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
655 };
656 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000657 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
658 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
659 write32le(Buf + 2, Got - Plt + 2); // GOT+8
660 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000661}
Rafael Espindola01205f72015-09-22 18:19:46 +0000662
Rui Ueyama9398f862016-01-29 04:15:02 +0000663void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
664 uint64_t PltEntryAddr, int32_t Index,
665 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000666 const uint8_t Inst[] = {
667 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
668 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
669 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
670 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000671 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000672
George Rimar648a2c32015-10-20 08:54:27 +0000673 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
674 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000675 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000676}
677
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000678bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
679 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
680 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000681}
682
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000683bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000684 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000685 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000686 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000687 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000688 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000689}
690
George Rimar2960c982016-02-11 11:14:46 +0000691unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const {
692 // No other types of TLS relocations requiring GOT should
693 // reach here.
694 assert(Type == R_X86_64_GOTTPOFF);
695 return R_X86_64_PC32;
696}
697
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000698bool X86_64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
699 return Type == R_X86_64_TLSGD;
700}
701
702bool X86_64TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
703 return Type == R_X86_64_TLSLD;
704}
705
Rui Ueyamac516ae12016-01-29 02:33:45 +0000706bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000707 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000708}
709
Rafael Espindola852860e2016-02-12 15:47:37 +0000710TargetInfo::PltNeed X86_64TargetInfo::needsPlt(uint32_t Type,
711 const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000712 if (needsCopyRel<ELF64LE>(Type, S))
Rafael Espindola852860e2016-02-12 15:47:37 +0000713 return Plt_No;
George Rimara07ff662015-12-21 10:12:06 +0000714 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola852860e2016-02-12 15:47:37 +0000715 return Plt_Explicit;
George Rimarbc590fe2015-10-28 16:48:58 +0000716
Rafael Espindola01205f72015-09-22 18:19:46 +0000717 switch (Type) {
718 default:
Rafael Espindola852860e2016-02-12 15:47:37 +0000719 return Plt_No;
Rafael Espindola227556e2015-10-14 16:15:46 +0000720 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000721 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000722 case R_X86_64_PC32:
723 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000724 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000725 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000726 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000727 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000728 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
729 // libc.so.
730 //
731 // The general idea on how to handle such cases is to create a PLT entry
732 // and use that as the function value.
733 //
734 // For the static linking part, we just return true and everything else
735 // will use the the PLT entry as the address.
736 //
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000737 // The remaining problem is making sure pointer equality still works. We
738 // need the help of the dynamic linker for that. We let it know that we have
739 // a direct reference to a so symbol by creating an undefined symbol with a
740 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
741 // the value of the symbol we created. This is true even for got entries, so
742 // pointer equality is maintained. To avoid an infinite loop, the only entry
743 // that points to the real function is a dedicated got entry used by the
744 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
Rafael Espindola3c412e12015-09-30 12:30:58 +0000745 // R_386_JMP_SLOT, etc).
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000746 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) {
747 if (SS->Sym.getType() == STT_FUNC)
748 return Plt_Implicit;
749 }
750 return Plt_No;
Rafael Espindola01205f72015-09-22 18:19:46 +0000751 case R_X86_64_PLT32:
Rafael Espindola852860e2016-02-12 15:47:37 +0000752 if (canBePreempted(&S, true))
753 return Plt_Explicit;
754 return Plt_No;
Rafael Espindola01205f72015-09-22 18:19:46 +0000755 }
756}
Rafael Espindolac4010882015-09-22 20:54:08 +0000757
Rafael Espindolaae244002015-10-05 19:30:12 +0000758bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
759 switch (Type) {
760 default:
761 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000762 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000763 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000764 case R_X86_64_PC8:
765 case R_X86_64_PC16:
766 case R_X86_64_PC32:
767 case R_X86_64_PC64:
768 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000769 return true;
770 }
771}
772
Rui Ueyamac516ae12016-01-29 02:33:45 +0000773bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000774 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000775}
776
Rui Ueyamabaf16512016-01-29 00:20:12 +0000777bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +0000778 if (Config->Shared || (S && !S->IsTls))
George Rimar77d1cb12015-11-24 09:00:06 +0000779 return false;
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 ||
George Rimar6713cf82015-11-25 21:46:05 +0000782 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
783}
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: {
George Rimar237b2182016-01-22 18:02:28 +0000899 if (canBePreempted(S, true))
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 {
Rui Ueyamac516ae12016-01-29 02:33:45 +00001046 if (needsPlt(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 Espindola852860e2016-02-12 15:47:37 +00001061TargetInfo::PltNeed PPC64TargetInfo::needsPlt(uint32_t Type,
1062 const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola852860e2016-02-12 15:47:37 +00001064 if (Type == R_PPC64_REL24 && canBePreempted(&S, false))
1065 return Plt_Explicit;
1066 return Plt_No;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001067}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068
Hal Finkelbe0823d2015-10-12 20:58:52 +00001069bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1070 switch (Type) {
1071 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001072 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001073 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001074 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001075 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001076 }
1077}
1078
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001079void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001080 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001081 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082 uint64_t TB = getPPC64TocBase();
1083
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1085 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001086 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001087 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1088 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001089 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1090 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001091 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1092 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001093 default: break;
1094 }
1095
Hal Finkel3c8cc672015-10-12 20:56:18 +00001096 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001097 case R_PPC64_ADDR14: {
1098 checkAlignment<4>(SA, Type);
1099 // Preserve the AA/LK bits in the branch instruction
1100 uint8_t AALK = Loc[3];
1101 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1102 break;
1103 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001105 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001106 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001107 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001108 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001109 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001110 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001112 case R_PPC64_ADDR16_HA:
1113 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 break;
1115 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001118 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
1121 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001122 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 break;
1124 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001129 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001130 case R_PPC64_ADDR16_LO:
1131 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001133 case R_PPC64_ADDR16_LO_DS:
1134 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001135 break;
1136 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001137 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001140 case R_PPC64_ADDR64:
1141 write64be(Loc, SA);
1142 break;
1143 case R_PPC64_REL16_HA:
1144 write16be(Loc, applyPPCHa(SA - P));
1145 break;
1146 case R_PPC64_REL16_HI:
1147 write16be(Loc, applyPPCHi(SA - P));
1148 break;
1149 case R_PPC64_REL16_LO:
1150 write16be(Loc, applyPPCLo(SA - P));
1151 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001152 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001153 // If we have an undefined weak symbol, we might get here with a symbol
1154 // address of zero. That could overflow, but the code must be unreachable,
1155 // so don't bother doing anything at all.
1156 if (!SA)
1157 break;
1158
Hal Finkeldaedc122015-10-12 23:16:53 +00001159 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1160 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001161 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001162
1163 if (!InPlt && Out<ELF64BE>::Opd) {
1164 // If this is a local call, and we currently have the address of a
1165 // function-descriptor, get the underlying code address instead.
1166 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1167 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001168 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001169
1170 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001171 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001172 }
1173
Hal Finkel3c8cc672015-10-12 20:56:18 +00001174 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001175 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001176 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001177
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001178 uint32_t Nop = 0x60000000;
1179 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1180 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001181 break;
1182 }
1183 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001184 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001185 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001186 break;
1187 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001189 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001190 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001191 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001192 break;
1193 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001194 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001195 }
1196}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001197
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001198AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001199 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001200 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001201 IRelativeRel = R_AARCH64_IRELATIVE;
1202 GotRel = R_AARCH64_GLOB_DAT;
1203 PltRel = R_AARCH64_JUMP_SLOT;
1204 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001205 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1206 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001207 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001208 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001209 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001210}
George Rimar648a2c32015-10-20 08:54:27 +00001211
Rafael Espindola435c00f2016-02-23 20:19:44 +00001212bool AArch64TargetInfo::isRelRelative(uint32_t Type) const { return false; }
1213
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001214bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
1215 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1216 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1217 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1218 Type == R_AARCH64_TLSDESC_CALL;
1219}
1220
Rui Ueyamac516ae12016-01-29 02:33:45 +00001221unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001222 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1223 return Type;
1224 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001225 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001226 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001227 // Keep it going with a dummy value so that we can find more reloc errors.
1228 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001229}
1230
Rui Ueyamac516ae12016-01-29 02:33:45 +00001231void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001232 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1233}
1234
Rui Ueyama900e2d22016-01-29 03:51:49 +00001235void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001236 const uint8_t PltData[] = {
1237 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1238 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1239 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1240 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1241 0x20, 0x02, 0x1f, 0xd6, // br x17
1242 0x1f, 0x20, 0x03, 0xd5, // nop
1243 0x1f, 0x20, 0x03, 0xd5, // nop
1244 0x1f, 0x20, 0x03, 0xd5 // nop
1245 };
1246 memcpy(Buf, PltData, sizeof(PltData));
1247
Rui Ueyama900e2d22016-01-29 03:51:49 +00001248 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1249 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1250 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1251 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1252 Got + 16);
1253 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1254 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001255}
1256
Rui Ueyama9398f862016-01-29 04:15:02 +00001257void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1258 uint64_t PltEntryAddr, int32_t Index,
1259 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001260 const uint8_t Inst[] = {
1261 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1262 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1263 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1264 0x20, 0x02, 0x1f, 0xd6 // br x17
1265 };
1266 memcpy(Buf, Inst, sizeof(Inst));
1267
1268 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1269 GotEntryAddr);
1270 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1271 GotEntryAddr);
1272 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1273 GotEntryAddr);
1274}
1275
Rui Ueyama724d6252016-01-29 01:49:32 +00001276unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001277 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1278 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001279 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001280}
1281
Rui Ueyamac516ae12016-01-29 02:33:45 +00001282bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001283 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1284 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1285 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1286 Type == R_AARCH64_TLSDESC_CALL ||
1287 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001288 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1289}
1290
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001291bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001292 switch (Type) {
1293 default:
1294 return false;
1295 case R_AARCH64_ABS16:
1296 case R_AARCH64_ABS32:
1297 case R_AARCH64_ABS64:
1298 case R_AARCH64_ADD_ABS_LO12_NC:
1299 case R_AARCH64_ADR_PREL_LO21:
1300 case R_AARCH64_ADR_PREL_PG_HI21:
1301 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001302 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001303 case R_AARCH64_LDST32_ABS_LO12_NC:
1304 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001305 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001306 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001307 }
1308}
1309
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001310bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001311 switch (Type) {
1312 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1313 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1314 case R_AARCH64_ADR_GOT_PAGE:
1315 case R_AARCH64_LD64_GOT_LO12_NC:
1316 return true;
1317 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001318 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001319 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001320}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001321
Rafael Espindola852860e2016-02-12 15:47:37 +00001322TargetInfo::PltNeed AArch64TargetInfo::needsPlt(uint32_t Type,
1323 const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001324 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola852860e2016-02-12 15:47:37 +00001325 return Plt_Explicit;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001326 switch (Type) {
1327 default:
Rafael Espindola852860e2016-02-12 15:47:37 +00001328 return Plt_No;
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 Espindola852860e2016-02-12 15:47:37 +00001333 if (canBePreempted(&S, true))
1334 return Plt_Explicit;
1335 return Plt_No;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001336 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001337}
Davide Italiano1d750a62015-09-27 08:45:38 +00001338
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001339static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001340 uint32_t ImmLo = (Imm & 0x3) << 29;
1341 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1342 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001343 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001344}
1345
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001346static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1347 or32le(L, (Imm & 0xFFF) << 10);
1348}
1349
Davide Italiano318ca222015-10-02 22:13:51 +00001350// Page(Expr) is the page address of the expression Expr, defined
1351// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001352// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001353static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001354 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001355}
1356
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001357void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001358 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001359 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001360 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001361 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001362 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001363 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001364 break;
1365 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001366 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001367 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001368 break;
1369 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001370 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001371 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001372 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001373 // This relocation stores 12 bits and there's no instruction
1374 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001375 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1376 // bits in Loc are zero.
1377 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001378 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001379 case R_AARCH64_ADR_GOT_PAGE: {
1380 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1381 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001382 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001383 break;
1384 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001385 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001386 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001387 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001388 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001389 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001390 }
George Rimar3d737e42016-01-13 13:04:46 +00001391 case R_AARCH64_ADR_PREL_PG_HI21:
1392 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001393 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001394 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001395 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001396 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001397 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001398 case R_AARCH64_CALL26:
1399 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001400 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001401 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001402 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1403 break;
1404 }
George Rimar4102bfb2016-01-11 14:22:00 +00001405 case R_AARCH64_CONDBR19: {
1406 uint64_t X = SA - P;
1407 checkInt<21>(X, Type);
1408 or32le(Loc, (X & 0x1FFFFC) << 3);
1409 break;
1410 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001411 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001412 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001413 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001414 or32le(Loc, (SA & 0xFF8) << 7);
1415 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001416 case R_AARCH64_LDST128_ABS_LO12_NC:
1417 or32le(Loc, (SA & 0x0FF8) << 6);
1418 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001419 case R_AARCH64_LDST16_ABS_LO12_NC:
1420 or32le(Loc, (SA & 0x0FFC) << 9);
1421 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001422 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001423 or32le(Loc, (SA & 0xFFF) << 10);
1424 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001425 case R_AARCH64_LDST32_ABS_LO12_NC:
1426 or32le(Loc, (SA & 0xFFC) << 8);
1427 break;
1428 case R_AARCH64_LDST64_ABS_LO12_NC:
1429 or32le(Loc, (SA & 0xFF8) << 7);
1430 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001431 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001432 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001433 write16le(Loc, SA - P);
1434 break;
1435 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001436 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001437 write32le(Loc, SA - P);
1438 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001439 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001440 write64le(Loc, SA - P);
1441 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001442 case R_AARCH64_TSTBR14: {
1443 uint64_t X = SA - P;
1444 checkInt<16>(X, Type);
1445 or32le(Loc, (X & 0xFFFC) << 3);
1446 break;
1447 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001448 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1449 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1450 checkInt<24>(V, Type);
1451 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1452 break;
1453 }
1454 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1455 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1456 updateAArch64Add(Loc, V & 0xFFF);
1457 break;
1458 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001459 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001460 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001461 }
1462}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001463
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001464bool AArch64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +00001465 if (Config->Shared || (S && !S->IsTls))
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001466 return false;
1467
1468 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is
1469 // an executable. And if the target is local it can also be fully relaxed to
1470 // Local-Exec.
1471 if (isTlsGlobalDynamicRel(Type))
1472 return !canBePreempted(S, true);
1473
1474 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local
1475 // symbol.
1476 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1477 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1478 return !canBePreempted(S, true);
1479
1480 return false;
1481}
1482
1483unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1484 uint32_t Type, uint64_t P, uint64_t SA,
1485 const SymbolBody *S) const {
1486 switch (Type) {
1487 case R_AARCH64_TLSDESC_ADR_PAGE21:
1488 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1489 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1490 case R_AARCH64_TLSDESC_CALL: {
1491 if (canBePreempted(S, true))
1492 fatal("Unsupported TLS optimization");
1493 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1494 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1495 return 0;
1496 }
1497 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1498 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1499 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1500 return 0;
1501 }
1502 llvm_unreachable("Unknown TLS optimization");
1503}
1504
1505// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1506// an executable and target is final (can notbe preempted).
1507void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc,
1508 uint8_t *BufEnd, uint64_t P,
1509 uint64_t SA) const {
1510 // TLSDESC Global-Dynamic relocation are in the form:
1511 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1512 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1513 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1514 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1515 // And it can optimized to:
1516 // movz x0, #0x0, lsl #16
1517 // movk x0, #0x10
1518 // nop
1519 // nop
1520
1521 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1522 uint64_t X = SA + TPOff;
1523 checkUInt<32>(X, Type);
1524
1525 uint32_t NewInst;
1526 switch (Type) {
1527 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1528 case R_AARCH64_TLSDESC_CALL:
1529 // nop
1530 NewInst = 0xd503201f;
1531 break;
1532 case R_AARCH64_TLSDESC_ADR_PAGE21:
1533 // movz
1534 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1535 break;
1536 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1537 // movk
1538 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1539 break;
1540 default:
1541 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1542 }
1543 write32le(Loc, NewInst);
1544}
1545
1546// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1547// (can not be preempted).
1548void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
1549 uint8_t *BufEnd, uint64_t P,
1550 uint64_t SA) const {
1551 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1552 uint64_t X = SA + TPOff;
1553 checkUInt<32>(X, Type);
1554
1555 uint32_t Inst = read32le (Loc);
1556 uint32_t NewInst;
1557 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1558 // Generate movz.
1559 unsigned RegNo = (Inst & 0x1f);
1560 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1561 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1562 // Generate movk
1563 unsigned RegNo = (Inst & 0x1f);
1564 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1565 } else {
1566 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1567 }
1568 write32le(Loc, NewInst);
1569}
1570
1571
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001572// Implementing relocations for AMDGPU is low priority since most
1573// programs don't use relocations now. Thus, this function is not
1574// actually called (relocateOne is called for each relocation).
1575// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001576void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1577 uint64_t P, uint64_t SA, uint64_t ZA,
1578 uint8_t *PairedLoc) const {
1579 llvm_unreachable("not implemented");
1580}
1581
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001582template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001583 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001584 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001585 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001586 PltEntrySize = 16;
1587 PltZeroSize = 32;
1588 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001589 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001590 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001591 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001592}
1593
1594template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001595unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001596 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1597 return R_MIPS_REL32;
1598 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001599 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001600 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001601 // Keep it going with a dummy value so that we can find more reloc errors.
1602 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001603}
1604
1605template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001606void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001607 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001608 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1609
1610 // Set the MSB of the second GOT slot. This is not required by any
1611 // MIPS ABI documentation, though.
1612 //
1613 // There is a comment in glibc saying that "The MSB of got[1] of a
1614 // gnu object is set to identify gnu objects," and in GNU gold it
1615 // says "the second entry will be used by some runtime loaders".
1616 // But how this field is being used is unclear.
1617 //
1618 // We are not really willing to mimic other linkers behaviors
1619 // without understanding why they do that, but because all files
1620 // generated by GNU tools have this special GOT value, and because
1621 // we've been doing this for years, it is probably a safe bet to
1622 // keep doing this for now. We really need to revisit this to see
1623 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001624 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001625 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001626}
1627
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001628template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001629void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1630 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001631}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001632
Simon Atanasyan35031192015-12-15 06:06:34 +00001633static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001634
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001635template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001636static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001637 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001638 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001639 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001640 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1641 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001642 checkAlignment<(1 << SHIFT)>(S + A, Type);
1643 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001644 checkInt<BSIZE + SHIFT>(V, Type);
1645 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001646}
1647
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001648template <endianness E>
1649static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1650 uint32_t Instr = read32<E>(Loc);
1651 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1652}
1653
1654template <class ELFT>
1655void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1656 const endianness E = ELFT::TargetEndianness;
1657 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1658 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1659 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1660 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1661 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1662 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1663 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1664 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1665 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1666 uint64_t Plt = Out<ELFT>::Plt->getVA();
1667 applyMipsHi16Reloc<E>(Buf, Got, 0);
1668 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1669 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1670}
1671
1672template <class ELFT>
1673void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1674 uint64_t PltEntryAddr, int32_t Index,
1675 unsigned RelOff) const {
1676 const endianness E = ELFT::TargetEndianness;
1677 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1678 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1679 write32<E>(Buf + 8, 0x03200008); // jr $25
1680 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1681 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1682 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1683 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1684}
1685
1686template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001687bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
1688 return Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001689}
1690
1691template <class ELFT>
1692bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
1693 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
1694}
1695
1696template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +00001697TargetInfo::PltNeed MipsTargetInfo<ELFT>::needsPlt(uint32_t Type,
1698 const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001699 if (needsCopyRel<ELFT>(Type, S))
Rafael Espindola852860e2016-02-12 15:47:37 +00001700 return Plt_No;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001701 if (Type == R_MIPS_26 && canBePreempted(&S, false))
Rafael Espindola852860e2016-02-12 15:47:37 +00001702 return Plt_Explicit;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001703 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
Rafael Espindola852860e2016-02-12 15:47:37 +00001704 if (S.isShared())
1705 return Plt_Explicit;
1706 return Plt_No;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001707}
1708
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001709template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001710void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001711 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001712 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001713 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001714 switch (Type) {
1715 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001716 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001717 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001718 case R_MIPS_26: {
1719 uint32_t Instr = read32<E>(Loc);
1720 // FIXME (simon): If the relocation target symbol is not a PLT entry
1721 // we should use another expression for calculation:
1722 // ((A << 2) | (P & 0xf0000000)) >> 2
1723 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1724 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1725 break;
1726 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001727 case R_MIPS_CALL16:
1728 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001729 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001730 if (Type == R_MIPS_GOT16)
1731 checkInt<16>(V, Type);
1732 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1733 break;
1734 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001735 case R_MIPS_GPREL16: {
1736 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001737 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001738 checkInt<16>(V, Type);
1739 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1740 break;
1741 }
1742 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001743 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001744 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001745 case R_MIPS_HI16: {
1746 uint32_t Instr = read32<E>(Loc);
1747 if (PairedLoc) {
1748 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001749 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001750 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001751 } else {
1752 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001753 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001754 }
1755 break;
1756 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001757 case R_MIPS_JALR:
1758 // Ignore this optimization relocation for now
1759 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001760 case R_MIPS_LO16: {
1761 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001762 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001763 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001764 break;
1765 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001766 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001767 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001768 break;
1769 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001770 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001771 break;
1772 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001773 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001774 break;
1775 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001776 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001777 break;
1778 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001779 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780 break;
1781 case R_MIPS_PCHI16: {
1782 uint32_t Instr = read32<E>(Loc);
1783 if (PairedLoc) {
1784 uint64_t AHL = ((Instr & 0xffff) << 16) +
1785 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001786 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001787 } else {
1788 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001789 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001790 }
1791 break;
1792 }
1793 case R_MIPS_PCLO16: {
1794 uint32_t Instr = read32<E>(Loc);
1795 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001796 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001797 break;
1798 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001799 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001800 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001801 }
1802}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001803
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001804template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001805bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001806 return Type == R_MIPS_JALR;
1807}
1808
1809template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001810bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1811 switch (Type) {
1812 default:
1813 return false;
1814 case R_MIPS_PC16:
1815 case R_MIPS_PC19_S2:
1816 case R_MIPS_PC21_S2:
1817 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001818 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001819 case R_MIPS_PCHI16:
1820 case R_MIPS_PCLO16:
1821 return true;
1822 }
1823}
1824
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001825// _gp is a MIPS-specific ABI-defined symbol which points to
1826// a location that is relative to GOT. This function returns
1827// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001828template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001829 unsigned GPOffset = 0x7ff0;
1830 if (uint64_t V = Out<ELFT>::Got->getVA())
1831 return V + GPOffset;
1832 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001833}
1834
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001835template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1836template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1837template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1838template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1839
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001840template uint32_t getMipsGpAddr<ELF32LE>();
1841template uint32_t getMipsGpAddr<ELF32BE>();
1842template uint64_t getMipsGpAddr<ELF64LE>();
1843template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001844
1845template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1846 const SymbolBody &) const;
1847template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1848 const SymbolBody &) const;
1849template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1850 const SymbolBody &) const;
1851template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1852 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001853}
1854}