blob: ef8ddcc0bcaaf7c766e4cf594a367592bdefb847 [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 Espindolaa4e35f72016-02-24 16:15:13 +00001212bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
1213 return Type == R_AARCH64_PREL32;
1214}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001215
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001216bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
1217 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1218 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1219 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1220 Type == R_AARCH64_TLSDESC_CALL;
1221}
1222
Rui Ueyamac516ae12016-01-29 02:33:45 +00001223unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001224 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1225 return Type;
1226 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001227 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001228 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001229 // Keep it going with a dummy value so that we can find more reloc errors.
1230 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001231}
1232
Rui Ueyamac516ae12016-01-29 02:33:45 +00001233void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001234 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1235}
1236
Rui Ueyama900e2d22016-01-29 03:51:49 +00001237void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001238 const uint8_t PltData[] = {
1239 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1240 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1241 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1242 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1243 0x20, 0x02, 0x1f, 0xd6, // br x17
1244 0x1f, 0x20, 0x03, 0xd5, // nop
1245 0x1f, 0x20, 0x03, 0xd5, // nop
1246 0x1f, 0x20, 0x03, 0xd5 // nop
1247 };
1248 memcpy(Buf, PltData, sizeof(PltData));
1249
Rui Ueyama900e2d22016-01-29 03:51:49 +00001250 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1251 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1252 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1253 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1254 Got + 16);
1255 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1256 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001257}
1258
Rui Ueyama9398f862016-01-29 04:15:02 +00001259void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1260 uint64_t PltEntryAddr, int32_t Index,
1261 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001262 const uint8_t Inst[] = {
1263 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1264 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1265 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1266 0x20, 0x02, 0x1f, 0xd6 // br x17
1267 };
1268 memcpy(Buf, Inst, sizeof(Inst));
1269
1270 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1271 GotEntryAddr);
1272 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1273 GotEntryAddr);
1274 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1275 GotEntryAddr);
1276}
1277
Rui Ueyama724d6252016-01-29 01:49:32 +00001278unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001279 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1280 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001281 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001282}
1283
Rui Ueyamac516ae12016-01-29 02:33:45 +00001284bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001285 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1286 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1287 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1288 Type == R_AARCH64_TLSDESC_CALL ||
1289 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001290 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1291}
1292
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001293bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001294 switch (Type) {
1295 default:
1296 return false;
1297 case R_AARCH64_ABS16:
1298 case R_AARCH64_ABS32:
1299 case R_AARCH64_ABS64:
1300 case R_AARCH64_ADD_ABS_LO12_NC:
1301 case R_AARCH64_ADR_PREL_LO21:
1302 case R_AARCH64_ADR_PREL_PG_HI21:
1303 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001304 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001305 case R_AARCH64_LDST32_ABS_LO12_NC:
1306 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001307 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001308 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001309 }
1310}
1311
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001312bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001313 switch (Type) {
1314 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1315 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1316 case R_AARCH64_ADR_GOT_PAGE:
1317 case R_AARCH64_LD64_GOT_LO12_NC:
1318 return true;
1319 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001320 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001321 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001322}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001323
Rafael Espindola852860e2016-02-12 15:47:37 +00001324TargetInfo::PltNeed AArch64TargetInfo::needsPlt(uint32_t Type,
1325 const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001326 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola852860e2016-02-12 15:47:37 +00001327 return Plt_Explicit;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001328 switch (Type) {
1329 default:
Rafael Espindola852860e2016-02-12 15:47:37 +00001330 return Plt_No;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001331 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001332 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001333 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001334 case R_AARCH64_TSTBR14:
Rafael Espindola852860e2016-02-12 15:47:37 +00001335 if (canBePreempted(&S, true))
1336 return Plt_Explicit;
1337 return Plt_No;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001338 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001339}
Davide Italiano1d750a62015-09-27 08:45:38 +00001340
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001341static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001342 uint32_t ImmLo = (Imm & 0x3) << 29;
1343 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1344 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001345 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001346}
1347
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001348static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1349 or32le(L, (Imm & 0xFFF) << 10);
1350}
1351
Davide Italiano318ca222015-10-02 22:13:51 +00001352// Page(Expr) is the page address of the expression Expr, defined
1353// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001354// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001355static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001356 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001357}
1358
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001359void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001360 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001361 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001362 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001363 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001364 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001365 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001366 break;
1367 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001368 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001369 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001370 break;
1371 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001372 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001373 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001374 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001375 // This relocation stores 12 bits and there's no instruction
1376 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001377 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1378 // bits in Loc are zero.
1379 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001380 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001381 case R_AARCH64_ADR_GOT_PAGE: {
1382 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1383 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001384 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001385 break;
1386 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001387 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001388 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001389 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001390 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001391 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001392 }
George Rimar3d737e42016-01-13 13:04:46 +00001393 case R_AARCH64_ADR_PREL_PG_HI21:
1394 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001395 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001396 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001397 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001398 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001399 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001400 case R_AARCH64_CALL26:
1401 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001402 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001403 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001404 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1405 break;
1406 }
George Rimar4102bfb2016-01-11 14:22:00 +00001407 case R_AARCH64_CONDBR19: {
1408 uint64_t X = SA - P;
1409 checkInt<21>(X, Type);
1410 or32le(Loc, (X & 0x1FFFFC) << 3);
1411 break;
1412 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001413 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001414 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001415 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001416 or32le(Loc, (SA & 0xFF8) << 7);
1417 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001418 case R_AARCH64_LDST128_ABS_LO12_NC:
1419 or32le(Loc, (SA & 0x0FF8) << 6);
1420 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001421 case R_AARCH64_LDST16_ABS_LO12_NC:
1422 or32le(Loc, (SA & 0x0FFC) << 9);
1423 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001424 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001425 or32le(Loc, (SA & 0xFFF) << 10);
1426 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001427 case R_AARCH64_LDST32_ABS_LO12_NC:
1428 or32le(Loc, (SA & 0xFFC) << 8);
1429 break;
1430 case R_AARCH64_LDST64_ABS_LO12_NC:
1431 or32le(Loc, (SA & 0xFF8) << 7);
1432 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001433 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001434 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001435 write16le(Loc, SA - P);
1436 break;
1437 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001438 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001439 write32le(Loc, SA - P);
1440 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001441 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001442 write64le(Loc, SA - P);
1443 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001444 case R_AARCH64_TSTBR14: {
1445 uint64_t X = SA - P;
1446 checkInt<16>(X, Type);
1447 or32le(Loc, (X & 0xFFFC) << 3);
1448 break;
1449 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001450 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1451 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1452 checkInt<24>(V, Type);
1453 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1454 break;
1455 }
1456 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1457 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1458 updateAArch64Add(Loc, V & 0xFFF);
1459 break;
1460 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001461 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001462 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001463 }
1464}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001465
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001466bool AArch64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rafael Espindola5e8b54a2016-02-22 23:16:05 +00001467 if (Config->Shared || (S && !S->IsTls))
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001468 return false;
1469
1470 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is
1471 // an executable. And if the target is local it can also be fully relaxed to
1472 // Local-Exec.
1473 if (isTlsGlobalDynamicRel(Type))
1474 return !canBePreempted(S, true);
1475
1476 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local
1477 // symbol.
1478 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1479 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1480 return !canBePreempted(S, true);
1481
1482 return false;
1483}
1484
1485unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1486 uint32_t Type, uint64_t P, uint64_t SA,
1487 const SymbolBody *S) const {
1488 switch (Type) {
1489 case R_AARCH64_TLSDESC_ADR_PAGE21:
1490 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1491 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1492 case R_AARCH64_TLSDESC_CALL: {
1493 if (canBePreempted(S, true))
1494 fatal("Unsupported TLS optimization");
1495 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1496 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1497 return 0;
1498 }
1499 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1500 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1501 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1502 return 0;
1503 }
1504 llvm_unreachable("Unknown TLS optimization");
1505}
1506
1507// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1508// an executable and target is final (can notbe preempted).
1509void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc,
1510 uint8_t *BufEnd, uint64_t P,
1511 uint64_t SA) const {
1512 // TLSDESC Global-Dynamic relocation are in the form:
1513 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1514 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1515 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1516 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1517 // And it can optimized to:
1518 // movz x0, #0x0, lsl #16
1519 // movk x0, #0x10
1520 // nop
1521 // nop
1522
1523 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1524 uint64_t X = SA + TPOff;
1525 checkUInt<32>(X, Type);
1526
1527 uint32_t NewInst;
1528 switch (Type) {
1529 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1530 case R_AARCH64_TLSDESC_CALL:
1531 // nop
1532 NewInst = 0xd503201f;
1533 break;
1534 case R_AARCH64_TLSDESC_ADR_PAGE21:
1535 // movz
1536 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1537 break;
1538 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1539 // movk
1540 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1541 break;
1542 default:
1543 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1544 }
1545 write32le(Loc, NewInst);
1546}
1547
1548// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1549// (can not be preempted).
1550void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
1551 uint8_t *BufEnd, uint64_t P,
1552 uint64_t SA) const {
1553 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1554 uint64_t X = SA + TPOff;
1555 checkUInt<32>(X, Type);
1556
1557 uint32_t Inst = read32le (Loc);
1558 uint32_t NewInst;
1559 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1560 // Generate movz.
1561 unsigned RegNo = (Inst & 0x1f);
1562 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1563 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1564 // Generate movk
1565 unsigned RegNo = (Inst & 0x1f);
1566 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1567 } else {
1568 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1569 }
1570 write32le(Loc, NewInst);
1571}
1572
1573
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001574// Implementing relocations for AMDGPU is low priority since most
1575// programs don't use relocations now. Thus, this function is not
1576// actually called (relocateOne is called for each relocation).
1577// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001578void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1579 uint64_t P, uint64_t SA, uint64_t ZA,
1580 uint8_t *PairedLoc) const {
1581 llvm_unreachable("not implemented");
1582}
1583
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001584template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001585 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001586 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001587 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001588 PltEntrySize = 16;
1589 PltZeroSize = 32;
1590 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001591 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001592 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001593 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001594}
1595
1596template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001597unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001598 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1599 return R_MIPS_REL32;
1600 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001601 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001602 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001603 // Keep it going with a dummy value so that we can find more reloc errors.
1604 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001605}
1606
1607template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001608void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001609 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001610 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1611
1612 // Set the MSB of the second GOT slot. This is not required by any
1613 // MIPS ABI documentation, though.
1614 //
1615 // There is a comment in glibc saying that "The MSB of got[1] of a
1616 // gnu object is set to identify gnu objects," and in GNU gold it
1617 // says "the second entry will be used by some runtime loaders".
1618 // But how this field is being used is unclear.
1619 //
1620 // We are not really willing to mimic other linkers behaviors
1621 // without understanding why they do that, but because all files
1622 // generated by GNU tools have this special GOT value, and because
1623 // we've been doing this for years, it is probably a safe bet to
1624 // keep doing this for now. We really need to revisit this to see
1625 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001626 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001627 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001628}
1629
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001630template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001631void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1632 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001633}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001634
Simon Atanasyan35031192015-12-15 06:06:34 +00001635static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001636
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001637template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001638static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001639 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001640 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001641 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001642 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1643 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001644 checkAlignment<(1 << SHIFT)>(S + A, Type);
1645 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001646 checkInt<BSIZE + SHIFT>(V, Type);
1647 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001648}
1649
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001650template <endianness E>
1651static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1652 uint32_t Instr = read32<E>(Loc);
1653 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1654}
1655
1656template <class ELFT>
1657void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1658 const endianness E = ELFT::TargetEndianness;
1659 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1660 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1661 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1662 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1663 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1664 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1665 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1666 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1667 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1668 uint64_t Plt = Out<ELFT>::Plt->getVA();
1669 applyMipsHi16Reloc<E>(Buf, Got, 0);
1670 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1671 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1672}
1673
1674template <class ELFT>
1675void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1676 uint64_t PltEntryAddr, int32_t Index,
1677 unsigned RelOff) const {
1678 const endianness E = ELFT::TargetEndianness;
1679 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1680 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1681 write32<E>(Buf + 8, 0x03200008); // jr $25
1682 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1683 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1684 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1685 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1686}
1687
1688template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001689bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
1690 return Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001691}
1692
1693template <class ELFT>
1694bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
1695 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
1696}
1697
1698template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +00001699TargetInfo::PltNeed MipsTargetInfo<ELFT>::needsPlt(uint32_t Type,
1700 const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001701 if (needsCopyRel<ELFT>(Type, S))
Rafael Espindola852860e2016-02-12 15:47:37 +00001702 return Plt_No;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001703 if (Type == R_MIPS_26 && canBePreempted(&S, false))
Rafael Espindola852860e2016-02-12 15:47:37 +00001704 return Plt_Explicit;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001705 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
Rafael Espindola852860e2016-02-12 15:47:37 +00001706 if (S.isShared())
1707 return Plt_Explicit;
1708 return Plt_No;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001709}
1710
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001711template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001712void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001713 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001714 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001715 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001716 switch (Type) {
1717 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001718 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001719 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001720 case R_MIPS_26: {
1721 uint32_t Instr = read32<E>(Loc);
1722 // FIXME (simon): If the relocation target symbol is not a PLT entry
1723 // we should use another expression for calculation:
1724 // ((A << 2) | (P & 0xf0000000)) >> 2
1725 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1726 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1727 break;
1728 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001729 case R_MIPS_CALL16:
1730 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001731 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001732 if (Type == R_MIPS_GOT16)
1733 checkInt<16>(V, Type);
1734 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1735 break;
1736 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001737 case R_MIPS_GPREL16: {
1738 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001739 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001740 checkInt<16>(V, Type);
1741 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1742 break;
1743 }
1744 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001745 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001746 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001747 case R_MIPS_HI16: {
1748 uint32_t Instr = read32<E>(Loc);
1749 if (PairedLoc) {
1750 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001751 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001752 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001753 } else {
1754 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001755 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001756 }
1757 break;
1758 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001759 case R_MIPS_JALR:
1760 // Ignore this optimization relocation for now
1761 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001762 case R_MIPS_LO16: {
1763 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001764 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001765 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001766 break;
1767 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001768 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001769 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001770 break;
1771 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001772 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001773 break;
1774 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001775 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001776 break;
1777 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001778 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001779 break;
1780 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001781 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001782 break;
1783 case R_MIPS_PCHI16: {
1784 uint32_t Instr = read32<E>(Loc);
1785 if (PairedLoc) {
1786 uint64_t AHL = ((Instr & 0xffff) << 16) +
1787 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001788 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001789 } else {
1790 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001791 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001792 }
1793 break;
1794 }
1795 case R_MIPS_PCLO16: {
1796 uint32_t Instr = read32<E>(Loc);
1797 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001798 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001799 break;
1800 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001801 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001802 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001803 }
1804}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001805
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001806template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001807bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001808 return Type == R_MIPS_JALR;
1809}
1810
1811template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001812bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1813 switch (Type) {
1814 default:
1815 return false;
1816 case R_MIPS_PC16:
1817 case R_MIPS_PC19_S2:
1818 case R_MIPS_PC21_S2:
1819 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001820 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001821 case R_MIPS_PCHI16:
1822 case R_MIPS_PCLO16:
1823 return true;
1824 }
1825}
1826
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001827// _gp is a MIPS-specific ABI-defined symbol which points to
1828// a location that is relative to GOT. This function returns
1829// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001830template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001831 unsigned GPOffset = 0x7ff0;
1832 if (uint64_t V = Out<ELFT>::Got->getVA())
1833 return V + GPOffset;
1834 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001835}
1836
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001837template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1838template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1839template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1840template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1841
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001842template uint32_t getMipsGpAddr<ELF32LE>();
1843template uint32_t getMipsGpAddr<ELF32BE>();
1844template uint64_t getMipsGpAddr<ELF64LE>();
1845template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001846
1847template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1848 const SymbolBody &) const;
1849template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1850 const SymbolBody &) const;
1851template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1852 const SymbolBody &) const;
1853template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1854 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001855}
1856}