blob: e21e3efd747ae94745570e1df8199318cb7a1add [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//
Rui Ueyama55274e32016-04-23 01:10:15 +000016// Some functions defined in this file has "relaxTls" as part of their names.
17// They do peephole optimization for TLS variables by rewriting instructions.
18// They are not part of the ABI but optional optimization, so you can skip
19// them if you are not interested in how TLS variables are optimized.
20// See the following paper for the details.
21//
22// Ulrich Drepper, ELF Handling For Thread-Local Storage
23// http://www.akkadia.org/drepper/tls.pdf
24//
Rui Ueyama34f29242015-10-13 19:51:57 +000025//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000026
27#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000028#include "Error.h"
Simon Atanasyan13f6da12016-03-31 21:26:23 +000029#include "InputFiles.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000030#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000031#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000032
33#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000034#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000035#include "llvm/Support/Endian.h"
36#include "llvm/Support/ELF.h"
37
38using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000039using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000040using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000041using namespace llvm::ELF;
42
43namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000044namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000045
Rui Ueyamac1c282a2016-02-11 21:18:01 +000046TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000047
Rafael Espindolae7e57b22015-11-09 21:43:00 +000048static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000049
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
51 if (isInt<N>(V))
52 return;
53 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000054 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000055}
56
57template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
58 if (isUInt<N>(V))
59 return;
60 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000061 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000062}
63
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
65 if (isInt<N>(V) || isUInt<N>(V))
66 return;
67 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000068 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000069}
70
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
72 if ((V & (N - 1)) == 0)
73 return;
74 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000075 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000076}
77
Rui Ueyamaefc23de2015-10-14 21:30:32 +000078namespace {
79class X86TargetInfo final : public TargetInfo {
80public:
81 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000082 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000083 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000084 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000085 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000086 bool isTlsLocalDynamicRel(uint32_t Type) const override;
87 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
88 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000089 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000090 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000091 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
92 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000093 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000094
Rafael Espindola22ef9562016-04-13 01:40:19 +000095 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
96 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
97 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
98 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000099};
100
101class X86_64TargetInfo final : public TargetInfo {
102public:
103 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000104 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000105 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000106 bool isTlsLocalDynamicRel(uint32_t Type) const override;
107 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
108 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000109 void writeGotPltHeader(uint8_t *Buf) const override;
110 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000111 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000112 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
113 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000114 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000115
Rafael Espindola22ef9562016-04-13 01:40:19 +0000116 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
117 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
118 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
119 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000120};
121
Davide Italiano8c3444362016-01-11 19:45:33 +0000122class PPCTargetInfo final : public TargetInfo {
123public:
124 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000125 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000126 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000127};
128
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129class PPC64TargetInfo final : public TargetInfo {
130public:
131 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000132 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000133 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
134 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000135 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136};
137
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000138class AArch64TargetInfo final : public TargetInfo {
139public:
140 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000141 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000142 uint32_t getDynRel(uint32_t Type) const override;
143 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
144 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000145 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000146 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000147 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
148 int32_t Index, unsigned RelOff) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000149 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000150 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
151 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
152 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000153
154private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000155 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000156};
157
Tom Stellard80efb162016-01-07 03:59:08 +0000158class AMDGPUTargetInfo final : public TargetInfo {
159public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000160 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000161 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
162 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000163};
164
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000165template <class ELFT> class MipsTargetInfo final : public TargetInfo {
166public:
167 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000168 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000169 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000170 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000171 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
172 void writePltZero(uint8_t *Buf) const override;
173 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
174 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000175 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000176 bool needsThunk(uint32_t Type, const InputFile &File,
177 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000178 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000179 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000180};
181} // anonymous namespace
182
Rui Ueyama91004392015-10-13 16:08:15 +0000183TargetInfo *createTarget() {
184 switch (Config->EMachine) {
185 case EM_386:
186 return new X86TargetInfo();
187 case EM_AARCH64:
188 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000189 case EM_AMDGPU:
190 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000191 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000192 switch (Config->EKind) {
193 case ELF32LEKind:
194 return new MipsTargetInfo<ELF32LE>();
195 case ELF32BEKind:
196 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000197 case ELF64LEKind:
198 return new MipsTargetInfo<ELF64LE>();
199 case ELF64BEKind:
200 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000201 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 Espindolab8ff59a2016-04-28 14:34:39 +0000223bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000224
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000225bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
226 const SymbolBody &S) const {
227 return false;
228}
229
George Rimar98b060d2016-03-06 06:01:07 +0000230bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000231
George Rimar98b060d2016-03-06 06:01:07 +0000232bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000233
George Rimar98b060d2016-03-06 06:01:07 +0000234bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000235 return false;
236}
237
Rafael Espindola22ef9562016-04-13 01:40:19 +0000238void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
239 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000240 llvm_unreachable("Should not have claimed to be relaxable");
241}
242
Rafael Espindola22ef9562016-04-13 01:40:19 +0000243void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
244 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000245 llvm_unreachable("Should not have claimed to be relaxable");
246}
247
Rafael Espindola22ef9562016-04-13 01:40:19 +0000248void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
249 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000250 llvm_unreachable("Should not have claimed to be relaxable");
251}
252
Rafael Espindola22ef9562016-04-13 01:40:19 +0000253void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
254 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000255 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000256}
George Rimar77d1cb12015-11-24 09:00:06 +0000257
Rafael Espindola7f074422015-09-22 21:35:51 +0000258X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000259 CopyRel = R_386_COPY;
260 GotRel = R_386_GLOB_DAT;
261 PltRel = R_386_JUMP_SLOT;
262 IRelativeRel = R_386_IRELATIVE;
263 RelativeRel = R_386_RELATIVE;
264 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000265 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
266 TlsOffsetRel = R_386_TLS_DTPOFF32;
267 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000268 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000269 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000270 TlsGdToLeSkip = 2;
271}
272
273RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
274 switch (Type) {
275 default:
276 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000277 case R_386_TLS_GD:
278 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000279 case R_386_TLS_LDM:
280 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000281 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000282 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000283 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000284 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000285 case R_386_GOTPC:
286 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000287 case R_386_TLS_IE:
288 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000289 case R_386_GOT32:
290 case R_386_TLS_GOTIE:
291 return R_GOT_FROM_END;
292 case R_386_GOTOFF:
293 return R_GOTREL;
294 case R_386_TLS_LE:
295 return R_TLS;
296 case R_386_TLS_LE_32:
297 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000298 }
George Rimar77b77792015-11-25 22:15:01 +0000299}
300
Rui Ueyamac516ae12016-01-29 02:33:45 +0000301void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000302 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
303}
304
Rui Ueyamac516ae12016-01-29 02:33:45 +0000305void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000306 // Entries in .got.plt initially points back to the corresponding
307 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000308 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000309}
Rafael Espindola01205f72015-09-22 18:19:46 +0000310
George Rimar98b060d2016-03-06 06:01:07 +0000311uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000312 if (Type == R_386_TLS_LE)
313 return R_386_TLS_TPOFF;
314 if (Type == R_386_TLS_LE_32)
315 return R_386_TLS_TPOFF32;
316 return Type;
317}
318
George Rimar98b060d2016-03-06 06:01:07 +0000319bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000320 return Type == R_386_TLS_GD;
321}
322
George Rimar98b060d2016-03-06 06:01:07 +0000323bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000324 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
325}
326
George Rimar98b060d2016-03-06 06:01:07 +0000327bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000328 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
329}
330
Rui Ueyama900e2d22016-01-29 03:51:49 +0000331void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000332 // Executable files and shared object files have
333 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000334 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000335 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000336 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000337 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
338 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000339 };
340 memcpy(Buf, V, sizeof(V));
341 return;
342 }
George Rimar648a2c32015-10-20 08:54:27 +0000343
George Rimar77b77792015-11-25 22:15:01 +0000344 const uint8_t PltData[] = {
345 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000346 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
347 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000348 };
349 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000350 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000351 write32le(Buf + 2, Got + 4);
352 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000353}
354
Rui Ueyama9398f862016-01-29 04:15:02 +0000355void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
356 uint64_t PltEntryAddr, int32_t Index,
357 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000358 const uint8_t Inst[] = {
359 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
360 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
361 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
362 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000363 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000364
George Rimar77b77792015-11-25 22:15:01 +0000365 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000366 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000367 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
368 : Out<ELF32LE>::Got->getVA();
369 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000370 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000371 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000372}
373
Rafael Espindola666625b2016-04-01 14:36:09 +0000374uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
375 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000376 switch (Type) {
377 default:
378 return 0;
379 case R_386_32:
380 case R_386_GOT32:
381 case R_386_GOTOFF:
382 case R_386_GOTPC:
383 case R_386_PC32:
384 case R_386_PLT32:
385 return read32le(Buf);
386 }
387}
388
Rafael Espindola22ef9562016-04-13 01:40:19 +0000389void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
390 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000391 checkInt<32>(Val, Type);
392 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000393}
394
Rafael Espindola22ef9562016-04-13 01:40:19 +0000395void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
396 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000397 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000398 // leal x@tlsgd(, %ebx, 1),
399 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000400 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000401 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000402 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000403 const uint8_t Inst[] = {
404 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
405 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
406 };
407 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000408 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000409}
410
Rafael Espindola22ef9562016-04-13 01:40:19 +0000411void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
412 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000413 // Convert
414 // leal x@tlsgd(, %ebx, 1),
415 // call __tls_get_addr@plt
416 // to
417 // movl %gs:0, %eax
418 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000419 const uint8_t Inst[] = {
420 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
421 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
422 };
423 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000424 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
425 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000426}
427
George Rimar6f17e092015-12-17 09:32:21 +0000428// In some conditions, relocations can be optimized to avoid using GOT.
429// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000430void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
431 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000432 // Ulrich's document section 6.2 says that @gotntpoff can
433 // be used with MOVL or ADDL instructions.
434 // @indntpoff is similar to @gotntpoff, but for use in
435 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000436 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000437 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000438 uint8_t Reg = (Loc[-1] >> 3) & 7;
439 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000440 if (Type == R_386_TLS_IE) {
441 // For R_386_TLS_IE relocation we perform the next transformations:
442 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
443 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
444 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
445 // First one is special because when EAX is used the sequence is 5 bytes
446 // long, otherwise it is 6 bytes.
447 if (*Op == 0xa1) {
448 *Op = 0xb8;
449 } else {
450 *Inst = IsMov ? 0xc7 : 0x81;
451 *Op = 0xc0 | ((*Op >> 3) & 7);
452 }
453 } else {
454 // R_386_TLS_GOTIE relocation can be optimized to
455 // R_386_TLS_LE so that it does not use GOT.
456 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
457 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
458 // Note: gold converts to ADDL instead of LEAL.
459 *Inst = IsMov ? 0xc7 : 0x8d;
460 if (IsMov)
461 *Op = 0xc0 | ((*Op >> 3) & 7);
462 else
463 *Op = 0x80 | Reg | (Reg << 3);
464 }
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000465 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000466}
467
Rafael Espindola22ef9562016-04-13 01:40:19 +0000468void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
469 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000470 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000471 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000472 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000473 }
474
Rui Ueyama55274e32016-04-23 01:10:15 +0000475 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000476 // leal foo(%reg),%eax
477 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000478 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000479 // movl %gs:0,%eax
480 // nop
481 // leal 0(%esi,1),%esi
482 const uint8_t Inst[] = {
483 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
484 0x90, // nop
485 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
486 };
487 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000488}
489
Rafael Espindola7f074422015-09-22 21:35:51 +0000490X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000491 CopyRel = R_X86_64_COPY;
492 GotRel = R_X86_64_GLOB_DAT;
493 PltRel = R_X86_64_JUMP_SLOT;
494 RelativeRel = R_X86_64_RELATIVE;
495 IRelativeRel = R_X86_64_IRELATIVE;
496 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000497 TlsModuleIndexRel = R_X86_64_DTPMOD64;
498 TlsOffsetRel = R_X86_64_DTPOFF64;
499 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000500 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000501 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000502 TlsGdToLeSkip = 2;
503}
504
505RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
506 switch (Type) {
507 default:
508 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000509 case R_X86_64_TPOFF32:
510 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000511 case R_X86_64_TLSLD:
512 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000513 case R_X86_64_TLSGD:
514 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000515 case R_X86_64_SIZE32:
516 case R_X86_64_SIZE64:
517 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000518 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000519 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000520 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000521 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000522 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000523 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000524 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000525 case R_X86_64_GOTPCREL:
Rafael Espindolaf350d252016-04-19 20:18:52 +0000526 case R_X86_64_GOTPCRELX:
527 case R_X86_64_REX_GOTPCRELX:
Rafael Espindola5628ee72016-04-15 19:14:18 +0000528 case R_X86_64_GOTTPOFF:
529 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000530 }
George Rimar648a2c32015-10-20 08:54:27 +0000531}
532
Rui Ueyamac516ae12016-01-29 02:33:45 +0000533void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000534 // The first entry holds the value of _DYNAMIC. It is not clear why that is
535 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000536 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000537 // other program).
Igor Kudrin351b41d2015-11-16 17:44:08 +0000538 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
539}
540
Rui Ueyamac516ae12016-01-29 02:33:45 +0000541void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000542 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000543 write32le(Buf, Plt + 6);
544}
545
Rui Ueyama900e2d22016-01-29 03:51:49 +0000546void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000547 const uint8_t PltData[] = {
548 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
549 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
550 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
551 };
552 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000553 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
554 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
555 write32le(Buf + 2, Got - Plt + 2); // GOT+8
556 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000557}
Rafael Espindola01205f72015-09-22 18:19:46 +0000558
Rui Ueyama9398f862016-01-29 04:15:02 +0000559void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
560 uint64_t PltEntryAddr, int32_t Index,
561 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000562 const uint8_t Inst[] = {
563 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
564 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
565 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
566 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000567 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000568
George Rimar648a2c32015-10-20 08:54:27 +0000569 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
570 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000571 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000572}
573
George Rimar86971052016-03-29 08:35:42 +0000574uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
575 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
576 if (Config->Shared)
577 error(getELFRelocationTypeName(EM_X86_64, Type) +
578 " cannot be a dynamic relocation");
579 return Type;
580}
581
George Rimar98b060d2016-03-06 06:01:07 +0000582bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000583 return Type == R_X86_64_GOTTPOFF;
584}
585
George Rimar98b060d2016-03-06 06:01:07 +0000586bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000587 return Type == R_X86_64_TLSGD;
588}
589
George Rimar98b060d2016-03-06 06:01:07 +0000590bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000591 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
592 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000593}
594
Rafael Espindola22ef9562016-04-13 01:40:19 +0000595void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
596 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000597 // Convert
598 // .byte 0x66
599 // leaq x@tlsgd(%rip), %rdi
600 // .word 0x6666
601 // rex64
602 // call __tls_get_addr@plt
603 // to
604 // mov %fs:0x0,%rax
605 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000606 const uint8_t Inst[] = {
607 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
608 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
609 };
610 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000611 relocateOne(Loc + 8, R_X86_64_TPOFF32,
612 Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000613}
614
Rafael Espindola22ef9562016-04-13 01:40:19 +0000615void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
616 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000617 // Convert
618 // .byte 0x66
619 // leaq x@tlsgd(%rip), %rdi
620 // .word 0x6666
621 // rex64
622 // call __tls_get_addr@plt
623 // to
624 // mov %fs:0x0,%rax
625 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000626 const uint8_t Inst[] = {
627 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
628 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
629 };
630 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000631 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000632}
633
George Rimar77d1cb12015-11-24 09:00:06 +0000634// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000635// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000636void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
637 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000638 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
639 // used in MOVQ or ADDQ instructions only.
640 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
641 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
642 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
643 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
644 uint8_t *Prefix = Loc - 3;
645 uint8_t *Inst = Loc - 2;
646 uint8_t *RegSlot = Loc - 1;
647 uint8_t Reg = Loc[-1] >> 3;
648 bool IsMov = *Inst == 0x8b;
649 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000650
George Rimar77d1cb12015-11-24 09:00:06 +0000651 // r12 and rsp registers requires special handling.
652 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
653 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
654 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
655 // The same true for rsp. So we convert to addq for them, saving 1 byte that
656 // we dont have.
657 if (RspAdd)
658 *Inst = 0x81;
659 else
660 *Inst = IsMov ? 0xc7 : 0x8d;
661 if (*Prefix == 0x4c)
662 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
663 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000664 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000665}
666
Rafael Espindola22ef9562016-04-13 01:40:19 +0000667void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
668 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000669 // Convert
670 // leaq bar@tlsld(%rip), %rdi
671 // callq __tls_get_addr@PLT
672 // leaq bar@dtpoff(%rax), %rcx
673 // to
674 // .word 0x6666
675 // .byte 0x66
676 // mov %fs:0,%rax
677 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000678 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000679 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
680 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000681 }
682 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindolaece62b92016-04-18 12:44:33 +0000683 relocateOne(Loc, R_X86_64_TPOFF32, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000684 return;
George Rimar25411f252015-12-04 11:20:13 +0000685 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000686
687 const uint8_t Inst[] = {
688 0x66, 0x66, //.word 0x6666
689 0x66, //.byte 0x66
690 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
691 };
692 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000693}
694
Rafael Espindola22ef9562016-04-13 01:40:19 +0000695void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
696 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000697 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000698 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000699 checkUInt<32>(Val, Type);
700 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000701 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000702 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000703 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000704 case R_X86_64_GOT32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000705 checkInt<32>(Val, Type);
706 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000707 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000708 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000709 case R_X86_64_DTPOFF64:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000710 case R_X86_64_SIZE64:
Rafael Espindola926bff82016-04-25 14:05:44 +0000711 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000712 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000713 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000714 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000715 case R_X86_64_GOTPCRELX:
716 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000717 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000718 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000719 case R_X86_64_PLT32:
720 case R_X86_64_TLSGD:
721 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000722 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000723 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000724 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000725 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000726 default:
George Rimar57610422016-03-11 14:43:02 +0000727 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000728 }
729}
730
Hal Finkel3c8cc672015-10-12 20:56:18 +0000731// Relocation masks following the #lo(value), #hi(value), #ha(value),
732// #higher(value), #highera(value), #highest(value), and #highesta(value)
733// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
734// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000735static uint16_t applyPPCLo(uint64_t V) { return V; }
736static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
737static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
738static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
739static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000740static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000741static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
742
Davide Italiano8c3444362016-01-11 19:45:33 +0000743PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000744
Rafael Espindola22ef9562016-04-13 01:40:19 +0000745void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
746 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000747 switch (Type) {
748 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000749 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000750 break;
751 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000752 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000753 break;
754 default:
George Rimar57610422016-03-11 14:43:02 +0000755 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000756 }
757}
758
Rafael Espindola22ef9562016-04-13 01:40:19 +0000759RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
760 return R_ABS;
761}
762
Rafael Espindolac4010882015-09-22 20:54:08 +0000763PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000764 GotRel = R_PPC64_GLOB_DAT;
765 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000766 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000767
768 // We need 64K pages (at least under glibc/Linux, the loader won't
769 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000770 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000771
772 // The PPC64 ELF ABI v1 spec, says:
773 //
774 // It is normally desirable to put segments with different characteristics
775 // in separate 256 Mbyte portions of the address space, to give the
776 // operating system full paging flexibility in the 64-bit address space.
777 //
778 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
779 // use 0x10000000 as the starting address.
780 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000781}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000782
Rafael Espindola15cec292016-04-27 12:25:22 +0000783static uint64_t PPC64TocOffset = 0x8000;
784
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000785uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +0000786 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
787 // TOC starts where the first of these sections starts. We always create a
788 // .got when we see a relocation that uses it, so for us the start is always
789 // the .got.
Hal Finkel3c8cc672015-10-12 20:56:18 +0000790 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +0000791
792 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
793 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
794 // code (crt1.o) assumes that you can get from the TOC base to the
795 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +0000796 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000797}
798
Rafael Espindola22ef9562016-04-13 01:40:19 +0000799RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
800 switch (Type) {
801 default:
802 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +0000803 case R_PPC64_TOC16:
804 case R_PPC64_TOC16_DS:
805 case R_PPC64_TOC16_HA:
806 case R_PPC64_TOC16_HI:
807 case R_PPC64_TOC16_LO:
808 case R_PPC64_TOC16_LO_DS:
809 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000810 case R_PPC64_TOC:
811 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000812 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000813 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000814 }
815}
816
Rui Ueyama9398f862016-01-29 04:15:02 +0000817void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
818 uint64_t PltEntryAddr, int32_t Index,
819 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000820 uint64_t Off = GotEntryAddr - getPPC64TocBase();
821
822 // FIXME: What we should do, in theory, is get the offset of the function
823 // descriptor in the .opd section, and use that as the offset from %r2 (the
824 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
825 // be a pointer to the function descriptor in the .opd section. Using
826 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
827
Hal Finkelfa92f682015-10-13 21:47:34 +0000828 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000829 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
830 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
831 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
832 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
833 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
834 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
835 write32be(Buf + 28, 0x4e800420); // bctr
836}
837
Rafael Espindola22ef9562016-04-13 01:40:19 +0000838void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
839 uint64_t Val) const {
Rafael Espindola15cec292016-04-27 12:25:22 +0000840 uint64_t TO = PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000841
Rafael Espindola15cec292016-04-27 12:25:22 +0000842 // For a TOC-relative relocation, proceed in terms of the corresponding
843 // ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000844 switch (Type) {
Rafael Espindola15cec292016-04-27 12:25:22 +0000845 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break;
846 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break;
847 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break;
848 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break;
849 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break;
850 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000851 default: break;
852 }
853
Hal Finkel3c8cc672015-10-12 20:56:18 +0000854 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000855 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000856 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000857 // Preserve the AA/LK bits in the branch instruction
858 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +0000859 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000860 break;
861 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000862 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000863 checkInt<16>(Val, Type);
864 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000865 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000866 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000867 checkInt<16>(Val, Type);
868 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000869 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000870 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000871 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000872 break;
873 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000874 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000875 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000876 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000877 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000878 break;
879 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000880 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000881 break;
882 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000883 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000884 break;
885 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000886 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000887 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000888 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000889 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000890 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000891 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000892 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000893 break;
894 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000895 checkInt<32>(Val, Type);
896 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000897 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000898 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000899 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000900 break;
901 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000902 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000903 break;
904 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000905 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000906 break;
907 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000908 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000909 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000910 case R_PPC64_REL24: {
911 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000912 checkInt<24>(Val, Type);
913 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000914 break;
915 }
916 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000917 checkInt<32>(Val, Type);
918 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000919 break;
920 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000921 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000922 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000923 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000924 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000925 break;
926 default:
George Rimar57610422016-03-11 14:43:02 +0000927 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000928 }
929}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000930
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000931AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000932 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +0000933 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +0000934 IRelativeRel = R_AARCH64_IRELATIVE;
935 GotRel = R_AARCH64_GLOB_DAT;
936 PltRel = R_AARCH64_JUMP_SLOT;
937 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000938 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
939 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000940 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000941 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000942 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000943}
George Rimar648a2c32015-10-20 08:54:27 +0000944
Rafael Espindola22ef9562016-04-13 01:40:19 +0000945RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
946 const SymbolBody &S) const {
947 switch (Type) {
948 default:
949 return R_ABS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000950 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +0000951 case R_AARCH64_CONDBR19:
952 case R_AARCH64_JUMP26:
953 case R_AARCH64_TSTBR14:
954 return R_PLT_PC;
955
Rafael Espindola22ef9562016-04-13 01:40:19 +0000956 case R_AARCH64_PREL16:
957 case R_AARCH64_PREL32:
958 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000959 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000960 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000961 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000962 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000963 case R_AARCH64_LD64_GOT_LO12_NC:
964 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
965 return R_GOT;
966 case R_AARCH64_ADR_GOT_PAGE:
967 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
968 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000969 }
970}
971
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000972bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +0000973 switch (Type) {
974 default:
975 return false;
Ed Schouten39aca422016-04-06 18:21:07 +0000976 case R_AARCH64_ADD_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +0000977 case R_AARCH64_LDST8_ABS_LO12_NC:
978 case R_AARCH64_LDST16_ABS_LO12_NC:
979 case R_AARCH64_LDST32_ABS_LO12_NC:
980 case R_AARCH64_LDST64_ABS_LO12_NC:
981 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola6eda85a2016-04-20 14:36:24 +0000982 case R_AARCH64_LD64_GOT_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +0000983 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +0000984 return true;
985 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +0000986}
Rafael Espindola435c00f2016-02-23 20:19:44 +0000987
George Rimar98b060d2016-03-06 06:01:07 +0000988bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000989 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
990 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
991 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
992 Type == R_AARCH64_TLSDESC_CALL;
993}
994
George Rimar98b060d2016-03-06 06:01:07 +0000995bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000996 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
997 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
998}
999
George Rimar98b060d2016-03-06 06:01:07 +00001000uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001001 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1002 return Type;
1003 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001004 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001005 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001006 // Keep it going with a dummy value so that we can find more reloc errors.
1007 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001008}
1009
Rui Ueyamac516ae12016-01-29 02:33:45 +00001010void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001011 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1012}
1013
Rafael Espindola22ef9562016-04-13 01:40:19 +00001014static uint64_t getAArch64Page(uint64_t Expr) {
1015 return Expr & (~static_cast<uint64_t>(0xFFF));
1016}
1017
Rui Ueyama900e2d22016-01-29 03:51:49 +00001018void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001019 const uint8_t PltData[] = {
1020 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1021 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1022 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1023 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1024 0x20, 0x02, 0x1f, 0xd6, // br x17
1025 0x1f, 0x20, 0x03, 0xd5, // nop
1026 0x1f, 0x20, 0x03, 0xd5, // nop
1027 0x1f, 0x20, 0x03, 0xd5 // nop
1028 };
1029 memcpy(Buf, PltData, sizeof(PltData));
1030
Rui Ueyama900e2d22016-01-29 03:51:49 +00001031 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1032 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001033 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1034 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1035 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1036 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001037}
1038
Rui Ueyama9398f862016-01-29 04:15:02 +00001039void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1040 uint64_t PltEntryAddr, int32_t Index,
1041 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001042 const uint8_t Inst[] = {
1043 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1044 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1045 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1046 0x20, 0x02, 0x1f, 0xd6 // br x17
1047 };
1048 memcpy(Buf, Inst, sizeof(Inst));
1049
Rafael Espindola22ef9562016-04-13 01:40:19 +00001050 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1051 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1052 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1053 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001054}
1055
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001056static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001057 uint32_t ImmLo = (Imm & 0x3) << 29;
1058 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1059 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001060 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001061}
1062
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001063static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1064 or32le(L, (Imm & 0xFFF) << 10);
1065}
1066
Rafael Espindola22ef9562016-04-13 01:40:19 +00001067void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1068 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001069 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001070 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001071 checkIntUInt<16>(Val, Type);
1072 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001073 break;
1074 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001075 checkIntUInt<32>(Val, Type);
1076 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001077 break;
1078 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001079 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001080 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001081 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001082 // This relocation stores 12 bits and there's no instruction
1083 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001084 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1085 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001086 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001087 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001088 case R_AARCH64_ADR_GOT_PAGE:
1089 checkInt<33>(Val, Type);
1090 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001091 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001092 case R_AARCH64_ADR_PREL_LO21:
1093 checkInt<21>(Val, Type);
1094 updateAArch64Addr(Loc, Val & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001095 break;
George Rimar3d737e42016-01-13 13:04:46 +00001096 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001097 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1098 checkInt<33>(Val, Type);
1099 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001100 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001101 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001102 case R_AARCH64_JUMP26:
1103 checkInt<28>(Val, Type);
1104 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001105 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001106 case R_AARCH64_CONDBR19:
1107 checkInt<21>(Val, Type);
1108 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001109 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001110 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001111 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001112 checkAlignment<8>(Val, Type);
1113 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001114 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001115 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001116 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001117 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001118 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001119 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001120 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001121 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001122 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001123 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001124 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001125 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001126 break;
1127 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001128 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001129 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001130 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001131 checkIntUInt<16>(Val, Type);
1132 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001133 break;
1134 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001135 checkIntUInt<32>(Val, Type);
1136 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001137 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001138 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001139 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001140 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001141 case R_AARCH64_TSTBR14:
1142 checkInt<16>(Val, Type);
1143 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001144 break;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001145 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001146 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001147 checkInt<24>(V, Type);
1148 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1149 break;
1150 }
1151 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001152 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001153 updateAArch64Add(Loc, V & 0xFFF);
1154 break;
1155 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001156 default:
George Rimar57610422016-03-11 14:43:02 +00001157 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001158 }
1159}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001160
Rafael Espindola22ef9562016-04-13 01:40:19 +00001161void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1162 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001163 // TLSDESC Global-Dynamic relocation are in the form:
1164 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1165 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1166 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1167 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1168 // And it can optimized to:
1169 // movz x0, #0x0, lsl #16
1170 // movk x0, #0x10
1171 // nop
1172 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001173 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001174 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001175 checkUInt<32>(X, Type);
1176
1177 uint32_t NewInst;
1178 switch (Type) {
1179 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1180 case R_AARCH64_TLSDESC_CALL:
1181 // nop
1182 NewInst = 0xd503201f;
1183 break;
1184 case R_AARCH64_TLSDESC_ADR_PAGE21:
1185 // movz
1186 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1187 break;
1188 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1189 // movk
1190 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1191 break;
1192 default:
George Rimar777f9632016-03-12 08:31:34 +00001193 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001194 }
1195 write32le(Loc, NewInst);
1196}
1197
Rafael Espindola22ef9562016-04-13 01:40:19 +00001198void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1199 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001200 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001201 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001202 checkUInt<32>(X, Type);
1203
George Rimar4d1d16d2016-03-06 06:16:05 +00001204 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001205 uint32_t NewInst;
1206 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1207 // Generate movz.
1208 unsigned RegNo = (Inst & 0x1f);
1209 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1210 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1211 // Generate movk
1212 unsigned RegNo = (Inst & 0x1f);
1213 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1214 } else {
George Rimar777f9632016-03-12 08:31:34 +00001215 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001216 }
1217 write32le(Loc, NewInst);
1218}
1219
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001220// Implementing relocations for AMDGPU is low priority since most
1221// programs don't use relocations now. Thus, this function is not
1222// actually called (relocateOne is called for each relocation).
1223// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001224void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1225 uint64_t Val) const {
1226 llvm_unreachable("not implemented");
1227}
1228
1229RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001230 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001231}
1232
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001233template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001234 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001235 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001236 PltEntrySize = 16;
1237 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001238 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001239 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001240 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001241 PltRel = R_MIPS_JUMP_SLOT;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001242 if (ELFT::Is64Bits)
1243 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1244 else
1245 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001246}
1247
1248template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001249RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1250 const SymbolBody &S) const {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001251 if (ELFT::Is64Bits)
1252 // See comment in the calculateMips64RelChain.
1253 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001254 switch (Type) {
1255 default:
1256 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001257 case R_MIPS_JALR:
1258 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001259 case R_MIPS_GPREL16:
1260 case R_MIPS_GPREL32:
1261 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001262 case R_MIPS_26:
1263 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001264 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001265 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001266 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001267 // MIPS _gp_disp designates offset between start of function and 'gp'
1268 // pointer into GOT. __gnu_local_gp is equal to the current value of
1269 // the 'gp'. Therefore any relocations against them do not require
1270 // dynamic relocation.
1271 if (&S == ElfSym<ELFT>::MipsGpDisp)
1272 return R_PC;
1273 return R_ABS;
1274 case R_MIPS_PC32:
1275 case R_MIPS_PC16:
1276 case R_MIPS_PC19_S2:
1277 case R_MIPS_PC21_S2:
1278 case R_MIPS_PC26_S2:
1279 case R_MIPS_PCHI16:
1280 case R_MIPS_PCLO16:
1281 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001282 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001283 if (S.isLocal())
1284 return R_MIPS_GOT_LOCAL;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001285 // fallthrough
1286 case R_MIPS_CALL16:
1287 case R_MIPS_GOT_DISP:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001288 if (!S.isPreemptible())
1289 return R_MIPS_GOT;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001290 return R_GOT_OFF;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001291 case R_MIPS_GOT_PAGE:
1292 return R_MIPS_GOT_LOCAL;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001293 }
1294}
1295
1296template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001297uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001298 if (Type == R_MIPS_32 || Type == R_MIPS_64)
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001299 return RelativeRel;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001300 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001301 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001302 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001303 // Keep it going with a dummy value so that we can find more reloc errors.
1304 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001305}
1306
1307template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001308void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1309 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001310}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001311
Simon Atanasyan35031192015-12-15 06:06:34 +00001312static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001313
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001314template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001315static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001316 uint32_t Instr = read32<E>(Loc);
1317 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1318 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1319}
1320
1321template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001322static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001323 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001324 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001325 if (SHIFT > 0)
1326 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001327 checkInt<BSIZE + SHIFT>(V, Type);
1328 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001329}
1330
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001331template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001332static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001333 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001334 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001335}
1336
Simon Atanasyan3b377852016-03-04 10:55:20 +00001337template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001338static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1339 uint32_t Instr = read32<E>(Loc);
1340 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1341}
1342
Rafael Espindola666625b2016-04-01 14:36:09 +00001343template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001344 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1345}
1346
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001347template <class ELFT>
1348void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1349 const endianness E = ELFT::TargetEndianness;
1350 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1351 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1352 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1353 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1354 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1355 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1356 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1357 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1358 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001359 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001360 writeMipsLo16<E>(Buf + 4, Got);
1361 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001362}
1363
1364template <class ELFT>
1365void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1366 uint64_t PltEntryAddr, int32_t Index,
1367 unsigned RelOff) const {
1368 const endianness E = ELFT::TargetEndianness;
1369 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1370 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1371 write32<E>(Buf + 8, 0x03200008); // jr $25
1372 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001373 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001374 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1375 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001376}
1377
1378template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001379void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1380 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1381 // See MipsTargetInfo::writeThunk for details.
1382 const endianness E = ELFT::TargetEndianness;
1383 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1384 write32<E>(Buf + 4, 0x08000000); // j func
1385 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1386 write32<E>(Buf + 12, 0x00000000); // nop
1387 writeMipsHi16<E>(Buf, S);
1388 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1389 writeMipsLo16<E>(Buf + 8, S);
1390}
1391
1392template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001393bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1394 const SymbolBody &S) const {
1395 // Any MIPS PIC code function is invoked with its address in register $t9.
1396 // So if we have a branch instruction from non-PIC code to the PIC one
1397 // we cannot make the jump directly and need to create a small stubs
1398 // to save the target function address.
1399 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1400 if (Type != R_MIPS_26)
1401 return false;
1402 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1403 if (!F)
1404 return false;
1405 // If current file has PIC code, LA25 stub is not required.
1406 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1407 return false;
1408 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1409 if (!D || !D->Section)
1410 return false;
1411 // LA25 is required if target file has PIC code
1412 // or target symbol is a PIC symbol.
1413 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001414 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001415}
1416
1417template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001418uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001419 uint32_t Type) const {
1420 const endianness E = ELFT::TargetEndianness;
1421 switch (Type) {
1422 default:
1423 return 0;
1424 case R_MIPS_32:
1425 case R_MIPS_GPREL32:
1426 return read32<E>(Buf);
1427 case R_MIPS_26:
1428 // FIXME (simon): If the relocation target symbol is not a PLT entry
1429 // we should use another expression for calculation:
1430 // ((A << 2) | (P & 0xf0000000)) >> 2
1431 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1432 case R_MIPS_GPREL16:
1433 case R_MIPS_LO16:
1434 case R_MIPS_PCLO16:
1435 case R_MIPS_TLS_DTPREL_HI16:
1436 case R_MIPS_TLS_DTPREL_LO16:
1437 case R_MIPS_TLS_TPREL_HI16:
1438 case R_MIPS_TLS_TPREL_LO16:
1439 return readSignedLo16<E>(Buf);
1440 case R_MIPS_PC16:
1441 return getPcRelocAddend<E, 16, 2>(Buf);
1442 case R_MIPS_PC19_S2:
1443 return getPcRelocAddend<E, 19, 2>(Buf);
1444 case R_MIPS_PC21_S2:
1445 return getPcRelocAddend<E, 21, 2>(Buf);
1446 case R_MIPS_PC26_S2:
1447 return getPcRelocAddend<E, 26, 2>(Buf);
1448 case R_MIPS_PC32:
1449 return getPcRelocAddend<E, 32, 0>(Buf);
1450 }
1451}
1452
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001453static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1454 uint64_t Val) {
1455 // MIPS N64 ABI packs multiple relocations into the single relocation
1456 // record. In general, all up to three relocations can have arbitrary
1457 // types. In fact, Clang and GCC uses only a few combinations. For now,
1458 // we support two of them. That is allow to pass at least all LLVM
1459 // test suite cases.
1460 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1461 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1462 // The first relocation is a 'real' relocation which is calculated
1463 // using the corresponding symbol's value. The second and the third
1464 // relocations used to modify result of the first one: extend it to
1465 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1466 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1467 uint32_t Type2 = (Type >> 8) & 0xff;
1468 uint32_t Type3 = (Type >> 16) & 0xff;
1469 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1470 return std::make_pair(Type, Val);
1471 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1472 return std::make_pair(Type2, Val);
1473 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1474 return std::make_pair(Type3, -Val);
1475 error("unsupported relocations combination " + Twine(Type));
1476 return std::make_pair(Type & 0xff, Val);
1477}
1478
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001479template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001480void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1481 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001482 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001483 // Thread pointer and DRP offsets from the start of TLS data area.
1484 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001485 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1486 Val -= 0x8000;
1487 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1488 Val -= 0x7000;
1489 if (ELFT::Is64Bits)
1490 std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001491 switch (Type) {
1492 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001493 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001494 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001495 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001496 case R_MIPS_64:
1497 write64<E>(Loc, Val);
1498 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00001499 case R_MIPS_26:
1500 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001501 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001502 case R_MIPS_GOT_DISP:
1503 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001504 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001505 case R_MIPS_GPREL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001506 checkInt<16>(Val, Type);
1507 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001508 case R_MIPS_CALL16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001509 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001510 case R_MIPS_LO16:
1511 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001512 case R_MIPS_TLS_DTPREL_LO16:
1513 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001514 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001515 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001516 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001517 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001518 case R_MIPS_TLS_DTPREL_HI16:
1519 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001520 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001521 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001522 case R_MIPS_JALR:
1523 // Ignore this optimization relocation for now
1524 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001525 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001526 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001527 break;
1528 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001529 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001530 break;
1531 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001532 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001533 break;
1534 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001535 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001536 break;
1537 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001538 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001539 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001540 default:
George Rimar57610422016-03-11 14:43:02 +00001541 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001542 }
1543}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001544
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001545template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001546bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00001547 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001548}
Rafael Espindola01205f72015-09-22 18:19:46 +00001549}
1550}