blob: dc63dae0ddf77c24e6940ad0489c45357ca40876 [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 Espindola5c66b822016-06-04 22:58:54 +0000116 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
117 RelExpr Expr) const override;
George Rimar5c33b912016-05-25 14:31:37 +0000118 void relaxGot(uint8_t *Loc, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000119 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
120 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
121 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
122 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimarb7204302016-06-02 09:22:00 +0000123
124private:
125 void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
126 uint8_t ModRm) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000127};
128
Davide Italiano8c3444362016-01-11 19:45:33 +0000129class PPCTargetInfo final : public TargetInfo {
130public:
131 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000132 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000133 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000134};
135
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136class PPC64TargetInfo final : public TargetInfo {
137public:
138 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000139 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000140 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
141 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000142 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000143};
144
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000145class AArch64TargetInfo final : public TargetInfo {
146public:
147 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000148 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000149 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000150 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;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000155 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000156 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
157 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
158 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000159};
160
Tom Stellard80efb162016-01-07 03:59:08 +0000161class AMDGPUTargetInfo final : public TargetInfo {
162public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000163 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000164 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
165 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000166};
167
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000168template <class ELFT> class MipsTargetInfo final : public TargetInfo {
169public:
170 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000171 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000172 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000173 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000174 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
175 void writePltZero(uint8_t *Buf) const override;
176 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
177 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000178 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000179 bool needsThunk(uint32_t Type, const InputFile &File,
180 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000181 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000182 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000183};
184} // anonymous namespace
185
Rui Ueyama91004392015-10-13 16:08:15 +0000186TargetInfo *createTarget() {
187 switch (Config->EMachine) {
188 case EM_386:
189 return new X86TargetInfo();
190 case EM_AARCH64:
191 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000192 case EM_AMDGPU:
193 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000194 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000195 switch (Config->EKind) {
196 case ELF32LEKind:
197 return new MipsTargetInfo<ELF32LE>();
198 case ELF32BEKind:
199 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000200 case ELF64LEKind:
201 return new MipsTargetInfo<ELF64LE>();
202 case ELF64BEKind:
203 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000204 default:
George Rimar777f9632016-03-12 08:31:34 +0000205 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000206 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000207 case EM_PPC:
208 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000209 case EM_PPC64:
210 return new PPC64TargetInfo();
211 case EM_X86_64:
212 return new X86_64TargetInfo();
213 }
George Rimar777f9632016-03-12 08:31:34 +0000214 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000215}
216
Rafael Espindola01205f72015-09-22 18:19:46 +0000217TargetInfo::~TargetInfo() {}
218
Rafael Espindola666625b2016-04-01 14:36:09 +0000219uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
220 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000221 return 0;
222}
223
George Rimar786e8662016-03-17 05:57:33 +0000224uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000225
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000226bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000227
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000228bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
229 const SymbolBody &S) const {
230 return false;
231}
232
George Rimar98b060d2016-03-06 06:01:07 +0000233bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000234
George Rimar98b060d2016-03-06 06:01:07 +0000235bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000236
George Rimar98b060d2016-03-06 06:01:07 +0000237bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000238 return false;
239}
240
Rafael Espindola5c66b822016-06-04 22:58:54 +0000241RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
242 RelExpr Expr) const {
George Rimarf10c8292016-06-01 16:45:30 +0000243 return Expr;
George Rimar5c33b912016-05-25 14:31:37 +0000244}
245
246void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
247 llvm_unreachable("Should not have claimed to be relaxable");
248}
249
Rafael Espindola22ef9562016-04-13 01:40:19 +0000250void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
251 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000252 llvm_unreachable("Should not have claimed to be relaxable");
253}
254
Rafael Espindola22ef9562016-04-13 01:40:19 +0000255void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
256 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000257 llvm_unreachable("Should not have claimed to be relaxable");
258}
259
Rafael Espindola22ef9562016-04-13 01:40:19 +0000260void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
261 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000262 llvm_unreachable("Should not have claimed to be relaxable");
263}
264
Rafael Espindola22ef9562016-04-13 01:40:19 +0000265void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
266 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000267 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000268}
George Rimar77d1cb12015-11-24 09:00:06 +0000269
Rafael Espindola7f074422015-09-22 21:35:51 +0000270X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000271 CopyRel = R_386_COPY;
272 GotRel = R_386_GLOB_DAT;
273 PltRel = R_386_JUMP_SLOT;
274 IRelativeRel = R_386_IRELATIVE;
275 RelativeRel = R_386_RELATIVE;
276 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000277 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
278 TlsOffsetRel = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000279 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000280 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000281 TlsGdToLeSkip = 2;
282}
283
284RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
285 switch (Type) {
286 default:
287 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000288 case R_386_TLS_GD:
289 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000290 case R_386_TLS_LDM:
291 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000292 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000293 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000294 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000295 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000296 case R_386_GOTPC:
297 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000298 case R_386_TLS_IE:
299 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000300 case R_386_GOT32:
301 case R_386_TLS_GOTIE:
302 return R_GOT_FROM_END;
303 case R_386_GOTOFF:
304 return R_GOTREL;
305 case R_386_TLS_LE:
306 return R_TLS;
307 case R_386_TLS_LE_32:
308 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000309 }
George Rimar77b77792015-11-25 22:15:01 +0000310}
311
Rui Ueyamac516ae12016-01-29 02:33:45 +0000312void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000313 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
314}
315
Rui Ueyamac516ae12016-01-29 02:33:45 +0000316void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000317 // Entries in .got.plt initially points back to the corresponding
318 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000319 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000320}
Rafael Espindola01205f72015-09-22 18:19:46 +0000321
George Rimar98b060d2016-03-06 06:01:07 +0000322uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000323 if (Type == R_386_TLS_LE)
324 return R_386_TLS_TPOFF;
325 if (Type == R_386_TLS_LE_32)
326 return R_386_TLS_TPOFF32;
327 return Type;
328}
329
George Rimar98b060d2016-03-06 06:01:07 +0000330bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000331 return Type == R_386_TLS_GD;
332}
333
George Rimar98b060d2016-03-06 06:01:07 +0000334bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000335 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
336}
337
George Rimar98b060d2016-03-06 06:01:07 +0000338bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000339 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
340}
341
Rui Ueyama900e2d22016-01-29 03:51:49 +0000342void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000343 // Executable files and shared object files have
344 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000345 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000346 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000347 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000348 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
349 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000350 };
351 memcpy(Buf, V, sizeof(V));
352 return;
353 }
George Rimar648a2c32015-10-20 08:54:27 +0000354
George Rimar77b77792015-11-25 22:15:01 +0000355 const uint8_t PltData[] = {
356 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000357 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
358 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000359 };
360 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000361 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000362 write32le(Buf + 2, Got + 4);
363 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000364}
365
Rui Ueyama9398f862016-01-29 04:15:02 +0000366void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
367 uint64_t PltEntryAddr, int32_t Index,
368 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000369 const uint8_t Inst[] = {
370 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
371 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
372 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
373 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000374 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000375
George Rimar77b77792015-11-25 22:15:01 +0000376 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000377 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rafael Espindolae2f43772016-05-18 20:44:24 +0000378 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyama9398f862016-01-29 04:15:02 +0000379 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000380 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000381 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000382}
383
Rafael Espindola666625b2016-04-01 14:36:09 +0000384uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
385 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000386 switch (Type) {
387 default:
388 return 0;
389 case R_386_32:
390 case R_386_GOT32:
391 case R_386_GOTOFF:
392 case R_386_GOTPC:
393 case R_386_PC32:
394 case R_386_PLT32:
395 return read32le(Buf);
396 }
397}
398
Rafael Espindola22ef9562016-04-13 01:40:19 +0000399void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
400 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000401 checkInt<32>(Val, Type);
402 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000403}
404
Rafael Espindola22ef9562016-04-13 01:40:19 +0000405void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
406 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000407 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000408 // leal x@tlsgd(, %ebx, 1),
409 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000410 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000411 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000412 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000413 const uint8_t Inst[] = {
414 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
415 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
416 };
417 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindolaebed1fe2016-05-20 21:23:52 +0000418 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000419}
420
Rafael Espindola22ef9562016-04-13 01:40:19 +0000421void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
422 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000423 // Convert
424 // leal x@tlsgd(, %ebx, 1),
425 // call __tls_get_addr@plt
426 // to
427 // movl %gs:0, %eax
428 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000429 const uint8_t Inst[] = {
430 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
431 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
432 };
433 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola74f3dbe2016-05-20 20:09:35 +0000434 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000435}
436
George Rimar6f17e092015-12-17 09:32:21 +0000437// In some conditions, relocations can be optimized to avoid using GOT.
438// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000439void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
440 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000441 // Ulrich's document section 6.2 says that @gotntpoff can
442 // be used with MOVL or ADDL instructions.
443 // @indntpoff is similar to @gotntpoff, but for use in
444 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000445 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000446 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000447 uint8_t Reg = (Loc[-1] >> 3) & 7;
448 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000449 if (Type == R_386_TLS_IE) {
450 // For R_386_TLS_IE relocation we perform the next transformations:
451 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
452 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
453 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
454 // First one is special because when EAX is used the sequence is 5 bytes
455 // long, otherwise it is 6 bytes.
456 if (*Op == 0xa1) {
457 *Op = 0xb8;
458 } else {
459 *Inst = IsMov ? 0xc7 : 0x81;
460 *Op = 0xc0 | ((*Op >> 3) & 7);
461 }
462 } else {
463 // R_386_TLS_GOTIE relocation can be optimized to
464 // R_386_TLS_LE so that it does not use GOT.
465 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
466 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
467 // Note: gold converts to ADDL instead of LEAL.
468 *Inst = IsMov ? 0xc7 : 0x8d;
469 if (IsMov)
470 *Op = 0xc0 | ((*Op >> 3) & 7);
471 else
472 *Op = 0x80 | Reg | (Reg << 3);
473 }
Rafael Espindola8818ca62016-05-20 17:41:09 +0000474 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000475}
476
Rafael Espindola22ef9562016-04-13 01:40:19 +0000477void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
478 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000479 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000480 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000481 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000482 }
483
Rui Ueyama55274e32016-04-23 01:10:15 +0000484 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000485 // leal foo(%reg),%eax
486 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000487 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000488 // movl %gs:0,%eax
489 // nop
490 // leal 0(%esi,1),%esi
491 const uint8_t Inst[] = {
492 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
493 0x90, // nop
494 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
495 };
496 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000497}
498
Rafael Espindola7f074422015-09-22 21:35:51 +0000499X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000500 CopyRel = R_X86_64_COPY;
501 GotRel = R_X86_64_GLOB_DAT;
502 PltRel = R_X86_64_JUMP_SLOT;
503 RelativeRel = R_X86_64_RELATIVE;
504 IRelativeRel = R_X86_64_IRELATIVE;
505 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000506 TlsModuleIndexRel = R_X86_64_DTPMOD64;
507 TlsOffsetRel = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000508 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000509 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000510 TlsGdToLeSkip = 2;
511}
512
513RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
514 switch (Type) {
515 default:
516 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000517 case R_X86_64_TPOFF32:
518 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000519 case R_X86_64_TLSLD:
520 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000521 case R_X86_64_TLSGD:
522 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000523 case R_X86_64_SIZE32:
524 case R_X86_64_SIZE64:
525 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000526 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000527 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000528 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000529 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000530 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000531 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000532 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000533 case R_X86_64_GOTPCREL:
Rafael Espindoladba64b82016-05-24 11:53:15 +0000534 case R_X86_64_GOTPCRELX:
535 case R_X86_64_REX_GOTPCRELX:
Rafael Espindolafe3a2f12016-05-24 12:12:06 +0000536 case R_X86_64_GOTTPOFF:
537 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000538 }
George Rimar648a2c32015-10-20 08:54:27 +0000539}
540
Rui Ueyamac516ae12016-01-29 02:33:45 +0000541void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000542 // The first entry holds the value of _DYNAMIC. It is not clear why that is
543 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000544 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000545 // other program).
Igor Kudrin351b41d2015-11-16 17:44:08 +0000546 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
547}
548
Rui Ueyamac516ae12016-01-29 02:33:45 +0000549void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000550 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000551 write32le(Buf, Plt + 6);
552}
553
Rui Ueyama900e2d22016-01-29 03:51:49 +0000554void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000555 const uint8_t PltData[] = {
556 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
557 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
558 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
559 };
560 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000561 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
562 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
563 write32le(Buf + 2, Got - Plt + 2); // GOT+8
564 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000565}
Rafael Espindola01205f72015-09-22 18:19:46 +0000566
Rui Ueyama9398f862016-01-29 04:15:02 +0000567void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
568 uint64_t PltEntryAddr, int32_t Index,
569 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000570 const uint8_t Inst[] = {
571 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
572 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
573 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
574 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000575 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000576
George Rimar648a2c32015-10-20 08:54:27 +0000577 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
578 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000579 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000580}
581
George Rimar86971052016-03-29 08:35:42 +0000582uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
583 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
584 if (Config->Shared)
585 error(getELFRelocationTypeName(EM_X86_64, Type) +
586 " cannot be a dynamic relocation");
587 return Type;
588}
589
George Rimar98b060d2016-03-06 06:01:07 +0000590bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000591 return Type == R_X86_64_GOTTPOFF;
592}
593
George Rimar98b060d2016-03-06 06:01:07 +0000594bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000595 return Type == R_X86_64_TLSGD;
596}
597
George Rimar98b060d2016-03-06 06:01:07 +0000598bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000599 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
600 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000601}
602
Rafael Espindola22ef9562016-04-13 01:40:19 +0000603void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
604 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000605 // Convert
606 // .byte 0x66
607 // leaq x@tlsgd(%rip), %rdi
608 // .word 0x6666
609 // rex64
610 // call __tls_get_addr@plt
611 // to
612 // mov %fs:0x0,%rax
613 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000614 const uint8_t Inst[] = {
615 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
616 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
617 };
618 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000619 // The original code used a pc relative relocation and so we have to
620 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000621 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000622}
623
Rafael Espindola22ef9562016-04-13 01:40:19 +0000624void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
625 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000626 // Convert
627 // .byte 0x66
628 // leaq x@tlsgd(%rip), %rdi
629 // .word 0x6666
630 // rex64
631 // call __tls_get_addr@plt
632 // to
633 // mov %fs:0x0,%rax
634 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000635 const uint8_t Inst[] = {
636 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
637 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
638 };
639 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000640 // Both code sequences are PC relatives, but since we are moving the constant
641 // forward by 8 bytes we have to subtract the value by 8.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000642 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000643}
644
George Rimar77d1cb12015-11-24 09:00:06 +0000645// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000646// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000647void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
648 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000649 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
650 // used in MOVQ or ADDQ instructions only.
651 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
652 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
653 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
654 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
655 uint8_t *Prefix = Loc - 3;
656 uint8_t *Inst = Loc - 2;
657 uint8_t *RegSlot = Loc - 1;
658 uint8_t Reg = Loc[-1] >> 3;
659 bool IsMov = *Inst == 0x8b;
660 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000661
George Rimar77d1cb12015-11-24 09:00:06 +0000662 // r12 and rsp registers requires special handling.
663 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
664 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
665 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
666 // The same true for rsp. So we convert to addq for them, saving 1 byte that
667 // we dont have.
668 if (RspAdd)
669 *Inst = 0x81;
670 else
671 *Inst = IsMov ? 0xc7 : 0x8d;
672 if (*Prefix == 0x4c)
673 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
674 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000675 // The original code used a pc relative relocation and so we have to
676 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000677 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000678}
679
Rafael Espindola22ef9562016-04-13 01:40:19 +0000680void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
681 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000682 // Convert
683 // leaq bar@tlsld(%rip), %rdi
684 // callq __tls_get_addr@PLT
685 // leaq bar@dtpoff(%rax), %rcx
686 // to
687 // .word 0x6666
688 // .byte 0x66
689 // mov %fs:0,%rax
690 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000691 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000692 write64le(Loc, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000693 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000694 }
695 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000696 relocateOne(Loc, R_X86_64_TPOFF32, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000697 return;
George Rimar25411f252015-12-04 11:20:13 +0000698 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000699
700 const uint8_t Inst[] = {
Rui Ueyama02fcf112016-05-25 04:29:53 +0000701 0x66, 0x66, // .word 0x6666
702 0x66, // .byte 0x66
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000703 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
704 };
705 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000706}
707
Rafael Espindola22ef9562016-04-13 01:40:19 +0000708void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
709 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000710 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000711 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000712 checkUInt<32>(Val, Type);
713 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000714 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000715 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000716 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000717 case R_X86_64_GOT32:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000718 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000719 case R_X86_64_GOTPCRELX:
720 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000721 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000722 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000723 case R_X86_64_PLT32:
724 case R_X86_64_TLSGD:
725 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000726 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000727 case R_X86_64_SIZE32:
Rafael Espindolafb0ceb52016-05-20 20:02:27 +0000728 checkInt<32>(Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000729 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000730 break;
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000731 case R_X86_64_64:
732 case R_X86_64_DTPOFF64:
733 case R_X86_64_SIZE64:
734 case R_X86_64_PC64:
735 write64le(Loc, Val);
736 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000737 default:
George Rimar57610422016-03-11 14:43:02 +0000738 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000739 }
740}
741
Rafael Espindola5c66b822016-06-04 22:58:54 +0000742RelExpr X86_64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
743 RelExpr RelExpr) const {
George Rimar5c33b912016-05-25 14:31:37 +0000744 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
George Rimarf10c8292016-06-01 16:45:30 +0000745 return RelExpr;
George Rimara8f9cf12016-05-26 13:37:12 +0000746 const uint8_t Op = Data[-2];
747 const uint8_t ModRm = Data[-1];
George Rimarf10c8292016-06-01 16:45:30 +0000748 // FIXME: When PIC is disabled and foo is defined locally in the
749 // lower 32 bit address space, memory operand in mov can be converted into
750 // immediate operand. Otherwise, mov must be changed to lea. We support only
751 // latter relaxation at this moment.
George Rimar95433df2016-05-25 16:51:08 +0000752 if (Op == 0x8b)
George Rimarf10c8292016-06-01 16:45:30 +0000753 return R_RELAX_GOT_PC;
George Rimar95433df2016-05-25 16:51:08 +0000754 // Relax call and jmp.
George Rimarf10c8292016-06-01 16:45:30 +0000755 if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
756 return R_RELAX_GOT_PC;
757
758 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
759 // If PIC then no relaxation is available.
760 // We also don't relax test/binop instructions without REX byte,
761 // they are 32bit operations and not common to have.
762 assert(Type == R_X86_64_REX_GOTPCRELX);
763 return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
George Rimar5c33b912016-05-25 14:31:37 +0000764}
765
George Rimarb7204302016-06-02 09:22:00 +0000766// A subset of relaxations can only be applied for no-PIC. This method
767// handles such relaxations. Instructions encoding information was taken from:
768// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
769// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
770// 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
771void X86_64TargetInfo::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
772 uint8_t ModRm) const {
George Rimarf10c8292016-06-01 16:45:30 +0000773 const uint8_t Rex = Loc[-3];
774 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
775 if (Op == 0x85) {
776 // See "TEST-Logical Compare" (4-428 Vol. 2B),
777 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
778
779 // ModR/M byte has form XX YYY ZZZ, where
780 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
781 // XX has different meanings:
782 // 00: The operand's memory address is in reg1.
783 // 01: The operand's memory address is reg1 + a byte-sized displacement.
784 // 10: The operand's memory address is reg1 + a word-sized displacement.
785 // 11: The operand is reg1 itself.
786 // If an instruction requires only one operand, the unused reg2 field
787 // holds extra opcode bits rather than a register code
788 // 0xC0 == 11 000 000 binary.
789 // 0x38 == 00 111 000 binary.
790 // We transfer reg2 to reg1 here as operand.
791 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
792 *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
793
794 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
795 // See "TEST-Logical Compare" (4-428 Vol. 2B).
796 *(Loc - 2) = 0xf7;
797
798 // Move R bit to the B bit in REX byte.
799 // REX byte is encoded as 0100WRXB, where
800 // 0100 is 4bit fixed pattern.
801 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
802 // default operand size is used (which is 32-bit for most but not all
803 // instructions).
804 // REX.R This 1-bit value is an extension to the MODRM.reg field.
805 // REX.X This 1-bit value is an extension to the SIB.index field.
806 // REX.B This 1-bit value is an extension to the MODRM.rm field or the
807 // SIB.base field.
808 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
809 *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
810 relocateOne(Loc, R_X86_64_PC32, Val);
811 return;
812 }
813
814 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
815 // or xor operations.
816
817 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
818 // Logic is close to one for test instruction above, but we also
819 // write opcode extension here, see below for details.
820 *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
821
822 // Primary opcode is 0x81, opcode extension is one of:
823 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
824 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
825 // This value was wrote to MODRM.reg in a line above.
826 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
827 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
828 // descriptions about each operation.
829 *(Loc - 2) = 0x81;
830 *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
George Rimar5c33b912016-05-25 14:31:37 +0000831 relocateOne(Loc, R_X86_64_PC32, Val);
832}
833
George Rimarb7204302016-06-02 09:22:00 +0000834void X86_64TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
835 const uint8_t Op = Loc[-2];
836 const uint8_t ModRm = Loc[-1];
837
838 // Convert mov foo@GOTPCREL(%rip), %reg to lea foo(%rip), %reg.
839 if (Op == 0x8b) {
840 *(Loc - 2) = 0x8d;
841 relocateOne(Loc, R_X86_64_PC32, Val);
842 return;
843 }
844
845 // Convert call/jmp instructions.
846 if (Op == 0xff) {
847 if (ModRm == 0x15) {
848 // ABI says we can convert call *foo@GOTPCREL(%rip) to nop call foo.
849 // Instead we convert to addr32 call foo, where addr32 is instruction
850 // prefix. That makes result expression to be a single instruction.
851 *(Loc - 2) = 0x67; // addr32 prefix
852 *(Loc - 1) = 0xe8; // call
853 } else {
854 assert(ModRm == 0x25);
855 // Convert jmp *foo@GOTPCREL(%rip) to jmp foo nop.
856 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
857 *(Loc - 2) = 0xe9; // jmp
858 *(Loc + 3) = 0x90; // nop
859 Loc -= 1;
860 Val += 1;
861 }
862 relocateOne(Loc, R_X86_64_PC32, Val);
863 return;
864 }
865
866 assert(!Config->Pic);
867 // We are relaxing a rip relative to an absolute, so compensate
868 // for the old -4 addend.
869 relaxGotNoPic(Loc, Val + 4, Op, ModRm);
870}
871
Hal Finkel3c8cc672015-10-12 20:56:18 +0000872// Relocation masks following the #lo(value), #hi(value), #ha(value),
873// #higher(value), #highera(value), #highest(value), and #highesta(value)
874// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
875// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000876static uint16_t applyPPCLo(uint64_t V) { return V; }
877static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
878static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
879static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
880static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000881static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000882static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
883
Davide Italiano8c3444362016-01-11 19:45:33 +0000884PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000885
Rafael Espindola22ef9562016-04-13 01:40:19 +0000886void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
887 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000888 switch (Type) {
889 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000890 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000891 break;
892 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000893 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000894 break;
895 default:
George Rimar57610422016-03-11 14:43:02 +0000896 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000897 }
898}
899
Rafael Espindola22ef9562016-04-13 01:40:19 +0000900RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
901 return R_ABS;
902}
903
Rafael Espindolac4010882015-09-22 20:54:08 +0000904PPC64TargetInfo::PPC64TargetInfo() {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000905 PltRel = GotRel = R_PPC64_GLOB_DAT;
Rui Ueyama724d6252016-01-29 01:49:32 +0000906 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000907 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000908
909 // We need 64K pages (at least under glibc/Linux, the loader won't
910 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000911 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000912
913 // The PPC64 ELF ABI v1 spec, says:
914 //
915 // It is normally desirable to put segments with different characteristics
916 // in separate 256 Mbyte portions of the address space, to give the
917 // operating system full paging flexibility in the 64-bit address space.
918 //
919 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
920 // use 0x10000000 as the starting address.
921 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000922}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000923
Rafael Espindola15cec292016-04-27 12:25:22 +0000924static uint64_t PPC64TocOffset = 0x8000;
925
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000926uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +0000927 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
928 // TOC starts where the first of these sections starts. We always create a
929 // .got when we see a relocation that uses it, so for us the start is always
930 // the .got.
Hal Finkel3c8cc672015-10-12 20:56:18 +0000931 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +0000932
933 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
934 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
935 // code (crt1.o) assumes that you can get from the TOC base to the
936 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +0000937 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000938}
939
Rafael Espindola22ef9562016-04-13 01:40:19 +0000940RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
941 switch (Type) {
942 default:
943 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +0000944 case R_PPC64_TOC16:
945 case R_PPC64_TOC16_DS:
946 case R_PPC64_TOC16_HA:
947 case R_PPC64_TOC16_HI:
948 case R_PPC64_TOC16_LO:
949 case R_PPC64_TOC16_LO_DS:
950 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000951 case R_PPC64_TOC:
952 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000953 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000954 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000955 }
956}
957
Rui Ueyama9398f862016-01-29 04:15:02 +0000958void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
959 uint64_t PltEntryAddr, int32_t Index,
960 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000961 uint64_t Off = GotEntryAddr - getPPC64TocBase();
962
963 // FIXME: What we should do, in theory, is get the offset of the function
964 // descriptor in the .opd section, and use that as the offset from %r2 (the
965 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
966 // be a pointer to the function descriptor in the .opd section. Using
967 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
968
Hal Finkelfa92f682015-10-13 21:47:34 +0000969 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000970 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
971 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
972 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
973 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
974 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
975 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
976 write32be(Buf + 28, 0x4e800420); // bctr
977}
978
Rafael Espindola22ef9562016-04-13 01:40:19 +0000979void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
980 uint64_t Val) const {
Rafael Espindola15cec292016-04-27 12:25:22 +0000981 uint64_t TO = PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982
Rafael Espindola15cec292016-04-27 12:25:22 +0000983 // For a TOC-relative relocation, proceed in terms of the corresponding
984 // ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000985 switch (Type) {
Rafael Espindola15cec292016-04-27 12:25:22 +0000986 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break;
987 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break;
988 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break;
989 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break;
990 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break;
991 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000992 default: break;
993 }
994
Hal Finkel3c8cc672015-10-12 20:56:18 +0000995 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000996 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000997 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000998 // Preserve the AA/LK bits in the branch instruction
999 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001000 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001001 break;
1002 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001003 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001004 checkInt<16>(Val, Type);
1005 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001006 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001007 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001008 checkInt<16>(Val, Type);
1009 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001011 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001012 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001013 break;
1014 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001015 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001016 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001017 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001018 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019 break;
1020 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001021 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001022 break;
1023 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001024 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001025 break;
1026 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001027 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001028 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001029 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001030 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001031 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001032 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001033 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001034 break;
1035 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001036 checkInt<32>(Val, Type);
1037 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001039 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001040 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001041 break;
1042 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001043 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001044 break;
1045 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001046 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001047 break;
1048 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001049 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001050 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 case R_PPC64_REL24: {
1052 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001053 checkInt<24>(Val, Type);
1054 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 break;
1056 }
1057 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001058 checkInt<32>(Val, Type);
1059 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060 break;
1061 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001062 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001064 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001065 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001066 break;
1067 default:
George Rimar57610422016-03-11 14:43:02 +00001068 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001069 }
1070}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001071
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001072AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001073 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001074 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001075 IRelativeRel = R_AARCH64_IRELATIVE;
1076 GotRel = R_AARCH64_GLOB_DAT;
1077 PltRel = R_AARCH64_JUMP_SLOT;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001078 TlsDescRel = R_AARCH64_TLSDESC;
Rui Ueyama724d6252016-01-29 01:49:32 +00001079 TlsGotRel = R_AARCH64_TLS_TPREL64;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001080 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001081 PltZeroSize = 32;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001082
1083 // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
1084 // 1 of the tls structures and the tcb size is 16.
1085 TcbSize = 16;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001086}
George Rimar648a2c32015-10-20 08:54:27 +00001087
Rafael Espindola22ef9562016-04-13 01:40:19 +00001088RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1089 const SymbolBody &S) const {
1090 switch (Type) {
1091 default:
1092 return R_ABS;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001093
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001094 case R_AARCH64_TLSDESC_ADR_PAGE21:
1095 return R_TLSDESC_PAGE;
1096
1097 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1098 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1099 return R_TLSDESC;
1100
1101 case R_AARCH64_TLSDESC_CALL:
1102 return R_HINT;
1103
Rafael Espindola8818ca62016-05-20 17:41:09 +00001104 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1105 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1106 return R_TLS;
1107
Rafael Espindola22ef9562016-04-13 01:40:19 +00001108 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +00001109 case R_AARCH64_CONDBR19:
1110 case R_AARCH64_JUMP26:
1111 case R_AARCH64_TSTBR14:
1112 return R_PLT_PC;
1113
Rafael Espindola22ef9562016-04-13 01:40:19 +00001114 case R_AARCH64_PREL16:
1115 case R_AARCH64_PREL32:
1116 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001117 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001118 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001119 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001120 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001121 case R_AARCH64_LD64_GOT_LO12_NC:
1122 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1123 return R_GOT;
1124 case R_AARCH64_ADR_GOT_PAGE:
1125 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1126 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001127 }
1128}
1129
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001130bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001131 switch (Type) {
1132 default:
1133 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001134 case R_AARCH64_ADD_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001135 case R_AARCH64_LD64_GOT_LO12_NC:
1136 case R_AARCH64_LDST128_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001137 case R_AARCH64_LDST16_ABS_LO12_NC:
1138 case R_AARCH64_LDST32_ABS_LO12_NC:
1139 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001140 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001141 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1142 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +00001143 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001144 return true;
1145 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001146}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001147
George Rimar98b060d2016-03-06 06:01:07 +00001148bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001149 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1150 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1151}
1152
George Rimar98b060d2016-03-06 06:01:07 +00001153uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001154 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1155 return Type;
1156 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001157 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001158 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001159 // Keep it going with a dummy value so that we can find more reloc errors.
1160 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001161}
1162
Rui Ueyamac516ae12016-01-29 02:33:45 +00001163void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001164 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1165}
1166
Rafael Espindola22ef9562016-04-13 01:40:19 +00001167static uint64_t getAArch64Page(uint64_t Expr) {
1168 return Expr & (~static_cast<uint64_t>(0xFFF));
1169}
1170
Rui Ueyama900e2d22016-01-29 03:51:49 +00001171void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001172 const uint8_t PltData[] = {
1173 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1174 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1175 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1176 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1177 0x20, 0x02, 0x1f, 0xd6, // br x17
1178 0x1f, 0x20, 0x03, 0xd5, // nop
1179 0x1f, 0x20, 0x03, 0xd5, // nop
1180 0x1f, 0x20, 0x03, 0xd5 // nop
1181 };
1182 memcpy(Buf, PltData, sizeof(PltData));
1183
Rui Ueyama900e2d22016-01-29 03:51:49 +00001184 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1185 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001186 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1187 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1188 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1189 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001190}
1191
Rui Ueyama9398f862016-01-29 04:15:02 +00001192void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1193 uint64_t PltEntryAddr, int32_t Index,
1194 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001195 const uint8_t Inst[] = {
1196 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1197 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1198 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1199 0x20, 0x02, 0x1f, 0xd6 // br x17
1200 };
1201 memcpy(Buf, Inst, sizeof(Inst));
1202
Rafael Espindola22ef9562016-04-13 01:40:19 +00001203 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1204 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1205 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1206 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001207}
1208
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001209static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001210 uint32_t ImmLo = (Imm & 0x3) << 29;
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001211 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
1212 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001213 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001214}
1215
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001216static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1217 or32le(L, (Imm & 0xFFF) << 10);
1218}
1219
Rafael Espindola22ef9562016-04-13 01:40:19 +00001220void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1221 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001222 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001223 case R_AARCH64_ABS16:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001224 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001225 checkIntUInt<16>(Val, Type);
1226 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001227 break;
1228 case R_AARCH64_ABS32:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001229 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001230 checkIntUInt<32>(Val, Type);
1231 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001232 break;
1233 case R_AARCH64_ABS64:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001234 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001235 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001236 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001237 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001238 // This relocation stores 12 bits and there's no instruction
1239 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001240 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1241 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001242 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001243 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001244 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001245 case R_AARCH64_ADR_PREL_PG_HI21:
1246 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001247 case R_AARCH64_TLSDESC_ADR_PAGE21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001248 checkInt<33>(Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001249 updateAArch64Addr(Loc, Val >> 12);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001250 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001251 case R_AARCH64_ADR_PREL_LO21:
1252 checkInt<21>(Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001253 updateAArch64Addr(Loc, Val);
Davide Italiano1d750a62015-09-27 08:45:38 +00001254 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001255 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001256 case R_AARCH64_JUMP26:
1257 checkInt<28>(Val, Type);
1258 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001259 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001260 case R_AARCH64_CONDBR19:
1261 checkInt<21>(Val, Type);
1262 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001263 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001264 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001265 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001266 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001267 checkAlignment<8>(Val, Type);
1268 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001269 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001270 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001271 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001272 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001273 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001274 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001275 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001276 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001277 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001278 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001279 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001280 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001281 break;
1282 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001283 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001284 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001285 case R_AARCH64_TSTBR14:
1286 checkInt<16>(Val, Type);
1287 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001288 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001289 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1290 checkInt<24>(Val, Type);
Rafael Espindola1016f192016-06-02 15:51:40 +00001291 updateAArch64Add(Loc, Val >> 12);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001292 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001293 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001294 case R_AARCH64_TLSDESC_ADD_LO12_NC:
Rafael Espindola1016f192016-06-02 15:51:40 +00001295 updateAArch64Add(Loc, Val);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001296 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001297 default:
George Rimar57610422016-03-11 14:43:02 +00001298 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001299 }
1300}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001301
Rafael Espindola22ef9562016-04-13 01:40:19 +00001302void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1303 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001304 // TLSDESC Global-Dynamic relocation are in the form:
1305 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1306 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1307 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1308 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1309 // And it can optimized to:
1310 // movz x0, #0x0, lsl #16
1311 // movk x0, #0x10
1312 // nop
1313 // nop
Rafael Espindola8818ca62016-05-20 17:41:09 +00001314 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001315
1316 uint32_t NewInst;
1317 switch (Type) {
1318 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1319 case R_AARCH64_TLSDESC_CALL:
1320 // nop
1321 NewInst = 0xd503201f;
1322 break;
1323 case R_AARCH64_TLSDESC_ADR_PAGE21:
1324 // movz
Rafael Espindola8818ca62016-05-20 17:41:09 +00001325 NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001326 break;
1327 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1328 // movk
Rafael Espindola8818ca62016-05-20 17:41:09 +00001329 NewInst = 0xf2800000 | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001330 break;
1331 default:
George Rimar777f9632016-03-12 08:31:34 +00001332 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001333 }
1334 write32le(Loc, NewInst);
1335}
1336
Rafael Espindola22ef9562016-04-13 01:40:19 +00001337void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1338 uint64_t Val) const {
Rafael Espindola8818ca62016-05-20 17:41:09 +00001339 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001340
George Rimar4d1d16d2016-03-06 06:16:05 +00001341 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001342 uint32_t NewInst;
1343 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1344 // Generate movz.
1345 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001346 NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001347 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1348 // Generate movk
1349 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001350 NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001351 } else {
George Rimar777f9632016-03-12 08:31:34 +00001352 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001353 }
1354 write32le(Loc, NewInst);
1355}
1356
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001357// Implementing relocations for AMDGPU is low priority since most
1358// programs don't use relocations now. Thus, this function is not
1359// actually called (relocateOne is called for each relocation).
1360// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001361void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1362 uint64_t Val) const {
1363 llvm_unreachable("not implemented");
1364}
1365
1366RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001367 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001368}
1369
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001370template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001371 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001372 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001373 PltEntrySize = 16;
1374 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001375 ThunkSize = 16;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001376 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001377 PltRel = R_MIPS_JUMP_SLOT;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001378 if (ELFT::Is64Bits)
1379 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1380 else
1381 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001382}
1383
1384template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001385RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1386 const SymbolBody &S) const {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001387 if (ELFT::Is64Bits)
1388 // See comment in the calculateMips64RelChain.
1389 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001390 switch (Type) {
1391 default:
1392 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001393 case R_MIPS_JALR:
1394 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001395 case R_MIPS_GPREL16:
1396 case R_MIPS_GPREL32:
1397 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001398 case R_MIPS_26:
1399 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001400 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001401 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001402 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001403 // MIPS _gp_disp designates offset between start of function and 'gp'
1404 // pointer into GOT. __gnu_local_gp is equal to the current value of
1405 // the 'gp'. Therefore any relocations against them do not require
1406 // dynamic relocation.
1407 if (&S == ElfSym<ELFT>::MipsGpDisp)
1408 return R_PC;
1409 return R_ABS;
1410 case R_MIPS_PC32:
1411 case R_MIPS_PC16:
1412 case R_MIPS_PC19_S2:
1413 case R_MIPS_PC21_S2:
1414 case R_MIPS_PC26_S2:
1415 case R_MIPS_PCHI16:
1416 case R_MIPS_PCLO16:
1417 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001418 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001419 if (S.isLocal())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001420 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001421 // fallthrough
1422 case R_MIPS_CALL16:
1423 case R_MIPS_GOT_DISP:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001424 if (!S.isPreemptible())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001425 return R_MIPS_GOT_LOCAL;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001426 return R_GOT_OFF;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001427 case R_MIPS_GOT_PAGE:
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001428 return R_MIPS_GOT_LOCAL_PAGE;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001429 }
1430}
1431
1432template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001433uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001434 if (Type == R_MIPS_32 || Type == R_MIPS_64)
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001435 return RelativeRel;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001436 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001437 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001438 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001439 // Keep it going with a dummy value so that we can find more reloc errors.
1440 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001441}
1442
1443template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001444void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1445 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001446}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001447
Simon Atanasyan35031192015-12-15 06:06:34 +00001448static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001449
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001450template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001451static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001452 uint32_t Instr = read32<E>(Loc);
1453 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1454 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1455}
1456
1457template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001458static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001459 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001460 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001461 if (SHIFT > 0)
1462 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001463 checkInt<BSIZE + SHIFT>(V, Type);
1464 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001465}
1466
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001467template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001468static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001469 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001470 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001471}
1472
Simon Atanasyan3b377852016-03-04 10:55:20 +00001473template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001474static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1475 uint32_t Instr = read32<E>(Loc);
1476 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1477}
1478
Rafael Espindola666625b2016-04-01 14:36:09 +00001479template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001480 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1481}
1482
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001483template <class ELFT>
1484void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1485 const endianness E = ELFT::TargetEndianness;
1486 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1487 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1488 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1489 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1490 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1491 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1492 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1493 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1494 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001495 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001496 writeMipsLo16<E>(Buf + 4, Got);
1497 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001498}
1499
1500template <class ELFT>
1501void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1502 uint64_t PltEntryAddr, int32_t Index,
1503 unsigned RelOff) const {
1504 const endianness E = ELFT::TargetEndianness;
1505 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1506 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1507 write32<E>(Buf + 8, 0x03200008); // jr $25
1508 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001509 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001510 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1511 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001512}
1513
1514template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001515void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1516 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1517 // See MipsTargetInfo::writeThunk for details.
1518 const endianness E = ELFT::TargetEndianness;
1519 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1520 write32<E>(Buf + 4, 0x08000000); // j func
1521 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1522 write32<E>(Buf + 12, 0x00000000); // nop
1523 writeMipsHi16<E>(Buf, S);
1524 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1525 writeMipsLo16<E>(Buf + 8, S);
1526}
1527
1528template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001529bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1530 const SymbolBody &S) const {
1531 // Any MIPS PIC code function is invoked with its address in register $t9.
1532 // So if we have a branch instruction from non-PIC code to the PIC one
1533 // we cannot make the jump directly and need to create a small stubs
1534 // to save the target function address.
1535 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1536 if (Type != R_MIPS_26)
1537 return false;
1538 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1539 if (!F)
1540 return false;
1541 // If current file has PIC code, LA25 stub is not required.
1542 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1543 return false;
1544 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1545 if (!D || !D->Section)
1546 return false;
1547 // LA25 is required if target file has PIC code
1548 // or target symbol is a PIC symbol.
1549 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001550 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001551}
1552
1553template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001554uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001555 uint32_t Type) const {
1556 const endianness E = ELFT::TargetEndianness;
1557 switch (Type) {
1558 default:
1559 return 0;
1560 case R_MIPS_32:
1561 case R_MIPS_GPREL32:
1562 return read32<E>(Buf);
1563 case R_MIPS_26:
1564 // FIXME (simon): If the relocation target symbol is not a PLT entry
1565 // we should use another expression for calculation:
1566 // ((A << 2) | (P & 0xf0000000)) >> 2
1567 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1568 case R_MIPS_GPREL16:
1569 case R_MIPS_LO16:
1570 case R_MIPS_PCLO16:
1571 case R_MIPS_TLS_DTPREL_HI16:
1572 case R_MIPS_TLS_DTPREL_LO16:
1573 case R_MIPS_TLS_TPREL_HI16:
1574 case R_MIPS_TLS_TPREL_LO16:
1575 return readSignedLo16<E>(Buf);
1576 case R_MIPS_PC16:
1577 return getPcRelocAddend<E, 16, 2>(Buf);
1578 case R_MIPS_PC19_S2:
1579 return getPcRelocAddend<E, 19, 2>(Buf);
1580 case R_MIPS_PC21_S2:
1581 return getPcRelocAddend<E, 21, 2>(Buf);
1582 case R_MIPS_PC26_S2:
1583 return getPcRelocAddend<E, 26, 2>(Buf);
1584 case R_MIPS_PC32:
1585 return getPcRelocAddend<E, 32, 0>(Buf);
1586 }
1587}
1588
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001589static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1590 uint64_t Val) {
1591 // MIPS N64 ABI packs multiple relocations into the single relocation
1592 // record. In general, all up to three relocations can have arbitrary
1593 // types. In fact, Clang and GCC uses only a few combinations. For now,
1594 // we support two of them. That is allow to pass at least all LLVM
1595 // test suite cases.
1596 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1597 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1598 // The first relocation is a 'real' relocation which is calculated
1599 // using the corresponding symbol's value. The second and the third
1600 // relocations used to modify result of the first one: extend it to
1601 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1602 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1603 uint32_t Type2 = (Type >> 8) & 0xff;
1604 uint32_t Type3 = (Type >> 16) & 0xff;
1605 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1606 return std::make_pair(Type, Val);
1607 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1608 return std::make_pair(Type2, Val);
1609 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1610 return std::make_pair(Type3, -Val);
1611 error("unsupported relocations combination " + Twine(Type));
1612 return std::make_pair(Type & 0xff, Val);
1613}
1614
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001615template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001616void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1617 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001618 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001619 // Thread pointer and DRP offsets from the start of TLS data area.
1620 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001621 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1622 Val -= 0x8000;
1623 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1624 Val -= 0x7000;
1625 if (ELFT::Is64Bits)
1626 std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001627 switch (Type) {
1628 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001629 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001630 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001631 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001632 case R_MIPS_64:
1633 write64<E>(Loc, Val);
1634 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00001635 case R_MIPS_26:
1636 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001637 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001638 case R_MIPS_GOT_DISP:
1639 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001640 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001641 case R_MIPS_GPREL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001642 checkInt<16>(Val, Type);
1643 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001644 case R_MIPS_CALL16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001645 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001646 case R_MIPS_LO16:
1647 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001648 case R_MIPS_TLS_DTPREL_LO16:
1649 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001650 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001651 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001652 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001653 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001654 case R_MIPS_TLS_DTPREL_HI16:
1655 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001656 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001657 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001658 case R_MIPS_JALR:
1659 // Ignore this optimization relocation for now
1660 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001661 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001662 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001663 break;
1664 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001665 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001666 break;
1667 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001668 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001669 break;
1670 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001671 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001672 break;
1673 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001674 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001675 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001676 default:
George Rimar57610422016-03-11 14:43:02 +00001677 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001678 }
1679}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001680
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001681template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001682bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00001683 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001684}
Rafael Espindola01205f72015-09-22 18:19:46 +00001685}
1686}