blob: 65fb0324e3cdf2fd883a9d660d2a5ede7667ba15 [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000035
Rui Ueyamac1c282a2016-02-11 21:18:01 +000036TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000037
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +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);
George Rimar777f9632016-03-12 08:31:34 +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);
George Rimar777f9632016-03-12 08:31:34 +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);
George Rimar777f9632016-03-12 08:31:34 +000070 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
Rui Ueyamaefc23de2015-10-14 21:30:32 +000073namespace {
74class X86TargetInfo final : public TargetInfo {
75public:
76 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000077 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000078 uint32_t getDynRel(uint32_t Type) const override;
79 uint32_t getTlsGotRel(uint32_t Type) const override;
80 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
81 bool isTlsLocalDynamicRel(uint32_t Type) const override;
82 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
83 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000084 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000085 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000086 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
87 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000088 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000089 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000090 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000091 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000092 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000093 uint64_t SA, uint64_t ZA = 0,
94 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000095
96 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
97 uint64_t P, uint64_t SA) const override;
98 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +000099 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000100 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000101 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000102 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
103 uint64_t P, uint64_t SA) const override;
104
George Rimarbfb7bf72015-12-21 10:00:12 +0000105 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000106 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000107};
108
109class X86_64TargetInfo final : public TargetInfo {
110public:
111 X86_64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000112 uint32_t getTlsGotRel(uint32_t Type) const override;
113 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
114 bool isTlsLocalDynamicRel(uint32_t Type) const override;
115 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
116 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000117 void writeGotPltHeader(uint8_t *Buf) const override;
118 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000119 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000120 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
121 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000122 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000123 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000124 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000125 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000126 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000127 uint64_t SA, uint64_t ZA = 0,
128 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000130 bool isSizeRel(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000131
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000132 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
133 uint64_t P, uint64_t SA) const override;
134 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000135 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000136 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000137 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000138 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
139 uint64_t P, uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000140};
141
Davide Italiano8c3444362016-01-11 19:45:33 +0000142class PPCTargetInfo final : public TargetInfo {
143public:
144 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000145 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
146 uint64_t SA, uint64_t ZA = 0,
147 uint8_t *PairedLoc = nullptr) const override;
148 bool isRelRelative(uint32_t Type) const override;
149};
150
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000151class PPC64TargetInfo final : public TargetInfo {
152public:
153 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000154 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
155 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000156 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000157 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000158 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000159 uint64_t SA, uint64_t ZA = 0,
160 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000161 bool isRelRelative(uint32_t Type) const override;
162};
163
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000164class AArch64TargetInfo final : public TargetInfo {
165public:
166 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000167 uint32_t getDynRel(uint32_t Type) const override;
168 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
169 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000170 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000171 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000172 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
173 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000174 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000175 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000176 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000177 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000178 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000179 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000180 uint64_t SA, uint64_t ZA = 0,
181 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000182
183 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000184 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000185 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000186 uint64_t P, uint64_t SA) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000187
188private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000189 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000190};
191
Tom Stellard80efb162016-01-07 03:59:08 +0000192class AMDGPUTargetInfo final : public TargetInfo {
193public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000194 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000195 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
196 uint64_t SA, uint64_t ZA = 0,
197 uint8_t *PairedLoc = nullptr) const override;
198};
199
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000200template <class ELFT> class MipsTargetInfo final : public TargetInfo {
201public:
202 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000203 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000204 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
205 void writePltZero(uint8_t *Buf) const override;
206 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
207 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000208 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000209 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000210 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000211 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000212 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000213 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000214 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000215 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000216 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000217 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000218};
219} // anonymous namespace
220
Rui Ueyama91004392015-10-13 16:08:15 +0000221TargetInfo *createTarget() {
222 switch (Config->EMachine) {
223 case EM_386:
224 return new X86TargetInfo();
225 case EM_AARCH64:
226 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000227 case EM_AMDGPU:
228 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000229 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000230 switch (Config->EKind) {
231 case ELF32LEKind:
232 return new MipsTargetInfo<ELF32LE>();
233 case ELF32BEKind:
234 return new MipsTargetInfo<ELF32BE>();
235 default:
George Rimar777f9632016-03-12 08:31:34 +0000236 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000237 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000238 case EM_PPC:
239 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000240 case EM_PPC64:
241 return new PPC64TargetInfo();
242 case EM_X86_64:
243 return new X86_64TargetInfo();
244 }
George Rimar777f9632016-03-12 08:31:34 +0000245 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000246}
247
Rafael Espindola01205f72015-09-22 18:19:46 +0000248TargetInfo::~TargetInfo() {}
249
George Rimar98b060d2016-03-06 06:01:07 +0000250bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000251 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000252 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000253
Rafael Espindolad405f472016-03-04 21:37:09 +0000254 // We know we are producing an executable.
255
256 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
257 // depending on the symbol being locally defined or not.
258 if (isTlsGlobalDynamicRel(Type))
259 return true;
260
261 // Local-Dynamic relocs can be relaxed to Local-Exec.
262 if (isTlsLocalDynamicRel(Type))
263 return true;
264
265 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
266 // defined.
267 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000268 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000269
George Rimar77d1cb12015-11-24 09:00:06 +0000270 return false;
271}
272
George Rimar786e8662016-03-17 05:57:33 +0000273uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000274
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000275bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
276
277template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
278 if (Config->Shared)
279 return false;
280 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
281 if (!SS)
282 return false;
283 return SS->Sym.getType() == STT_OBJECT;
284}
285
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000286template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000287bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000288 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000289}
290
George Rimarbfb7bf72015-12-21 10:00:12 +0000291bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000292bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000293bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000294bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000295
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000296bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000297
Rafael Espindola993f0272016-02-26 14:27:47 +0000298bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000299
300bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
301
302template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000303TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
304 const SymbolBody &S) const {
Rui Ueyama7ede5432016-03-13 04:40:14 +0000305 if (S.isGnuIfunc<ELFT>())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000306 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000307 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000308 return Plt_Explicit;
309
310 // This handles a non PIC program call to function in a shared library.
311 // In an ideal world, we could just report an error saying the relocation
312 // can overflow at runtime.
313 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
314 // libc.so.
315 //
316 // The general idea on how to handle such cases is to create a PLT entry
317 // and use that as the function value.
318 //
319 // For the static linking part, we just return true and everything else
320 // will use the the PLT entry as the address.
321 //
322 // The remaining problem is making sure pointer equality still works. We
323 // need the help of the dynamic linker for that. We let it know that we have
324 // a direct reference to a so symbol by creating an undefined symbol with a
325 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
326 // the value of the symbol we created. This is true even for got entries, so
327 // pointer equality is maintained. To avoid an infinite loop, the only entry
328 // that points to the real function is a dedicated got entry used by the
329 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
330 // R_386_JMP_SLOT, etc).
331 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
George Rimar786e8662016-03-17 05:57:33 +0000332 if (!Config->Pic && SS->Sym.getType() == STT_FUNC &&
Rafael Espindola005d8442016-03-03 18:44:38 +0000333 !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000334 return Plt_Implicit;
335
Rafael Espindola852860e2016-02-12 15:47:37 +0000336 return Plt_No;
337}
Rui Ueyama012eb782016-01-29 04:05:09 +0000338
George Rimar98b060d2016-03-06 06:01:07 +0000339bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000340
George Rimar98b060d2016-03-06 06:01:07 +0000341bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000342 return false;
343}
344
George Rimar98b060d2016-03-06 06:01:07 +0000345bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000346
George Rimar98b060d2016-03-06 06:01:07 +0000347bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000348 return false;
349}
350
George Rimar7b8850f2016-03-06 06:09:50 +0000351size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
352 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000353 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000354 if (isTlsGlobalDynamicRel(Type)) {
355 if (S.isPreemptible())
356 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000357 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000358 }
359 if (isTlsLocalDynamicRel(Type))
360 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
361 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000362 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000363}
364
365size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000366 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000367 llvm_unreachable("Should not have claimed to be relaxable");
368}
369
370size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
371 uint64_t P, uint64_t SA) const {
372 llvm_unreachable("Should not have claimed to be relaxable");
373}
374
375size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000376 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000377 llvm_unreachable("Should not have claimed to be relaxable");
378}
379
380size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
381 uint64_t P, uint64_t SA) const {
382 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000383}
George Rimar77d1cb12015-11-24 09:00:06 +0000384
Rafael Espindola7f074422015-09-22 21:35:51 +0000385X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000386 CopyRel = R_386_COPY;
387 GotRel = R_386_GLOB_DAT;
388 PltRel = R_386_JUMP_SLOT;
389 IRelativeRel = R_386_IRELATIVE;
390 RelativeRel = R_386_RELATIVE;
391 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000392 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
393 TlsOffsetRel = R_386_TLS_DTPOFF32;
394 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000395 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000396 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000397}
398
Rui Ueyamac516ae12016-01-29 02:33:45 +0000399void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000400 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
401}
402
Rui Ueyamac516ae12016-01-29 02:33:45 +0000403void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000404 // Entries in .got.plt initially points back to the corresponding
405 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000406 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000407}
Rafael Espindola01205f72015-09-22 18:19:46 +0000408
George Rimar98b060d2016-03-06 06:01:07 +0000409uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000410 if (Type == R_386_TLS_LE)
411 return R_386_TLS_TPOFF;
412 if (Type == R_386_TLS_LE_32)
413 return R_386_TLS_TPOFF32;
414 return Type;
415}
416
George Rimar98b060d2016-03-06 06:01:07 +0000417uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000418 if (Type == R_386_TLS_IE)
419 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000420 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000421}
422
George Rimar98b060d2016-03-06 06:01:07 +0000423bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000424 return Type == R_386_TLS_GD;
425}
426
George Rimar98b060d2016-03-06 06:01:07 +0000427bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000428 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
429}
430
George Rimar98b060d2016-03-06 06:01:07 +0000431bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000432 return Type == R_386_TLS_LDM;
433}
434
George Rimar98b060d2016-03-06 06:01:07 +0000435bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000436 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
437}
438
Rui Ueyama900e2d22016-01-29 03:51:49 +0000439void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000440 // Executable files and shared object files have
441 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000442 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000443 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000444 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000445 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
446 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000447 };
448 memcpy(Buf, V, sizeof(V));
449 return;
450 }
George Rimar648a2c32015-10-20 08:54:27 +0000451
George Rimar77b77792015-11-25 22:15:01 +0000452 const uint8_t PltData[] = {
453 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000454 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
455 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000456 };
457 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000458 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000459 write32le(Buf + 2, Got + 4);
460 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000461}
462
Rui Ueyama9398f862016-01-29 04:15:02 +0000463void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
464 uint64_t PltEntryAddr, int32_t Index,
465 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000466 const uint8_t Inst[] = {
467 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
468 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
469 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
470 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000471 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000472
George Rimar77b77792015-11-25 22:15:01 +0000473 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000474 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000475 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
476 : Out<ELF32LE>::Got->getVA();
477 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000478 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000479 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000480}
481
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000482bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
483 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000484}
485
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000486bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000487 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000488 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000489 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000490 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000491 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000492}
493
Rafael Espindola993f0272016-02-26 14:27:47 +0000494bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
495 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000496}
497
George Rimarbfb7bf72015-12-21 10:00:12 +0000498bool X86TargetInfo::isGotRelative(uint32_t Type) const {
499 // This relocation does not require got entry,
500 // but it is relative to got and needs it to be created.
501 // Here we request for that.
502 return Type == R_386_GOTOFF;
503}
504
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000505bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
506 return Type == R_386_GOT32;
507}
508
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000509void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000510 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000511 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000512 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000513 case R_386_32:
514 add32le(Loc, SA);
515 break;
George Rimarffb67352016-01-19 11:00:48 +0000516 case R_386_GOT32: {
517 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
518 Out<ELF32LE>::Got->getNumEntries() * 4;
519 checkInt<32>(V, Type);
520 add32le(Loc, V);
521 break;
522 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000523 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000524 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000525 break;
George Rimar13934772015-11-25 20:20:31 +0000526 case R_386_GOTPC:
527 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
528 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000529 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000530 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000531 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000532 break;
George Rimar9db204a2015-12-02 09:58:20 +0000533 case R_386_TLS_GD:
534 case R_386_TLS_LDM:
535 case R_386_TLS_TPOFF: {
536 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
537 Out<ELF32LE>::Got->getNumEntries() * 4;
538 checkInt<32>(V, Type);
539 write32le(Loc, V);
540 break;
541 }
George Rimar6f17e092015-12-17 09:32:21 +0000542 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000543 case R_386_TLS_LDO_32:
544 write32le(Loc, SA);
545 break;
George Rimard23970f2015-11-25 20:41:53 +0000546 case R_386_TLS_LE:
547 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
548 break;
549 case R_386_TLS_LE_32:
550 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
551 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000552 default:
George Rimar57610422016-03-11 14:43:02 +0000553 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000554 }
555}
556
George Rimar98b060d2016-03-06 06:01:07 +0000557bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000558 return Config->Shared && Type == R_386_TLS_IE;
559}
560
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000561size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000562 uint32_t Type, uint64_t P,
563 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000564 // GD can be optimized to LE:
565 // leal x@tlsgd(, %ebx, 1),
566 // call __tls_get_addr@plt
567 // Can be converted to:
568 // movl %gs:0,%eax
569 // addl $x@ntpoff,%eax
570 // But gold emits subl $foo@tpoff,%eax instead of addl.
571 // These instructions are completely equal in behavior.
572 // This method generates subl to be consistent with gold.
573 const uint8_t Inst[] = {
574 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
575 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
576 };
577 memcpy(Loc - 3, Inst, sizeof(Inst));
578 relocateOne(Loc + 5, BufEnd, R_386_32, P,
579 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
580
581 // The next relocation should be against __tls_get_addr, so skip it
582 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000583}
584
585// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
586// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
587// how GD can be optimized to IE:
588// leal x@tlsgd(, %ebx, 1),
589// call __tls_get_addr@plt
590// Is converted to:
591// movl %gs:0, %eax
592// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000593size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
594 uint32_t Type, uint64_t P,
595 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000596 const uint8_t Inst[] = {
597 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
598 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
599 };
600 memcpy(Loc - 3, Inst, sizeof(Inst));
601 relocateOne(Loc + 5, BufEnd, R_386_32, P,
602 SA - Out<ELF32LE>::Got->getVA() -
603 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000604
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000605 // The next relocation should be against __tls_get_addr, so skip it
606 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000607}
608
George Rimar6f17e092015-12-17 09:32:21 +0000609// In some conditions, relocations can be optimized to avoid using GOT.
610// This function does that for Initial Exec to Local Exec case.
611// Read "ELF Handling For Thread-Local Storage, 5.1
612// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000613// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000614
615size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000616 uint32_t Type, uint64_t P,
617 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000618 // Ulrich's document section 6.2 says that @gotntpoff can
619 // be used with MOVL or ADDL instructions.
620 // @indntpoff is similar to @gotntpoff, but for use in
621 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000622 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000623 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000624 uint8_t Reg = (Loc[-1] >> 3) & 7;
625 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000626 if (Type == R_386_TLS_IE) {
627 // For R_386_TLS_IE relocation we perform the next transformations:
628 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
629 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
630 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
631 // First one is special because when EAX is used the sequence is 5 bytes
632 // long, otherwise it is 6 bytes.
633 if (*Op == 0xa1) {
634 *Op = 0xb8;
635 } else {
636 *Inst = IsMov ? 0xc7 : 0x81;
637 *Op = 0xc0 | ((*Op >> 3) & 7);
638 }
639 } else {
640 // R_386_TLS_GOTIE relocation can be optimized to
641 // R_386_TLS_LE so that it does not use GOT.
642 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
643 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
644 // Note: gold converts to ADDL instead of LEAL.
645 *Inst = IsMov ? 0xc7 : 0x8d;
646 if (IsMov)
647 *Op = 0xc0 | ((*Op >> 3) & 7);
648 else
649 *Op = 0x80 | Reg | (Reg << 3);
650 }
George Rimar2558e122015-12-09 09:55:54 +0000651 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000652
653 return 0;
654}
655
656size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
657 uint32_t Type, uint64_t P,
658 uint64_t SA) const {
659 if (Type == R_386_TLS_LDO_32) {
660 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
661 return 0;
662 }
663
664 // LD can be optimized to LE:
665 // leal foo(%reg),%eax
666 // call ___tls_get_addr
667 // Is converted to:
668 // movl %gs:0,%eax
669 // nop
670 // leal 0(%esi,1),%esi
671 const uint8_t Inst[] = {
672 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
673 0x90, // nop
674 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
675 };
676 memcpy(Loc - 2, Inst, sizeof(Inst));
677
678 // The next relocation should be against __tls_get_addr, so skip it
679 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000680}
681
Rafael Espindola7f074422015-09-22 21:35:51 +0000682X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000683 CopyRel = R_X86_64_COPY;
684 GotRel = R_X86_64_GLOB_DAT;
685 PltRel = R_X86_64_JUMP_SLOT;
686 RelativeRel = R_X86_64_RELATIVE;
687 IRelativeRel = R_X86_64_IRELATIVE;
688 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000689 TlsModuleIndexRel = R_X86_64_DTPMOD64;
690 TlsOffsetRel = R_X86_64_DTPOFF64;
691 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000692 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000693 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000694}
695
Rui Ueyamac516ae12016-01-29 02:33:45 +0000696void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000697 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
698}
699
Rui Ueyamac516ae12016-01-29 02:33:45 +0000700void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000701 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000702 write32le(Buf, Plt + 6);
703}
704
Rui Ueyama900e2d22016-01-29 03:51:49 +0000705void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000706 const uint8_t PltData[] = {
707 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
708 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
709 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
710 };
711 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000712 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
713 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
714 write32le(Buf + 2, Got - Plt + 2); // GOT+8
715 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000716}
Rafael Espindola01205f72015-09-22 18:19:46 +0000717
Rui Ueyama9398f862016-01-29 04:15:02 +0000718void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
719 uint64_t PltEntryAddr, int32_t Index,
720 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000721 const uint8_t Inst[] = {
722 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
723 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
724 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
725 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000726 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000727
George Rimar648a2c32015-10-20 08:54:27 +0000728 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
729 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000730 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000731}
732
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000733bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
734 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
735 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000736}
737
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000738bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000739 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
740 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000741}
742
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000743bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000744 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000745 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000746 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000747 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000748 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000749}
750
George Rimar98b060d2016-03-06 06:01:07 +0000751uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000752 // No other types of TLS relocations requiring GOT should
753 // reach here.
754 assert(Type == R_X86_64_GOTTPOFF);
755 return R_X86_64_PC32;
756}
757
George Rimar98b060d2016-03-06 06:01:07 +0000758bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000759 return Type == R_X86_64_GOTTPOFF;
760}
761
George Rimar98b060d2016-03-06 06:01:07 +0000762bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000763 return Type == R_X86_64_TLSGD;
764}
765
George Rimar98b060d2016-03-06 06:01:07 +0000766bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000767 return Type == R_X86_64_TLSLD;
768}
769
George Rimar98b060d2016-03-06 06:01:07 +0000770bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000771 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
772 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000773}
774
Rafael Espindola993f0272016-02-26 14:27:47 +0000775bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
776 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000777}
Rafael Espindolac4010882015-09-22 20:54:08 +0000778
Rafael Espindolaae244002015-10-05 19:30:12 +0000779bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
780 switch (Type) {
781 default:
782 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000783 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000784 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000785 case R_X86_64_PC8:
786 case R_X86_64_PC16:
787 case R_X86_64_PC32:
788 case R_X86_64_PC64:
789 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000790 return true;
791 }
792}
793
Rui Ueyamac516ae12016-01-29 02:33:45 +0000794bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000795 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000796}
797
George Rimar6713cf82015-11-25 21:46:05 +0000798// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
799// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000800// how GD can be optimized to LE:
801// .byte 0x66
802// leaq x@tlsgd(%rip), %rdi
803// .word 0x6666
804// rex64
805// call __tls_get_addr@plt
806// Is converted to:
807// mov %fs:0x0,%rax
808// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000809size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000810 uint32_t Type, uint64_t P,
811 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000812 const uint8_t Inst[] = {
813 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
814 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
815 };
816 memcpy(Loc - 4, Inst, sizeof(Inst));
817 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000818
819 // The next relocation should be against __tls_get_addr, so skip it
820 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000821}
822
George Rimar25411f252015-12-04 11:20:13 +0000823// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
824// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
825// how GD can be optimized to IE:
826// .byte 0x66
827// leaq x@tlsgd(%rip), %rdi
828// .word 0x6666
829// rex64
830// call __tls_get_addr@plt
831// Is converted to:
832// mov %fs:0x0,%rax
833// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000834size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
835 uint32_t Type, uint64_t P,
836 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000837 const uint8_t Inst[] = {
838 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
839 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
840 };
841 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000842 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000843
844 // The next relocation should be against __tls_get_addr, so skip it
845 return 1;
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.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000853size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000854 uint32_t Type, uint64_t P,
855 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000856 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
857 // used in MOVQ or ADDQ instructions only.
858 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
859 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
860 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
861 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
862 uint8_t *Prefix = Loc - 3;
863 uint8_t *Inst = Loc - 2;
864 uint8_t *RegSlot = Loc - 1;
865 uint8_t Reg = Loc[-1] >> 3;
866 bool IsMov = *Inst == 0x8b;
867 bool RspAdd = !IsMov && Reg == 4;
868 // r12 and rsp registers requires special handling.
869 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
870 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
871 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
872 // The same true for rsp. So we convert to addq for them, saving 1 byte that
873 // we dont have.
874 if (RspAdd)
875 *Inst = 0x81;
876 else
877 *Inst = IsMov ? 0xc7 : 0x8d;
878 if (*Prefix == 0x4c)
879 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
880 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
881 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000882 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000883}
884
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000885// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
886// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
887// how LD can be optimized to LE:
888// leaq bar@tlsld(%rip), %rdi
889// callq __tls_get_addr@PLT
890// leaq bar@dtpoff(%rax), %rcx
891// Is converted to:
892// .word 0x6666
893// .byte 0x66
894// mov %fs:0,%rax
895// leaq bar@tpoff(%rax), %rcx
896size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
897 uint32_t Type, uint64_t P,
898 uint64_t SA) const {
899 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000900 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
901 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000902 }
903 if (Type == R_X86_64_DTPOFF32) {
904 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000905 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000906 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000907
908 const uint8_t Inst[] = {
909 0x66, 0x66, //.word 0x6666
910 0x66, //.byte 0x66
911 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
912 };
913 memcpy(Loc - 3, Inst, sizeof(Inst));
914 // The next relocation should be against __tls_get_addr, so skip it
915 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000916}
917
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000918void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000919 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000920 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000921 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000922 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000923 checkUInt<32>(SA, Type);
924 write32le(Loc, SA);
925 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000926 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000927 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000928 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000929 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000930 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000931 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000932 write64le(Loc, SA);
933 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000934 case R_X86_64_DTPOFF32:
935 write32le(Loc, SA);
936 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000937 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000938 case R_X86_64_GOTPCRELX:
939 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000940 case R_X86_64_PC32:
941 case R_X86_64_PLT32:
942 case R_X86_64_TLSGD:
943 case R_X86_64_TLSLD:
944 write32le(Loc, SA - P);
945 break;
George Rimar48651482015-12-11 08:59:37 +0000946 case R_X86_64_SIZE32:
947 write32le(Loc, ZA);
948 break;
949 case R_X86_64_SIZE64:
950 write64le(Loc, ZA);
951 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000952 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000953 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000954 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000955 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000956 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000957 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000958 default:
George Rimar57610422016-03-11 14:43:02 +0000959 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000960 }
961}
962
Hal Finkel3c8cc672015-10-12 20:56:18 +0000963// Relocation masks following the #lo(value), #hi(value), #ha(value),
964// #higher(value), #highera(value), #highest(value), and #highesta(value)
965// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
966// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000967static uint16_t applyPPCLo(uint64_t V) { return V; }
968static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
969static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
970static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
971static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000972static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000973static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
974
Davide Italiano8c3444362016-01-11 19:45:33 +0000975PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000976bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
977
978void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
979 uint64_t P, uint64_t SA, uint64_t ZA,
980 uint8_t *PairedLoc) const {
981 switch (Type) {
982 case R_PPC_ADDR16_HA:
983 write16be(Loc, applyPPCHa(SA));
984 break;
985 case R_PPC_ADDR16_LO:
986 write16be(Loc, applyPPCLo(SA));
987 break;
988 default:
George Rimar57610422016-03-11 14:43:02 +0000989 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000990 }
991}
992
Rafael Espindolac4010882015-09-22 20:54:08 +0000993PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000994 GotRel = R_PPC64_GLOB_DAT;
995 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000996 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000997
998 // We need 64K pages (at least under glibc/Linux, the loader won't
999 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001000 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001001
1002 // The PPC64 ELF ABI v1 spec, says:
1003 //
1004 // It is normally desirable to put segments with different characteristics
1005 // in separate 256 Mbyte portions of the address space, to give the
1006 // operating system full paging flexibility in the 64-bit address space.
1007 //
1008 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1009 // use 0x10000000 as the starting address.
1010 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001011}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001012
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001013uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001014 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1015 // order. The TOC starts where the first of these sections starts.
1016
1017 // FIXME: This obviously does not do the right thing when there is no .got
1018 // section, but there is a .toc or .tocbss section.
1019 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1020 if (!TocVA)
1021 TocVA = Out<ELF64BE>::Plt->getVA();
1022
1023 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1024 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1025 // code (crt1.o) assumes that you can get from the TOC base to the
1026 // start of the .toc section with only a single (signed) 16-bit relocation.
1027 return TocVA + 0x8000;
1028}
1029
Rui Ueyama9398f862016-01-29 04:15:02 +00001030void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1031 uint64_t PltEntryAddr, int32_t Index,
1032 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001033 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1034
1035 // FIXME: What we should do, in theory, is get the offset of the function
1036 // descriptor in the .opd section, and use that as the offset from %r2 (the
1037 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1038 // be a pointer to the function descriptor in the .opd section. Using
1039 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1040
Hal Finkelfa92f682015-10-13 21:47:34 +00001041 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001042 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1043 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1044 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1045 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1046 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1047 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1048 write32be(Buf + 28, 0x4e800420); // bctr
1049}
1050
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001051bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001052 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001053 return true;
1054
1055 switch (Type) {
1056 default: return false;
1057 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001059 case R_PPC64_GOT16_HA:
1060 case R_PPC64_GOT16_HI:
1061 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 case R_PPC64_GOT16_LO_DS:
1063 return true;
1064 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001065}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001066
Rafael Espindola993f0272016-02-26 14:27:47 +00001067bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001069 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001070}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071
Hal Finkelbe0823d2015-10-12 20:58:52 +00001072bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1073 switch (Type) {
1074 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001075 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001076 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001077 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001078 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001079 }
1080}
1081
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001082void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001083 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001084 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001085 uint64_t TB = getPPC64TocBase();
1086
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1088 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001089 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001090 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1091 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001092 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1093 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001094 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1095 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001096 default: break;
1097 }
1098
Hal Finkel3c8cc672015-10-12 20:56:18 +00001099 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001100 case R_PPC64_ADDR14: {
1101 checkAlignment<4>(SA, Type);
1102 // Preserve the AA/LK bits in the branch instruction
1103 uint8_t AALK = Loc[3];
1104 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1105 break;
1106 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001107 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001108 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001109 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001110 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001112 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001113 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001115 case R_PPC64_ADDR16_HA:
1116 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 break;
1118 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001121 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001122 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 break;
1124 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001129 break;
1130 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001131 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001133 case R_PPC64_ADDR16_LO:
1134 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001135 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001136 case R_PPC64_ADDR16_LO_DS:
1137 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001138 break;
1139 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001140 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001141 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001143 case R_PPC64_ADDR64:
1144 write64be(Loc, SA);
1145 break;
1146 case R_PPC64_REL16_HA:
1147 write16be(Loc, applyPPCHa(SA - P));
1148 break;
1149 case R_PPC64_REL16_HI:
1150 write16be(Loc, applyPPCHi(SA - P));
1151 break;
1152 case R_PPC64_REL16_LO:
1153 write16be(Loc, applyPPCLo(SA - P));
1154 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001155 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001156 // If we have an undefined weak symbol, we might get here with a symbol
1157 // address of zero. That could overflow, but the code must be unreachable,
1158 // so don't bother doing anything at all.
1159 if (!SA)
1160 break;
1161
Hal Finkeldaedc122015-10-12 23:16:53 +00001162 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1163 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001164 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001165
1166 if (!InPlt && Out<ELF64BE>::Opd) {
1167 // If this is a local call, and we currently have the address of a
1168 // function-descriptor, get the underlying code address instead.
1169 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1170 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001171 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001172
1173 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001174 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001175 }
1176
Hal Finkel3c8cc672015-10-12 20:56:18 +00001177 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001178 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001179 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001180
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001181 uint32_t Nop = 0x60000000;
1182 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1183 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001184 break;
1185 }
1186 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001187 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001189 break;
1190 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001191 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001192 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001193 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001194 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001195 break;
1196 default:
George Rimar57610422016-03-11 14:43:02 +00001197 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001198 }
1199}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001200
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001201AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001202 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001203 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001204 IRelativeRel = R_AARCH64_IRELATIVE;
1205 GotRel = R_AARCH64_GLOB_DAT;
1206 PltRel = R_AARCH64_JUMP_SLOT;
1207 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001208 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1209 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001210 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001211 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001212 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001213}
George Rimar648a2c32015-10-20 08:54:27 +00001214
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001215bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001216 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001217 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
Rafael Espindola57ca2702016-02-24 20:52:58 +00001218 Type == R_AARCH64_LDST32_ABS_LO12_NC ||
Rafael Espindola47ed5422016-02-24 21:48:06 +00001219 Type == R_AARCH64_LDST64_ABS_LO12_NC ||
Rafael Espindolaa598a3a2016-02-24 22:07:12 +00001220 Type == R_AARCH64_ADD_ABS_LO12_NC || Type == R_AARCH64_CALL26;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001221}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001222
George Rimar98b060d2016-03-06 06:01:07 +00001223bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001224 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1225 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1226 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1227 Type == R_AARCH64_TLSDESC_CALL;
1228}
1229
George Rimar98b060d2016-03-06 06:01:07 +00001230bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001231 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1232 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1233}
1234
George Rimar98b060d2016-03-06 06:01:07 +00001235uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001236 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1237 return Type;
1238 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001239 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001240 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001241 // Keep it going with a dummy value so that we can find more reloc errors.
1242 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001243}
1244
Rui Ueyamac516ae12016-01-29 02:33:45 +00001245void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001246 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1247}
1248
Rui Ueyama900e2d22016-01-29 03:51:49 +00001249void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001250 const uint8_t PltData[] = {
1251 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1252 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1253 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1254 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1255 0x20, 0x02, 0x1f, 0xd6, // br x17
1256 0x1f, 0x20, 0x03, 0xd5, // nop
1257 0x1f, 0x20, 0x03, 0xd5, // nop
1258 0x1f, 0x20, 0x03, 0xd5 // nop
1259 };
1260 memcpy(Buf, PltData, sizeof(PltData));
1261
Rui Ueyama900e2d22016-01-29 03:51:49 +00001262 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1263 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1264 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1265 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1266 Got + 16);
1267 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1268 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001269}
1270
Rui Ueyama9398f862016-01-29 04:15:02 +00001271void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1272 uint64_t PltEntryAddr, int32_t Index,
1273 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001274 const uint8_t Inst[] = {
1275 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1276 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1277 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1278 0x20, 0x02, 0x1f, 0xd6 // br x17
1279 };
1280 memcpy(Buf, Inst, sizeof(Inst));
1281
1282 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1283 GotEntryAddr);
1284 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1285 GotEntryAddr);
1286 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1287 GotEntryAddr);
1288}
1289
George Rimar98b060d2016-03-06 06:01:07 +00001290uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001291 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001292 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1293 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001294}
1295
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001296bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001297 switch (Type) {
1298 default:
1299 return false;
1300 case R_AARCH64_ABS16:
1301 case R_AARCH64_ABS32:
1302 case R_AARCH64_ABS64:
1303 case R_AARCH64_ADD_ABS_LO12_NC:
1304 case R_AARCH64_ADR_PREL_LO21:
1305 case R_AARCH64_ADR_PREL_PG_HI21:
1306 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001307 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001308 case R_AARCH64_LDST32_ABS_LO12_NC:
1309 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001310 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001311 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001312 }
1313}
1314
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001315bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001316 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001317 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001318 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001319 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001320 case R_AARCH64_ADR_GOT_PAGE:
1321 case R_AARCH64_LD64_GOT_LO12_NC:
1322 return true;
1323 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001324 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001325 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001326}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001327
Rafael Espindola993f0272016-02-26 14:27:47 +00001328bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001329 switch (Type) {
1330 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001331 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001332 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001333 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001334 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001335 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001336 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001337 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001338}
Davide Italiano1d750a62015-09-27 08:45:38 +00001339
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001340static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001341 uint32_t ImmLo = (Imm & 0x3) << 29;
1342 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1343 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001344 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001345}
1346
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001347static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1348 or32le(L, (Imm & 0xFFF) << 10);
1349}
1350
Davide Italiano318ca222015-10-02 22:13:51 +00001351// Page(Expr) is the page address of the expression Expr, defined
1352// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001353// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001354static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001355 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001356}
1357
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001358void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001359 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001360 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001361 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001362 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001363 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001364 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001365 break;
1366 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001367 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001368 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001369 break;
1370 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001371 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001372 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001373 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001374 // This relocation stores 12 bits and there's no instruction
1375 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001376 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1377 // bits in Loc are zero.
1378 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001379 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001380 case R_AARCH64_ADR_GOT_PAGE: {
1381 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1382 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001383 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001384 break;
1385 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001386 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001387 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001388 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001389 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001390 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001391 }
George Rimar3d737e42016-01-13 13:04:46 +00001392 case R_AARCH64_ADR_PREL_PG_HI21:
1393 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001394 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001395 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001396 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001397 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001398 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001399 case R_AARCH64_CALL26:
1400 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001401 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001402 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001403 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1404 break;
1405 }
George Rimar4102bfb2016-01-11 14:22:00 +00001406 case R_AARCH64_CONDBR19: {
1407 uint64_t X = SA - P;
1408 checkInt<21>(X, Type);
1409 or32le(Loc, (X & 0x1FFFFC) << 3);
1410 break;
1411 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001412 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001413 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001414 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001415 or32le(Loc, (SA & 0xFF8) << 7);
1416 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001417 case R_AARCH64_LDST128_ABS_LO12_NC:
1418 or32le(Loc, (SA & 0x0FF8) << 6);
1419 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001420 case R_AARCH64_LDST16_ABS_LO12_NC:
1421 or32le(Loc, (SA & 0x0FFC) << 9);
1422 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001423 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001424 or32le(Loc, (SA & 0xFFF) << 10);
1425 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001426 case R_AARCH64_LDST32_ABS_LO12_NC:
1427 or32le(Loc, (SA & 0xFFC) << 8);
1428 break;
1429 case R_AARCH64_LDST64_ABS_LO12_NC:
1430 or32le(Loc, (SA & 0xFF8) << 7);
1431 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001432 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001433 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001434 write16le(Loc, SA - P);
1435 break;
1436 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001437 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001438 write32le(Loc, SA - P);
1439 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001440 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001441 write64le(Loc, SA - P);
1442 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001443 case R_AARCH64_TSTBR14: {
1444 uint64_t X = SA - P;
1445 checkInt<16>(X, Type);
1446 or32le(Loc, (X & 0xFFFC) << 3);
1447 break;
1448 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001449 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1450 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1451 checkInt<24>(V, Type);
1452 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1453 break;
1454 }
1455 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1456 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1457 updateAArch64Add(Loc, V & 0xFFF);
1458 break;
1459 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001460 default:
George Rimar57610422016-03-11 14:43:02 +00001461 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001462 }
1463}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001464
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001465size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001466 uint32_t Type, uint64_t P,
1467 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001468 // TLSDESC Global-Dynamic relocation are in the form:
1469 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1470 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1471 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1472 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1473 // And it can optimized to:
1474 // movz x0, #0x0, lsl #16
1475 // movk x0, #0x10
1476 // nop
1477 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001478 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1479 uint64_t X = SA + TPOff;
1480 checkUInt<32>(X, Type);
1481
1482 uint32_t NewInst;
1483 switch (Type) {
1484 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1485 case R_AARCH64_TLSDESC_CALL:
1486 // nop
1487 NewInst = 0xd503201f;
1488 break;
1489 case R_AARCH64_TLSDESC_ADR_PAGE21:
1490 // movz
1491 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1492 break;
1493 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1494 // movk
1495 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1496 break;
1497 default:
George Rimar777f9632016-03-12 08:31:34 +00001498 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001499 }
1500 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001501
1502 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001503}
1504
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001505size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001506 uint32_t Type, uint64_t P,
1507 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001508 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1509 uint64_t X = SA + TPOff;
1510 checkUInt<32>(X, Type);
1511
George Rimar4d1d16d2016-03-06 06:16:05 +00001512 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001513 uint32_t NewInst;
1514 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1515 // Generate movz.
1516 unsigned RegNo = (Inst & 0x1f);
1517 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1518 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1519 // Generate movk
1520 unsigned RegNo = (Inst & 0x1f);
1521 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1522 } else {
George Rimar777f9632016-03-12 08:31:34 +00001523 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001524 }
1525 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001526
1527 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001528}
1529
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001530// Implementing relocations for AMDGPU is low priority since most
1531// programs don't use relocations now. Thus, this function is not
1532// actually called (relocateOne is called for each relocation).
1533// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001534void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1535 uint64_t P, uint64_t SA, uint64_t ZA,
1536 uint8_t *PairedLoc) const {
George Rimar57610422016-03-11 14:43:02 +00001537 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001538}
1539
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001540template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001541 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001542 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001543 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001544 PltEntrySize = 16;
1545 PltZeroSize = 32;
1546 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001547 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001548 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001549 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001550}
1551
1552template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001553uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001554 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1555 return R_MIPS_REL32;
1556 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001557 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001558 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001559 // Keep it going with a dummy value so that we can find more reloc errors.
1560 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001561}
1562
1563template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001564void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001565 typedef typename ELFT::Off Elf_Off;
1566 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001567
1568 // Set the MSB of the second GOT slot. This is not required by any
1569 // MIPS ABI documentation, though.
1570 //
1571 // There is a comment in glibc saying that "The MSB of got[1] of a
1572 // gnu object is set to identify gnu objects," and in GNU gold it
1573 // says "the second entry will be used by some runtime loaders".
1574 // But how this field is being used is unclear.
1575 //
1576 // We are not really willing to mimic other linkers behaviors
1577 // without understanding why they do that, but because all files
1578 // generated by GNU tools have this special GOT value, and because
1579 // we've been doing this for years, it is probably a safe bet to
1580 // keep doing this for now. We really need to revisit this to see
1581 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001582 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001583 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001584}
1585
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001586template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001587void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1588 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001589}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001590
Simon Atanasyan35031192015-12-15 06:06:34 +00001591static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001592
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001593template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001594static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001595 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001596 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001597 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001598 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1599 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001600 checkAlignment<(1 << SHIFT)>(S + A, Type);
1601 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001602 checkInt<BSIZE + SHIFT>(V, Type);
1603 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001604}
1605
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001606template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001607static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001608 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001609 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001610}
1611
Simon Atanasyan3b377852016-03-04 10:55:20 +00001612template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001613static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1614 uint32_t Instr = read32<E>(Loc);
1615 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1616}
1617
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001618template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1619 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1620}
1621
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001622template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001623static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001624 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001625}
1626
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001627template <class ELFT>
1628void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1629 const endianness E = ELFT::TargetEndianness;
1630 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1631 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1632 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1633 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1634 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1635 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1636 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1637 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1638 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001639 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001640 writeMipsLo16<E>(Buf + 4, Got);
1641 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001642}
1643
1644template <class ELFT>
1645void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1646 uint64_t PltEntryAddr, int32_t Index,
1647 unsigned RelOff) const {
1648 const endianness E = ELFT::TargetEndianness;
1649 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1650 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1651 write32<E>(Buf + 8, 0x03200008); // jr $25
1652 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001653 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001654 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1655 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001656}
1657
1658template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001659bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001660 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001661}
1662
1663template <class ELFT>
1664bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyand040a582016-02-25 16:19:15 +00001665 return needsPlt<ELFT>(Type, S) || refersToGotEntry(Type);
1666}
1667
1668template <class ELFT>
1669bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1670 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001671}
1672
1673template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001674bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1675 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001676}
1677
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001678template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001679void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001680 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001681 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001682 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001683 // Thread pointer and DRP offsets from the start of TLS data area.
1684 // https://www.linux-mips.org/wiki/NPTL
1685 const uint32_t TPOffset = 0x7000;
1686 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001687 switch (Type) {
1688 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001689 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001690 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001691 case R_MIPS_26: {
1692 uint32_t Instr = read32<E>(Loc);
1693 // FIXME (simon): If the relocation target symbol is not a PLT entry
1694 // we should use another expression for calculation:
1695 // ((A << 2) | (P & 0xf0000000)) >> 2
1696 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1697 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1698 break;
1699 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001700 case R_MIPS_CALL16:
1701 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001702 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001703 if (Type == R_MIPS_GOT16)
1704 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001705 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001706 break;
1707 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001708 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001709 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001710 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001711 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001712 break;
1713 }
1714 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001715 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001716 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001717 case R_MIPS_HI16:
1718 if (PairedLoc)
1719 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1720 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001721 warning("can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001722 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001723 }
1724 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001725 case R_MIPS_JALR:
1726 // Ignore this optimization relocation for now
1727 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001728 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001729 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001730 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001731 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001732 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001733 break;
1734 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001735 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001736 break;
1737 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001738 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001739 break;
1740 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001741 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001742 break;
1743 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001744 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001745 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001746 case R_MIPS_PCHI16:
1747 if (PairedLoc)
1748 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1749 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001750 warning("can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001751 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001752 }
1753 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001754 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001755 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001756 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001757 case R_MIPS_TLS_DTPREL_HI16:
1758 writeMipsHi16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1759 break;
1760 case R_MIPS_TLS_DTPREL_LO16:
1761 writeMipsLo16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1762 break;
1763 case R_MIPS_TLS_TPREL_HI16:
1764 writeMipsHi16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1765 break;
1766 case R_MIPS_TLS_TPREL_LO16:
1767 writeMipsLo16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1768 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001769 default:
George Rimar57610422016-03-11 14:43:02 +00001770 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001771 }
1772}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001773
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001774template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001775bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001776 return Type == R_MIPS_JALR;
1777}
1778
1779template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1781 switch (Type) {
1782 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001783 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001784 case R_MIPS_26:
1785 case R_MIPS_32:
1786 case R_MIPS_64:
1787 case R_MIPS_HI16:
1788 case R_MIPS_LO16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001789 case R_MIPS_TLS_DTPREL_HI16:
1790 case R_MIPS_TLS_DTPREL_LO16:
1791 case R_MIPS_TLS_TPREL_HI16:
1792 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001793 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001794 }
1795}
1796
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001797// _gp is a MIPS-specific ABI-defined symbol which points to
1798// a location that is relative to GOT. This function returns
1799// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001800template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001801 unsigned GPOffset = 0x7ff0;
1802 if (uint64_t V = Out<ELFT>::Got->getVA())
1803 return V + GPOffset;
1804 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001805}
1806
1807template uint32_t getMipsGpAddr<ELF32LE>();
1808template uint32_t getMipsGpAddr<ELF32BE>();
1809template uint64_t getMipsGpAddr<ELF64LE>();
1810template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001811
1812template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1813 const SymbolBody &) const;
1814template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1815 const SymbolBody &) const;
1816template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1817 const SymbolBody &) const;
1818template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1819 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001820
1821template TargetInfo::PltNeed
1822TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1823template TargetInfo::PltNeed
1824TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1825template TargetInfo::PltNeed
1826TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1827template TargetInfo::PltNeed
1828TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001829}
1830}