blob: b13989556ecfb9fafbfea575251124027eca5e89 [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
George Rimara8f9cf12016-05-26 13:37:12 +0000116 bool canRelaxGot(uint32_t Type, const uint8_t *Data) const override;
George Rimar5c33b912016-05-25 14:31:37 +0000117 void relaxGot(uint8_t *Loc, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000118 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
119 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
120 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
121 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000122};
123
Davide Italiano8c3444362016-01-11 19:45:33 +0000124class PPCTargetInfo final : public TargetInfo {
125public:
126 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000127 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000128 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000129};
130
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000131class PPC64TargetInfo final : public TargetInfo {
132public:
133 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000134 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000135 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
136 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000137 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000138};
139
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000140class AArch64TargetInfo final : public TargetInfo {
141public:
142 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000143 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000144 uint32_t getDynRel(uint32_t Type) const override;
145 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
146 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000147 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000148 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000149 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
150 int32_t Index, unsigned RelOff) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000151 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000152 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
153 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
154 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155};
156
Tom Stellard80efb162016-01-07 03:59:08 +0000157class AMDGPUTargetInfo final : public TargetInfo {
158public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000159 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000160 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
161 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000162};
163
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000164template <class ELFT> class MipsTargetInfo final : public TargetInfo {
165public:
166 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000167 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000168 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000169 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000170 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
171 void writePltZero(uint8_t *Buf) const override;
172 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
173 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000174 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000175 bool needsThunk(uint32_t Type, const InputFile &File,
176 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000177 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000178 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000179};
180} // anonymous namespace
181
Rui Ueyama91004392015-10-13 16:08:15 +0000182TargetInfo *createTarget() {
183 switch (Config->EMachine) {
184 case EM_386:
185 return new X86TargetInfo();
186 case EM_AARCH64:
187 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000188 case EM_AMDGPU:
189 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000190 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000191 switch (Config->EKind) {
192 case ELF32LEKind:
193 return new MipsTargetInfo<ELF32LE>();
194 case ELF32BEKind:
195 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000196 case ELF64LEKind:
197 return new MipsTargetInfo<ELF64LE>();
198 case ELF64BEKind:
199 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000200 default:
George Rimar777f9632016-03-12 08:31:34 +0000201 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000202 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000203 case EM_PPC:
204 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000205 case EM_PPC64:
206 return new PPC64TargetInfo();
207 case EM_X86_64:
208 return new X86_64TargetInfo();
209 }
George Rimar777f9632016-03-12 08:31:34 +0000210 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000211}
212
Rafael Espindola01205f72015-09-22 18:19:46 +0000213TargetInfo::~TargetInfo() {}
214
Rafael Espindola666625b2016-04-01 14:36:09 +0000215uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
216 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000217 return 0;
218}
219
George Rimar786e8662016-03-17 05:57:33 +0000220uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000221
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000222bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000223
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000224bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
225 const SymbolBody &S) const {
226 return false;
227}
228
George Rimar98b060d2016-03-06 06:01:07 +0000229bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000230
George Rimar98b060d2016-03-06 06:01:07 +0000231bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000232
George Rimar98b060d2016-03-06 06:01:07 +0000233bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000234 return false;
235}
236
George Rimara8f9cf12016-05-26 13:37:12 +0000237bool TargetInfo::canRelaxGot(uint32_t Type, const uint8_t *Data) const {
George Rimar5c33b912016-05-25 14:31:37 +0000238 return false;
239}
240
241void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
242 llvm_unreachable("Should not have claimed to be relaxable");
243}
244
Rafael Espindola22ef9562016-04-13 01:40:19 +0000245void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
246 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000247 llvm_unreachable("Should not have claimed to be relaxable");
248}
249
Rafael Espindola22ef9562016-04-13 01:40:19 +0000250void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
251 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000252 llvm_unreachable("Should not have claimed to be relaxable");
253}
254
Rafael Espindola22ef9562016-04-13 01:40:19 +0000255void TargetInfo::relaxTlsIeToLe(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::relaxTlsLdToLe(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");
George Rimar6713cf82015-11-25 21:46:05 +0000263}
George Rimar77d1cb12015-11-24 09:00:06 +0000264
Rafael Espindola7f074422015-09-22 21:35:51 +0000265X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000266 CopyRel = R_386_COPY;
267 GotRel = R_386_GLOB_DAT;
268 PltRel = R_386_JUMP_SLOT;
269 IRelativeRel = R_386_IRELATIVE;
270 RelativeRel = R_386_RELATIVE;
271 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000272 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
273 TlsOffsetRel = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000274 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000275 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000276 TlsGdToLeSkip = 2;
277}
278
279RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
280 switch (Type) {
281 default:
282 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000283 case R_386_TLS_GD:
284 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000285 case R_386_TLS_LDM:
286 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000287 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000288 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000289 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000290 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000291 case R_386_GOTPC:
292 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000293 case R_386_TLS_IE:
294 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000295 case R_386_GOT32:
296 case R_386_TLS_GOTIE:
297 return R_GOT_FROM_END;
298 case R_386_GOTOFF:
299 return R_GOTREL;
300 case R_386_TLS_LE:
301 return R_TLS;
302 case R_386_TLS_LE_32:
303 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000304 }
George Rimar77b77792015-11-25 22:15:01 +0000305}
306
Rui Ueyamac516ae12016-01-29 02:33:45 +0000307void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000308 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
309}
310
Rui Ueyamac516ae12016-01-29 02:33:45 +0000311void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000312 // Entries in .got.plt initially points back to the corresponding
313 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000314 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000315}
Rafael Espindola01205f72015-09-22 18:19:46 +0000316
George Rimar98b060d2016-03-06 06:01:07 +0000317uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000318 if (Type == R_386_TLS_LE)
319 return R_386_TLS_TPOFF;
320 if (Type == R_386_TLS_LE_32)
321 return R_386_TLS_TPOFF32;
322 return Type;
323}
324
George Rimar98b060d2016-03-06 06:01:07 +0000325bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000326 return Type == R_386_TLS_GD;
327}
328
George Rimar98b060d2016-03-06 06:01:07 +0000329bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000330 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
331}
332
George Rimar98b060d2016-03-06 06:01:07 +0000333bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000334 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
335}
336
Rui Ueyama900e2d22016-01-29 03:51:49 +0000337void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000338 // Executable files and shared object files have
339 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000340 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000341 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000342 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000343 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
344 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000345 };
346 memcpy(Buf, V, sizeof(V));
347 return;
348 }
George Rimar648a2c32015-10-20 08:54:27 +0000349
George Rimar77b77792015-11-25 22:15:01 +0000350 const uint8_t PltData[] = {
351 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000352 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
353 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000354 };
355 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000356 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000357 write32le(Buf + 2, Got + 4);
358 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000359}
360
Rui Ueyama9398f862016-01-29 04:15:02 +0000361void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
362 uint64_t PltEntryAddr, int32_t Index,
363 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000364 const uint8_t Inst[] = {
365 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
366 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
367 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
368 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000369 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000370
George Rimar77b77792015-11-25 22:15:01 +0000371 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000372 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rafael Espindolae2f43772016-05-18 20:44:24 +0000373 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyama9398f862016-01-29 04:15:02 +0000374 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000375 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000376 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000377}
378
Rafael Espindola666625b2016-04-01 14:36:09 +0000379uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
380 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000381 switch (Type) {
382 default:
383 return 0;
384 case R_386_32:
385 case R_386_GOT32:
386 case R_386_GOTOFF:
387 case R_386_GOTPC:
388 case R_386_PC32:
389 case R_386_PLT32:
390 return read32le(Buf);
391 }
392}
393
Rafael Espindola22ef9562016-04-13 01:40:19 +0000394void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
395 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000396 checkInt<32>(Val, Type);
397 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000398}
399
Rafael Espindola22ef9562016-04-13 01:40:19 +0000400void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
401 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000402 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000403 // leal x@tlsgd(, %ebx, 1),
404 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000405 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000406 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000407 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000408 const uint8_t Inst[] = {
409 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
410 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
411 };
412 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindolaebed1fe2016-05-20 21:23:52 +0000413 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000414}
415
Rafael Espindola22ef9562016-04-13 01:40:19 +0000416void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
417 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000418 // Convert
419 // leal x@tlsgd(, %ebx, 1),
420 // call __tls_get_addr@plt
421 // to
422 // movl %gs:0, %eax
423 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000424 const uint8_t Inst[] = {
425 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
426 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
427 };
428 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola74f3dbe2016-05-20 20:09:35 +0000429 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000430}
431
George Rimar6f17e092015-12-17 09:32:21 +0000432// In some conditions, relocations can be optimized to avoid using GOT.
433// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000434void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
435 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000436 // Ulrich's document section 6.2 says that @gotntpoff can
437 // be used with MOVL or ADDL instructions.
438 // @indntpoff is similar to @gotntpoff, but for use in
439 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000440 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000441 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000442 uint8_t Reg = (Loc[-1] >> 3) & 7;
443 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000444 if (Type == R_386_TLS_IE) {
445 // For R_386_TLS_IE relocation we perform the next transformations:
446 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
447 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
448 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
449 // First one is special because when EAX is used the sequence is 5 bytes
450 // long, otherwise it is 6 bytes.
451 if (*Op == 0xa1) {
452 *Op = 0xb8;
453 } else {
454 *Inst = IsMov ? 0xc7 : 0x81;
455 *Op = 0xc0 | ((*Op >> 3) & 7);
456 }
457 } else {
458 // R_386_TLS_GOTIE relocation can be optimized to
459 // R_386_TLS_LE so that it does not use GOT.
460 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
461 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
462 // Note: gold converts to ADDL instead of LEAL.
463 *Inst = IsMov ? 0xc7 : 0x8d;
464 if (IsMov)
465 *Op = 0xc0 | ((*Op >> 3) & 7);
466 else
467 *Op = 0x80 | Reg | (Reg << 3);
468 }
Rafael Espindola8818ca62016-05-20 17:41:09 +0000469 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000470}
471
Rafael Espindola22ef9562016-04-13 01:40:19 +0000472void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
473 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000474 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000475 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000476 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000477 }
478
Rui Ueyama55274e32016-04-23 01:10:15 +0000479 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000480 // leal foo(%reg),%eax
481 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000482 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000483 // movl %gs:0,%eax
484 // nop
485 // leal 0(%esi,1),%esi
486 const uint8_t Inst[] = {
487 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
488 0x90, // nop
489 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
490 };
491 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000492}
493
Rafael Espindola7f074422015-09-22 21:35:51 +0000494X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000495 CopyRel = R_X86_64_COPY;
496 GotRel = R_X86_64_GLOB_DAT;
497 PltRel = R_X86_64_JUMP_SLOT;
498 RelativeRel = R_X86_64_RELATIVE;
499 IRelativeRel = R_X86_64_IRELATIVE;
500 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000501 TlsModuleIndexRel = R_X86_64_DTPMOD64;
502 TlsOffsetRel = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000503 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000504 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000505 TlsGdToLeSkip = 2;
506}
507
508RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
509 switch (Type) {
510 default:
511 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000512 case R_X86_64_TPOFF32:
513 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000514 case R_X86_64_TLSLD:
515 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000516 case R_X86_64_TLSGD:
517 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000518 case R_X86_64_SIZE32:
519 case R_X86_64_SIZE64:
520 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000521 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000522 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000523 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000524 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000525 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000526 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000527 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000528 case R_X86_64_GOTPCREL:
Rafael Espindoladba64b82016-05-24 11:53:15 +0000529 case R_X86_64_GOTPCRELX:
530 case R_X86_64_REX_GOTPCRELX:
Rafael Espindolafe3a2f12016-05-24 12:12:06 +0000531 case R_X86_64_GOTTPOFF:
532 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000533 }
George Rimar648a2c32015-10-20 08:54:27 +0000534}
535
Rui Ueyamac516ae12016-01-29 02:33:45 +0000536void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000537 // The first entry holds the value of _DYNAMIC. It is not clear why that is
538 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000539 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000540 // other program).
Igor Kudrin351b41d2015-11-16 17:44:08 +0000541 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
542}
543
Rui Ueyamac516ae12016-01-29 02:33:45 +0000544void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000545 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000546 write32le(Buf, Plt + 6);
547}
548
Rui Ueyama900e2d22016-01-29 03:51:49 +0000549void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000550 const uint8_t PltData[] = {
551 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
552 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
553 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
554 };
555 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000556 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
557 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
558 write32le(Buf + 2, Got - Plt + 2); // GOT+8
559 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000560}
Rafael Espindola01205f72015-09-22 18:19:46 +0000561
Rui Ueyama9398f862016-01-29 04:15:02 +0000562void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
563 uint64_t PltEntryAddr, int32_t Index,
564 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000565 const uint8_t Inst[] = {
566 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
567 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
568 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
569 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000570 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000571
George Rimar648a2c32015-10-20 08:54:27 +0000572 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
573 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000574 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000575}
576
George Rimar86971052016-03-29 08:35:42 +0000577uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
578 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
579 if (Config->Shared)
580 error(getELFRelocationTypeName(EM_X86_64, Type) +
581 " cannot be a dynamic relocation");
582 return Type;
583}
584
George Rimar98b060d2016-03-06 06:01:07 +0000585bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000586 return Type == R_X86_64_GOTTPOFF;
587}
588
George Rimar98b060d2016-03-06 06:01:07 +0000589bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000590 return Type == R_X86_64_TLSGD;
591}
592
George Rimar98b060d2016-03-06 06:01:07 +0000593bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000594 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
595 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000596}
597
Rafael Espindola22ef9562016-04-13 01:40:19 +0000598void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
599 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000600 // Convert
601 // .byte 0x66
602 // leaq x@tlsgd(%rip), %rdi
603 // .word 0x6666
604 // rex64
605 // call __tls_get_addr@plt
606 // to
607 // mov %fs:0x0,%rax
608 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000609 const uint8_t Inst[] = {
610 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
611 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
612 };
613 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000614 // The original code used a pc relative relocation and so we have to
615 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000616 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000617}
618
Rafael Espindola22ef9562016-04-13 01:40:19 +0000619void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
620 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000621 // Convert
622 // .byte 0x66
623 // leaq x@tlsgd(%rip), %rdi
624 // .word 0x6666
625 // rex64
626 // call __tls_get_addr@plt
627 // to
628 // mov %fs:0x0,%rax
629 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000630 const uint8_t Inst[] = {
631 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
632 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
633 };
634 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000635 // Both code sequences are PC relatives, but since we are moving the constant
636 // forward by 8 bytes we have to subtract the value by 8.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000637 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000638}
639
George Rimar77d1cb12015-11-24 09:00:06 +0000640// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000641// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000642void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
643 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000644 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
645 // used in MOVQ or ADDQ instructions only.
646 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
647 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
648 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
649 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
650 uint8_t *Prefix = Loc - 3;
651 uint8_t *Inst = Loc - 2;
652 uint8_t *RegSlot = Loc - 1;
653 uint8_t Reg = Loc[-1] >> 3;
654 bool IsMov = *Inst == 0x8b;
655 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000656
George Rimar77d1cb12015-11-24 09:00:06 +0000657 // r12 and rsp registers requires special handling.
658 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
659 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
660 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
661 // The same true for rsp. So we convert to addq for them, saving 1 byte that
662 // we dont have.
663 if (RspAdd)
664 *Inst = 0x81;
665 else
666 *Inst = IsMov ? 0xc7 : 0x8d;
667 if (*Prefix == 0x4c)
668 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
669 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000670 // The original code used a pc relative relocation and so we have to
671 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000672 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000673}
674
Rafael Espindola22ef9562016-04-13 01:40:19 +0000675void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
676 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000677 // Convert
678 // leaq bar@tlsld(%rip), %rdi
679 // callq __tls_get_addr@PLT
680 // leaq bar@dtpoff(%rax), %rcx
681 // to
682 // .word 0x6666
683 // .byte 0x66
684 // mov %fs:0,%rax
685 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000686 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000687 write64le(Loc, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000688 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000689 }
690 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000691 relocateOne(Loc, R_X86_64_TPOFF32, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000692 return;
George Rimar25411f252015-12-04 11:20:13 +0000693 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000694
695 const uint8_t Inst[] = {
Rui Ueyama02fcf112016-05-25 04:29:53 +0000696 0x66, 0x66, // .word 0x6666
697 0x66, // .byte 0x66
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000698 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
699 };
700 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000701}
702
Rafael Espindola22ef9562016-04-13 01:40:19 +0000703void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
704 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000705 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000706 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000707 checkUInt<32>(Val, Type);
708 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000709 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000710 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000711 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000712 case R_X86_64_GOT32:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000713 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000714 case R_X86_64_GOTPCRELX:
715 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000716 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000717 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000718 case R_X86_64_PLT32:
719 case R_X86_64_TLSGD:
720 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000721 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000722 case R_X86_64_SIZE32:
Rafael Espindolafb0ceb52016-05-20 20:02:27 +0000723 checkInt<32>(Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000724 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000725 break;
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000726 case R_X86_64_64:
727 case R_X86_64_DTPOFF64:
728 case R_X86_64_SIZE64:
729 case R_X86_64_PC64:
730 write64le(Loc, Val);
731 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000732 default:
George Rimar57610422016-03-11 14:43:02 +0000733 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000734 }
735}
736
George Rimara8f9cf12016-05-26 13:37:12 +0000737bool X86_64TargetInfo::canRelaxGot(uint32_t Type, const uint8_t *Data) const {
George Rimar5c33b912016-05-25 14:31:37 +0000738 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
739 return false;
George Rimara8f9cf12016-05-26 13:37:12 +0000740 const uint8_t Op = Data[-2];
741 const uint8_t ModRm = Data[-1];
George Rimar95433df2016-05-25 16:51:08 +0000742 // Relax mov.
743 if (Op == 0x8b)
744 return true;
745 // Relax call and jmp.
746 return Op == 0xff && (ModRm == 0x15 || ModRm == 0x25);
George Rimar5c33b912016-05-25 14:31:37 +0000747}
748
749void X86_64TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
George Rimar95433df2016-05-25 16:51:08 +0000750 const uint8_t Op = Loc[-2];
751 const uint8_t ModRm = Loc[-1];
752
753 // Convert mov foo@GOTPCREL(%rip), %reg to lea foo(%rip), %reg.
754 if (Op == 0x8b) {
755 *(Loc - 2) = 0x8d;
756 relocateOne(Loc, R_X86_64_PC32, Val);
757 return;
758 }
759
760 assert(Op == 0xff);
761 if (ModRm == 0x15) {
762 // ABI says we can convert call *foo@GOTPCREL(%rip) to nop call foo.
763 // Instead we convert to addr32 call foo, where addr32 is instruction
764 // prefix. That makes result expression to be a single instruction.
765 *(Loc - 2) = 0x67; // addr32 prefix
766 *(Loc - 1) = 0xe8; // call
767 } else {
768 assert(ModRm == 0x25);
769 // Convert jmp *foo@GOTPCREL(%rip) to jmp foo nop.
770 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
771 *(Loc - 2) = 0xe9; // jmp
772 *(Loc + 3) = 0x90; // nop
773 Loc -= 1;
774 Val += 1;
775 }
George Rimar5c33b912016-05-25 14:31:37 +0000776 relocateOne(Loc, R_X86_64_PC32, Val);
777}
778
Hal Finkel3c8cc672015-10-12 20:56:18 +0000779// Relocation masks following the #lo(value), #hi(value), #ha(value),
780// #higher(value), #highera(value), #highest(value), and #highesta(value)
781// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
782// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000783static uint16_t applyPPCLo(uint64_t V) { return V; }
784static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
785static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
786static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
787static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000788static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000789static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
790
Davide Italiano8c3444362016-01-11 19:45:33 +0000791PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000792
Rafael Espindola22ef9562016-04-13 01:40:19 +0000793void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
794 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000795 switch (Type) {
796 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000797 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000798 break;
799 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000800 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000801 break;
802 default:
George Rimar57610422016-03-11 14:43:02 +0000803 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000804 }
805}
806
Rafael Espindola22ef9562016-04-13 01:40:19 +0000807RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
808 return R_ABS;
809}
810
Rafael Espindolac4010882015-09-22 20:54:08 +0000811PPC64TargetInfo::PPC64TargetInfo() {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000812 PltRel = GotRel = R_PPC64_GLOB_DAT;
Rui Ueyama724d6252016-01-29 01:49:32 +0000813 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000814 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000815
816 // We need 64K pages (at least under glibc/Linux, the loader won't
817 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000818 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000819
820 // The PPC64 ELF ABI v1 spec, says:
821 //
822 // It is normally desirable to put segments with different characteristics
823 // in separate 256 Mbyte portions of the address space, to give the
824 // operating system full paging flexibility in the 64-bit address space.
825 //
826 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
827 // use 0x10000000 as the starting address.
828 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000829}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000830
Rafael Espindola15cec292016-04-27 12:25:22 +0000831static uint64_t PPC64TocOffset = 0x8000;
832
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000833uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +0000834 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
835 // TOC starts where the first of these sections starts. We always create a
836 // .got when we see a relocation that uses it, so for us the start is always
837 // the .got.
Hal Finkel3c8cc672015-10-12 20:56:18 +0000838 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +0000839
840 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
841 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
842 // code (crt1.o) assumes that you can get from the TOC base to the
843 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +0000844 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000845}
846
Rafael Espindola22ef9562016-04-13 01:40:19 +0000847RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
848 switch (Type) {
849 default:
850 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +0000851 case R_PPC64_TOC16:
852 case R_PPC64_TOC16_DS:
853 case R_PPC64_TOC16_HA:
854 case R_PPC64_TOC16_HI:
855 case R_PPC64_TOC16_LO:
856 case R_PPC64_TOC16_LO_DS:
857 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000858 case R_PPC64_TOC:
859 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000860 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000861 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000862 }
863}
864
Rui Ueyama9398f862016-01-29 04:15:02 +0000865void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
866 uint64_t PltEntryAddr, int32_t Index,
867 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000868 uint64_t Off = GotEntryAddr - getPPC64TocBase();
869
870 // FIXME: What we should do, in theory, is get the offset of the function
871 // descriptor in the .opd section, and use that as the offset from %r2 (the
872 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
873 // be a pointer to the function descriptor in the .opd section. Using
874 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
875
Hal Finkelfa92f682015-10-13 21:47:34 +0000876 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000877 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
878 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
879 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
880 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
881 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
882 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
883 write32be(Buf + 28, 0x4e800420); // bctr
884}
885
Rafael Espindola22ef9562016-04-13 01:40:19 +0000886void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
887 uint64_t Val) const {
Rafael Espindola15cec292016-04-27 12:25:22 +0000888 uint64_t TO = PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000889
Rafael Espindola15cec292016-04-27 12:25:22 +0000890 // For a TOC-relative relocation, proceed in terms of the corresponding
891 // ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000892 switch (Type) {
Rafael Espindola15cec292016-04-27 12:25:22 +0000893 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break;
894 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break;
895 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break;
896 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break;
897 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break;
898 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000899 default: break;
900 }
901
Hal Finkel3c8cc672015-10-12 20:56:18 +0000902 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000903 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000904 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000905 // Preserve the AA/LK bits in the branch instruction
906 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +0000907 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000908 break;
909 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000910 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000911 checkInt<16>(Val, Type);
912 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000913 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000914 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000915 checkInt<16>(Val, Type);
916 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000917 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000918 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000919 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000920 break;
921 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000922 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000923 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000924 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000925 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000926 break;
927 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000928 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000929 break;
930 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000931 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000932 break;
933 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000934 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000935 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000936 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000937 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000938 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000939 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000940 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000941 break;
942 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000943 checkInt<32>(Val, Type);
944 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000945 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000946 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000947 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000948 break;
949 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000950 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000951 break;
952 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000953 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000954 break;
955 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000956 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000957 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000958 case R_PPC64_REL24: {
959 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000960 checkInt<24>(Val, Type);
961 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000962 break;
963 }
964 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000965 checkInt<32>(Val, Type);
966 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000967 break;
968 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000969 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000970 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000971 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000972 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000973 break;
974 default:
George Rimar57610422016-03-11 14:43:02 +0000975 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000976 }
977}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000978
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000979AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000980 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +0000981 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +0000982 IRelativeRel = R_AARCH64_IRELATIVE;
983 GotRel = R_AARCH64_GLOB_DAT;
984 PltRel = R_AARCH64_JUMP_SLOT;
985 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000986 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
987 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000988 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000989 PltZeroSize = 32;
Rafael Espindola8818ca62016-05-20 17:41:09 +0000990
991 // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
992 // 1 of the tls structures and the tcb size is 16.
993 TcbSize = 16;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000994}
George Rimar648a2c32015-10-20 08:54:27 +0000995
Rafael Espindola22ef9562016-04-13 01:40:19 +0000996RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
997 const SymbolBody &S) const {
998 switch (Type) {
999 default:
1000 return R_ABS;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001001
1002 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1003 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1004 return R_TLS;
1005
Rafael Espindola22ef9562016-04-13 01:40:19 +00001006 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +00001007 case R_AARCH64_CONDBR19:
1008 case R_AARCH64_JUMP26:
1009 case R_AARCH64_TSTBR14:
1010 return R_PLT_PC;
1011
Rafael Espindola22ef9562016-04-13 01:40:19 +00001012 case R_AARCH64_PREL16:
1013 case R_AARCH64_PREL32:
1014 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001015 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001016 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001017 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001018 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001019 case R_AARCH64_LD64_GOT_LO12_NC:
1020 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1021 return R_GOT;
1022 case R_AARCH64_ADR_GOT_PAGE:
1023 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1024 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001025 }
1026}
1027
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001028bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001029 switch (Type) {
1030 default:
1031 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001032 case R_AARCH64_ADD_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001033 case R_AARCH64_LDST8_ABS_LO12_NC:
1034 case R_AARCH64_LDST16_ABS_LO12_NC:
1035 case R_AARCH64_LDST32_ABS_LO12_NC:
1036 case R_AARCH64_LDST64_ABS_LO12_NC:
1037 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola6eda85a2016-04-20 14:36:24 +00001038 case R_AARCH64_LD64_GOT_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +00001039 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001040 return true;
1041 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001042}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001043
George Rimar98b060d2016-03-06 06:01:07 +00001044bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001045 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1046 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1047 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1048 Type == R_AARCH64_TLSDESC_CALL;
1049}
1050
George Rimar98b060d2016-03-06 06:01:07 +00001051bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001052 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1053 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1054}
1055
George Rimar98b060d2016-03-06 06:01:07 +00001056uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001057 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1058 return Type;
1059 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001060 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001061 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001062 // Keep it going with a dummy value so that we can find more reloc errors.
1063 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001064}
1065
Rui Ueyamac516ae12016-01-29 02:33:45 +00001066void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001067 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1068}
1069
Rafael Espindola22ef9562016-04-13 01:40:19 +00001070static uint64_t getAArch64Page(uint64_t Expr) {
1071 return Expr & (~static_cast<uint64_t>(0xFFF));
1072}
1073
Rui Ueyama900e2d22016-01-29 03:51:49 +00001074void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001075 const uint8_t PltData[] = {
1076 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1077 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1078 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1079 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1080 0x20, 0x02, 0x1f, 0xd6, // br x17
1081 0x1f, 0x20, 0x03, 0xd5, // nop
1082 0x1f, 0x20, 0x03, 0xd5, // nop
1083 0x1f, 0x20, 0x03, 0xd5 // nop
1084 };
1085 memcpy(Buf, PltData, sizeof(PltData));
1086
Rui Ueyama900e2d22016-01-29 03:51:49 +00001087 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1088 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001089 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1090 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1091 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1092 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001093}
1094
Rui Ueyama9398f862016-01-29 04:15:02 +00001095void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1096 uint64_t PltEntryAddr, int32_t Index,
1097 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001098 const uint8_t Inst[] = {
1099 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1100 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1101 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1102 0x20, 0x02, 0x1f, 0xd6 // br x17
1103 };
1104 memcpy(Buf, Inst, sizeof(Inst));
1105
Rafael Espindola22ef9562016-04-13 01:40:19 +00001106 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1107 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1108 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1109 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001110}
1111
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001112static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001113 uint32_t ImmLo = (Imm & 0x3) << 29;
1114 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1115 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001116 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001117}
1118
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001119static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1120 or32le(L, (Imm & 0xFFF) << 10);
1121}
1122
Rafael Espindola22ef9562016-04-13 01:40:19 +00001123void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1124 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001125 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001126 case R_AARCH64_ABS16:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001127 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001128 checkIntUInt<16>(Val, Type);
1129 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001130 break;
1131 case R_AARCH64_ABS32:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001132 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001133 checkIntUInt<32>(Val, Type);
1134 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001135 break;
1136 case R_AARCH64_ABS64:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001137 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001138 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001139 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001140 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001141 // This relocation stores 12 bits and there's no instruction
1142 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001143 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1144 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001145 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001146 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001147 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001148 case R_AARCH64_ADR_PREL_PG_HI21:
1149 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001150 checkInt<33>(Val, Type);
1151 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001152 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001153 case R_AARCH64_ADR_PREL_LO21:
1154 checkInt<21>(Val, Type);
1155 updateAArch64Addr(Loc, Val & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001156 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001157 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001158 case R_AARCH64_JUMP26:
1159 checkInt<28>(Val, Type);
1160 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001161 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001162 case R_AARCH64_CONDBR19:
1163 checkInt<21>(Val, Type);
1164 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001165 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001166 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001167 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001168 checkAlignment<8>(Val, Type);
1169 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001170 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001171 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001172 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001173 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001174 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001175 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001176 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001177 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001178 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001179 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001180 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001181 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001182 break;
1183 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001184 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001185 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001186 case R_AARCH64_TSTBR14:
1187 checkInt<16>(Val, Type);
1188 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001189 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001190 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1191 checkInt<24>(Val, Type);
1192 updateAArch64Add(Loc, (Val & 0xFFF000) >> 12);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001193 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001194 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1195 updateAArch64Add(Loc, Val & 0xFFF);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001196 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001197 default:
George Rimar57610422016-03-11 14:43:02 +00001198 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001199 }
1200}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001201
Rafael Espindola22ef9562016-04-13 01:40:19 +00001202void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1203 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001204 // TLSDESC Global-Dynamic relocation are in the form:
1205 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1206 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1207 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1208 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1209 // And it can optimized to:
1210 // movz x0, #0x0, lsl #16
1211 // movk x0, #0x10
1212 // nop
1213 // nop
Rafael Espindola8818ca62016-05-20 17:41:09 +00001214 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001215
1216 uint32_t NewInst;
1217 switch (Type) {
1218 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1219 case R_AARCH64_TLSDESC_CALL:
1220 // nop
1221 NewInst = 0xd503201f;
1222 break;
1223 case R_AARCH64_TLSDESC_ADR_PAGE21:
1224 // movz
Rafael Espindola8818ca62016-05-20 17:41:09 +00001225 NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001226 break;
1227 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1228 // movk
Rafael Espindola8818ca62016-05-20 17:41:09 +00001229 NewInst = 0xf2800000 | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001230 break;
1231 default:
George Rimar777f9632016-03-12 08:31:34 +00001232 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001233 }
1234 write32le(Loc, NewInst);
1235}
1236
Rafael Espindola22ef9562016-04-13 01:40:19 +00001237void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1238 uint64_t Val) const {
Rafael Espindola8818ca62016-05-20 17:41:09 +00001239 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001240
George Rimar4d1d16d2016-03-06 06:16:05 +00001241 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001242 uint32_t NewInst;
1243 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1244 // Generate movz.
1245 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001246 NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001247 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1248 // Generate movk
1249 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001250 NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001251 } else {
George Rimar777f9632016-03-12 08:31:34 +00001252 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001253 }
1254 write32le(Loc, NewInst);
1255}
1256
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001257// Implementing relocations for AMDGPU is low priority since most
1258// programs don't use relocations now. Thus, this function is not
1259// actually called (relocateOne is called for each relocation).
1260// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001261void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1262 uint64_t Val) const {
1263 llvm_unreachable("not implemented");
1264}
1265
1266RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001267 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001268}
1269
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001270template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001271 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001272 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001273 PltEntrySize = 16;
1274 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001275 ThunkSize = 16;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001276 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001277 PltRel = R_MIPS_JUMP_SLOT;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001278 if (ELFT::Is64Bits)
1279 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1280 else
1281 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001282}
1283
1284template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001285RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1286 const SymbolBody &S) const {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001287 if (ELFT::Is64Bits)
1288 // See comment in the calculateMips64RelChain.
1289 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001290 switch (Type) {
1291 default:
1292 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001293 case R_MIPS_JALR:
1294 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001295 case R_MIPS_GPREL16:
1296 case R_MIPS_GPREL32:
1297 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001298 case R_MIPS_26:
1299 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001300 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001301 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001302 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001303 // MIPS _gp_disp designates offset between start of function and 'gp'
1304 // pointer into GOT. __gnu_local_gp is equal to the current value of
1305 // the 'gp'. Therefore any relocations against them do not require
1306 // dynamic relocation.
1307 if (&S == ElfSym<ELFT>::MipsGpDisp)
1308 return R_PC;
1309 return R_ABS;
1310 case R_MIPS_PC32:
1311 case R_MIPS_PC16:
1312 case R_MIPS_PC19_S2:
1313 case R_MIPS_PC21_S2:
1314 case R_MIPS_PC26_S2:
1315 case R_MIPS_PCHI16:
1316 case R_MIPS_PCLO16:
1317 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001318 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001319 if (S.isLocal())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001320 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001321 // fallthrough
1322 case R_MIPS_CALL16:
1323 case R_MIPS_GOT_DISP:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001324 if (!S.isPreemptible())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001325 return R_MIPS_GOT_LOCAL;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001326 return R_GOT_OFF;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001327 case R_MIPS_GOT_PAGE:
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001328 return R_MIPS_GOT_LOCAL_PAGE;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001329 }
1330}
1331
1332template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001333uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001334 if (Type == R_MIPS_32 || Type == R_MIPS_64)
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001335 return RelativeRel;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001336 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001337 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001338 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001339 // Keep it going with a dummy value so that we can find more reloc errors.
1340 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001341}
1342
1343template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001344void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1345 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001346}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001347
Simon Atanasyan35031192015-12-15 06:06:34 +00001348static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001349
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001350template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001351static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001352 uint32_t Instr = read32<E>(Loc);
1353 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1354 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1355}
1356
1357template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001358static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001359 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001360 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001361 if (SHIFT > 0)
1362 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001363 checkInt<BSIZE + SHIFT>(V, Type);
1364 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001365}
1366
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001367template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001368static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001369 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001370 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001371}
1372
Simon Atanasyan3b377852016-03-04 10:55:20 +00001373template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001374static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1375 uint32_t Instr = read32<E>(Loc);
1376 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1377}
1378
Rafael Espindola666625b2016-04-01 14:36:09 +00001379template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001380 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1381}
1382
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001383template <class ELFT>
1384void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1385 const endianness E = ELFT::TargetEndianness;
1386 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1387 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1388 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1389 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1390 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1391 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1392 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1393 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1394 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001395 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001396 writeMipsLo16<E>(Buf + 4, Got);
1397 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001398}
1399
1400template <class ELFT>
1401void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1402 uint64_t PltEntryAddr, int32_t Index,
1403 unsigned RelOff) const {
1404 const endianness E = ELFT::TargetEndianness;
1405 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1406 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1407 write32<E>(Buf + 8, 0x03200008); // jr $25
1408 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001409 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001410 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1411 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001412}
1413
1414template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001415void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1416 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1417 // See MipsTargetInfo::writeThunk for details.
1418 const endianness E = ELFT::TargetEndianness;
1419 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1420 write32<E>(Buf + 4, 0x08000000); // j func
1421 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1422 write32<E>(Buf + 12, 0x00000000); // nop
1423 writeMipsHi16<E>(Buf, S);
1424 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1425 writeMipsLo16<E>(Buf + 8, S);
1426}
1427
1428template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001429bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1430 const SymbolBody &S) const {
1431 // Any MIPS PIC code function is invoked with its address in register $t9.
1432 // So if we have a branch instruction from non-PIC code to the PIC one
1433 // we cannot make the jump directly and need to create a small stubs
1434 // to save the target function address.
1435 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1436 if (Type != R_MIPS_26)
1437 return false;
1438 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1439 if (!F)
1440 return false;
1441 // If current file has PIC code, LA25 stub is not required.
1442 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1443 return false;
1444 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1445 if (!D || !D->Section)
1446 return false;
1447 // LA25 is required if target file has PIC code
1448 // or target symbol is a PIC symbol.
1449 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001450 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001451}
1452
1453template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001454uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001455 uint32_t Type) const {
1456 const endianness E = ELFT::TargetEndianness;
1457 switch (Type) {
1458 default:
1459 return 0;
1460 case R_MIPS_32:
1461 case R_MIPS_GPREL32:
1462 return read32<E>(Buf);
1463 case R_MIPS_26:
1464 // FIXME (simon): If the relocation target symbol is not a PLT entry
1465 // we should use another expression for calculation:
1466 // ((A << 2) | (P & 0xf0000000)) >> 2
1467 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1468 case R_MIPS_GPREL16:
1469 case R_MIPS_LO16:
1470 case R_MIPS_PCLO16:
1471 case R_MIPS_TLS_DTPREL_HI16:
1472 case R_MIPS_TLS_DTPREL_LO16:
1473 case R_MIPS_TLS_TPREL_HI16:
1474 case R_MIPS_TLS_TPREL_LO16:
1475 return readSignedLo16<E>(Buf);
1476 case R_MIPS_PC16:
1477 return getPcRelocAddend<E, 16, 2>(Buf);
1478 case R_MIPS_PC19_S2:
1479 return getPcRelocAddend<E, 19, 2>(Buf);
1480 case R_MIPS_PC21_S2:
1481 return getPcRelocAddend<E, 21, 2>(Buf);
1482 case R_MIPS_PC26_S2:
1483 return getPcRelocAddend<E, 26, 2>(Buf);
1484 case R_MIPS_PC32:
1485 return getPcRelocAddend<E, 32, 0>(Buf);
1486 }
1487}
1488
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001489static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1490 uint64_t Val) {
1491 // MIPS N64 ABI packs multiple relocations into the single relocation
1492 // record. In general, all up to three relocations can have arbitrary
1493 // types. In fact, Clang and GCC uses only a few combinations. For now,
1494 // we support two of them. That is allow to pass at least all LLVM
1495 // test suite cases.
1496 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1497 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1498 // The first relocation is a 'real' relocation which is calculated
1499 // using the corresponding symbol's value. The second and the third
1500 // relocations used to modify result of the first one: extend it to
1501 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1502 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1503 uint32_t Type2 = (Type >> 8) & 0xff;
1504 uint32_t Type3 = (Type >> 16) & 0xff;
1505 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1506 return std::make_pair(Type, Val);
1507 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1508 return std::make_pair(Type2, Val);
1509 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1510 return std::make_pair(Type3, -Val);
1511 error("unsupported relocations combination " + Twine(Type));
1512 return std::make_pair(Type & 0xff, Val);
1513}
1514
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001515template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001516void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1517 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001518 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001519 // Thread pointer and DRP offsets from the start of TLS data area.
1520 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001521 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1522 Val -= 0x8000;
1523 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1524 Val -= 0x7000;
1525 if (ELFT::Is64Bits)
1526 std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001527 switch (Type) {
1528 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001529 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001530 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001531 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001532 case R_MIPS_64:
1533 write64<E>(Loc, Val);
1534 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00001535 case R_MIPS_26:
1536 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001537 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001538 case R_MIPS_GOT_DISP:
1539 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001540 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001541 case R_MIPS_GPREL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001542 checkInt<16>(Val, Type);
1543 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001544 case R_MIPS_CALL16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001545 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001546 case R_MIPS_LO16:
1547 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001548 case R_MIPS_TLS_DTPREL_LO16:
1549 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001550 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001551 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001552 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001553 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001554 case R_MIPS_TLS_DTPREL_HI16:
1555 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001556 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001557 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001558 case R_MIPS_JALR:
1559 // Ignore this optimization relocation for now
1560 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001561 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001562 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001563 break;
1564 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001565 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001566 break;
1567 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001568 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001569 break;
1570 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001571 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001572 break;
1573 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001574 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001575 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001576 default:
George Rimar57610422016-03-11 14:43:02 +00001577 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001578 }
1579}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001580
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001581template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001582bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00001583 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001584}
Rafael Espindola01205f72015-09-22 18:19:46 +00001585}
1586}