blob: dbc164a958e35dc2bcdaddcfa10b4b779d235452 [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;
86 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000087 bool isTlsLocalDynamicRel(uint32_t Type) const override;
88 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
89 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000090 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000091 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000092 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
93 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000094 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000095 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000096
Rafael Espindola22ef9562016-04-13 01:40:19 +000097 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
98 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
99 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
100 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000101};
102
103class X86_64TargetInfo final : public TargetInfo {
104public:
105 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000106 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000107 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000108 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000109 bool isTlsLocalDynamicRel(uint32_t Type) const override;
110 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
111 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000112 void writeGotPltHeader(uint8_t *Buf) const override;
113 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000114 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000115 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
116 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000117 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000118 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000119
Rafael Espindola22ef9562016-04-13 01:40:19 +0000120 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
121 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
122 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
123 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000124};
125
Davide Italiano8c3444362016-01-11 19:45:33 +0000126class PPCTargetInfo final : public TargetInfo {
127public:
128 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000129 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000130 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000131 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000132};
133
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134class PPC64TargetInfo final : public TargetInfo {
135public:
136 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000137 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000138 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
139 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000140 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000141 bool isRelRelative(uint32_t Type) const override;
142};
143
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000144class AArch64TargetInfo final : public TargetInfo {
145public:
146 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000147 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000148 uint32_t getDynRel(uint32_t Type) const override;
149 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
150 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000151 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000152 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000153 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
154 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000155 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000156 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000157 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
158 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
159 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000160
161private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000162 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000163};
164
Tom Stellard80efb162016-01-07 03:59:08 +0000165class AMDGPUTargetInfo final : public TargetInfo {
166public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000167 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000168 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
169 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000170};
171
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172template <class ELFT> class MipsTargetInfo final : public TargetInfo {
173public:
174 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000175 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000176 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000177 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000178 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
179 void writePltZero(uint8_t *Buf) const override;
180 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
181 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000182 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000183 bool needsThunk(uint32_t Type, const InputFile &File,
184 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000185 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000186 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000187 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000188};
189} // anonymous namespace
190
Rui Ueyama91004392015-10-13 16:08:15 +0000191TargetInfo *createTarget() {
192 switch (Config->EMachine) {
193 case EM_386:
194 return new X86TargetInfo();
195 case EM_AARCH64:
196 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000197 case EM_AMDGPU:
198 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000199 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000200 switch (Config->EKind) {
201 case ELF32LEKind:
202 return new MipsTargetInfo<ELF32LE>();
203 case ELF32BEKind:
204 return new MipsTargetInfo<ELF32BE>();
205 default:
George Rimar777f9632016-03-12 08:31:34 +0000206 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000207 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000208 case EM_PPC:
209 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000210 case EM_PPC64:
211 return new PPC64TargetInfo();
212 case EM_X86_64:
213 return new X86_64TargetInfo();
214 }
George Rimar777f9632016-03-12 08:31:34 +0000215 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000216}
217
Rafael Espindola01205f72015-09-22 18:19:46 +0000218TargetInfo::~TargetInfo() {}
219
Rafael Espindola666625b2016-04-01 14:36:09 +0000220uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
221 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000222 return 0;
223}
224
George Rimar786e8662016-03-17 05:57:33 +0000225uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000226
Rui Ueyamac516ae12016-01-29 02:33:45 +0000227bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000228bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
George Rimar48651482015-12-11 08:59:37 +0000229
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000230bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
231 const SymbolBody &S) const {
232 return false;
233}
234
George Rimar98b060d2016-03-06 06:01:07 +0000235bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000236
George Rimar98b060d2016-03-06 06:01:07 +0000237bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000238
George Rimar98b060d2016-03-06 06:01:07 +0000239bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000240 return false;
241}
242
Rafael Espindola22ef9562016-04-13 01:40:19 +0000243void TargetInfo::relaxTlsGdToLe(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::relaxTlsGdToIe(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::relaxTlsIeToLe(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");
256}
257
Rafael Espindola22ef9562016-04-13 01:40:19 +0000258void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
259 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000260 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000261}
George Rimar77d1cb12015-11-24 09:00:06 +0000262
Rafael Espindola7f074422015-09-22 21:35:51 +0000263X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000264 CopyRel = R_386_COPY;
265 GotRel = R_386_GLOB_DAT;
266 PltRel = R_386_JUMP_SLOT;
267 IRelativeRel = R_386_IRELATIVE;
268 RelativeRel = R_386_RELATIVE;
269 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000270 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
271 TlsOffsetRel = R_386_TLS_DTPOFF32;
272 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000273 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000274 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000275 TlsGdToLeSkip = 2;
276}
277
278RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
279 switch (Type) {
280 default:
281 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000282 case R_386_TLS_GD:
283 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000284 case R_386_TLS_LDM:
285 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000286 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000287 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000288 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000289 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000290 case R_386_GOTPC:
291 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000292 case R_386_TLS_IE:
293 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000294 case R_386_GOT32:
295 case R_386_TLS_GOTIE:
296 return R_GOT_FROM_END;
297 case R_386_GOTOFF:
298 return R_GOTREL;
299 case R_386_TLS_LE:
300 return R_TLS;
301 case R_386_TLS_LE_32:
302 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000303 }
George Rimar77b77792015-11-25 22:15:01 +0000304}
305
Rafael Espindolaffcad442016-03-23 14:58:25 +0000306bool X86TargetInfo::isRelRelative(uint32_t Type) const {
307 switch (Type) {
308 default:
309 return false;
310 case R_386_PC32:
311 case R_386_PLT32:
312 case R_386_TLS_LDO_32:
313 return true;
314 }
315}
316
Rui Ueyamac516ae12016-01-29 02:33:45 +0000317void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000318 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
319}
320
Rui Ueyamac516ae12016-01-29 02:33:45 +0000321void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000322 // Entries in .got.plt initially points back to the corresponding
323 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000324 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000325}
Rafael Espindola01205f72015-09-22 18:19:46 +0000326
George Rimar98b060d2016-03-06 06:01:07 +0000327uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000328 if (Type == R_386_TLS_LE)
329 return R_386_TLS_TPOFF;
330 if (Type == R_386_TLS_LE_32)
331 return R_386_TLS_TPOFF32;
332 return Type;
333}
334
George Rimar98b060d2016-03-06 06:01:07 +0000335uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000336 if (Type == R_386_TLS_IE)
337 return Type;
Rafael Espindolae149b482016-04-14 16:05:42 +0000338 return R_386_GOT32;
George Rimar6f17e092015-12-17 09:32:21 +0000339}
340
George Rimar98b060d2016-03-06 06:01:07 +0000341bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000342 return Type == R_386_TLS_GD;
343}
344
George Rimar98b060d2016-03-06 06:01:07 +0000345bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000346 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
347}
348
George Rimar98b060d2016-03-06 06:01:07 +0000349bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000350 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
351}
352
Rui Ueyama900e2d22016-01-29 03:51:49 +0000353void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000354 // Executable files and shared object files have
355 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000356 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000357 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000358 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000359 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
360 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000361 };
362 memcpy(Buf, V, sizeof(V));
363 return;
364 }
George Rimar648a2c32015-10-20 08:54:27 +0000365
George Rimar77b77792015-11-25 22:15:01 +0000366 const uint8_t PltData[] = {
367 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000368 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
369 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000370 };
371 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000372 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000373 write32le(Buf + 2, Got + 4);
374 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000375}
376
Rui Ueyama9398f862016-01-29 04:15:02 +0000377void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
378 uint64_t PltEntryAddr, int32_t Index,
379 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000380 const uint8_t Inst[] = {
381 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
382 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
383 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
384 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000385 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000386
George Rimar77b77792015-11-25 22:15:01 +0000387 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000388 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000389 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
390 : Out<ELF32LE>::Got->getVA();
391 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000392 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000393 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000394}
395
Rafael Espindola666625b2016-04-01 14:36:09 +0000396uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
397 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000398 switch (Type) {
399 default:
400 return 0;
401 case R_386_32:
402 case R_386_GOT32:
403 case R_386_GOTOFF:
404 case R_386_GOTPC:
405 case R_386_PC32:
406 case R_386_PLT32:
407 return read32le(Buf);
408 }
409}
410
Rafael Espindola22ef9562016-04-13 01:40:19 +0000411void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
412 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000413 checkInt<32>(Val, Type);
414 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000415}
416
Rafael Espindola22ef9562016-04-13 01:40:19 +0000417void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
418 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000419 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000420 // leal x@tlsgd(, %ebx, 1),
421 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000422 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000423 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000424 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000425 const uint8_t Inst[] = {
426 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
427 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
428 };
429 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000430 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000431}
432
Rafael Espindola22ef9562016-04-13 01:40:19 +0000433void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
434 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000435 // Convert
436 // leal x@tlsgd(, %ebx, 1),
437 // call __tls_get_addr@plt
438 // to
439 // movl %gs:0, %eax
440 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000441 const uint8_t Inst[] = {
442 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
443 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
444 };
445 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000446 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
447 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000448}
449
George Rimar6f17e092015-12-17 09:32:21 +0000450// In some conditions, relocations can be optimized to avoid using GOT.
451// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000452void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
453 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000454 // Ulrich's document section 6.2 says that @gotntpoff can
455 // be used with MOVL or ADDL instructions.
456 // @indntpoff is similar to @gotntpoff, but for use in
457 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000458 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000459 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000460 uint8_t Reg = (Loc[-1] >> 3) & 7;
461 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000462 if (Type == R_386_TLS_IE) {
463 // For R_386_TLS_IE relocation we perform the next transformations:
464 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
465 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
466 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
467 // First one is special because when EAX is used the sequence is 5 bytes
468 // long, otherwise it is 6 bytes.
469 if (*Op == 0xa1) {
470 *Op = 0xb8;
471 } else {
472 *Inst = IsMov ? 0xc7 : 0x81;
473 *Op = 0xc0 | ((*Op >> 3) & 7);
474 }
475 } else {
476 // R_386_TLS_GOTIE relocation can be optimized to
477 // R_386_TLS_LE so that it does not use GOT.
478 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
479 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
480 // Note: gold converts to ADDL instead of LEAL.
481 *Inst = IsMov ? 0xc7 : 0x8d;
482 if (IsMov)
483 *Op = 0xc0 | ((*Op >> 3) & 7);
484 else
485 *Op = 0x80 | Reg | (Reg << 3);
486 }
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000487 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000488}
489
Rafael Espindola22ef9562016-04-13 01:40:19 +0000490void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
491 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000492 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000493 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000494 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000495 }
496
Rui Ueyama55274e32016-04-23 01:10:15 +0000497 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000498 // leal foo(%reg),%eax
499 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000500 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000501 // movl %gs:0,%eax
502 // nop
503 // leal 0(%esi,1),%esi
504 const uint8_t Inst[] = {
505 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
506 0x90, // nop
507 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
508 };
509 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000510}
511
Rafael Espindola7f074422015-09-22 21:35:51 +0000512X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000513 CopyRel = R_X86_64_COPY;
514 GotRel = R_X86_64_GLOB_DAT;
515 PltRel = R_X86_64_JUMP_SLOT;
516 RelativeRel = R_X86_64_RELATIVE;
517 IRelativeRel = R_X86_64_IRELATIVE;
518 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000519 TlsModuleIndexRel = R_X86_64_DTPMOD64;
520 TlsOffsetRel = R_X86_64_DTPOFF64;
521 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000522 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000523 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000524 TlsGdToLeSkip = 2;
525}
526
527RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
528 switch (Type) {
529 default:
530 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000531 case R_X86_64_TPOFF32:
532 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000533 case R_X86_64_TLSLD:
534 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000535 case R_X86_64_TLSGD:
536 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000537 case R_X86_64_SIZE32:
538 case R_X86_64_SIZE64:
539 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000540 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000541 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000542 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000543 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000544 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000545 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000546 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000547 case R_X86_64_GOTPCREL:
Rafael Espindolaf350d252016-04-19 20:18:52 +0000548 case R_X86_64_GOTPCRELX:
549 case R_X86_64_REX_GOTPCRELX:
Rafael Espindola5628ee72016-04-15 19:14:18 +0000550 case R_X86_64_GOTTPOFF:
551 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000552 }
George Rimar648a2c32015-10-20 08:54:27 +0000553}
554
Rui Ueyamac516ae12016-01-29 02:33:45 +0000555void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000556 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
557}
558
Rui Ueyamac516ae12016-01-29 02:33:45 +0000559void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000560 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000561 write32le(Buf, Plt + 6);
562}
563
Rui Ueyama900e2d22016-01-29 03:51:49 +0000564void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000565 const uint8_t PltData[] = {
566 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
567 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
568 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
569 };
570 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000571 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
572 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
573 write32le(Buf + 2, Got - Plt + 2); // GOT+8
574 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000575}
Rafael Espindola01205f72015-09-22 18:19:46 +0000576
Rui Ueyama9398f862016-01-29 04:15:02 +0000577void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
578 uint64_t PltEntryAddr, int32_t Index,
579 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000580 const uint8_t Inst[] = {
581 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
582 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
583 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
584 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000585 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000586
George Rimar648a2c32015-10-20 08:54:27 +0000587 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
588 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000589 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000590}
591
George Rimar86971052016-03-29 08:35:42 +0000592uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
593 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
594 if (Config->Shared)
595 error(getELFRelocationTypeName(EM_X86_64, Type) +
596 " cannot be a dynamic relocation");
597 return Type;
598}
599
George Rimar98b060d2016-03-06 06:01:07 +0000600uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000601 // No other types of TLS relocations requiring GOT should
602 // reach here.
603 assert(Type == R_X86_64_GOTTPOFF);
604 return R_X86_64_PC32;
605}
606
George Rimar98b060d2016-03-06 06:01:07 +0000607bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000608 return Type == R_X86_64_GOTTPOFF;
609}
610
George Rimar98b060d2016-03-06 06:01:07 +0000611bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000612 return Type == R_X86_64_TLSGD;
613}
614
George Rimar98b060d2016-03-06 06:01:07 +0000615bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000616 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
617 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000618}
619
Rafael Espindolaae244002015-10-05 19:30:12 +0000620bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
621 switch (Type) {
622 default:
623 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000624 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000625 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000626 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000627 case R_X86_64_PC8:
628 case R_X86_64_PC16:
629 case R_X86_64_PC32:
630 case R_X86_64_PC64:
631 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000632 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000633 return true;
634 }
635}
636
Rafael Espindola22ef9562016-04-13 01:40:19 +0000637void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
638 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000639 // Convert
640 // .byte 0x66
641 // leaq x@tlsgd(%rip), %rdi
642 // .word 0x6666
643 // rex64
644 // call __tls_get_addr@plt
645 // to
646 // mov %fs:0x0,%rax
647 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000648 const uint8_t Inst[] = {
649 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
650 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
651 };
652 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000653 relocateOne(Loc + 8, R_X86_64_TPOFF32,
654 Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000655}
656
Rafael Espindola22ef9562016-04-13 01:40:19 +0000657void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
658 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000659 // Convert
660 // .byte 0x66
661 // leaq x@tlsgd(%rip), %rdi
662 // .word 0x6666
663 // rex64
664 // call __tls_get_addr@plt
665 // to
666 // mov %fs:0x0,%rax
667 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000668 const uint8_t Inst[] = {
669 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
670 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
671 };
672 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000673 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000674}
675
George Rimar77d1cb12015-11-24 09:00:06 +0000676// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000677// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000678void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
679 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000680 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
681 // used in MOVQ or ADDQ instructions only.
682 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
683 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
684 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
685 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
686 uint8_t *Prefix = Loc - 3;
687 uint8_t *Inst = Loc - 2;
688 uint8_t *RegSlot = Loc - 1;
689 uint8_t Reg = Loc[-1] >> 3;
690 bool IsMov = *Inst == 0x8b;
691 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000692
George Rimar77d1cb12015-11-24 09:00:06 +0000693 // r12 and rsp registers requires special handling.
694 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
695 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
696 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
697 // The same true for rsp. So we convert to addq for them, saving 1 byte that
698 // we dont have.
699 if (RspAdd)
700 *Inst = 0x81;
701 else
702 *Inst = IsMov ? 0xc7 : 0x8d;
703 if (*Prefix == 0x4c)
704 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
705 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000706 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000707}
708
Rafael Espindola22ef9562016-04-13 01:40:19 +0000709void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
710 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000711 // Convert
712 // leaq bar@tlsld(%rip), %rdi
713 // callq __tls_get_addr@PLT
714 // leaq bar@dtpoff(%rax), %rcx
715 // to
716 // .word 0x6666
717 // .byte 0x66
718 // mov %fs:0,%rax
719 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000720 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000721 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
722 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000723 }
724 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindolaece62b92016-04-18 12:44:33 +0000725 relocateOne(Loc, R_X86_64_TPOFF32, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000726 return;
George Rimar25411f252015-12-04 11:20:13 +0000727 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000728
729 const uint8_t Inst[] = {
730 0x66, 0x66, //.word 0x6666
731 0x66, //.byte 0x66
732 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
733 };
734 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000735}
736
Rafael Espindola22ef9562016-04-13 01:40:19 +0000737void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
738 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000739 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000740 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000741 checkUInt<32>(Val, Type);
742 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000743 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000744 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000745 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000746 case R_X86_64_GOT32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000747 checkInt<32>(Val, Type);
748 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000749 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000750 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000751 case R_X86_64_DTPOFF64:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000752 case R_X86_64_SIZE64:
Rafael Espindola926bff82016-04-25 14:05:44 +0000753 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000754 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000755 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000756 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000757 case R_X86_64_GOTPCRELX:
758 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000759 case R_X86_64_PC32:
760 case R_X86_64_PLT32:
761 case R_X86_64_TLSGD:
762 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000763 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000764 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000765 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000766 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000767 default:
George Rimar57610422016-03-11 14:43:02 +0000768 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000769 }
770}
771
Hal Finkel3c8cc672015-10-12 20:56:18 +0000772// Relocation masks following the #lo(value), #hi(value), #ha(value),
773// #higher(value), #highera(value), #highest(value), and #highesta(value)
774// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
775// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000776static uint16_t applyPPCLo(uint64_t V) { return V; }
777static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
778static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
779static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
780static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000781static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000782static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
783
Davide Italiano8c3444362016-01-11 19:45:33 +0000784PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000785bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
786
Rafael Espindola22ef9562016-04-13 01:40:19 +0000787void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
788 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000789 switch (Type) {
790 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000791 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000792 break;
793 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000794 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000795 break;
796 default:
George Rimar57610422016-03-11 14:43:02 +0000797 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000798 }
799}
800
Rafael Espindola22ef9562016-04-13 01:40:19 +0000801RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
802 return R_ABS;
803}
804
Rafael Espindolac4010882015-09-22 20:54:08 +0000805PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000806 GotRel = R_PPC64_GLOB_DAT;
807 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000808 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000809
810 // We need 64K pages (at least under glibc/Linux, the loader won't
811 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000812 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000813
814 // The PPC64 ELF ABI v1 spec, says:
815 //
816 // It is normally desirable to put segments with different characteristics
817 // in separate 256 Mbyte portions of the address space, to give the
818 // operating system full paging flexibility in the 64-bit address space.
819 //
820 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
821 // use 0x10000000 as the starting address.
822 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000823}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000824
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000825uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000826 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
827 // order. The TOC starts where the first of these sections starts.
828
829 // FIXME: This obviously does not do the right thing when there is no .got
830 // section, but there is a .toc or .tocbss section.
831 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
832 if (!TocVA)
833 TocVA = Out<ELF64BE>::Plt->getVA();
834
835 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
836 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
837 // code (crt1.o) assumes that you can get from the TOC base to the
838 // start of the .toc section with only a single (signed) 16-bit relocation.
839 return TocVA + 0x8000;
840}
841
Rafael Espindola22ef9562016-04-13 01:40:19 +0000842RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
843 switch (Type) {
844 default:
845 return R_ABS;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000846 case R_PPC64_TOC:
847 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000848 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000849 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000850 }
851}
852
Rui Ueyama9398f862016-01-29 04:15:02 +0000853void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
854 uint64_t PltEntryAddr, int32_t Index,
855 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000856 uint64_t Off = GotEntryAddr - getPPC64TocBase();
857
858 // FIXME: What we should do, in theory, is get the offset of the function
859 // descriptor in the .opd section, and use that as the offset from %r2 (the
860 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
861 // be a pointer to the function descriptor in the .opd section. Using
862 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
863
Hal Finkelfa92f682015-10-13 21:47:34 +0000864 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000865 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
866 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
867 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
868 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
869 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
870 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
871 write32be(Buf + 28, 0x4e800420); // bctr
872}
873
Hal Finkelbe0823d2015-10-12 20:58:52 +0000874bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
875 switch (Type) {
876 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000877 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000878 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000879 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +0000880 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000881 }
882}
883
Rafael Espindola22ef9562016-04-13 01:40:19 +0000884void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
885 uint64_t Val) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000886 uint64_t TB = getPPC64TocBase();
887
Hal Finkel3c8cc672015-10-12 20:56:18 +0000888 // For a TOC-relative relocation, adjust the addend and proceed in terms of
889 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000890 switch (Type) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000891 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TB; break;
892 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TB; break;
893 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TB; break;
894 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TB; break;
895 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TB; break;
896 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000897 default: break;
898 }
899
Hal Finkel3c8cc672015-10-12 20:56:18 +0000900 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000901 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000902 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000903 // Preserve the AA/LK bits in the branch instruction
904 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +0000905 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000906 break;
907 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000908 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000909 checkInt<16>(Val, Type);
910 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000911 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000912 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000913 checkInt<16>(Val, Type);
914 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000915 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000916 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000917 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000918 break;
919 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000920 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000921 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000922 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000923 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000924 break;
925 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000926 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000927 break;
928 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000929 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000930 break;
931 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000932 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000933 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000934 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000935 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000936 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000937 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000938 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000939 break;
940 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000941 checkInt<32>(Val, Type);
942 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000943 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000944 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000945 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000946 break;
947 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000948 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000949 break;
950 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000951 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000952 break;
953 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000954 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000955 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000956 case R_PPC64_REL24: {
957 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000958 checkInt<24>(Val, Type);
959 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000960 break;
961 }
962 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000963 checkInt<32>(Val, Type);
964 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000965 break;
966 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000967 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000968 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000969 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000970 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000971 break;
972 default:
George Rimar57610422016-03-11 14:43:02 +0000973 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000974 }
975}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000976
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000977AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000978 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +0000979 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +0000980 IRelativeRel = R_AARCH64_IRELATIVE;
981 GotRel = R_AARCH64_GLOB_DAT;
982 PltRel = R_AARCH64_JUMP_SLOT;
983 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000984 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
985 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000986 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000987 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000988 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000989}
George Rimar648a2c32015-10-20 08:54:27 +0000990
Rafael Espindola22ef9562016-04-13 01:40:19 +0000991RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
992 const SymbolBody &S) const {
993 switch (Type) {
994 default:
995 return R_ABS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000996 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +0000997 case R_AARCH64_CONDBR19:
998 case R_AARCH64_JUMP26:
999 case R_AARCH64_TSTBR14:
1000 return R_PLT_PC;
1001
Rafael Espindola22ef9562016-04-13 01:40:19 +00001002 case R_AARCH64_PREL16:
1003 case R_AARCH64_PREL32:
1004 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001005 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001006 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001007 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001008 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001009 case R_AARCH64_LD64_GOT_LO12_NC:
1010 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1011 return R_GOT;
1012 case R_AARCH64_ADR_GOT_PAGE:
1013 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1014 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001015 }
1016}
1017
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001018bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001019 switch (Type) {
1020 default:
1021 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001022 case R_AARCH64_ADD_ABS_LO12_NC:
1023 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001024 case R_AARCH64_ADR_PREL_LO21:
1025 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001026 case R_AARCH64_CALL26:
1027 case R_AARCH64_CONDBR19:
1028 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001029 case R_AARCH64_LDST8_ABS_LO12_NC:
1030 case R_AARCH64_LDST16_ABS_LO12_NC:
1031 case R_AARCH64_LDST32_ABS_LO12_NC:
1032 case R_AARCH64_LDST64_ABS_LO12_NC:
1033 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001034 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001035 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001036 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1037 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1038 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1039 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1040 case R_AARCH64_TSTBR14:
Rafael Espindola6eda85a2016-04-20 14:36:24 +00001041 case R_AARCH64_LD64_GOT_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001042 return true;
1043 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001044}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001045
George Rimar98b060d2016-03-06 06:01:07 +00001046bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001047 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1048 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1049 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1050 Type == R_AARCH64_TLSDESC_CALL;
1051}
1052
George Rimar98b060d2016-03-06 06:01:07 +00001053bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001054 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1055 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1056}
1057
George Rimar98b060d2016-03-06 06:01:07 +00001058uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001059 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1060 return Type;
1061 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001062 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001063 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001064 // Keep it going with a dummy value so that we can find more reloc errors.
1065 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001066}
1067
Rui Ueyamac516ae12016-01-29 02:33:45 +00001068void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001069 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1070}
1071
Rafael Espindola22ef9562016-04-13 01:40:19 +00001072static uint64_t getAArch64Page(uint64_t Expr) {
1073 return Expr & (~static_cast<uint64_t>(0xFFF));
1074}
1075
Rui Ueyama900e2d22016-01-29 03:51:49 +00001076void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001077 const uint8_t PltData[] = {
1078 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1079 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1080 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1081 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1082 0x20, 0x02, 0x1f, 0xd6, // br x17
1083 0x1f, 0x20, 0x03, 0xd5, // nop
1084 0x1f, 0x20, 0x03, 0xd5, // nop
1085 0x1f, 0x20, 0x03, 0xd5 // nop
1086 };
1087 memcpy(Buf, PltData, sizeof(PltData));
1088
Rui Ueyama900e2d22016-01-29 03:51:49 +00001089 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1090 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001091 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1092 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1093 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1094 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001095}
1096
Rui Ueyama9398f862016-01-29 04:15:02 +00001097void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1098 uint64_t PltEntryAddr, int32_t Index,
1099 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001100 const uint8_t Inst[] = {
1101 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1102 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1103 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1104 0x20, 0x02, 0x1f, 0xd6 // br x17
1105 };
1106 memcpy(Buf, Inst, sizeof(Inst));
1107
Rafael Espindola22ef9562016-04-13 01:40:19 +00001108 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1109 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1110 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1111 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001112}
1113
George Rimar98b060d2016-03-06 06:01:07 +00001114uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001115 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001116 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1117 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001118}
1119
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001120static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001121 uint32_t ImmLo = (Imm & 0x3) << 29;
1122 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1123 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001124 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001125}
1126
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001127static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1128 or32le(L, (Imm & 0xFFF) << 10);
1129}
1130
Rafael Espindola22ef9562016-04-13 01:40:19 +00001131void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1132 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001133 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001134 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001135 checkIntUInt<16>(Val, Type);
1136 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001137 break;
1138 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001139 checkIntUInt<32>(Val, Type);
1140 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001141 break;
1142 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001143 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001144 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001145 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001146 // This relocation stores 12 bits and there's no instruction
1147 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001148 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1149 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001150 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001151 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001152 case R_AARCH64_ADR_GOT_PAGE:
1153 checkInt<33>(Val, Type);
1154 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001155 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001156 case R_AARCH64_ADR_PREL_LO21:
1157 checkInt<21>(Val, Type);
1158 updateAArch64Addr(Loc, Val & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001159 break;
George Rimar3d737e42016-01-13 13:04:46 +00001160 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001161 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1162 checkInt<33>(Val, Type);
1163 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001164 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001165 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001166 case R_AARCH64_JUMP26:
1167 checkInt<28>(Val, Type);
1168 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001169 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001170 case R_AARCH64_CONDBR19:
1171 checkInt<21>(Val, Type);
1172 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001173 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001174 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001175 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001176 checkAlignment<8>(Val, Type);
1177 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001178 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001179 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001180 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001181 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001182 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001183 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001184 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001185 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001186 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001187 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001188 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001189 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001190 break;
1191 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001192 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001193 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001194 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001195 checkIntUInt<16>(Val, Type);
1196 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001197 break;
1198 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001199 checkIntUInt<32>(Val, Type);
1200 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001201 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001202 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001203 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001204 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001205 case R_AARCH64_TSTBR14:
1206 checkInt<16>(Val, Type);
1207 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001208 break;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001209 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001210 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001211 checkInt<24>(V, Type);
1212 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1213 break;
1214 }
1215 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001216 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001217 updateAArch64Add(Loc, V & 0xFFF);
1218 break;
1219 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001220 default:
George Rimar57610422016-03-11 14:43:02 +00001221 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001222 }
1223}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001224
Rafael Espindola22ef9562016-04-13 01:40:19 +00001225void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1226 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001227 // TLSDESC Global-Dynamic relocation are in the form:
1228 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1229 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1230 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1231 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1232 // And it can optimized to:
1233 // movz x0, #0x0, lsl #16
1234 // movk x0, #0x10
1235 // nop
1236 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001237 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001238 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001239 checkUInt<32>(X, Type);
1240
1241 uint32_t NewInst;
1242 switch (Type) {
1243 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1244 case R_AARCH64_TLSDESC_CALL:
1245 // nop
1246 NewInst = 0xd503201f;
1247 break;
1248 case R_AARCH64_TLSDESC_ADR_PAGE21:
1249 // movz
1250 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1251 break;
1252 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1253 // movk
1254 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1255 break;
1256 default:
George Rimar777f9632016-03-12 08:31:34 +00001257 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001258 }
1259 write32le(Loc, NewInst);
1260}
1261
Rafael Espindola22ef9562016-04-13 01:40:19 +00001262void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1263 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001264 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001265 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001266 checkUInt<32>(X, Type);
1267
George Rimar4d1d16d2016-03-06 06:16:05 +00001268 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001269 uint32_t NewInst;
1270 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1271 // Generate movz.
1272 unsigned RegNo = (Inst & 0x1f);
1273 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1274 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1275 // Generate movk
1276 unsigned RegNo = (Inst & 0x1f);
1277 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1278 } else {
George Rimar777f9632016-03-12 08:31:34 +00001279 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001280 }
1281 write32le(Loc, NewInst);
1282}
1283
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001284// Implementing relocations for AMDGPU is low priority since most
1285// programs don't use relocations now. Thus, this function is not
1286// actually called (relocateOne is called for each relocation).
1287// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001288void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1289 uint64_t Val) const {
1290 llvm_unreachable("not implemented");
1291}
1292
1293RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001294 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001295}
1296
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001297template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001298 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001299 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001300 PltEntrySize = 16;
1301 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001302 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001303 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001304 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001305 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001306 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001307}
1308
1309template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001310RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1311 const SymbolBody &S) const {
1312 switch (Type) {
1313 default:
1314 return R_ABS;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001315 case R_MIPS_GPREL16:
1316 case R_MIPS_GPREL32:
1317 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001318 case R_MIPS_26:
1319 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001320 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001321 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001322 // MIPS _gp_disp designates offset between start of function and 'gp'
1323 // pointer into GOT. __gnu_local_gp is equal to the current value of
1324 // the 'gp'. Therefore any relocations against them do not require
1325 // dynamic relocation.
1326 if (&S == ElfSym<ELFT>::MipsGpDisp)
1327 return R_PC;
1328 return R_ABS;
1329 case R_MIPS_PC32:
1330 case R_MIPS_PC16:
1331 case R_MIPS_PC19_S2:
1332 case R_MIPS_PC21_S2:
1333 case R_MIPS_PC26_S2:
1334 case R_MIPS_PCHI16:
1335 case R_MIPS_PCLO16:
1336 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001337 case R_MIPS_GOT16:
1338 case R_MIPS_CALL16:
1339 if (S.isLocal())
1340 return R_MIPS_GOT_LOCAL;
1341 if (!S.isPreemptible())
1342 return R_MIPS_GOT;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001343 return R_GOT_OFF;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001344 }
1345}
1346
1347template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001348uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001349 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1350 return R_MIPS_REL32;
1351 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001352 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001353 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001354 // Keep it going with a dummy value so that we can find more reloc errors.
1355 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001356}
1357
1358template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001359void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1360 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001361}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001362
Simon Atanasyan35031192015-12-15 06:06:34 +00001363static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001364
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001365template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001366static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001367 uint32_t Instr = read32<E>(Loc);
1368 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1369 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1370}
1371
1372template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001373static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001374 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001375 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001376 if (SHIFT > 0)
1377 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001378 checkInt<BSIZE + SHIFT>(V, Type);
1379 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001380}
1381
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001382template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001383static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001384 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001385 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001386}
1387
Simon Atanasyan3b377852016-03-04 10:55:20 +00001388template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001389static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1390 uint32_t Instr = read32<E>(Loc);
1391 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1392}
1393
Rafael Espindola666625b2016-04-01 14:36:09 +00001394template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001395 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1396}
1397
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001398template <class ELFT>
1399void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1400 const endianness E = ELFT::TargetEndianness;
1401 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1402 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1403 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1404 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1405 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1406 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1407 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1408 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1409 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001410 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001411 writeMipsLo16<E>(Buf + 4, Got);
1412 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001413}
1414
1415template <class ELFT>
1416void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1417 uint64_t PltEntryAddr, int32_t Index,
1418 unsigned RelOff) const {
1419 const endianness E = ELFT::TargetEndianness;
1420 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1421 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1422 write32<E>(Buf + 8, 0x03200008); // jr $25
1423 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001424 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001425 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1426 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001427}
1428
1429template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001430void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1431 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1432 // See MipsTargetInfo::writeThunk for details.
1433 const endianness E = ELFT::TargetEndianness;
1434 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1435 write32<E>(Buf + 4, 0x08000000); // j func
1436 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1437 write32<E>(Buf + 12, 0x00000000); // nop
1438 writeMipsHi16<E>(Buf, S);
1439 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1440 writeMipsLo16<E>(Buf + 8, S);
1441}
1442
1443template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001444bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1445 const SymbolBody &S) const {
1446 // Any MIPS PIC code function is invoked with its address in register $t9.
1447 // So if we have a branch instruction from non-PIC code to the PIC one
1448 // we cannot make the jump directly and need to create a small stubs
1449 // to save the target function address.
1450 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1451 if (Type != R_MIPS_26)
1452 return false;
1453 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1454 if (!F)
1455 return false;
1456 // If current file has PIC code, LA25 stub is not required.
1457 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1458 return false;
1459 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1460 if (!D || !D->Section)
1461 return false;
1462 // LA25 is required if target file has PIC code
1463 // or target symbol is a PIC symbol.
1464 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001465 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001466}
1467
1468template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001469uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001470 uint32_t Type) const {
1471 const endianness E = ELFT::TargetEndianness;
1472 switch (Type) {
1473 default:
1474 return 0;
1475 case R_MIPS_32:
1476 case R_MIPS_GPREL32:
1477 return read32<E>(Buf);
1478 case R_MIPS_26:
1479 // FIXME (simon): If the relocation target symbol is not a PLT entry
1480 // we should use another expression for calculation:
1481 // ((A << 2) | (P & 0xf0000000)) >> 2
1482 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1483 case R_MIPS_GPREL16:
1484 case R_MIPS_LO16:
1485 case R_MIPS_PCLO16:
1486 case R_MIPS_TLS_DTPREL_HI16:
1487 case R_MIPS_TLS_DTPREL_LO16:
1488 case R_MIPS_TLS_TPREL_HI16:
1489 case R_MIPS_TLS_TPREL_LO16:
1490 return readSignedLo16<E>(Buf);
1491 case R_MIPS_PC16:
1492 return getPcRelocAddend<E, 16, 2>(Buf);
1493 case R_MIPS_PC19_S2:
1494 return getPcRelocAddend<E, 19, 2>(Buf);
1495 case R_MIPS_PC21_S2:
1496 return getPcRelocAddend<E, 21, 2>(Buf);
1497 case R_MIPS_PC26_S2:
1498 return getPcRelocAddend<E, 26, 2>(Buf);
1499 case R_MIPS_PC32:
1500 return getPcRelocAddend<E, 32, 0>(Buf);
1501 }
1502}
1503
1504template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001505void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1506 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001507 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001508 // Thread pointer and DRP offsets from the start of TLS data area.
1509 // https://www.linux-mips.org/wiki/NPTL
1510 const uint32_t TPOffset = 0x7000;
1511 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001512 switch (Type) {
1513 case R_MIPS_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001514 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001515 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001516 case R_MIPS_26: {
1517 uint32_t Instr = read32<E>(Loc);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001518 write32<E>(Loc, (Instr & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001519 break;
1520 }
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001521 case R_MIPS_GOT16:
1522 checkInt<16>(Val, Type);
1523 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001524 case R_MIPS_CALL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001525 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001526 break;
Simon Atanasyan57830b62015-12-25 13:02:13 +00001527 case R_MIPS_GPREL16: {
Rafael Espindola1763dc42016-04-26 22:00:04 +00001528 int64_t V = Val - MipsGPOffset;
Simon Atanasyan57830b62015-12-25 13:02:13 +00001529 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001530 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001531 break;
1532 }
1533 case R_MIPS_GPREL32:
Rafael Espindola1763dc42016-04-26 22:00:04 +00001534 write32<E>(Loc, Val - MipsGPOffset);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001535 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001536 case R_MIPS_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001537 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001538 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001539 case R_MIPS_JALR:
1540 // Ignore this optimization relocation for now
1541 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001542 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001543 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001544 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001545 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001546 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001547 break;
1548 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001549 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001550 break;
1551 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001552 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001553 break;
1554 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001555 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001556 break;
1557 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001558 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001559 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001560 case R_MIPS_PCHI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001561 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001562 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001563 case R_MIPS_PCLO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001564 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001565 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001566 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001567 writeMipsHi16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001568 break;
1569 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001570 writeMipsLo16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001571 break;
1572 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001573 writeMipsHi16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001574 break;
1575 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001576 writeMipsLo16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001577 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001578 default:
George Rimar57610422016-03-11 14:43:02 +00001579 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001580 }
1581}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001582
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001583template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001584bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001585 return Type == R_MIPS_JALR;
1586}
1587
1588template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001589bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1590 switch (Type) {
1591 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001592 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001593 case R_MIPS_26:
1594 case R_MIPS_32:
1595 case R_MIPS_64:
1596 case R_MIPS_HI16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001597 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001598 }
1599}
Rafael Espindola01205f72015-09-22 18:19:46 +00001600}
1601}