blob: bf66c51359dfeda614c6bc6a7eab2c397a5dc8e8 [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
Rafael Espindola22ef9562016-04-13 01:40:19 +000014// in this file.
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"
Simon Atanasyan13f6da12016-03-31 21:26:23 +000020#include "InputFiles.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000021#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000022#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000023
24#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000025#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000026#include "llvm/Support/Endian.h"
27#include "llvm/Support/ELF.h"
28
29using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000030using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000031using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000032using namespace llvm::ELF;
33
34namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000035namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000036
Rui Ueyamac1c282a2016-02-11 21:18:01 +000037TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000038
Rafael Espindolae7e57b22015-11-09 21:43:00 +000039static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000040
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000041template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
42 if (isInt<N>(V))
43 return;
44 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000045 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000046}
47
48template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
49 if (isUInt<N>(V))
50 return;
51 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000052 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000053}
54
Igor Kudrinfea8ed52015-11-26 10:05:24 +000055template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
56 if (isInt<N>(V) || isUInt<N>(V))
57 return;
58 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000059 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000060}
61
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000062template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
63 if ((V & (N - 1)) == 0)
64 return;
65 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000066 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000067}
68
Rui Ueyamaefc23de2015-10-14 21:30:32 +000069namespace {
70class X86TargetInfo final : public TargetInfo {
71public:
72 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000073 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000074 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000075 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000076 uint32_t getDynRel(uint32_t Type) const override;
77 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000078 bool isTlsLocalDynamicRel(uint32_t Type) const override;
79 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
80 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000081 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000082 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000083 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
84 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000085 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000086 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000087 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000088
Rafael Espindola22ef9562016-04-13 01:40:19 +000089 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
90 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
91 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
92 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000093};
94
95class X86_64TargetInfo final : public TargetInfo {
96public:
97 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000098 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +000099 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000100 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000101 bool isTlsLocalDynamicRel(uint32_t Type) const override;
102 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
103 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000104 void writeGotPltHeader(uint8_t *Buf) const override;
105 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000106 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000107 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
108 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000109 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000110 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000111 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000112
Rafael Espindola22ef9562016-04-13 01:40:19 +0000113 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
114 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
115 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
116 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000117};
118
Davide Italiano8c3444362016-01-11 19:45:33 +0000119class PPCTargetInfo final : public TargetInfo {
120public:
121 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000122 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000123 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000124 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000125};
126
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000127class PPC64TargetInfo final : public TargetInfo {
128public:
129 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000130 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000131 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
132 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000133 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool isRelRelative(uint32_t Type) const override;
135};
136
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000137class AArch64TargetInfo final : public TargetInfo {
138public:
139 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000140 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000141 uint32_t getDynRel(uint32_t Type) const override;
142 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
143 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000144 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000145 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000146 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
147 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000148 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000149 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000150 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000151 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
152 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
153 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000154
155private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000156 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000157};
158
Tom Stellard80efb162016-01-07 03:59:08 +0000159class AMDGPUTargetInfo final : public TargetInfo {
160public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000161 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000162 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
163 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000164};
165
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000166template <class ELFT> class MipsTargetInfo final : public TargetInfo {
167public:
168 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000169 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000170 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000171 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000172 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
173 void writePltZero(uint8_t *Buf) const override;
174 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
175 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000176 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000177 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000178 bool needsCopyRelImpl(uint32_t Type) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000179 bool needsThunk(uint32_t Type, const InputFile &File,
180 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000181 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000182 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000183 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000184};
185} // anonymous namespace
186
Rui Ueyama91004392015-10-13 16:08:15 +0000187TargetInfo *createTarget() {
188 switch (Config->EMachine) {
189 case EM_386:
190 return new X86TargetInfo();
191 case EM_AARCH64:
192 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000193 case EM_AMDGPU:
194 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000195 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000196 switch (Config->EKind) {
197 case ELF32LEKind:
198 return new MipsTargetInfo<ELF32LE>();
199 case ELF32BEKind:
200 return new MipsTargetInfo<ELF32BE>();
201 default:
George Rimar777f9632016-03-12 08:31:34 +0000202 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000203 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000204 case EM_PPC:
205 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000206 case EM_PPC64:
207 return new PPC64TargetInfo();
208 case EM_X86_64:
209 return new X86_64TargetInfo();
210 }
George Rimar777f9632016-03-12 08:31:34 +0000211 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000212}
213
Rafael Espindola01205f72015-09-22 18:19:46 +0000214TargetInfo::~TargetInfo() {}
215
Rafael Espindola666625b2016-04-01 14:36:09 +0000216uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
217 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000218 return 0;
219}
220
George Rimar786e8662016-03-17 05:57:33 +0000221uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000222
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000223bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
224
225template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
226 if (Config->Shared)
227 return false;
228 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
229 if (!SS)
230 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000231 return SS->isObject();
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000232}
233
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000234template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000235bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000236 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000237}
238
Rui Ueyamac516ae12016-01-29 02:33:45 +0000239bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000240bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
George Rimar48651482015-12-11 08:59:37 +0000241
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000242bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
243 const SymbolBody &S) const {
244 return false;
245}
246
George Rimar98b060d2016-03-06 06:01:07 +0000247bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000248
George Rimar98b060d2016-03-06 06:01:07 +0000249bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000250
George Rimar98b060d2016-03-06 06:01:07 +0000251bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000252 return false;
253}
254
Rafael Espindola22ef9562016-04-13 01:40:19 +0000255void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
256 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000257 llvm_unreachable("Should not have claimed to be relaxable");
258}
259
Rafael Espindola22ef9562016-04-13 01:40:19 +0000260void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
261 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000262 llvm_unreachable("Should not have claimed to be relaxable");
263}
264
Rafael Espindola22ef9562016-04-13 01:40:19 +0000265void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
266 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000267 llvm_unreachable("Should not have claimed to be relaxable");
268}
269
Rafael Espindola22ef9562016-04-13 01:40:19 +0000270void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
271 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000272 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000273}
George Rimar77d1cb12015-11-24 09:00:06 +0000274
Rafael Espindola7f074422015-09-22 21:35:51 +0000275X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000276 CopyRel = R_386_COPY;
277 GotRel = R_386_GLOB_DAT;
278 PltRel = R_386_JUMP_SLOT;
279 IRelativeRel = R_386_IRELATIVE;
280 RelativeRel = R_386_RELATIVE;
281 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000282 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
283 TlsOffsetRel = R_386_TLS_DTPOFF32;
284 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000285 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000286 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000287 TlsGdToLeSkip = 2;
288}
289
290RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
291 switch (Type) {
292 default:
293 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000294 case R_386_TLS_GD:
295 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000296 case R_386_TLS_LDM:
297 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000298 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000299 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000300 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000301 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000302 case R_386_GOTPC:
303 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000304 case R_386_TLS_IE:
305 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000306 case R_386_GOT32:
307 case R_386_TLS_GOTIE:
308 return R_GOT_FROM_END;
309 case R_386_GOTOFF:
310 return R_GOTREL;
311 case R_386_TLS_LE:
312 return R_TLS;
313 case R_386_TLS_LE_32:
314 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000315 }
George Rimar77b77792015-11-25 22:15:01 +0000316}
317
Rafael Espindolaffcad442016-03-23 14:58:25 +0000318bool X86TargetInfo::isRelRelative(uint32_t Type) const {
319 switch (Type) {
320 default:
321 return false;
322 case R_386_PC32:
323 case R_386_PLT32:
324 case R_386_TLS_LDO_32:
325 return true;
326 }
327}
328
Rui Ueyamac516ae12016-01-29 02:33:45 +0000329void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000330 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
331}
332
Rui Ueyamac516ae12016-01-29 02:33:45 +0000333void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000334 // Entries in .got.plt initially points back to the corresponding
335 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000336 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000337}
Rafael Espindola01205f72015-09-22 18:19:46 +0000338
George Rimar98b060d2016-03-06 06:01:07 +0000339uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000340 if (Type == R_386_TLS_LE)
341 return R_386_TLS_TPOFF;
342 if (Type == R_386_TLS_LE_32)
343 return R_386_TLS_TPOFF32;
344 return Type;
345}
346
George Rimar98b060d2016-03-06 06:01:07 +0000347uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000348 if (Type == R_386_TLS_IE)
349 return Type;
Rafael Espindolae149b482016-04-14 16:05:42 +0000350 return R_386_GOT32;
George Rimar6f17e092015-12-17 09:32:21 +0000351}
352
George Rimar98b060d2016-03-06 06:01:07 +0000353bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000354 return Type == R_386_TLS_GD;
355}
356
George Rimar98b060d2016-03-06 06:01:07 +0000357bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000358 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
359}
360
George Rimar98b060d2016-03-06 06:01:07 +0000361bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000362 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
363}
364
Rui Ueyama900e2d22016-01-29 03:51:49 +0000365void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000366 // Executable files and shared object files have
367 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000368 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000369 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000370 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000371 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
372 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000373 };
374 memcpy(Buf, V, sizeof(V));
375 return;
376 }
George Rimar648a2c32015-10-20 08:54:27 +0000377
George Rimar77b77792015-11-25 22:15:01 +0000378 const uint8_t PltData[] = {
379 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000380 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
381 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000382 };
383 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000384 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000385 write32le(Buf + 2, Got + 4);
386 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000387}
388
Rui Ueyama9398f862016-01-29 04:15:02 +0000389void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
390 uint64_t PltEntryAddr, int32_t Index,
391 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000392 const uint8_t Inst[] = {
393 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
394 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
395 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
396 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000397 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000398
George Rimar77b77792015-11-25 22:15:01 +0000399 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000400 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000401 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
402 : Out<ELF32LE>::Got->getVA();
403 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000404 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000405 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000406}
407
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000408bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
409 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000410}
411
Rafael Espindola666625b2016-04-01 14:36:09 +0000412uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
413 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000414 switch (Type) {
415 default:
416 return 0;
417 case R_386_32:
418 case R_386_GOT32:
419 case R_386_GOTOFF:
420 case R_386_GOTPC:
421 case R_386_PC32:
422 case R_386_PLT32:
423 return read32le(Buf);
424 }
425}
426
Rafael Espindola22ef9562016-04-13 01:40:19 +0000427void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
428 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000429 checkInt<32>(Val, Type);
430 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000431}
432
Rafael Espindola22ef9562016-04-13 01:40:19 +0000433void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
434 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000435 // GD can be optimized to LE:
436 // leal x@tlsgd(, %ebx, 1),
437 // call __tls_get_addr@plt
438 // Can be converted to:
439 // movl %gs:0,%eax
440 // addl $x@ntpoff,%eax
441 // But gold emits subl $foo@tpoff,%eax instead of addl.
442 // These instructions are completely equal in behavior.
443 // This method generates subl to be consistent with gold.
444 const uint8_t Inst[] = {
445 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
446 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
447 };
448 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000449 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000450}
451
452// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
453// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
454// how GD can be optimized to IE:
455// leal x@tlsgd(, %ebx, 1),
456// call __tls_get_addr@plt
457// Is converted to:
458// movl %gs:0, %eax
459// addl x@gotntpoff(%ebx), %eax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000460void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
461 uint64_t Val) const {
George Rimar2558e122015-12-09 09:55:54 +0000462 const uint8_t Inst[] = {
463 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
464 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
465 };
466 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000467 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
468 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000469}
470
George Rimar6f17e092015-12-17 09:32:21 +0000471// In some conditions, relocations can be optimized to avoid using GOT.
472// This function does that for Initial Exec to Local Exec case.
473// Read "ELF Handling For Thread-Local Storage, 5.1
474// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000475// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000476void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
477 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000478 // Ulrich's document section 6.2 says that @gotntpoff can
479 // be used with MOVL or ADDL instructions.
480 // @indntpoff is similar to @gotntpoff, but for use in
481 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000482 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000483 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000484 uint8_t Reg = (Loc[-1] >> 3) & 7;
485 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000486 if (Type == R_386_TLS_IE) {
487 // For R_386_TLS_IE relocation we perform the next transformations:
488 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
489 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
490 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
491 // First one is special because when EAX is used the sequence is 5 bytes
492 // long, otherwise it is 6 bytes.
493 if (*Op == 0xa1) {
494 *Op = 0xb8;
495 } else {
496 *Inst = IsMov ? 0xc7 : 0x81;
497 *Op = 0xc0 | ((*Op >> 3) & 7);
498 }
499 } else {
500 // R_386_TLS_GOTIE relocation can be optimized to
501 // R_386_TLS_LE so that it does not use GOT.
502 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
503 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
504 // Note: gold converts to ADDL instead of LEAL.
505 *Inst = IsMov ? 0xc7 : 0x8d;
506 if (IsMov)
507 *Op = 0xc0 | ((*Op >> 3) & 7);
508 else
509 *Op = 0x80 | Reg | (Reg << 3);
510 }
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000511 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000512}
513
Rafael Espindola22ef9562016-04-13 01:40:19 +0000514void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
515 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000516 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000517 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000518 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000519 }
520
521 // LD can be optimized to LE:
522 // leal foo(%reg),%eax
523 // call ___tls_get_addr
524 // Is converted to:
525 // movl %gs:0,%eax
526 // nop
527 // leal 0(%esi,1),%esi
528 const uint8_t Inst[] = {
529 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
530 0x90, // nop
531 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
532 };
533 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000534}
535
Rafael Espindola7f074422015-09-22 21:35:51 +0000536X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000537 CopyRel = R_X86_64_COPY;
538 GotRel = R_X86_64_GLOB_DAT;
539 PltRel = R_X86_64_JUMP_SLOT;
540 RelativeRel = R_X86_64_RELATIVE;
541 IRelativeRel = R_X86_64_IRELATIVE;
542 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000543 TlsModuleIndexRel = R_X86_64_DTPMOD64;
544 TlsOffsetRel = R_X86_64_DTPOFF64;
545 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000546 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000547 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000548 TlsGdToLeSkip = 2;
549}
550
551RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
552 switch (Type) {
553 default:
554 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000555 case R_X86_64_TPOFF32:
556 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000557 case R_X86_64_TLSLD:
558 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000559 case R_X86_64_TLSGD:
560 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000561 case R_X86_64_SIZE32:
562 case R_X86_64_SIZE64:
563 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000564 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000565 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000566 case R_X86_64_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000567 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000568 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000569 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000570 case R_X86_64_GOTPCREL:
Rafael Espindolaf350d252016-04-19 20:18:52 +0000571 case R_X86_64_GOTPCRELX:
572 case R_X86_64_REX_GOTPCRELX:
Rafael Espindola5628ee72016-04-15 19:14:18 +0000573 case R_X86_64_GOTTPOFF:
574 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000575 }
George Rimar648a2c32015-10-20 08:54:27 +0000576}
577
Rui Ueyamac516ae12016-01-29 02:33:45 +0000578void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000579 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
580}
581
Rui Ueyamac516ae12016-01-29 02:33:45 +0000582void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000583 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000584 write32le(Buf, Plt + 6);
585}
586
Rui Ueyama900e2d22016-01-29 03:51:49 +0000587void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000588 const uint8_t PltData[] = {
589 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
590 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
591 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
592 };
593 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000594 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
595 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
596 write32le(Buf + 2, Got - Plt + 2); // GOT+8
597 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000598}
Rafael Espindola01205f72015-09-22 18:19:46 +0000599
Rui Ueyama9398f862016-01-29 04:15:02 +0000600void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
601 uint64_t PltEntryAddr, int32_t Index,
602 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000603 const uint8_t Inst[] = {
604 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
605 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
606 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
607 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000608 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000609
George Rimar648a2c32015-10-20 08:54:27 +0000610 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
611 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000612 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000613}
614
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000615bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
616 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
617 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000618}
619
George Rimar86971052016-03-29 08:35:42 +0000620uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
621 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
622 if (Config->Shared)
623 error(getELFRelocationTypeName(EM_X86_64, Type) +
624 " cannot be a dynamic relocation");
625 return Type;
626}
627
George Rimar98b060d2016-03-06 06:01:07 +0000628uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000629 // No other types of TLS relocations requiring GOT should
630 // reach here.
631 assert(Type == R_X86_64_GOTTPOFF);
632 return R_X86_64_PC32;
633}
634
George Rimar98b060d2016-03-06 06:01:07 +0000635bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000636 return Type == R_X86_64_GOTTPOFF;
637}
638
George Rimar98b060d2016-03-06 06:01:07 +0000639bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000640 return Type == R_X86_64_TLSGD;
641}
642
George Rimar98b060d2016-03-06 06:01:07 +0000643bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000644 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
645 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000646}
647
Rafael Espindolaae244002015-10-05 19:30:12 +0000648bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
649 switch (Type) {
650 default:
651 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000652 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000653 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000654 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000655 case R_X86_64_PC8:
656 case R_X86_64_PC16:
657 case R_X86_64_PC32:
658 case R_X86_64_PC64:
659 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000660 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000661 return true;
662 }
663}
664
George Rimar6713cf82015-11-25 21:46:05 +0000665// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
666// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000667// how GD can be optimized to LE:
668// .byte 0x66
669// leaq x@tlsgd(%rip), %rdi
670// .word 0x6666
671// rex64
672// call __tls_get_addr@plt
673// Is converted to:
674// mov %fs:0x0,%rax
675// lea x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000676void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
677 uint64_t Val) const {
George Rimar6713cf82015-11-25 21:46:05 +0000678 const uint8_t Inst[] = {
679 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
680 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
681 };
682 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000683 relocateOne(Loc + 8, R_X86_64_TPOFF32,
684 Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000685}
686
George Rimar25411f252015-12-04 11:20:13 +0000687// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
688// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
689// how GD can be optimized to IE:
690// .byte 0x66
691// leaq x@tlsgd(%rip), %rdi
692// .word 0x6666
693// rex64
694// call __tls_get_addr@plt
695// Is converted to:
696// mov %fs:0x0,%rax
697// addq x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000698void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
699 uint64_t Val) const {
George Rimar25411f252015-12-04 11:20:13 +0000700 const uint8_t Inst[] = {
701 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
702 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
703 };
704 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000705 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000706}
707
George Rimar77d1cb12015-11-24 09:00:06 +0000708// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000709// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000710// This function does that. Read "ELF Handling For Thread-Local Storage,
711// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
712// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000713void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
714 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000715 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
716 // used in MOVQ or ADDQ instructions only.
717 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
718 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
719 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
720 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
721 uint8_t *Prefix = Loc - 3;
722 uint8_t *Inst = Loc - 2;
723 uint8_t *RegSlot = Loc - 1;
724 uint8_t Reg = Loc[-1] >> 3;
725 bool IsMov = *Inst == 0x8b;
726 bool RspAdd = !IsMov && Reg == 4;
727 // r12 and rsp registers requires special handling.
728 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
729 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
730 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
731 // The same true for rsp. So we convert to addq for them, saving 1 byte that
732 // we dont have.
733 if (RspAdd)
734 *Inst = 0x81;
735 else
736 *Inst = IsMov ? 0xc7 : 0x8d;
737 if (*Prefix == 0x4c)
738 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
739 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000740 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000741}
742
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000743// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
744// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
745// how LD can be optimized to LE:
746// leaq bar@tlsld(%rip), %rdi
747// callq __tls_get_addr@PLT
748// leaq bar@dtpoff(%rax), %rcx
749// Is converted to:
750// .word 0x6666
751// .byte 0x66
752// mov %fs:0,%rax
753// leaq bar@tpoff(%rax), %rcx
Rafael Espindola22ef9562016-04-13 01:40:19 +0000754void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
755 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000756 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000757 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
758 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000759 }
760 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindolaece62b92016-04-18 12:44:33 +0000761 relocateOne(Loc, R_X86_64_TPOFF32, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000762 return;
George Rimar25411f252015-12-04 11:20:13 +0000763 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000764
765 const uint8_t Inst[] = {
766 0x66, 0x66, //.word 0x6666
767 0x66, //.byte 0x66
768 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
769 };
770 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000771}
772
Rafael Espindola22ef9562016-04-13 01:40:19 +0000773void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
774 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000775 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000776 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000777 checkUInt<32>(Val, Type);
778 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000779 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000780 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000781 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000782 case R_X86_64_GOT32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000783 checkInt<32>(Val, Type);
784 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000785 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000786 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000787 case R_X86_64_DTPOFF64:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000788 case R_X86_64_SIZE64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000789 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000790 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000791 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000792 case R_X86_64_GOTPCRELX:
793 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000794 case R_X86_64_PC32:
795 case R_X86_64_PLT32:
796 case R_X86_64_TLSGD:
797 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000798 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000799 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000800 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000801 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000802 default:
George Rimar57610422016-03-11 14:43:02 +0000803 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000804 }
805}
806
Hal Finkel3c8cc672015-10-12 20:56:18 +0000807// Relocation masks following the #lo(value), #hi(value), #ha(value),
808// #higher(value), #highera(value), #highest(value), and #highesta(value)
809// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
810// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000811static uint16_t applyPPCLo(uint64_t V) { return V; }
812static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
813static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
814static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
815static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000816static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000817static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
818
Davide Italiano8c3444362016-01-11 19:45:33 +0000819PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000820bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
821
Rafael Espindola22ef9562016-04-13 01:40:19 +0000822void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
823 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000824 switch (Type) {
825 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000826 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000827 break;
828 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000829 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000830 break;
831 default:
George Rimar57610422016-03-11 14:43:02 +0000832 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000833 }
834}
835
Rafael Espindola22ef9562016-04-13 01:40:19 +0000836RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
837 return R_ABS;
838}
839
Rafael Espindolac4010882015-09-22 20:54:08 +0000840PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000841 GotRel = R_PPC64_GLOB_DAT;
842 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000843 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000844
845 // We need 64K pages (at least under glibc/Linux, the loader won't
846 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000847 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000848
849 // The PPC64 ELF ABI v1 spec, says:
850 //
851 // It is normally desirable to put segments with different characteristics
852 // in separate 256 Mbyte portions of the address space, to give the
853 // operating system full paging flexibility in the 64-bit address space.
854 //
855 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
856 // use 0x10000000 as the starting address.
857 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000858}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000859
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000860uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000861 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
862 // order. The TOC starts where the first of these sections starts.
863
864 // FIXME: This obviously does not do the right thing when there is no .got
865 // section, but there is a .toc or .tocbss section.
866 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
867 if (!TocVA)
868 TocVA = Out<ELF64BE>::Plt->getVA();
869
870 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
871 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
872 // code (crt1.o) assumes that you can get from the TOC base to the
873 // start of the .toc section with only a single (signed) 16-bit relocation.
874 return TocVA + 0x8000;
875}
876
Rafael Espindola22ef9562016-04-13 01:40:19 +0000877RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
878 switch (Type) {
879 default:
880 return R_ABS;
881 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000882 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000883 }
884}
885
Rui Ueyama9398f862016-01-29 04:15:02 +0000886void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
887 uint64_t PltEntryAddr, int32_t Index,
888 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000889 uint64_t Off = GotEntryAddr - getPPC64TocBase();
890
891 // FIXME: What we should do, in theory, is get the offset of the function
892 // descriptor in the .opd section, and use that as the offset from %r2 (the
893 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
894 // be a pointer to the function descriptor in the .opd section. Using
895 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
896
Hal Finkelfa92f682015-10-13 21:47:34 +0000897 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000898 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
899 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
900 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
901 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
902 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
903 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
904 write32be(Buf + 28, 0x4e800420); // bctr
905}
906
Hal Finkelbe0823d2015-10-12 20:58:52 +0000907bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
908 switch (Type) {
909 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000910 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000911 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000912 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000913 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000914 }
915}
916
Rafael Espindola22ef9562016-04-13 01:40:19 +0000917void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
918 uint64_t Val) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000919 uint64_t TB = getPPC64TocBase();
920
Hal Finkel3c8cc672015-10-12 20:56:18 +0000921 // For a TOC-relative relocation, adjust the addend and proceed in terms of
922 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000923 switch (Type) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000924 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TB; break;
925 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TB; break;
926 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TB; break;
927 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TB; break;
928 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TB; break;
929 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000930 default: break;
931 }
932
Hal Finkel3c8cc672015-10-12 20:56:18 +0000933 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000934 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000935 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000936 // Preserve the AA/LK bits in the branch instruction
937 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +0000938 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000939 break;
940 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000941 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000942 checkInt<16>(Val, Type);
943 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000944 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000945 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000946 checkInt<16>(Val, Type);
947 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000948 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000949 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000950 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000951 break;
952 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000953 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000954 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000955 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000956 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000957 break;
958 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000959 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000960 break;
961 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000962 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000963 break;
964 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000965 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000966 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000967 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000968 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000969 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000970 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000971 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000972 break;
973 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000974 checkInt<32>(Val, Type);
975 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000976 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000977 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000978 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000979 break;
980 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000981 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000982 break;
983 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000984 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000985 break;
986 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000987 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000988 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000989 case R_PPC64_REL24: {
990 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000991 checkInt<24>(Val, Type);
992 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000993 break;
994 }
995 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000996 checkInt<32>(Val, Type);
997 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000998 break;
999 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001000 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001001 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001002 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001003 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001004 break;
1005 default:
George Rimar57610422016-03-11 14:43:02 +00001006 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001007 }
1008}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001009
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001010AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001011 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001012 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001013 IRelativeRel = R_AARCH64_IRELATIVE;
1014 GotRel = R_AARCH64_GLOB_DAT;
1015 PltRel = R_AARCH64_JUMP_SLOT;
1016 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001017 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1018 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001019 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001020 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001021 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001022}
George Rimar648a2c32015-10-20 08:54:27 +00001023
Rafael Espindola22ef9562016-04-13 01:40:19 +00001024RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1025 const SymbolBody &S) const {
1026 switch (Type) {
1027 default:
1028 return R_ABS;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001029 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +00001030 case R_AARCH64_CONDBR19:
1031 case R_AARCH64_JUMP26:
1032 case R_AARCH64_TSTBR14:
1033 return R_PLT_PC;
1034
Rafael Espindola22ef9562016-04-13 01:40:19 +00001035 case R_AARCH64_PREL16:
1036 case R_AARCH64_PREL32:
1037 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001038 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001039 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001040 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001041 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001042 case R_AARCH64_LD64_GOT_LO12_NC:
1043 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1044 return R_GOT;
1045 case R_AARCH64_ADR_GOT_PAGE:
1046 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1047 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001048 }
1049}
1050
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001051bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001052 switch (Type) {
1053 default:
1054 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001055 case R_AARCH64_ADD_ABS_LO12_NC:
1056 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001057 case R_AARCH64_ADR_PREL_LO21:
1058 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001059 case R_AARCH64_CALL26:
1060 case R_AARCH64_CONDBR19:
1061 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001062 case R_AARCH64_LDST8_ABS_LO12_NC:
1063 case R_AARCH64_LDST16_ABS_LO12_NC:
1064 case R_AARCH64_LDST32_ABS_LO12_NC:
1065 case R_AARCH64_LDST64_ABS_LO12_NC:
1066 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001067 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001068 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001069 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1070 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1071 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1072 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1073 case R_AARCH64_TSTBR14:
Rafael Espindola6eda85a2016-04-20 14:36:24 +00001074 case R_AARCH64_LD64_GOT_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001075 return true;
1076 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001077}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001078
George Rimar98b060d2016-03-06 06:01:07 +00001079bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001080 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1081 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1082 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1083 Type == R_AARCH64_TLSDESC_CALL;
1084}
1085
George Rimar98b060d2016-03-06 06:01:07 +00001086bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001087 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1088 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1089}
1090
George Rimar98b060d2016-03-06 06:01:07 +00001091uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001092 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1093 return Type;
1094 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001095 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001096 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001097 // Keep it going with a dummy value so that we can find more reloc errors.
1098 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001099}
1100
Rui Ueyamac516ae12016-01-29 02:33:45 +00001101void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001102 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1103}
1104
Rafael Espindola22ef9562016-04-13 01:40:19 +00001105static uint64_t getAArch64Page(uint64_t Expr) {
1106 return Expr & (~static_cast<uint64_t>(0xFFF));
1107}
1108
Rui Ueyama900e2d22016-01-29 03:51:49 +00001109void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001110 const uint8_t PltData[] = {
1111 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1112 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1113 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1114 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1115 0x20, 0x02, 0x1f, 0xd6, // br x17
1116 0x1f, 0x20, 0x03, 0xd5, // nop
1117 0x1f, 0x20, 0x03, 0xd5, // nop
1118 0x1f, 0x20, 0x03, 0xd5 // nop
1119 };
1120 memcpy(Buf, PltData, sizeof(PltData));
1121
Rui Ueyama900e2d22016-01-29 03:51:49 +00001122 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1123 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001124 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1125 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1126 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1127 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001128}
1129
Rui Ueyama9398f862016-01-29 04:15:02 +00001130void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1131 uint64_t PltEntryAddr, int32_t Index,
1132 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001133 const uint8_t Inst[] = {
1134 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1135 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1136 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1137 0x20, 0x02, 0x1f, 0xd6 // br x17
1138 };
1139 memcpy(Buf, Inst, sizeof(Inst));
1140
Rafael Espindola22ef9562016-04-13 01:40:19 +00001141 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1142 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1143 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1144 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001145}
1146
George Rimar98b060d2016-03-06 06:01:07 +00001147uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001148 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001149 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1150 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001151}
1152
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001153bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001154 switch (Type) {
1155 default:
1156 return false;
1157 case R_AARCH64_ABS16:
1158 case R_AARCH64_ABS32:
1159 case R_AARCH64_ABS64:
1160 case R_AARCH64_ADD_ABS_LO12_NC:
1161 case R_AARCH64_ADR_PREL_LO21:
1162 case R_AARCH64_ADR_PREL_PG_HI21:
1163 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001164 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001165 case R_AARCH64_LDST32_ABS_LO12_NC:
1166 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001167 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001168 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001169 }
1170}
1171
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001172static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001173 uint32_t ImmLo = (Imm & 0x3) << 29;
1174 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1175 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001176 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001177}
1178
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001179static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1180 or32le(L, (Imm & 0xFFF) << 10);
1181}
1182
Rafael Espindola22ef9562016-04-13 01:40:19 +00001183void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1184 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001185 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001186 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001187 checkIntUInt<16>(Val, Type);
1188 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001189 break;
1190 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001191 checkIntUInt<32>(Val, Type);
1192 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001193 break;
1194 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001195 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001196 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001197 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001198 // This relocation stores 12 bits and there's no instruction
1199 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001200 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1201 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001202 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001203 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001204 case R_AARCH64_ADR_GOT_PAGE: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001205 uint64_t X = Val;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001206 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001207 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001208 break;
1209 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001210 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001211 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001212 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001213 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001214 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001215 }
George Rimar3d737e42016-01-13 13:04:46 +00001216 case R_AARCH64_ADR_PREL_PG_HI21:
1217 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001218 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001219 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001220 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001221 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001222 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001223 case R_AARCH64_CALL26:
1224 case R_AARCH64_JUMP26: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001225 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001226 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001227 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1228 break;
1229 }
George Rimar4102bfb2016-01-11 14:22:00 +00001230 case R_AARCH64_CONDBR19: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001231 uint64_t X = Val;
George Rimar4102bfb2016-01-11 14:22:00 +00001232 checkInt<21>(X, Type);
1233 or32le(Loc, (X & 0x1FFFFC) << 3);
1234 break;
1235 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001236 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001237 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001238 checkAlignment<8>(Val, Type);
1239 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001240 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001241 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001242 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001243 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001244 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001245 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001246 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001247 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001248 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001249 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001250 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001251 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001252 break;
1253 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001254 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001255 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001256 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001257 checkIntUInt<16>(Val, Type);
1258 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001259 break;
1260 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001261 checkIntUInt<32>(Val, Type);
1262 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001263 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001264 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001265 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001266 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001267 case R_AARCH64_TSTBR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001268 uint64_t X = Val;
George Rimar1395dbd2016-01-11 14:27:05 +00001269 checkInt<16>(X, Type);
1270 or32le(Loc, (X & 0xFFFC) << 3);
1271 break;
1272 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001273 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001274 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001275 checkInt<24>(V, Type);
1276 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1277 break;
1278 }
1279 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001280 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001281 updateAArch64Add(Loc, V & 0xFFF);
1282 break;
1283 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001284 default:
George Rimar57610422016-03-11 14:43:02 +00001285 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001286 }
1287}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001288
Rafael Espindola22ef9562016-04-13 01:40:19 +00001289void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1290 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001291 // TLSDESC Global-Dynamic relocation are in the form:
1292 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1293 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1294 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1295 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1296 // And it can optimized to:
1297 // movz x0, #0x0, lsl #16
1298 // movk x0, #0x10
1299 // nop
1300 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001301 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001302 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001303 checkUInt<32>(X, Type);
1304
1305 uint32_t NewInst;
1306 switch (Type) {
1307 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1308 case R_AARCH64_TLSDESC_CALL:
1309 // nop
1310 NewInst = 0xd503201f;
1311 break;
1312 case R_AARCH64_TLSDESC_ADR_PAGE21:
1313 // movz
1314 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1315 break;
1316 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1317 // movk
1318 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1319 break;
1320 default:
George Rimar777f9632016-03-12 08:31:34 +00001321 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001322 }
1323 write32le(Loc, NewInst);
1324}
1325
Rafael Espindola22ef9562016-04-13 01:40:19 +00001326void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1327 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001328 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001329 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001330 checkUInt<32>(X, Type);
1331
George Rimar4d1d16d2016-03-06 06:16:05 +00001332 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001333 uint32_t NewInst;
1334 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1335 // Generate movz.
1336 unsigned RegNo = (Inst & 0x1f);
1337 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1338 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1339 // Generate movk
1340 unsigned RegNo = (Inst & 0x1f);
1341 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1342 } else {
George Rimar777f9632016-03-12 08:31:34 +00001343 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001344 }
1345 write32le(Loc, NewInst);
1346}
1347
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001348// Implementing relocations for AMDGPU is low priority since most
1349// programs don't use relocations now. Thus, this function is not
1350// actually called (relocateOne is called for each relocation).
1351// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001352void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1353 uint64_t Val) const {
1354 llvm_unreachable("not implemented");
1355}
1356
1357RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001358 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001359}
1360
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001361template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001362 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001363 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001364 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001365 PltEntrySize = 16;
1366 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001367 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001368 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001369 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001370 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001371 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001372}
1373
1374template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001375RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1376 const SymbolBody &S) const {
1377 switch (Type) {
1378 default:
1379 return R_ABS;
Rafael Espindolab312a742016-04-21 17:30:24 +00001380 case R_MIPS_26:
1381 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001382 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001383 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001384 // MIPS _gp_disp designates offset between start of function and 'gp'
1385 // pointer into GOT. __gnu_local_gp is equal to the current value of
1386 // the 'gp'. Therefore any relocations against them do not require
1387 // dynamic relocation.
1388 if (&S == ElfSym<ELFT>::MipsGpDisp)
1389 return R_PC;
1390 return R_ABS;
1391 case R_MIPS_PC32:
1392 case R_MIPS_PC16:
1393 case R_MIPS_PC19_S2:
1394 case R_MIPS_PC21_S2:
1395 case R_MIPS_PC26_S2:
1396 case R_MIPS_PCHI16:
1397 case R_MIPS_PCLO16:
1398 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001399 case R_MIPS_GOT16:
1400 case R_MIPS_CALL16:
1401 if (S.isLocal())
1402 return R_MIPS_GOT_LOCAL;
1403 if (!S.isPreemptible())
1404 return R_MIPS_GOT;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001405 return R_GOT_OFF;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001406 }
1407}
1408
1409template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001410uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001411 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1412 return R_MIPS_REL32;
1413 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001414 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001415 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001416 // Keep it going with a dummy value so that we can find more reloc errors.
1417 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001418}
1419
1420template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001421void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001422 typedef typename ELFT::Off Elf_Off;
1423 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001424
1425 // Set the MSB of the second GOT slot. This is not required by any
1426 // MIPS ABI documentation, though.
1427 //
1428 // There is a comment in glibc saying that "The MSB of got[1] of a
1429 // gnu object is set to identify gnu objects," and in GNU gold it
1430 // says "the second entry will be used by some runtime loaders".
1431 // But how this field is being used is unclear.
1432 //
1433 // We are not really willing to mimic other linkers behaviors
1434 // without understanding why they do that, but because all files
1435 // generated by GNU tools have this special GOT value, and because
1436 // we've been doing this for years, it is probably a safe bet to
1437 // keep doing this for now. We really need to revisit this to see
1438 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001439 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001440 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001441}
1442
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001443template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001444void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1445 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001446}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001447
Simon Atanasyan35031192015-12-15 06:06:34 +00001448static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001449
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001450template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001451static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001452 uint32_t Instr = read32<E>(Loc);
1453 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1454 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1455}
1456
1457template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001458static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001459 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001460 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001461 if (SHIFT > 0)
1462 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001463 checkInt<BSIZE + SHIFT>(V, Type);
1464 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001465}
1466
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001467template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001468static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001469 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001470 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001471}
1472
Simon Atanasyan3b377852016-03-04 10:55:20 +00001473template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001474static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1475 uint32_t Instr = read32<E>(Loc);
1476 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1477}
1478
Rafael Espindola666625b2016-04-01 14:36:09 +00001479template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001480 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1481}
1482
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001483template <class ELFT>
1484void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1485 const endianness E = ELFT::TargetEndianness;
1486 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1487 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1488 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1489 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1490 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1491 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1492 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1493 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1494 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001495 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001496 writeMipsLo16<E>(Buf + 4, Got);
1497 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001498}
1499
1500template <class ELFT>
1501void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1502 uint64_t PltEntryAddr, int32_t Index,
1503 unsigned RelOff) const {
1504 const endianness E = ELFT::TargetEndianness;
1505 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1506 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1507 write32<E>(Buf + 8, 0x03200008); // jr $25
1508 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001509 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001510 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1511 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001512}
1513
1514template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001515void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1516 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1517 // See MipsTargetInfo::writeThunk for details.
1518 const endianness E = ELFT::TargetEndianness;
1519 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1520 write32<E>(Buf + 4, 0x08000000); // j func
1521 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1522 write32<E>(Buf + 12, 0x00000000); // nop
1523 writeMipsHi16<E>(Buf, S);
1524 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1525 writeMipsLo16<E>(Buf + 8, S);
1526}
1527
1528template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001529bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Rafael Espindola790db9c2016-04-01 17:00:36 +00001530 return !isRelRelative(Type) || Type == R_MIPS_LO16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001531}
1532
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001533template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001534bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1535 const SymbolBody &S) const {
1536 // Any MIPS PIC code function is invoked with its address in register $t9.
1537 // So if we have a branch instruction from non-PIC code to the PIC one
1538 // we cannot make the jump directly and need to create a small stubs
1539 // to save the target function address.
1540 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1541 if (Type != R_MIPS_26)
1542 return false;
1543 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1544 if (!F)
1545 return false;
1546 // If current file has PIC code, LA25 stub is not required.
1547 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1548 return false;
1549 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1550 if (!D || !D->Section)
1551 return false;
1552 // LA25 is required if target file has PIC code
1553 // or target symbol is a PIC symbol.
1554 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001555 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001556}
1557
1558template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001559uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001560 uint32_t Type) const {
1561 const endianness E = ELFT::TargetEndianness;
1562 switch (Type) {
1563 default:
1564 return 0;
1565 case R_MIPS_32:
1566 case R_MIPS_GPREL32:
1567 return read32<E>(Buf);
1568 case R_MIPS_26:
1569 // FIXME (simon): If the relocation target symbol is not a PLT entry
1570 // we should use another expression for calculation:
1571 // ((A << 2) | (P & 0xf0000000)) >> 2
1572 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1573 case R_MIPS_GPREL16:
1574 case R_MIPS_LO16:
1575 case R_MIPS_PCLO16:
1576 case R_MIPS_TLS_DTPREL_HI16:
1577 case R_MIPS_TLS_DTPREL_LO16:
1578 case R_MIPS_TLS_TPREL_HI16:
1579 case R_MIPS_TLS_TPREL_LO16:
1580 return readSignedLo16<E>(Buf);
1581 case R_MIPS_PC16:
1582 return getPcRelocAddend<E, 16, 2>(Buf);
1583 case R_MIPS_PC19_S2:
1584 return getPcRelocAddend<E, 19, 2>(Buf);
1585 case R_MIPS_PC21_S2:
1586 return getPcRelocAddend<E, 21, 2>(Buf);
1587 case R_MIPS_PC26_S2:
1588 return getPcRelocAddend<E, 26, 2>(Buf);
1589 case R_MIPS_PC32:
1590 return getPcRelocAddend<E, 32, 0>(Buf);
1591 }
1592}
1593
1594template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001595void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1596 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001597 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001598 // Thread pointer and DRP offsets from the start of TLS data area.
1599 // https://www.linux-mips.org/wiki/NPTL
1600 const uint32_t TPOffset = 0x7000;
1601 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001602 switch (Type) {
1603 case R_MIPS_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001604 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001605 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001606 case R_MIPS_26: {
1607 uint32_t Instr = read32<E>(Loc);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001608 write32<E>(Loc, (Instr & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001609 break;
1610 }
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001611 case R_MIPS_GOT16:
1612 checkInt<16>(Val, Type);
1613 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001614 case R_MIPS_CALL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001615 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001616 break;
Simon Atanasyan57830b62015-12-25 13:02:13 +00001617 case R_MIPS_GPREL16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001618 int64_t V = Val - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001619 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001620 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001621 break;
1622 }
1623 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001624 write32<E>(Loc, Val - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001625 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001626 case R_MIPS_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001627 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001628 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001629 case R_MIPS_JALR:
1630 // Ignore this optimization relocation for now
1631 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001632 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001633 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001634 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001635 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001636 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001637 break;
1638 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001639 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001640 break;
1641 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001642 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001643 break;
1644 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001645 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001646 break;
1647 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001648 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001649 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001650 case R_MIPS_PCHI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001651 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001652 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001653 case R_MIPS_PCLO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001654 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001655 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001656 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001657 writeMipsHi16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001658 break;
1659 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001660 writeMipsLo16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001661 break;
1662 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001663 writeMipsHi16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001664 break;
1665 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001666 writeMipsLo16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001667 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001668 default:
George Rimar57610422016-03-11 14:43:02 +00001669 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001670 }
1671}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001672
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001673template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001674bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001675 return Type == R_MIPS_JALR;
1676}
1677
1678template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001679bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1680 switch (Type) {
1681 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001682 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001683 case R_MIPS_26:
1684 case R_MIPS_32:
1685 case R_MIPS_64:
1686 case R_MIPS_HI16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001687 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001688 }
1689}
1690
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001691// _gp is a MIPS-specific ABI-defined symbol which points to
1692// a location that is relative to GOT. This function returns
1693// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001694template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rafael Espindola6f92e142016-04-12 13:26:51 +00001695 return Out<ELFT>::Got->getVA() + MipsGPOffset;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001696}
1697
1698template uint32_t getMipsGpAddr<ELF32LE>();
1699template uint32_t getMipsGpAddr<ELF32BE>();
1700template uint64_t getMipsGpAddr<ELF64LE>();
1701template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001702
1703template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1704 const SymbolBody &) const;
1705template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1706 const SymbolBody &) const;
1707template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1708 const SymbolBody &) const;
1709template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1710 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001711}
1712}