blob: 0a3cc0dbdb04eacc39f096faa1965c858307f6e4 [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
Rafael Espindola22ef9562016-04-13 01:40:19 +000014// in this file.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
Rui Ueyama55274e32016-04-23 01:10:15 +000016// Some functions defined in this file has "relaxTls" as part of their names.
17// They do peephole optimization for TLS variables by rewriting instructions.
18// They are not part of the ABI but optional optimization, so you can skip
19// them if you are not interested in how TLS variables are optimized.
20// See the following paper for the details.
21//
22// Ulrich Drepper, ELF Handling For Thread-Local Storage
23// http://www.akkadia.org/drepper/tls.pdf
24//
Rui Ueyama34f29242015-10-13 19:51:57 +000025//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000026
27#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000028#include "Error.h"
Simon Atanasyan13f6da12016-03-31 21:26:23 +000029#include "InputFiles.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000030#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000031#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000032
33#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000034#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000035#include "llvm/Support/Endian.h"
36#include "llvm/Support/ELF.h"
37
38using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000039using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000040using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000041using namespace llvm::ELF;
42
43namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000044namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000045
Rui Ueyamac1c282a2016-02-11 21:18:01 +000046TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000047
Rafael Espindolae7e57b22015-11-09 21:43:00 +000048static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000049
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
51 if (isInt<N>(V))
52 return;
53 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000054 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000055}
56
57template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
58 if (isUInt<N>(V))
59 return;
60 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000061 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000062}
63
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
65 if (isInt<N>(V) || isUInt<N>(V))
66 return;
67 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000068 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000069}
70
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
72 if ((V & (N - 1)) == 0)
73 return;
74 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000075 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000076}
77
Rui Ueyamaefc23de2015-10-14 21:30:32 +000078namespace {
79class X86TargetInfo final : public TargetInfo {
80public:
81 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000082 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000083 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000084 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000085 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000086 bool isTlsLocalDynamicRel(uint32_t Type) const override;
87 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
88 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000089 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000090 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000091 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
92 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000093 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000094
Rafael Espindola22ef9562016-04-13 01:40:19 +000095 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
96 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
97 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
98 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000099};
100
101class X86_64TargetInfo final : public TargetInfo {
102public:
103 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000104 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000105 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000106 bool isTlsLocalDynamicRel(uint32_t Type) const override;
107 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
108 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000109 void writeGotPltHeader(uint8_t *Buf) const override;
110 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000111 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000112 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
113 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000114 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000115
Rafael Espindola22ef9562016-04-13 01:40:19 +0000116 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
117 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
118 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
119 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000120};
121
Davide Italiano8c3444362016-01-11 19:45:33 +0000122class PPCTargetInfo final : public TargetInfo {
123public:
124 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000125 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000126 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000127};
128
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129class PPC64TargetInfo final : public TargetInfo {
130public:
131 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000132 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000133 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
134 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000135 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136};
137
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000138class AArch64TargetInfo final : public TargetInfo {
139public:
140 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000141 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000142 uint32_t getDynRel(uint32_t Type) const override;
143 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
144 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000145 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000146 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000147 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
148 int32_t Index, unsigned RelOff) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000149 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000150 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
151 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
152 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000153};
154
Tom Stellard80efb162016-01-07 03:59:08 +0000155class AMDGPUTargetInfo final : public TargetInfo {
156public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000157 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000158 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
159 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000160};
161
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000162template <class ELFT> class MipsTargetInfo final : public TargetInfo {
163public:
164 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000165 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000166 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000167 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000168 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
169 void writePltZero(uint8_t *Buf) const override;
170 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
171 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000172 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000173 bool needsThunk(uint32_t Type, const InputFile &File,
174 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000175 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000176 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000177};
178} // anonymous namespace
179
Rui Ueyama91004392015-10-13 16:08:15 +0000180TargetInfo *createTarget() {
181 switch (Config->EMachine) {
182 case EM_386:
183 return new X86TargetInfo();
184 case EM_AARCH64:
185 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000186 case EM_AMDGPU:
187 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000188 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000189 switch (Config->EKind) {
190 case ELF32LEKind:
191 return new MipsTargetInfo<ELF32LE>();
192 case ELF32BEKind:
193 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000194 case ELF64LEKind:
195 return new MipsTargetInfo<ELF64LE>();
196 case ELF64BEKind:
197 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000198 default:
George Rimar777f9632016-03-12 08:31:34 +0000199 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000200 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000201 case EM_PPC:
202 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000203 case EM_PPC64:
204 return new PPC64TargetInfo();
205 case EM_X86_64:
206 return new X86_64TargetInfo();
207 }
George Rimar777f9632016-03-12 08:31:34 +0000208 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000209}
210
Rafael Espindola01205f72015-09-22 18:19:46 +0000211TargetInfo::~TargetInfo() {}
212
Rafael Espindola666625b2016-04-01 14:36:09 +0000213uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
214 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000215 return 0;
216}
217
George Rimar786e8662016-03-17 05:57:33 +0000218uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000219
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000220bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000221
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000222bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
223 const SymbolBody &S) const {
224 return false;
225}
226
George Rimar98b060d2016-03-06 06:01:07 +0000227bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000228
George Rimar98b060d2016-03-06 06:01:07 +0000229bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000230
George Rimar98b060d2016-03-06 06:01:07 +0000231bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000232 return false;
233}
234
Rafael Espindola22ef9562016-04-13 01:40:19 +0000235void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
236 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000237 llvm_unreachable("Should not have claimed to be relaxable");
238}
239
Rafael Espindola22ef9562016-04-13 01:40:19 +0000240void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
241 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000242 llvm_unreachable("Should not have claimed to be relaxable");
243}
244
Rafael Espindola22ef9562016-04-13 01:40:19 +0000245void TargetInfo::relaxTlsIeToLe(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::relaxTlsLdToLe(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");
George Rimar6713cf82015-11-25 21:46:05 +0000253}
George Rimar77d1cb12015-11-24 09:00:06 +0000254
Rafael Espindola7f074422015-09-22 21:35:51 +0000255X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000256 CopyRel = R_386_COPY;
257 GotRel = R_386_GLOB_DAT;
258 PltRel = R_386_JUMP_SLOT;
259 IRelativeRel = R_386_IRELATIVE;
260 RelativeRel = R_386_RELATIVE;
261 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000262 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
263 TlsOffsetRel = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000264 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000265 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000266 TlsGdToLeSkip = 2;
267}
268
269RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
270 switch (Type) {
271 default:
272 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000273 case R_386_TLS_GD:
274 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000275 case R_386_TLS_LDM:
276 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000277 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000278 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000279 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000280 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000281 case R_386_GOTPC:
282 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000283 case R_386_TLS_IE:
284 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000285 case R_386_GOT32:
286 case R_386_TLS_GOTIE:
287 return R_GOT_FROM_END;
288 case R_386_GOTOFF:
289 return R_GOTREL;
290 case R_386_TLS_LE:
291 return R_TLS;
292 case R_386_TLS_LE_32:
293 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000294 }
George Rimar77b77792015-11-25 22:15:01 +0000295}
296
Rui Ueyamac516ae12016-01-29 02:33:45 +0000297void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000298 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
299}
300
Rui Ueyamac516ae12016-01-29 02:33:45 +0000301void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000302 // Entries in .got.plt initially points back to the corresponding
303 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000304 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000305}
Rafael Espindola01205f72015-09-22 18:19:46 +0000306
George Rimar98b060d2016-03-06 06:01:07 +0000307uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000308 if (Type == R_386_TLS_LE)
309 return R_386_TLS_TPOFF;
310 if (Type == R_386_TLS_LE_32)
311 return R_386_TLS_TPOFF32;
312 return Type;
313}
314
George Rimar98b060d2016-03-06 06:01:07 +0000315bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000316 return Type == R_386_TLS_GD;
317}
318
George Rimar98b060d2016-03-06 06:01:07 +0000319bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000320 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
321}
322
George Rimar98b060d2016-03-06 06:01:07 +0000323bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000324 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
325}
326
Rui Ueyama900e2d22016-01-29 03:51:49 +0000327void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000328 // Executable files and shared object files have
329 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000330 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000331 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000332 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000333 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
334 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000335 };
336 memcpy(Buf, V, sizeof(V));
337 return;
338 }
George Rimar648a2c32015-10-20 08:54:27 +0000339
George Rimar77b77792015-11-25 22:15:01 +0000340 const uint8_t PltData[] = {
341 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000342 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
343 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000344 };
345 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000346 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000347 write32le(Buf + 2, Got + 4);
348 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000349}
350
Rui Ueyama9398f862016-01-29 04:15:02 +0000351void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
352 uint64_t PltEntryAddr, int32_t Index,
353 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000354 const uint8_t Inst[] = {
355 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
356 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
357 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
358 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000359 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000360
George Rimar77b77792015-11-25 22:15:01 +0000361 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000362 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rafael Espindolae2f43772016-05-18 20:44:24 +0000363 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyama9398f862016-01-29 04:15:02 +0000364 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000365 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000366 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000367}
368
Rafael Espindola666625b2016-04-01 14:36:09 +0000369uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
370 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000371 switch (Type) {
372 default:
373 return 0;
374 case R_386_32:
375 case R_386_GOT32:
376 case R_386_GOTOFF:
377 case R_386_GOTPC:
378 case R_386_PC32:
379 case R_386_PLT32:
380 return read32le(Buf);
381 }
382}
383
Rafael Espindola22ef9562016-04-13 01:40:19 +0000384void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
385 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000386 checkInt<32>(Val, Type);
387 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000388}
389
Rafael Espindola22ef9562016-04-13 01:40:19 +0000390void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
391 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000392 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000393 // leal x@tlsgd(, %ebx, 1),
394 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000395 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000396 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000397 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000398 const uint8_t Inst[] = {
399 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
400 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
401 };
402 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola8818ca62016-05-20 17:41:09 +0000403 relocateOne(Loc + 5, R_386_32, -Val);
George Rimar2558e122015-12-09 09:55:54 +0000404}
405
Rafael Espindola22ef9562016-04-13 01:40:19 +0000406void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
407 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000408 // Convert
409 // leal x@tlsgd(, %ebx, 1),
410 // call __tls_get_addr@plt
411 // to
412 // movl %gs:0, %eax
413 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000414 const uint8_t Inst[] = {
415 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
416 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
417 };
418 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000419 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
420 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000421}
422
George Rimar6f17e092015-12-17 09:32:21 +0000423// In some conditions, relocations can be optimized to avoid using GOT.
424// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000425void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
426 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000427 // Ulrich's document section 6.2 says that @gotntpoff can
428 // be used with MOVL or ADDL instructions.
429 // @indntpoff is similar to @gotntpoff, but for use in
430 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000431 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000432 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000433 uint8_t Reg = (Loc[-1] >> 3) & 7;
434 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000435 if (Type == R_386_TLS_IE) {
436 // For R_386_TLS_IE relocation we perform the next transformations:
437 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
438 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
439 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
440 // First one is special because when EAX is used the sequence is 5 bytes
441 // long, otherwise it is 6 bytes.
442 if (*Op == 0xa1) {
443 *Op = 0xb8;
444 } else {
445 *Inst = IsMov ? 0xc7 : 0x81;
446 *Op = 0xc0 | ((*Op >> 3) & 7);
447 }
448 } else {
449 // R_386_TLS_GOTIE relocation can be optimized to
450 // R_386_TLS_LE so that it does not use GOT.
451 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
452 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
453 // Note: gold converts to ADDL instead of LEAL.
454 *Inst = IsMov ? 0xc7 : 0x8d;
455 if (IsMov)
456 *Op = 0xc0 | ((*Op >> 3) & 7);
457 else
458 *Op = 0x80 | Reg | (Reg << 3);
459 }
Rafael Espindola8818ca62016-05-20 17:41:09 +0000460 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000461}
462
Rafael Espindola22ef9562016-04-13 01:40:19 +0000463void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
464 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000465 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000466 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000467 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000468 }
469
Rui Ueyama55274e32016-04-23 01:10:15 +0000470 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000471 // leal foo(%reg),%eax
472 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000473 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000474 // movl %gs:0,%eax
475 // nop
476 // leal 0(%esi,1),%esi
477 const uint8_t Inst[] = {
478 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
479 0x90, // nop
480 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
481 };
482 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000483}
484
Rafael Espindola7f074422015-09-22 21:35:51 +0000485X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000486 CopyRel = R_X86_64_COPY;
487 GotRel = R_X86_64_GLOB_DAT;
488 PltRel = R_X86_64_JUMP_SLOT;
489 RelativeRel = R_X86_64_RELATIVE;
490 IRelativeRel = R_X86_64_IRELATIVE;
491 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000492 TlsModuleIndexRel = R_X86_64_DTPMOD64;
493 TlsOffsetRel = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000494 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000495 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000496 TlsGdToLeSkip = 2;
497}
498
499RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
500 switch (Type) {
501 default:
502 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000503 case R_X86_64_TPOFF32:
504 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000505 case R_X86_64_TLSLD:
506 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000507 case R_X86_64_TLSGD:
508 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000509 case R_X86_64_SIZE32:
510 case R_X86_64_SIZE64:
511 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000512 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000513 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000514 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000515 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000516 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000517 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000518 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000519 case R_X86_64_GOTPCREL:
Rafael Espindolaf350d252016-04-19 20:18:52 +0000520 case R_X86_64_GOTPCRELX:
521 case R_X86_64_REX_GOTPCRELX:
Rafael Espindola5628ee72016-04-15 19:14:18 +0000522 case R_X86_64_GOTTPOFF:
523 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000524 }
George Rimar648a2c32015-10-20 08:54:27 +0000525}
526
Rui Ueyamac516ae12016-01-29 02:33:45 +0000527void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000528 // The first entry holds the value of _DYNAMIC. It is not clear why that is
529 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000530 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000531 // other program).
Igor Kudrin351b41d2015-11-16 17:44:08 +0000532 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
533}
534
Rui Ueyamac516ae12016-01-29 02:33:45 +0000535void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000536 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000537 write32le(Buf, Plt + 6);
538}
539
Rui Ueyama900e2d22016-01-29 03:51:49 +0000540void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000541 const uint8_t PltData[] = {
542 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
543 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
544 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
545 };
546 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000547 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
548 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
549 write32le(Buf + 2, Got - Plt + 2); // GOT+8
550 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000551}
Rafael Espindola01205f72015-09-22 18:19:46 +0000552
Rui Ueyama9398f862016-01-29 04:15:02 +0000553void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
554 uint64_t PltEntryAddr, int32_t Index,
555 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000556 const uint8_t Inst[] = {
557 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
558 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
559 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
560 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000561 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000562
George Rimar648a2c32015-10-20 08:54:27 +0000563 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
564 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000565 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000566}
567
George Rimar86971052016-03-29 08:35:42 +0000568uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
569 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
570 if (Config->Shared)
571 error(getELFRelocationTypeName(EM_X86_64, Type) +
572 " cannot be a dynamic relocation");
573 return Type;
574}
575
George Rimar98b060d2016-03-06 06:01:07 +0000576bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000577 return Type == R_X86_64_GOTTPOFF;
578}
579
George Rimar98b060d2016-03-06 06:01:07 +0000580bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000581 return Type == R_X86_64_TLSGD;
582}
583
George Rimar98b060d2016-03-06 06:01:07 +0000584bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000585 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
586 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000587}
588
Rafael Espindola22ef9562016-04-13 01:40:19 +0000589void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
590 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000591 // Convert
592 // .byte 0x66
593 // leaq x@tlsgd(%rip), %rdi
594 // .word 0x6666
595 // rex64
596 // call __tls_get_addr@plt
597 // to
598 // mov %fs:0x0,%rax
599 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000600 const uint8_t Inst[] = {
601 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
602 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
603 };
604 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola8818ca62016-05-20 17:41:09 +0000605 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000606}
607
Rafael Espindola22ef9562016-04-13 01:40:19 +0000608void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
609 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000610 // Convert
611 // .byte 0x66
612 // leaq x@tlsgd(%rip), %rdi
613 // .word 0x6666
614 // rex64
615 // call __tls_get_addr@plt
616 // to
617 // mov %fs:0x0,%rax
618 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000619 const uint8_t Inst[] = {
620 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
621 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
622 };
623 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000624 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000625}
626
George Rimar77d1cb12015-11-24 09:00:06 +0000627// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000628// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000629void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
630 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000631 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
632 // used in MOVQ or ADDQ instructions only.
633 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
634 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
635 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
636 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
637 uint8_t *Prefix = Loc - 3;
638 uint8_t *Inst = Loc - 2;
639 uint8_t *RegSlot = Loc - 1;
640 uint8_t Reg = Loc[-1] >> 3;
641 bool IsMov = *Inst == 0x8b;
642 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000643
George Rimar77d1cb12015-11-24 09:00:06 +0000644 // r12 and rsp registers requires special handling.
645 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
646 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
647 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
648 // The same true for rsp. So we convert to addq for them, saving 1 byte that
649 // we dont have.
650 if (RspAdd)
651 *Inst = 0x81;
652 else
653 *Inst = IsMov ? 0xc7 : 0x8d;
654 if (*Prefix == 0x4c)
655 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
656 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola8818ca62016-05-20 17:41:09 +0000657 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000658}
659
Rafael Espindola22ef9562016-04-13 01:40:19 +0000660void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
661 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000662 // Convert
663 // leaq bar@tlsld(%rip), %rdi
664 // callq __tls_get_addr@PLT
665 // leaq bar@dtpoff(%rax), %rcx
666 // to
667 // .word 0x6666
668 // .byte 0x66
669 // mov %fs:0,%rax
670 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000671 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000672 write64le(Loc, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000673 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000674 }
675 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000676 relocateOne(Loc, R_X86_64_TPOFF32, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000677 return;
George Rimar25411f252015-12-04 11:20:13 +0000678 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000679
680 const uint8_t Inst[] = {
681 0x66, 0x66, //.word 0x6666
682 0x66, //.byte 0x66
683 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
684 };
685 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000686}
687
Rafael Espindola22ef9562016-04-13 01:40:19 +0000688void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
689 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000690 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000691 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000692 checkUInt<32>(Val, Type);
693 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000694 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000695 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000696 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000697 case R_X86_64_GOT32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000698 checkInt<32>(Val, Type);
699 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000700 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000701 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000702 case R_X86_64_DTPOFF64:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000703 case R_X86_64_SIZE64:
Rafael Espindola926bff82016-04-25 14:05:44 +0000704 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000705 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000706 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000707 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000708 case R_X86_64_GOTPCRELX:
709 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000710 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000711 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000712 case R_X86_64_PLT32:
713 case R_X86_64_TLSGD:
714 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000715 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000716 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000717 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000718 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000719 default:
George Rimar57610422016-03-11 14:43:02 +0000720 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000721 }
722}
723
Hal Finkel3c8cc672015-10-12 20:56:18 +0000724// Relocation masks following the #lo(value), #hi(value), #ha(value),
725// #higher(value), #highera(value), #highest(value), and #highesta(value)
726// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
727// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000728static uint16_t applyPPCLo(uint64_t V) { return V; }
729static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
730static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
731static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
732static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000733static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000734static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
735
Davide Italiano8c3444362016-01-11 19:45:33 +0000736PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000737
Rafael Espindola22ef9562016-04-13 01:40:19 +0000738void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
739 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000740 switch (Type) {
741 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000742 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000743 break;
744 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000745 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000746 break;
747 default:
George Rimar57610422016-03-11 14:43:02 +0000748 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000749 }
750}
751
Rafael Espindola22ef9562016-04-13 01:40:19 +0000752RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
753 return R_ABS;
754}
755
Rafael Espindolac4010882015-09-22 20:54:08 +0000756PPC64TargetInfo::PPC64TargetInfo() {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000757 PltRel = GotRel = R_PPC64_GLOB_DAT;
Rui Ueyama724d6252016-01-29 01:49:32 +0000758 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000759 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000760
761 // We need 64K pages (at least under glibc/Linux, the loader won't
762 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000763 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000764
765 // The PPC64 ELF ABI v1 spec, says:
766 //
767 // It is normally desirable to put segments with different characteristics
768 // in separate 256 Mbyte portions of the address space, to give the
769 // operating system full paging flexibility in the 64-bit address space.
770 //
771 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
772 // use 0x10000000 as the starting address.
773 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000774}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000775
Rafael Espindola15cec292016-04-27 12:25:22 +0000776static uint64_t PPC64TocOffset = 0x8000;
777
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000778uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +0000779 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
780 // TOC starts where the first of these sections starts. We always create a
781 // .got when we see a relocation that uses it, so for us the start is always
782 // the .got.
Hal Finkel3c8cc672015-10-12 20:56:18 +0000783 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +0000784
785 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
786 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
787 // code (crt1.o) assumes that you can get from the TOC base to the
788 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +0000789 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000790}
791
Rafael Espindola22ef9562016-04-13 01:40:19 +0000792RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
793 switch (Type) {
794 default:
795 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +0000796 case R_PPC64_TOC16:
797 case R_PPC64_TOC16_DS:
798 case R_PPC64_TOC16_HA:
799 case R_PPC64_TOC16_HI:
800 case R_PPC64_TOC16_LO:
801 case R_PPC64_TOC16_LO_DS:
802 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000803 case R_PPC64_TOC:
804 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000805 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000806 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000807 }
808}
809
Rui Ueyama9398f862016-01-29 04:15:02 +0000810void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
811 uint64_t PltEntryAddr, int32_t Index,
812 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000813 uint64_t Off = GotEntryAddr - getPPC64TocBase();
814
815 // FIXME: What we should do, in theory, is get the offset of the function
816 // descriptor in the .opd section, and use that as the offset from %r2 (the
817 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
818 // be a pointer to the function descriptor in the .opd section. Using
819 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
820
Hal Finkelfa92f682015-10-13 21:47:34 +0000821 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000822 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
823 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
824 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
825 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
826 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
827 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
828 write32be(Buf + 28, 0x4e800420); // bctr
829}
830
Rafael Espindola22ef9562016-04-13 01:40:19 +0000831void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
832 uint64_t Val) const {
Rafael Espindola15cec292016-04-27 12:25:22 +0000833 uint64_t TO = PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000834
Rafael Espindola15cec292016-04-27 12:25:22 +0000835 // For a TOC-relative relocation, proceed in terms of the corresponding
836 // ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000837 switch (Type) {
Rafael Espindola15cec292016-04-27 12:25:22 +0000838 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break;
839 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break;
840 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break;
841 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break;
842 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break;
843 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000844 default: break;
845 }
846
Hal Finkel3c8cc672015-10-12 20:56:18 +0000847 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000848 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000849 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000850 // Preserve the AA/LK bits in the branch instruction
851 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +0000852 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000853 break;
854 }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000855 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000856 checkInt<16>(Val, Type);
857 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000858 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000859 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000860 checkInt<16>(Val, Type);
861 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000862 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000863 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000864 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000865 break;
866 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000867 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000868 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000869 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000870 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000871 break;
872 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000873 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000874 break;
875 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000876 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000877 break;
878 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000879 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000880 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000881 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000882 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000883 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000884 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000885 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000886 break;
887 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000888 checkInt<32>(Val, Type);
889 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000890 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000891 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000892 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000893 break;
894 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000895 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000896 break;
897 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000898 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000899 break;
900 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000901 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +0000902 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000903 case R_PPC64_REL24: {
904 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000905 checkInt<24>(Val, Type);
906 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000907 break;
908 }
909 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000910 checkInt<32>(Val, Type);
911 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000912 break;
913 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000914 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000915 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000916 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000917 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000918 break;
919 default:
George Rimar57610422016-03-11 14:43:02 +0000920 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000921 }
922}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000923
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000924AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000925 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +0000926 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +0000927 IRelativeRel = R_AARCH64_IRELATIVE;
928 GotRel = R_AARCH64_GLOB_DAT;
929 PltRel = R_AARCH64_JUMP_SLOT;
930 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000931 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
932 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000933 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000934 PltZeroSize = 32;
Rafael Espindola8818ca62016-05-20 17:41:09 +0000935
936 // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
937 // 1 of the tls structures and the tcb size is 16.
938 TcbSize = 16;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000939}
George Rimar648a2c32015-10-20 08:54:27 +0000940
Rafael Espindola22ef9562016-04-13 01:40:19 +0000941RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
942 const SymbolBody &S) const {
943 switch (Type) {
944 default:
945 return R_ABS;
Rafael Espindola8818ca62016-05-20 17:41:09 +0000946
947 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
948 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
949 return R_TLS;
950
Rafael Espindola22ef9562016-04-13 01:40:19 +0000951 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +0000952 case R_AARCH64_CONDBR19:
953 case R_AARCH64_JUMP26:
954 case R_AARCH64_TSTBR14:
955 return R_PLT_PC;
956
Rafael Espindola22ef9562016-04-13 01:40:19 +0000957 case R_AARCH64_PREL16:
958 case R_AARCH64_PREL32:
959 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000960 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000961 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000962 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000963 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000964 case R_AARCH64_LD64_GOT_LO12_NC:
965 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
966 return R_GOT;
967 case R_AARCH64_ADR_GOT_PAGE:
968 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
969 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000970 }
971}
972
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000973bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +0000974 switch (Type) {
975 default:
976 return false;
Ed Schouten39aca422016-04-06 18:21:07 +0000977 case R_AARCH64_ADD_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +0000978 case R_AARCH64_LDST8_ABS_LO12_NC:
979 case R_AARCH64_LDST16_ABS_LO12_NC:
980 case R_AARCH64_LDST32_ABS_LO12_NC:
981 case R_AARCH64_LDST64_ABS_LO12_NC:
982 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola6eda85a2016-04-20 14:36:24 +0000983 case R_AARCH64_LD64_GOT_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +0000984 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +0000985 return true;
986 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +0000987}
Rafael Espindola435c00f2016-02-23 20:19:44 +0000988
George Rimar98b060d2016-03-06 06:01:07 +0000989bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000990 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
991 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
992 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
993 Type == R_AARCH64_TLSDESC_CALL;
994}
995
George Rimar98b060d2016-03-06 06:01:07 +0000996bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000997 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
998 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
999}
1000
George Rimar98b060d2016-03-06 06:01:07 +00001001uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001002 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1003 return Type;
1004 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001005 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001006 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001007 // Keep it going with a dummy value so that we can find more reloc errors.
1008 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001009}
1010
Rui Ueyamac516ae12016-01-29 02:33:45 +00001011void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001012 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1013}
1014
Rafael Espindola22ef9562016-04-13 01:40:19 +00001015static uint64_t getAArch64Page(uint64_t Expr) {
1016 return Expr & (~static_cast<uint64_t>(0xFFF));
1017}
1018
Rui Ueyama900e2d22016-01-29 03:51:49 +00001019void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001020 const uint8_t PltData[] = {
1021 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1022 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1023 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1024 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1025 0x20, 0x02, 0x1f, 0xd6, // br x17
1026 0x1f, 0x20, 0x03, 0xd5, // nop
1027 0x1f, 0x20, 0x03, 0xd5, // nop
1028 0x1f, 0x20, 0x03, 0xd5 // nop
1029 };
1030 memcpy(Buf, PltData, sizeof(PltData));
1031
Rui Ueyama900e2d22016-01-29 03:51:49 +00001032 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1033 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001034 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1035 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1036 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1037 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001038}
1039
Rui Ueyama9398f862016-01-29 04:15:02 +00001040void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1041 uint64_t PltEntryAddr, int32_t Index,
1042 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001043 const uint8_t Inst[] = {
1044 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1045 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1046 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1047 0x20, 0x02, 0x1f, 0xd6 // br x17
1048 };
1049 memcpy(Buf, Inst, sizeof(Inst));
1050
Rafael Espindola22ef9562016-04-13 01:40:19 +00001051 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1052 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1053 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1054 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001055}
1056
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001057static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001058 uint32_t ImmLo = (Imm & 0x3) << 29;
1059 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1060 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001061 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001062}
1063
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001064static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1065 or32le(L, (Imm & 0xFFF) << 10);
1066}
1067
Rafael Espindola22ef9562016-04-13 01:40:19 +00001068void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1069 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001070 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001071 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001072 checkIntUInt<16>(Val, Type);
1073 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001074 break;
1075 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001076 checkIntUInt<32>(Val, Type);
1077 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001078 break;
1079 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001080 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001081 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001082 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001083 // This relocation stores 12 bits and there's no instruction
1084 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001085 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1086 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001087 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001088 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001089 case R_AARCH64_ADR_GOT_PAGE:
1090 checkInt<33>(Val, Type);
1091 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001092 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001093 case R_AARCH64_ADR_PREL_LO21:
1094 checkInt<21>(Val, Type);
1095 updateAArch64Addr(Loc, Val & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001096 break;
George Rimar3d737e42016-01-13 13:04:46 +00001097 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001098 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1099 checkInt<33>(Val, Type);
1100 updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001101 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001102 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001103 case R_AARCH64_JUMP26:
1104 checkInt<28>(Val, Type);
1105 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001106 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001107 case R_AARCH64_CONDBR19:
1108 checkInt<21>(Val, Type);
1109 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001110 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001111 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001112 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001113 checkAlignment<8>(Val, Type);
1114 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001115 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001116 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001117 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001118 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001119 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001120 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001121 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001122 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001123 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001124 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001125 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001126 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001127 break;
1128 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001129 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001130 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001131 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001132 checkIntUInt<16>(Val, Type);
1133 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001134 break;
1135 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001136 checkIntUInt<32>(Val, Type);
1137 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001138 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001139 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001140 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001141 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001142 case R_AARCH64_TSTBR14:
1143 checkInt<16>(Val, Type);
1144 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001145 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001146 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1147 checkInt<24>(Val, Type);
1148 updateAArch64Add(Loc, (Val & 0xFFF000) >> 12);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001149 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001150 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1151 updateAArch64Add(Loc, Val & 0xFFF);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001152 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001153 default:
George Rimar57610422016-03-11 14:43:02 +00001154 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001155 }
1156}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001157
Rafael Espindola22ef9562016-04-13 01:40:19 +00001158void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1159 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001160 // TLSDESC Global-Dynamic relocation are in the form:
1161 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1162 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1163 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1164 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1165 // And it can optimized to:
1166 // movz x0, #0x0, lsl #16
1167 // movk x0, #0x10
1168 // nop
1169 // nop
Rafael Espindola8818ca62016-05-20 17:41:09 +00001170 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001171
1172 uint32_t NewInst;
1173 switch (Type) {
1174 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1175 case R_AARCH64_TLSDESC_CALL:
1176 // nop
1177 NewInst = 0xd503201f;
1178 break;
1179 case R_AARCH64_TLSDESC_ADR_PAGE21:
1180 // movz
Rafael Espindola8818ca62016-05-20 17:41:09 +00001181 NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001182 break;
1183 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1184 // movk
Rafael Espindola8818ca62016-05-20 17:41:09 +00001185 NewInst = 0xf2800000 | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001186 break;
1187 default:
George Rimar777f9632016-03-12 08:31:34 +00001188 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001189 }
1190 write32le(Loc, NewInst);
1191}
1192
Rafael Espindola22ef9562016-04-13 01:40:19 +00001193void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1194 uint64_t Val) const {
Rafael Espindola8818ca62016-05-20 17:41:09 +00001195 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001196
George Rimar4d1d16d2016-03-06 06:16:05 +00001197 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001198 uint32_t NewInst;
1199 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1200 // Generate movz.
1201 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001202 NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001203 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1204 // Generate movk
1205 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001206 NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001207 } else {
George Rimar777f9632016-03-12 08:31:34 +00001208 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001209 }
1210 write32le(Loc, NewInst);
1211}
1212
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001213// Implementing relocations for AMDGPU is low priority since most
1214// programs don't use relocations now. Thus, this function is not
1215// actually called (relocateOne is called for each relocation).
1216// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001217void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1218 uint64_t Val) const {
1219 llvm_unreachable("not implemented");
1220}
1221
1222RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001223 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001224}
1225
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001226template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001227 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001228 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001229 PltEntrySize = 16;
1230 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001231 ThunkSize = 16;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001232 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001233 PltRel = R_MIPS_JUMP_SLOT;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001234 if (ELFT::Is64Bits)
1235 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1236 else
1237 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001238}
1239
1240template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001241RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1242 const SymbolBody &S) const {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001243 if (ELFT::Is64Bits)
1244 // See comment in the calculateMips64RelChain.
1245 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001246 switch (Type) {
1247 default:
1248 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001249 case R_MIPS_JALR:
1250 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001251 case R_MIPS_GPREL16:
1252 case R_MIPS_GPREL32:
1253 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001254 case R_MIPS_26:
1255 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001256 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001257 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001258 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001259 // MIPS _gp_disp designates offset between start of function and 'gp'
1260 // pointer into GOT. __gnu_local_gp is equal to the current value of
1261 // the 'gp'. Therefore any relocations against them do not require
1262 // dynamic relocation.
1263 if (&S == ElfSym<ELFT>::MipsGpDisp)
1264 return R_PC;
1265 return R_ABS;
1266 case R_MIPS_PC32:
1267 case R_MIPS_PC16:
1268 case R_MIPS_PC19_S2:
1269 case R_MIPS_PC21_S2:
1270 case R_MIPS_PC26_S2:
1271 case R_MIPS_PCHI16:
1272 case R_MIPS_PCLO16:
1273 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001274 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001275 if (S.isLocal())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001276 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001277 // fallthrough
1278 case R_MIPS_CALL16:
1279 case R_MIPS_GOT_DISP:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001280 if (!S.isPreemptible())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001281 return R_MIPS_GOT_LOCAL;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001282 return R_GOT_OFF;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001283 case R_MIPS_GOT_PAGE:
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001284 return R_MIPS_GOT_LOCAL_PAGE;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001285 }
1286}
1287
1288template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001289uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001290 if (Type == R_MIPS_32 || Type == R_MIPS_64)
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001291 return RelativeRel;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001292 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001293 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001294 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001295 // Keep it going with a dummy value so that we can find more reloc errors.
1296 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001297}
1298
1299template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001300void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1301 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001302}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001303
Simon Atanasyan35031192015-12-15 06:06:34 +00001304static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001305
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001306template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001307static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001308 uint32_t Instr = read32<E>(Loc);
1309 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1310 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1311}
1312
1313template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001314static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001315 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001316 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001317 if (SHIFT > 0)
1318 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001319 checkInt<BSIZE + SHIFT>(V, Type);
1320 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001321}
1322
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001323template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001324static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001325 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001326 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001327}
1328
Simon Atanasyan3b377852016-03-04 10:55:20 +00001329template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001330static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1331 uint32_t Instr = read32<E>(Loc);
1332 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1333}
1334
Rafael Espindola666625b2016-04-01 14:36:09 +00001335template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001336 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1337}
1338
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001339template <class ELFT>
1340void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1341 const endianness E = ELFT::TargetEndianness;
1342 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1343 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1344 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1345 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1346 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1347 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1348 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1349 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1350 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001351 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001352 writeMipsLo16<E>(Buf + 4, Got);
1353 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001354}
1355
1356template <class ELFT>
1357void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1358 uint64_t PltEntryAddr, int32_t Index,
1359 unsigned RelOff) const {
1360 const endianness E = ELFT::TargetEndianness;
1361 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1362 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1363 write32<E>(Buf + 8, 0x03200008); // jr $25
1364 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001365 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001366 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1367 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001368}
1369
1370template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001371void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1372 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1373 // See MipsTargetInfo::writeThunk for details.
1374 const endianness E = ELFT::TargetEndianness;
1375 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1376 write32<E>(Buf + 4, 0x08000000); // j func
1377 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1378 write32<E>(Buf + 12, 0x00000000); // nop
1379 writeMipsHi16<E>(Buf, S);
1380 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1381 writeMipsLo16<E>(Buf + 8, S);
1382}
1383
1384template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001385bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1386 const SymbolBody &S) const {
1387 // Any MIPS PIC code function is invoked with its address in register $t9.
1388 // So if we have a branch instruction from non-PIC code to the PIC one
1389 // we cannot make the jump directly and need to create a small stubs
1390 // to save the target function address.
1391 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1392 if (Type != R_MIPS_26)
1393 return false;
1394 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1395 if (!F)
1396 return false;
1397 // If current file has PIC code, LA25 stub is not required.
1398 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1399 return false;
1400 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1401 if (!D || !D->Section)
1402 return false;
1403 // LA25 is required if target file has PIC code
1404 // or target symbol is a PIC symbol.
1405 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001406 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001407}
1408
1409template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001410uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001411 uint32_t Type) const {
1412 const endianness E = ELFT::TargetEndianness;
1413 switch (Type) {
1414 default:
1415 return 0;
1416 case R_MIPS_32:
1417 case R_MIPS_GPREL32:
1418 return read32<E>(Buf);
1419 case R_MIPS_26:
1420 // FIXME (simon): If the relocation target symbol is not a PLT entry
1421 // we should use another expression for calculation:
1422 // ((A << 2) | (P & 0xf0000000)) >> 2
1423 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1424 case R_MIPS_GPREL16:
1425 case R_MIPS_LO16:
1426 case R_MIPS_PCLO16:
1427 case R_MIPS_TLS_DTPREL_HI16:
1428 case R_MIPS_TLS_DTPREL_LO16:
1429 case R_MIPS_TLS_TPREL_HI16:
1430 case R_MIPS_TLS_TPREL_LO16:
1431 return readSignedLo16<E>(Buf);
1432 case R_MIPS_PC16:
1433 return getPcRelocAddend<E, 16, 2>(Buf);
1434 case R_MIPS_PC19_S2:
1435 return getPcRelocAddend<E, 19, 2>(Buf);
1436 case R_MIPS_PC21_S2:
1437 return getPcRelocAddend<E, 21, 2>(Buf);
1438 case R_MIPS_PC26_S2:
1439 return getPcRelocAddend<E, 26, 2>(Buf);
1440 case R_MIPS_PC32:
1441 return getPcRelocAddend<E, 32, 0>(Buf);
1442 }
1443}
1444
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001445static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1446 uint64_t Val) {
1447 // MIPS N64 ABI packs multiple relocations into the single relocation
1448 // record. In general, all up to three relocations can have arbitrary
1449 // types. In fact, Clang and GCC uses only a few combinations. For now,
1450 // we support two of them. That is allow to pass at least all LLVM
1451 // test suite cases.
1452 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1453 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1454 // The first relocation is a 'real' relocation which is calculated
1455 // using the corresponding symbol's value. The second and the third
1456 // relocations used to modify result of the first one: extend it to
1457 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1458 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1459 uint32_t Type2 = (Type >> 8) & 0xff;
1460 uint32_t Type3 = (Type >> 16) & 0xff;
1461 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1462 return std::make_pair(Type, Val);
1463 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1464 return std::make_pair(Type2, Val);
1465 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1466 return std::make_pair(Type3, -Val);
1467 error("unsupported relocations combination " + Twine(Type));
1468 return std::make_pair(Type & 0xff, Val);
1469}
1470
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001471template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001472void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1473 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001474 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001475 // Thread pointer and DRP offsets from the start of TLS data area.
1476 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001477 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1478 Val -= 0x8000;
1479 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1480 Val -= 0x7000;
1481 if (ELFT::Is64Bits)
1482 std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001483 switch (Type) {
1484 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001485 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001486 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001487 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001488 case R_MIPS_64:
1489 write64<E>(Loc, Val);
1490 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00001491 case R_MIPS_26:
1492 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001493 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001494 case R_MIPS_GOT_DISP:
1495 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001496 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001497 case R_MIPS_GPREL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001498 checkInt<16>(Val, Type);
1499 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001500 case R_MIPS_CALL16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001501 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001502 case R_MIPS_LO16:
1503 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001504 case R_MIPS_TLS_DTPREL_LO16:
1505 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001506 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001507 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001508 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001509 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001510 case R_MIPS_TLS_DTPREL_HI16:
1511 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001512 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001513 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001514 case R_MIPS_JALR:
1515 // Ignore this optimization relocation for now
1516 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001517 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001518 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001519 break;
1520 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001521 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001522 break;
1523 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001524 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001525 break;
1526 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001527 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001528 break;
1529 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001530 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001531 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001532 default:
George Rimar57610422016-03-11 14:43:02 +00001533 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001534 }
1535}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001536
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001537template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001538bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00001539 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001540}
Rafael Espindola01205f72015-09-22 18:19:46 +00001541}
1542}