blob: 0c9f7042ecc07ecaa26efd2e23d07c3aeae68360 [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 Espindolaffcad442016-03-23 14:58:25 +000088 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000089 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000090 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000091 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000092 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000093 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola163974d2016-03-29 23:05:59 +000094 uint64_t SA, uint64_t ZA = 0) 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 Rimar86971052016-03-29 08:35:42 +0000112 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000113 uint32_t getTlsGotRel(uint32_t Type) const override;
114 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
115 bool isTlsLocalDynamicRel(uint32_t Type) const override;
116 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
117 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000118 void writeGotPltHeader(uint8_t *Buf) const override;
119 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000120 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000121 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
122 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000123 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000124 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000125 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000126 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000127 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola163974d2016-03-29 23:05:59 +0000128 uint64_t SA, uint64_t ZA = 0) 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,
Rafael Espindola163974d2016-03-29 23:05:59 +0000146 uint64_t SA, uint64_t ZA) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000147 bool isRelRelative(uint32_t Type) const override;
148};
149
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000150class PPC64TargetInfo final : public TargetInfo {
151public:
152 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000153 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
154 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000155 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000156 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000157 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola163974d2016-03-29 23:05:59 +0000158 uint64_t SA, uint64_t ZA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000159 bool isRelRelative(uint32_t Type) const override;
160};
161
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000162class AArch64TargetInfo final : public TargetInfo {
163public:
164 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000165 uint32_t getDynRel(uint32_t Type) const override;
166 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
167 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000168 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000169 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000170 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
171 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000172 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000173 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000174 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000175 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000176 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000177 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola163974d2016-03-29 23:05:59 +0000178 uint64_t SA, uint64_t ZA = 0) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000179 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000180 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000181 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000182 uint64_t P, uint64_t SA) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000183
184private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000185 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000186};
187
Tom Stellard80efb162016-01-07 03:59:08 +0000188class AMDGPUTargetInfo final : public TargetInfo {
189public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000190 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000191 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola163974d2016-03-29 23:05:59 +0000192 uint64_t SA, uint64_t ZA) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000193};
194
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000195template <class ELFT> class MipsTargetInfo final : public TargetInfo {
196public:
197 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000198 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000199 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
200 void writePltZero(uint8_t *Buf) const override;
201 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
202 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000203 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000204 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000205 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000206 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000207 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola163974d2016-03-29 23:05:59 +0000208 uint64_t SA, uint64_t ZA) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000209 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000210 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000211 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000212};
213} // anonymous namespace
214
Rui Ueyama91004392015-10-13 16:08:15 +0000215TargetInfo *createTarget() {
216 switch (Config->EMachine) {
217 case EM_386:
218 return new X86TargetInfo();
219 case EM_AARCH64:
220 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000221 case EM_AMDGPU:
222 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000223 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000224 switch (Config->EKind) {
225 case ELF32LEKind:
226 return new MipsTargetInfo<ELF32LE>();
227 case ELF32BEKind:
228 return new MipsTargetInfo<ELF32BE>();
229 default:
George Rimar777f9632016-03-12 08:31:34 +0000230 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000231 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000232 case EM_PPC:
233 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000234 case EM_PPC64:
235 return new PPC64TargetInfo();
236 case EM_X86_64:
237 return new X86_64TargetInfo();
238 }
George Rimar777f9632016-03-12 08:31:34 +0000239 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000240}
241
Rafael Espindola01205f72015-09-22 18:19:46 +0000242TargetInfo::~TargetInfo() {}
243
George Rimar98b060d2016-03-06 06:01:07 +0000244bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000245 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000246 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000247
Rafael Espindolad405f472016-03-04 21:37:09 +0000248 // We know we are producing an executable.
249
250 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
251 // depending on the symbol being locally defined or not.
252 if (isTlsGlobalDynamicRel(Type))
253 return true;
254
255 // Local-Dynamic relocs can be relaxed to Local-Exec.
256 if (isTlsLocalDynamicRel(Type))
257 return true;
258
259 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
260 // defined.
261 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000262 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000263
George Rimar77d1cb12015-11-24 09:00:06 +0000264 return false;
265}
266
George Rimar786e8662016-03-17 05:57:33 +0000267uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000268
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000269bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
270
271template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
272 if (Config->Shared)
273 return false;
274 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
275 if (!SS)
276 return false;
277 return SS->Sym.getType() == STT_OBJECT;
278}
279
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000280template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000281bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000282 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000283}
284
George Rimarbfb7bf72015-12-21 10:00:12 +0000285bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000286bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000287bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000288bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000289
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000290bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000291
Rafael Espindola993f0272016-02-26 14:27:47 +0000292bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000293
294bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
295
Rafael Espindola852860e2016-02-12 15:47:37 +0000296TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
297 const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +0000298 if (S.IsGnuIFunc)
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000299 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000300 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000301 return Plt_Explicit;
302
303 // This handles a non PIC program call to function in a shared library.
304 // In an ideal world, we could just report an error saying the relocation
305 // can overflow at runtime.
306 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
307 // libc.so.
308 //
309 // The general idea on how to handle such cases is to create a PLT entry
310 // and use that as the function value.
311 //
312 // For the static linking part, we just return true and everything else
313 // will use the the PLT entry as the address.
314 //
315 // The remaining problem is making sure pointer equality still works. We
316 // need the help of the dynamic linker for that. We let it know that we have
317 // a direct reference to a so symbol by creating an undefined symbol with a
318 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
319 // the value of the symbol we created. This is true even for got entries, so
320 // pointer equality is maintained. To avoid an infinite loop, the only entry
321 // that points to the real function is a dedicated got entry used by the
322 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
323 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000324 if (S.isShared())
325 if (!Config->Pic && S.IsFunc && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000326 return Plt_Implicit;
327
Rafael Espindola852860e2016-02-12 15:47:37 +0000328 return Plt_No;
329}
Rui Ueyama012eb782016-01-29 04:05:09 +0000330
George Rimar98b060d2016-03-06 06:01:07 +0000331bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000332
George Rimar98b060d2016-03-06 06:01:07 +0000333bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000334 return false;
335}
336
George Rimar98b060d2016-03-06 06:01:07 +0000337bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000338
George Rimar98b060d2016-03-06 06:01:07 +0000339bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000340 return false;
341}
342
George Rimar7b8850f2016-03-06 06:09:50 +0000343size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
344 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000345 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000346 if (isTlsGlobalDynamicRel(Type)) {
347 if (S.isPreemptible())
348 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000349 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000350 }
351 if (isTlsLocalDynamicRel(Type))
352 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
353 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000354 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000355}
356
357size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000358 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000359 llvm_unreachable("Should not have claimed to be relaxable");
360}
361
362size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
363 uint64_t P, uint64_t SA) const {
364 llvm_unreachable("Should not have claimed to be relaxable");
365}
366
367size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000368 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000369 llvm_unreachable("Should not have claimed to be relaxable");
370}
371
372size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
373 uint64_t P, uint64_t SA) const {
374 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000375}
George Rimar77d1cb12015-11-24 09:00:06 +0000376
Rafael Espindola7f074422015-09-22 21:35:51 +0000377X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000378 CopyRel = R_386_COPY;
379 GotRel = R_386_GLOB_DAT;
380 PltRel = R_386_JUMP_SLOT;
381 IRelativeRel = R_386_IRELATIVE;
382 RelativeRel = R_386_RELATIVE;
383 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000384 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
385 TlsOffsetRel = R_386_TLS_DTPOFF32;
386 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000387 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000388 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000389}
390
Rafael Espindolaffcad442016-03-23 14:58:25 +0000391bool X86TargetInfo::isRelRelative(uint32_t Type) const {
392 switch (Type) {
393 default:
394 return false;
395 case R_386_PC32:
396 case R_386_PLT32:
397 case R_386_TLS_LDO_32:
398 return true;
399 }
400}
401
Rui Ueyamac516ae12016-01-29 02:33:45 +0000402void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000403 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
404}
405
Rui Ueyamac516ae12016-01-29 02:33:45 +0000406void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000407 // Entries in .got.plt initially points back to the corresponding
408 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000409 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000410}
Rafael Espindola01205f72015-09-22 18:19:46 +0000411
George Rimar98b060d2016-03-06 06:01:07 +0000412uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000413 if (Type == R_386_TLS_LE)
414 return R_386_TLS_TPOFF;
415 if (Type == R_386_TLS_LE_32)
416 return R_386_TLS_TPOFF32;
417 return Type;
418}
419
George Rimar98b060d2016-03-06 06:01:07 +0000420uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000421 if (Type == R_386_TLS_IE)
422 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000423 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000424}
425
George Rimar98b060d2016-03-06 06:01:07 +0000426bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000427 return Type == R_386_TLS_GD;
428}
429
George Rimar98b060d2016-03-06 06:01:07 +0000430bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000431 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
432}
433
George Rimar98b060d2016-03-06 06:01:07 +0000434bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000435 return Type == R_386_TLS_LDM;
436}
437
George Rimar98b060d2016-03-06 06:01:07 +0000438bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000439 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
440}
441
Rui Ueyama900e2d22016-01-29 03:51:49 +0000442void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000443 // Executable files and shared object files have
444 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000445 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000446 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000447 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000448 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
449 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000450 };
451 memcpy(Buf, V, sizeof(V));
452 return;
453 }
George Rimar648a2c32015-10-20 08:54:27 +0000454
George Rimar77b77792015-11-25 22:15:01 +0000455 const uint8_t PltData[] = {
456 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000457 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
458 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000459 };
460 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000461 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000462 write32le(Buf + 2, Got + 4);
463 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000464}
465
Rui Ueyama9398f862016-01-29 04:15:02 +0000466void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
467 uint64_t PltEntryAddr, int32_t Index,
468 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000469 const uint8_t Inst[] = {
470 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
471 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
472 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
473 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000474 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000475
George Rimar77b77792015-11-25 22:15:01 +0000476 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000477 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000478 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
479 : Out<ELF32LE>::Got->getVA();
480 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000481 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000482 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000483}
484
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000485bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
486 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000487}
488
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000489bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000490 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000491 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000492 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000493 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000494 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000495}
496
Rafael Espindola993f0272016-02-26 14:27:47 +0000497bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
498 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000499}
500
George Rimarbfb7bf72015-12-21 10:00:12 +0000501bool X86TargetInfo::isGotRelative(uint32_t Type) const {
502 // This relocation does not require got entry,
503 // but it is relative to got and needs it to be created.
504 // Here we request for that.
505 return Type == R_386_GOTOFF;
506}
507
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000508bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
509 return Type == R_386_GOT32;
510}
511
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000512void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola163974d2016-03-29 23:05:59 +0000513 uint64_t P, uint64_t SA, uint64_t ZA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000514 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000515 case R_386_32:
516 add32le(Loc, SA);
517 break;
George Rimarffb67352016-01-19 11:00:48 +0000518 case R_386_GOT32: {
519 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
520 Out<ELF32LE>::Got->getNumEntries() * 4;
521 checkInt<32>(V, Type);
522 add32le(Loc, V);
523 break;
524 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000525 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000526 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000527 break;
George Rimar13934772015-11-25 20:20:31 +0000528 case R_386_GOTPC:
529 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
530 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000531 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000532 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000533 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000534 break;
George Rimar9db204a2015-12-02 09:58:20 +0000535 case R_386_TLS_GD:
536 case R_386_TLS_LDM:
537 case R_386_TLS_TPOFF: {
538 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
539 Out<ELF32LE>::Got->getNumEntries() * 4;
540 checkInt<32>(V, Type);
541 write32le(Loc, V);
542 break;
543 }
George Rimar6f17e092015-12-17 09:32:21 +0000544 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000545 case R_386_TLS_LDO_32:
546 write32le(Loc, SA);
547 break;
George Rimard23970f2015-11-25 20:41:53 +0000548 case R_386_TLS_LE:
549 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
550 break;
551 case R_386_TLS_LE_32:
552 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
553 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000554 default:
George Rimar57610422016-03-11 14:43:02 +0000555 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000556 }
557}
558
George Rimar98b060d2016-03-06 06:01:07 +0000559bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000560 return Config->Shared && Type == R_386_TLS_IE;
561}
562
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000563size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000564 uint32_t Type, uint64_t P,
565 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000566 // GD can be optimized to LE:
567 // leal x@tlsgd(, %ebx, 1),
568 // call __tls_get_addr@plt
569 // Can be converted to:
570 // movl %gs:0,%eax
571 // addl $x@ntpoff,%eax
572 // But gold emits subl $foo@tpoff,%eax instead of addl.
573 // These instructions are completely equal in behavior.
574 // This method generates subl to be consistent with gold.
575 const uint8_t Inst[] = {
576 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
577 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
578 };
579 memcpy(Loc - 3, Inst, sizeof(Inst));
580 relocateOne(Loc + 5, BufEnd, R_386_32, P,
581 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
582
583 // The next relocation should be against __tls_get_addr, so skip it
584 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000585}
586
587// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
588// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
589// how GD can be optimized to IE:
590// leal x@tlsgd(, %ebx, 1),
591// call __tls_get_addr@plt
592// Is converted to:
593// movl %gs:0, %eax
594// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000595size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
596 uint32_t Type, uint64_t P,
597 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000598 const uint8_t Inst[] = {
599 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
600 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
601 };
602 memcpy(Loc - 3, Inst, sizeof(Inst));
603 relocateOne(Loc + 5, BufEnd, R_386_32, P,
604 SA - Out<ELF32LE>::Got->getVA() -
605 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000606
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000607 // The next relocation should be against __tls_get_addr, so skip it
608 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000609}
610
George Rimar6f17e092015-12-17 09:32:21 +0000611// In some conditions, relocations can be optimized to avoid using GOT.
612// This function does that for Initial Exec to Local Exec case.
613// Read "ELF Handling For Thread-Local Storage, 5.1
614// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000615// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000616
617size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000618 uint32_t Type, uint64_t P,
619 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000620 // Ulrich's document section 6.2 says that @gotntpoff can
621 // be used with MOVL or ADDL instructions.
622 // @indntpoff is similar to @gotntpoff, but for use in
623 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000624 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000625 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000626 uint8_t Reg = (Loc[-1] >> 3) & 7;
627 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000628 if (Type == R_386_TLS_IE) {
629 // For R_386_TLS_IE relocation we perform the next transformations:
630 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
631 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
632 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
633 // First one is special because when EAX is used the sequence is 5 bytes
634 // long, otherwise it is 6 bytes.
635 if (*Op == 0xa1) {
636 *Op = 0xb8;
637 } else {
638 *Inst = IsMov ? 0xc7 : 0x81;
639 *Op = 0xc0 | ((*Op >> 3) & 7);
640 }
641 } else {
642 // R_386_TLS_GOTIE relocation can be optimized to
643 // R_386_TLS_LE so that it does not use GOT.
644 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
645 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
646 // Note: gold converts to ADDL instead of LEAL.
647 *Inst = IsMov ? 0xc7 : 0x8d;
648 if (IsMov)
649 *Op = 0xc0 | ((*Op >> 3) & 7);
650 else
651 *Op = 0x80 | Reg | (Reg << 3);
652 }
George Rimar2558e122015-12-09 09:55:54 +0000653 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000654
655 return 0;
656}
657
658size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
659 uint32_t Type, uint64_t P,
660 uint64_t SA) const {
661 if (Type == R_386_TLS_LDO_32) {
662 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
663 return 0;
664 }
665
666 // LD can be optimized to LE:
667 // leal foo(%reg),%eax
668 // call ___tls_get_addr
669 // Is converted to:
670 // movl %gs:0,%eax
671 // nop
672 // leal 0(%esi,1),%esi
673 const uint8_t Inst[] = {
674 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
675 0x90, // nop
676 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
677 };
678 memcpy(Loc - 2, Inst, sizeof(Inst));
679
680 // The next relocation should be against __tls_get_addr, so skip it
681 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000682}
683
Rafael Espindola7f074422015-09-22 21:35:51 +0000684X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000685 CopyRel = R_X86_64_COPY;
686 GotRel = R_X86_64_GLOB_DAT;
687 PltRel = R_X86_64_JUMP_SLOT;
688 RelativeRel = R_X86_64_RELATIVE;
689 IRelativeRel = R_X86_64_IRELATIVE;
690 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000691 TlsModuleIndexRel = R_X86_64_DTPMOD64;
692 TlsOffsetRel = R_X86_64_DTPOFF64;
693 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000694 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000695 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000696}
697
Rui Ueyamac516ae12016-01-29 02:33:45 +0000698void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000699 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
700}
701
Rui Ueyamac516ae12016-01-29 02:33:45 +0000702void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000703 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000704 write32le(Buf, Plt + 6);
705}
706
Rui Ueyama900e2d22016-01-29 03:51:49 +0000707void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000708 const uint8_t PltData[] = {
709 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
710 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
711 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
712 };
713 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000714 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
715 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
716 write32le(Buf + 2, Got - Plt + 2); // GOT+8
717 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000718}
Rafael Espindola01205f72015-09-22 18:19:46 +0000719
Rui Ueyama9398f862016-01-29 04:15:02 +0000720void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
721 uint64_t PltEntryAddr, int32_t Index,
722 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000723 const uint8_t Inst[] = {
724 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
725 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
726 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
727 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000728 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000729
George Rimar648a2c32015-10-20 08:54:27 +0000730 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
731 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000732 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000733}
734
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000735bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
736 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
737 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000738}
739
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000740bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000741 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
742 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000743}
744
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000745bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000746 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000747 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000748 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000749 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000750 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000751}
752
George Rimar86971052016-03-29 08:35:42 +0000753uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
754 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
755 if (Config->Shared)
756 error(getELFRelocationTypeName(EM_X86_64, Type) +
757 " cannot be a dynamic relocation");
758 return Type;
759}
760
George Rimar98b060d2016-03-06 06:01:07 +0000761uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000762 // No other types of TLS relocations requiring GOT should
763 // reach here.
764 assert(Type == R_X86_64_GOTTPOFF);
765 return R_X86_64_PC32;
766}
767
George Rimar98b060d2016-03-06 06:01:07 +0000768bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000769 return Type == R_X86_64_GOTTPOFF;
770}
771
George Rimar98b060d2016-03-06 06:01:07 +0000772bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000773 return Type == R_X86_64_TLSGD;
774}
775
George Rimar98b060d2016-03-06 06:01:07 +0000776bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000777 return Type == R_X86_64_TLSLD;
778}
779
George Rimar98b060d2016-03-06 06:01:07 +0000780bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000781 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
782 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000783}
784
Rafael Espindola993f0272016-02-26 14:27:47 +0000785bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
786 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000787}
Rafael Espindolac4010882015-09-22 20:54:08 +0000788
Rafael Espindolaae244002015-10-05 19:30:12 +0000789bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
790 switch (Type) {
791 default:
792 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000793 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000794 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000795 case R_X86_64_PC8:
796 case R_X86_64_PC16:
797 case R_X86_64_PC32:
798 case R_X86_64_PC64:
799 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000800 return true;
801 }
802}
803
Rui Ueyamac516ae12016-01-29 02:33:45 +0000804bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000805 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000806}
807
George Rimar6713cf82015-11-25 21:46:05 +0000808// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
809// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000810// how GD can be optimized to LE:
811// .byte 0x66
812// leaq x@tlsgd(%rip), %rdi
813// .word 0x6666
814// rex64
815// call __tls_get_addr@plt
816// Is converted to:
817// mov %fs:0x0,%rax
818// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000819size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000820 uint32_t Type, uint64_t P,
821 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000822 const uint8_t Inst[] = {
823 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
824 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
825 };
826 memcpy(Loc - 4, Inst, sizeof(Inst));
827 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000828
829 // The next relocation should be against __tls_get_addr, so skip it
830 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000831}
832
George Rimar25411f252015-12-04 11:20:13 +0000833// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
834// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
835// how GD can be optimized to IE:
836// .byte 0x66
837// leaq x@tlsgd(%rip), %rdi
838// .word 0x6666
839// rex64
840// call __tls_get_addr@plt
841// Is converted to:
842// mov %fs:0x0,%rax
843// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000844size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
845 uint32_t Type, uint64_t P,
846 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000847 const uint8_t Inst[] = {
848 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
849 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
850 };
851 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000852 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000853
854 // The next relocation should be against __tls_get_addr, so skip it
855 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000856}
857
George Rimar77d1cb12015-11-24 09:00:06 +0000858// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000859// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000860// This function does that. Read "ELF Handling For Thread-Local Storage,
861// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
862// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000863size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000864 uint32_t Type, uint64_t P,
865 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000866 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
867 // used in MOVQ or ADDQ instructions only.
868 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
869 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
870 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
871 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
872 uint8_t *Prefix = Loc - 3;
873 uint8_t *Inst = Loc - 2;
874 uint8_t *RegSlot = Loc - 1;
875 uint8_t Reg = Loc[-1] >> 3;
876 bool IsMov = *Inst == 0x8b;
877 bool RspAdd = !IsMov && Reg == 4;
878 // r12 and rsp registers requires special handling.
879 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
880 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
881 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
882 // The same true for rsp. So we convert to addq for them, saving 1 byte that
883 // we dont have.
884 if (RspAdd)
885 *Inst = 0x81;
886 else
887 *Inst = IsMov ? 0xc7 : 0x8d;
888 if (*Prefix == 0x4c)
889 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
890 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
891 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000892 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000893}
894
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000895// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
896// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
897// how LD can be optimized to LE:
898// leaq bar@tlsld(%rip), %rdi
899// callq __tls_get_addr@PLT
900// leaq bar@dtpoff(%rax), %rcx
901// Is converted to:
902// .word 0x6666
903// .byte 0x66
904// mov %fs:0,%rax
905// leaq bar@tpoff(%rax), %rcx
906size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
907 uint32_t Type, uint64_t P,
908 uint64_t SA) const {
909 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000910 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
911 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000912 }
913 if (Type == R_X86_64_DTPOFF32) {
914 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000915 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000916 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000917
918 const uint8_t Inst[] = {
919 0x66, 0x66, //.word 0x6666
920 0x66, //.byte 0x66
921 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
922 };
923 memcpy(Loc - 3, Inst, sizeof(Inst));
924 // The next relocation should be against __tls_get_addr, so skip it
925 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000926}
927
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000928void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola163974d2016-03-29 23:05:59 +0000929 uint64_t P, uint64_t SA, uint64_t ZA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000930 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000931 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000932 checkUInt<32>(SA, Type);
933 write32le(Loc, SA);
934 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000935 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000936 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000937 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000938 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000939 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000940 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000941 write64le(Loc, SA);
942 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000943 case R_X86_64_DTPOFF32:
944 write32le(Loc, SA);
945 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000946 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000947 case R_X86_64_GOTPCRELX:
948 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000949 case R_X86_64_PC32:
950 case R_X86_64_PLT32:
951 case R_X86_64_TLSGD:
952 case R_X86_64_TLSLD:
953 write32le(Loc, SA - P);
954 break;
George Rimar48651482015-12-11 08:59:37 +0000955 case R_X86_64_SIZE32:
956 write32le(Loc, ZA);
957 break;
958 case R_X86_64_SIZE64:
959 write64le(Loc, ZA);
960 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000961 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000962 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000963 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000964 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000965 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000966 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000967 default:
George Rimar57610422016-03-11 14:43:02 +0000968 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000969 }
970}
971
Hal Finkel3c8cc672015-10-12 20:56:18 +0000972// Relocation masks following the #lo(value), #hi(value), #ha(value),
973// #higher(value), #highera(value), #highest(value), and #highesta(value)
974// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
975// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000976static uint16_t applyPPCLo(uint64_t V) { return V; }
977static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
978static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
979static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
980static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000981static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
983
Davide Italiano8c3444362016-01-11 19:45:33 +0000984PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000985bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
986
987void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola163974d2016-03-29 23:05:59 +0000988 uint64_t P, uint64_t SA, uint64_t ZA) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000989 switch (Type) {
990 case R_PPC_ADDR16_HA:
991 write16be(Loc, applyPPCHa(SA));
992 break;
993 case R_PPC_ADDR16_LO:
994 write16be(Loc, applyPPCLo(SA));
995 break;
996 default:
George Rimar57610422016-03-11 14:43:02 +0000997 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000998 }
999}
1000
Rafael Espindolac4010882015-09-22 20:54:08 +00001001PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001002 GotRel = R_PPC64_GLOB_DAT;
1003 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001004 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001005
1006 // We need 64K pages (at least under glibc/Linux, the loader won't
1007 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001008 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001009
1010 // The PPC64 ELF ABI v1 spec, says:
1011 //
1012 // It is normally desirable to put segments with different characteristics
1013 // in separate 256 Mbyte portions of the address space, to give the
1014 // operating system full paging flexibility in the 64-bit address space.
1015 //
1016 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1017 // use 0x10000000 as the starting address.
1018 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001019}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001020
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001021uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001022 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1023 // order. The TOC starts where the first of these sections starts.
1024
1025 // FIXME: This obviously does not do the right thing when there is no .got
1026 // section, but there is a .toc or .tocbss section.
1027 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1028 if (!TocVA)
1029 TocVA = Out<ELF64BE>::Plt->getVA();
1030
1031 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1032 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1033 // code (crt1.o) assumes that you can get from the TOC base to the
1034 // start of the .toc section with only a single (signed) 16-bit relocation.
1035 return TocVA + 0x8000;
1036}
1037
Rui Ueyama9398f862016-01-29 04:15:02 +00001038void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1039 uint64_t PltEntryAddr, int32_t Index,
1040 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001041 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1042
1043 // FIXME: What we should do, in theory, is get the offset of the function
1044 // descriptor in the .opd section, and use that as the offset from %r2 (the
1045 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1046 // be a pointer to the function descriptor in the .opd section. Using
1047 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1048
Hal Finkelfa92f682015-10-13 21:47:34 +00001049 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001050 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1051 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1052 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1053 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1054 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1055 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1056 write32be(Buf + 28, 0x4e800420); // bctr
1057}
1058
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001059bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001060 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 return true;
1062
1063 switch (Type) {
1064 default: return false;
1065 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001066 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001067 case R_PPC64_GOT16_HA:
1068 case R_PPC64_GOT16_HI:
1069 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001070 case R_PPC64_GOT16_LO_DS:
1071 return true;
1072 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001073}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001074
Rafael Espindola993f0272016-02-26 14:27:47 +00001075bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001077 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001078}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001079
Hal Finkelbe0823d2015-10-12 20:58:52 +00001080bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1081 switch (Type) {
1082 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001083 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001084 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001085 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001086 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001087 }
1088}
1089
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001090void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola163974d2016-03-29 23:05:59 +00001091 uint64_t P, uint64_t SA, uint64_t ZA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001092 uint64_t TB = getPPC64TocBase();
1093
Hal Finkel3c8cc672015-10-12 20:56:18 +00001094 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1095 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001096 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001097 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1098 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001099 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1100 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001101 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1102 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 default: break;
1104 }
1105
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001107 case R_PPC64_ADDR14: {
1108 checkAlignment<4>(SA, Type);
1109 // Preserve the AA/LK bits in the branch instruction
1110 uint8_t AALK = Loc[3];
1111 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1112 break;
1113 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001115 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001117 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001118 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001119 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001120 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001121 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001122 case R_PPC64_ADDR16_HA:
1123 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 break;
1125 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001126 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001127 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001128 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
1131 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001132 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
1137 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001140 case R_PPC64_ADDR16_LO:
1141 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001143 case R_PPC64_ADDR16_LO_DS:
1144 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
1146 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001147 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001148 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001149 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001150 case R_PPC64_ADDR64:
1151 write64be(Loc, SA);
1152 break;
1153 case R_PPC64_REL16_HA:
1154 write16be(Loc, applyPPCHa(SA - P));
1155 break;
1156 case R_PPC64_REL16_HI:
1157 write16be(Loc, applyPPCHi(SA - P));
1158 break;
1159 case R_PPC64_REL16_LO:
1160 write16be(Loc, applyPPCLo(SA - P));
1161 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001162 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001163 // If we have an undefined weak symbol, we might get here with a symbol
1164 // address of zero. That could overflow, but the code must be unreachable,
1165 // so don't bother doing anything at all.
1166 if (!SA)
1167 break;
1168
Hal Finkeldaedc122015-10-12 23:16:53 +00001169 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1170 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001171 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001172
1173 if (!InPlt && Out<ELF64BE>::Opd) {
1174 // If this is a local call, and we currently have the address of a
1175 // function-descriptor, get the underlying code address instead.
1176 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1177 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001178 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001179
1180 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001181 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001182 }
1183
Hal Finkel3c8cc672015-10-12 20:56:18 +00001184 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001185 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001186 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001187
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 uint32_t Nop = 0x60000000;
1189 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1190 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001191 break;
1192 }
1193 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001194 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001195 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001196 break;
1197 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001198 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001199 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001200 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001201 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001202 break;
1203 default:
George Rimar57610422016-03-11 14:43:02 +00001204 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001205 }
1206}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001207
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001208AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001209 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001210 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001211 IRelativeRel = R_AARCH64_IRELATIVE;
1212 GotRel = R_AARCH64_GLOB_DAT;
1213 PltRel = R_AARCH64_JUMP_SLOT;
1214 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001215 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1216 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001217 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001218 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001219 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001220}
George Rimar648a2c32015-10-20 08:54:27 +00001221
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001222bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001223 switch (Type) {
1224 default:
1225 return false;
1226 case R_AARCH64_PREL32:
1227 case R_AARCH64_ADR_PREL_LO21:
1228 case R_AARCH64_ADR_PREL_PG_HI21:
1229 case R_AARCH64_ADR_GOT_PAGE:
1230 case R_AARCH64_LDST8_ABS_LO12_NC:
1231 case R_AARCH64_LDST16_ABS_LO12_NC:
1232 case R_AARCH64_LDST32_ABS_LO12_NC:
1233 case R_AARCH64_LDST64_ABS_LO12_NC:
1234 case R_AARCH64_LDST128_ABS_LO12_NC:
1235 case R_AARCH64_ADD_ABS_LO12_NC:
1236 case R_AARCH64_CALL26:
1237 case R_AARCH64_JUMP26:
1238 case R_AARCH64_CONDBR19:
1239 case R_AARCH64_TSTBR14:
Rafael Espindola07275532016-03-28 01:31:11 +00001240 case R_AARCH64_PREL64:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001241 return true;
1242 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001243}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001244
George Rimar98b060d2016-03-06 06:01:07 +00001245bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001246 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1247 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1248 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1249 Type == R_AARCH64_TLSDESC_CALL;
1250}
1251
George Rimar98b060d2016-03-06 06:01:07 +00001252bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001253 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1254 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1255}
1256
George Rimar98b060d2016-03-06 06:01:07 +00001257uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001258 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1259 return Type;
1260 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001261 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001262 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001263 // Keep it going with a dummy value so that we can find more reloc errors.
1264 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001265}
1266
Rui Ueyamac516ae12016-01-29 02:33:45 +00001267void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001268 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1269}
1270
Rui Ueyama900e2d22016-01-29 03:51:49 +00001271void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001272 const uint8_t PltData[] = {
1273 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1274 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1275 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1276 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1277 0x20, 0x02, 0x1f, 0xd6, // br x17
1278 0x1f, 0x20, 0x03, 0xd5, // nop
1279 0x1f, 0x20, 0x03, 0xd5, // nop
1280 0x1f, 0x20, 0x03, 0xd5 // nop
1281 };
1282 memcpy(Buf, PltData, sizeof(PltData));
1283
Rui Ueyama900e2d22016-01-29 03:51:49 +00001284 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1285 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1286 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1287 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1288 Got + 16);
1289 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1290 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001291}
1292
Rui Ueyama9398f862016-01-29 04:15:02 +00001293void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1294 uint64_t PltEntryAddr, int32_t Index,
1295 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001296 const uint8_t Inst[] = {
1297 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1298 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1299 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1300 0x20, 0x02, 0x1f, 0xd6 // br x17
1301 };
1302 memcpy(Buf, Inst, sizeof(Inst));
1303
1304 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1305 GotEntryAddr);
1306 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1307 GotEntryAddr);
1308 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1309 GotEntryAddr);
1310}
1311
George Rimar98b060d2016-03-06 06:01:07 +00001312uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001313 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001314 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1315 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001316}
1317
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001318bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001319 switch (Type) {
1320 default:
1321 return false;
1322 case R_AARCH64_ABS16:
1323 case R_AARCH64_ABS32:
1324 case R_AARCH64_ABS64:
1325 case R_AARCH64_ADD_ABS_LO12_NC:
1326 case R_AARCH64_ADR_PREL_LO21:
1327 case R_AARCH64_ADR_PREL_PG_HI21:
1328 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001329 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001330 case R_AARCH64_LDST32_ABS_LO12_NC:
1331 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001332 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001333 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001334 }
1335}
1336
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001337bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001338 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001339 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001340 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001341 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001342 case R_AARCH64_ADR_GOT_PAGE:
1343 case R_AARCH64_LD64_GOT_LO12_NC:
1344 return true;
1345 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001346 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001347 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001348}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001349
Rafael Espindola993f0272016-02-26 14:27:47 +00001350bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001351 switch (Type) {
1352 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001353 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001354 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001355 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001356 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001357 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001358 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001359 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001360}
Davide Italiano1d750a62015-09-27 08:45:38 +00001361
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001362static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001363 uint32_t ImmLo = (Imm & 0x3) << 29;
1364 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1365 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001366 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001367}
1368
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001369static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1370 or32le(L, (Imm & 0xFFF) << 10);
1371}
1372
Davide Italiano318ca222015-10-02 22:13:51 +00001373// Page(Expr) is the page address of the expression Expr, defined
1374// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001375// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001376static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001377 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001378}
1379
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001380void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001381 uint32_t Type, uint64_t P, uint64_t SA,
Rafael Espindola163974d2016-03-29 23:05:59 +00001382 uint64_t ZA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001383 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001384 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001385 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001386 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001387 break;
1388 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001389 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001390 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001391 break;
1392 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001393 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001394 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001395 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001396 // This relocation stores 12 bits and there's no instruction
1397 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001398 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1399 // bits in Loc are zero.
1400 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001401 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001402 case R_AARCH64_ADR_GOT_PAGE: {
1403 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1404 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001405 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001406 break;
1407 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001408 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001409 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001410 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001411 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001412 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001413 }
George Rimar3d737e42016-01-13 13:04:46 +00001414 case R_AARCH64_ADR_PREL_PG_HI21:
1415 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001416 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001417 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001418 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001419 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001420 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001421 case R_AARCH64_CALL26:
1422 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001423 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001424 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001425 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1426 break;
1427 }
George Rimar4102bfb2016-01-11 14:22:00 +00001428 case R_AARCH64_CONDBR19: {
1429 uint64_t X = SA - P;
1430 checkInt<21>(X, Type);
1431 or32le(Loc, (X & 0x1FFFFC) << 3);
1432 break;
1433 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001434 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001435 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001436 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001437 or32le(Loc, (SA & 0xFF8) << 7);
1438 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001439 case R_AARCH64_LDST128_ABS_LO12_NC:
1440 or32le(Loc, (SA & 0x0FF8) << 6);
1441 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001442 case R_AARCH64_LDST16_ABS_LO12_NC:
1443 or32le(Loc, (SA & 0x0FFC) << 9);
1444 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001445 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001446 or32le(Loc, (SA & 0xFFF) << 10);
1447 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001448 case R_AARCH64_LDST32_ABS_LO12_NC:
1449 or32le(Loc, (SA & 0xFFC) << 8);
1450 break;
1451 case R_AARCH64_LDST64_ABS_LO12_NC:
1452 or32le(Loc, (SA & 0xFF8) << 7);
1453 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001454 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001455 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001456 write16le(Loc, SA - P);
1457 break;
1458 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001459 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001460 write32le(Loc, SA - P);
1461 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001462 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001463 write64le(Loc, SA - P);
1464 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001465 case R_AARCH64_TSTBR14: {
1466 uint64_t X = SA - P;
1467 checkInt<16>(X, Type);
1468 or32le(Loc, (X & 0xFFFC) << 3);
1469 break;
1470 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001471 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1472 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1473 checkInt<24>(V, Type);
1474 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1475 break;
1476 }
1477 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1478 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1479 updateAArch64Add(Loc, V & 0xFFF);
1480 break;
1481 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001482 default:
George Rimar57610422016-03-11 14:43:02 +00001483 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001484 }
1485}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001486
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001487size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001488 uint32_t Type, uint64_t P,
1489 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001490 // TLSDESC Global-Dynamic relocation are in the form:
1491 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1492 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1493 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1494 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1495 // And it can optimized to:
1496 // movz x0, #0x0, lsl #16
1497 // movk x0, #0x10
1498 // nop
1499 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001500 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1501 uint64_t X = SA + TPOff;
1502 checkUInt<32>(X, Type);
1503
1504 uint32_t NewInst;
1505 switch (Type) {
1506 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1507 case R_AARCH64_TLSDESC_CALL:
1508 // nop
1509 NewInst = 0xd503201f;
1510 break;
1511 case R_AARCH64_TLSDESC_ADR_PAGE21:
1512 // movz
1513 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1514 break;
1515 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1516 // movk
1517 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1518 break;
1519 default:
George Rimar777f9632016-03-12 08:31:34 +00001520 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001521 }
1522 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001523
1524 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001525}
1526
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001527size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001528 uint32_t Type, uint64_t P,
1529 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001530 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1531 uint64_t X = SA + TPOff;
1532 checkUInt<32>(X, Type);
1533
George Rimar4d1d16d2016-03-06 06:16:05 +00001534 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001535 uint32_t NewInst;
1536 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1537 // Generate movz.
1538 unsigned RegNo = (Inst & 0x1f);
1539 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1540 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1541 // Generate movk
1542 unsigned RegNo = (Inst & 0x1f);
1543 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1544 } else {
George Rimar777f9632016-03-12 08:31:34 +00001545 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001546 }
1547 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001548
1549 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001550}
1551
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001552// Implementing relocations for AMDGPU is low priority since most
1553// programs don't use relocations now. Thus, this function is not
1554// actually called (relocateOne is called for each relocation).
1555// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001556void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola163974d2016-03-29 23:05:59 +00001557 uint64_t P, uint64_t SA, uint64_t ZA) const {
George Rimar57610422016-03-11 14:43:02 +00001558 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001559}
1560
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001561template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001562 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001563 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001564 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001565 PltEntrySize = 16;
1566 PltZeroSize = 32;
1567 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001568 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001569 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001570 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001571}
1572
1573template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001574uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001575 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1576 return R_MIPS_REL32;
1577 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001578 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001579 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001580 // Keep it going with a dummy value so that we can find more reloc errors.
1581 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001582}
1583
1584template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001585void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001586 typedef typename ELFT::Off Elf_Off;
1587 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001588
1589 // Set the MSB of the second GOT slot. This is not required by any
1590 // MIPS ABI documentation, though.
1591 //
1592 // There is a comment in glibc saying that "The MSB of got[1] of a
1593 // gnu object is set to identify gnu objects," and in GNU gold it
1594 // says "the second entry will be used by some runtime loaders".
1595 // But how this field is being used is unclear.
1596 //
1597 // We are not really willing to mimic other linkers behaviors
1598 // without understanding why they do that, but because all files
1599 // generated by GNU tools have this special GOT value, and because
1600 // we've been doing this for years, it is probably a safe bet to
1601 // keep doing this for now. We really need to revisit this to see
1602 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001603 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001604 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001605}
1606
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001607template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001608void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1609 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001610}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001611
Simon Atanasyan35031192015-12-15 06:06:34 +00001612static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001613
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001614template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001615static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001616 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001617 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001618 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001619 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1620 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001621 checkAlignment<(1 << SHIFT)>(S + A, Type);
1622 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001623 checkInt<BSIZE + SHIFT>(V, Type);
1624 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001625}
1626
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001627template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001628static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001629 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001630 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001631}
1632
Simon Atanasyan3b377852016-03-04 10:55:20 +00001633template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001634static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1635 uint32_t Instr = read32<E>(Loc);
1636 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1637}
1638
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001639template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1640 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1641}
1642
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001643template <class ELFT>
1644void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1645 const endianness E = ELFT::TargetEndianness;
1646 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1647 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1648 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1649 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1650 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1651 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1652 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1653 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1654 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001655 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001656 writeMipsLo16<E>(Buf + 4, Got);
1657 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001658}
1659
1660template <class ELFT>
1661void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1662 uint64_t PltEntryAddr, int32_t Index,
1663 unsigned RelOff) const {
1664 const endianness E = ELFT::TargetEndianness;
1665 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1666 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1667 write32<E>(Buf + 8, 0x03200008); // jr $25
1668 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001669 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001670 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1671 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001672}
1673
1674template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001675bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001676 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001677}
1678
1679template <class ELFT>
1680bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001681 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001682}
1683
1684template <class ELFT>
1685bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1686 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001687}
1688
1689template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001690bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1691 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001692}
1693
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001694template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001695void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001696 uint32_t Type, uint64_t P, uint64_t S,
Rafael Espindola163974d2016-03-29 23:05:59 +00001697 uint64_t ZA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001698 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001699 // Thread pointer and DRP offsets from the start of TLS data area.
1700 // https://www.linux-mips.org/wiki/NPTL
1701 const uint32_t TPOffset = 0x7000;
1702 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001703 switch (Type) {
1704 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001705 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001706 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001707 case R_MIPS_26: {
1708 uint32_t Instr = read32<E>(Loc);
1709 // FIXME (simon): If the relocation target symbol is not a PLT entry
1710 // we should use another expression for calculation:
1711 // ((A << 2) | (P & 0xf0000000)) >> 2
1712 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1713 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1714 break;
1715 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001716 case R_MIPS_CALL16:
1717 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001718 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001719 if (Type == R_MIPS_GOT16)
1720 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001721 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001722 break;
1723 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001724 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001725 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001726 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001727 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001728 break;
1729 }
1730 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001731 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001732 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001733 case R_MIPS_HI16:
Rafael Espindola163974d2016-03-29 23:05:59 +00001734 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001735 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001736 case R_MIPS_JALR:
1737 // Ignore this optimization relocation for now
1738 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001739 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001740 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001741 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001742 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001743 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001744 break;
1745 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001746 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001747 break;
1748 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001749 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001750 break;
1751 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001752 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001753 break;
1754 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001755 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001756 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001757 case R_MIPS_PCHI16:
Rafael Espindola163974d2016-03-29 23:05:59 +00001758 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001759 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001760 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001761 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001762 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001763 case R_MIPS_TLS_DTPREL_HI16:
1764 writeMipsHi16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1765 break;
1766 case R_MIPS_TLS_DTPREL_LO16:
1767 writeMipsLo16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1768 break;
1769 case R_MIPS_TLS_TPREL_HI16:
1770 writeMipsHi16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1771 break;
1772 case R_MIPS_TLS_TPREL_LO16:
1773 writeMipsLo16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1774 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001775 default:
George Rimar57610422016-03-11 14:43:02 +00001776 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001777 }
1778}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001779
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001781bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001782 return Type == R_MIPS_JALR;
1783}
1784
1785template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001786bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1787 switch (Type) {
1788 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001789 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001790 case R_MIPS_26:
1791 case R_MIPS_32:
1792 case R_MIPS_64:
1793 case R_MIPS_HI16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001794 case R_MIPS_TLS_DTPREL_HI16:
1795 case R_MIPS_TLS_DTPREL_LO16:
1796 case R_MIPS_TLS_TPREL_HI16:
1797 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001798 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001799 }
1800}
1801
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001802// _gp is a MIPS-specific ABI-defined symbol which points to
1803// a location that is relative to GOT. This function returns
1804// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001805template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001806 unsigned GPOffset = 0x7ff0;
1807 if (uint64_t V = Out<ELFT>::Got->getVA())
1808 return V + GPOffset;
1809 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001810}
1811
1812template uint32_t getMipsGpAddr<ELF32LE>();
1813template uint32_t getMipsGpAddr<ELF32BE>();
1814template uint64_t getMipsGpAddr<ELF64LE>();
1815template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001816
1817template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1818 const SymbolBody &) const;
1819template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1820 const SymbolBody &) const;
1821template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1822 const SymbolBody &) const;
1823template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1824 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001825}
1826}