blob: 529abacae5f1cf8b83001954c154451f2f5e003c [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 Espindola69f54022016-06-04 23:22:34 +000095 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
96 RelExpr Expr) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000097 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
98 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
99 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
100 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000101};
102
103class X86_64TargetInfo final : public TargetInfo {
104public:
105 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000106 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000107 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000108 bool isTlsLocalDynamicRel(uint32_t Type) const override;
109 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
110 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000111 void writeGotPltHeader(uint8_t *Buf) const override;
112 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000113 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000114 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
115 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000116 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000117
Rafael Espindola5c66b822016-06-04 22:58:54 +0000118 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
119 RelExpr Expr) const override;
George Rimar5c33b912016-05-25 14:31:37 +0000120 void relaxGot(uint8_t *Loc, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000121 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
122 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
123 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
124 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimarb7204302016-06-02 09:22:00 +0000125
126private:
127 void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
128 uint8_t ModRm) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129};
130
Davide Italiano8c3444362016-01-11 19:45:33 +0000131class PPCTargetInfo final : public TargetInfo {
132public:
133 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000134 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000135 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000136};
137
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000138class PPC64TargetInfo final : public TargetInfo {
139public:
140 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000141 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000142 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
143 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000144 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000145};
146
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000147class AArch64TargetInfo final : public TargetInfo {
148public:
149 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000150 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000151 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000152 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000153 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000154 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000155 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
156 int32_t Index, unsigned RelOff) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000157 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000158 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000159 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
160 RelExpr Expr) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000161 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000162 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000163 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000164};
165
Tom Stellard80efb162016-01-07 03:59:08 +0000166class AMDGPUTargetInfo final : public TargetInfo {
167public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000168 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000169 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
170 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000171};
172
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173template <class ELFT> class MipsTargetInfo final : public TargetInfo {
174public:
175 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000176 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000177 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000178 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000179 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
180 void writePltZero(uint8_t *Buf) const override;
181 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
182 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000183 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000184 bool needsThunk(uint32_t Type, const InputFile &File,
185 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000186 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000187 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000188};
189} // anonymous namespace
190
Rui Ueyama91004392015-10-13 16:08:15 +0000191TargetInfo *createTarget() {
192 switch (Config->EMachine) {
193 case EM_386:
194 return new X86TargetInfo();
195 case EM_AARCH64:
196 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000197 case EM_AMDGPU:
198 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000199 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000200 switch (Config->EKind) {
201 case ELF32LEKind:
202 return new MipsTargetInfo<ELF32LE>();
203 case ELF32BEKind:
204 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000205 case ELF64LEKind:
206 return new MipsTargetInfo<ELF64LE>();
207 case ELF64BEKind:
208 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000209 default:
George Rimar777f9632016-03-12 08:31:34 +0000210 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000211 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000212 case EM_PPC:
213 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000214 case EM_PPC64:
215 return new PPC64TargetInfo();
216 case EM_X86_64:
217 return new X86_64TargetInfo();
218 }
George Rimar777f9632016-03-12 08:31:34 +0000219 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000220}
221
Rafael Espindola01205f72015-09-22 18:19:46 +0000222TargetInfo::~TargetInfo() {}
223
Rafael Espindola666625b2016-04-01 14:36:09 +0000224uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
225 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000226 return 0;
227}
228
George Rimar786e8662016-03-17 05:57:33 +0000229uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000230
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000231bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000232
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000233bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
234 const SymbolBody &S) const {
235 return false;
236}
237
George Rimar98b060d2016-03-06 06:01:07 +0000238bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000239
George Rimar98b060d2016-03-06 06:01:07 +0000240bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000241
George Rimar98b060d2016-03-06 06:01:07 +0000242bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000243 return false;
244}
245
Rafael Espindola5c66b822016-06-04 22:58:54 +0000246RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
247 RelExpr Expr) const {
George Rimarf10c8292016-06-01 16:45:30 +0000248 return Expr;
George Rimar5c33b912016-05-25 14:31:37 +0000249}
250
251void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
252 llvm_unreachable("Should not have claimed to be relaxable");
253}
254
Rafael Espindola22ef9562016-04-13 01:40:19 +0000255void TargetInfo::relaxTlsGdToLe(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::relaxTlsGdToIe(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::relaxTlsIeToLe(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");
268}
269
Rafael Espindola22ef9562016-04-13 01:40:19 +0000270void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
271 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000272 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000273}
George Rimar77d1cb12015-11-24 09:00:06 +0000274
Rafael Espindola7f074422015-09-22 21:35:51 +0000275X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000276 CopyRel = R_386_COPY;
277 GotRel = R_386_GLOB_DAT;
278 PltRel = R_386_JUMP_SLOT;
279 IRelativeRel = R_386_IRELATIVE;
280 RelativeRel = R_386_RELATIVE;
281 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000282 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
283 TlsOffsetRel = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000284 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000285 PltZeroSize = 16;
Rafael Espindolaf807d472016-06-04 23:04:39 +0000286 TlsGdRelaxSkip = 2;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000287}
288
289RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
290 switch (Type) {
291 default:
292 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000293 case R_386_TLS_GD:
294 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000295 case R_386_TLS_LDM:
296 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000297 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000298 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000299 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000300 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000301 case R_386_GOTPC:
302 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000303 case R_386_TLS_IE:
304 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000305 case R_386_GOT32:
306 case R_386_TLS_GOTIE:
307 return R_GOT_FROM_END;
308 case R_386_GOTOFF:
309 return R_GOTREL;
310 case R_386_TLS_LE:
311 return R_TLS;
312 case R_386_TLS_LE_32:
313 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000314 }
George Rimar77b77792015-11-25 22:15:01 +0000315}
316
Rafael Espindola69f54022016-06-04 23:22:34 +0000317RelExpr X86TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
318 RelExpr Expr) const {
319 switch (Expr) {
320 default:
321 return Expr;
322 case R_RELAX_TLS_GD_TO_IE:
323 return R_RELAX_TLS_GD_TO_IE_END;
324 case R_RELAX_TLS_GD_TO_LE:
325 return R_RELAX_TLS_GD_TO_LE_NEG;
326 }
327}
328
Rui Ueyamac516ae12016-01-29 02:33:45 +0000329void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000330 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
331}
332
Rui Ueyamac516ae12016-01-29 02:33:45 +0000333void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000334 // Entries in .got.plt initially points back to the corresponding
335 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000336 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000337}
Rafael Espindola01205f72015-09-22 18:19:46 +0000338
George Rimar98b060d2016-03-06 06:01:07 +0000339uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000340 if (Type == R_386_TLS_LE)
341 return R_386_TLS_TPOFF;
342 if (Type == R_386_TLS_LE_32)
343 return R_386_TLS_TPOFF32;
344 return Type;
345}
346
George Rimar98b060d2016-03-06 06:01:07 +0000347bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000348 return Type == R_386_TLS_GD;
349}
350
George Rimar98b060d2016-03-06 06:01:07 +0000351bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000352 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
353}
354
George Rimar98b060d2016-03-06 06:01:07 +0000355bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000356 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
357}
358
Rui Ueyama900e2d22016-01-29 03:51:49 +0000359void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000360 // Executable files and shared object files have
361 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000362 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000363 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000364 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000365 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
366 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000367 };
368 memcpy(Buf, V, sizeof(V));
369 return;
370 }
George Rimar648a2c32015-10-20 08:54:27 +0000371
George Rimar77b77792015-11-25 22:15:01 +0000372 const uint8_t PltData[] = {
373 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000374 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
375 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000376 };
377 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000378 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000379 write32le(Buf + 2, Got + 4);
380 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000381}
382
Rui Ueyama9398f862016-01-29 04:15:02 +0000383void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
384 uint64_t PltEntryAddr, int32_t Index,
385 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000386 const uint8_t Inst[] = {
387 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
388 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
389 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
390 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000391 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000392
George Rimar77b77792015-11-25 22:15:01 +0000393 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000394 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rafael Espindolae2f43772016-05-18 20:44:24 +0000395 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyama9398f862016-01-29 04:15:02 +0000396 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000397 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000398 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000399}
400
Rafael Espindola666625b2016-04-01 14:36:09 +0000401uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
402 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000403 switch (Type) {
404 default:
405 return 0;
406 case R_386_32:
407 case R_386_GOT32:
408 case R_386_GOTOFF:
409 case R_386_GOTPC:
410 case R_386_PC32:
411 case R_386_PLT32:
412 return read32le(Buf);
413 }
414}
415
Rafael Espindola22ef9562016-04-13 01:40:19 +0000416void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
417 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000418 checkInt<32>(Val, Type);
419 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000420}
421
Rafael Espindola22ef9562016-04-13 01:40:19 +0000422void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
423 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000424 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000425 // leal x@tlsgd(, %ebx, 1),
426 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000427 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000428 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000429 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000430 const uint8_t Inst[] = {
431 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
432 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
433 };
434 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindolaebed1fe2016-05-20 21:23:52 +0000435 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000436}
437
Rafael Espindola22ef9562016-04-13 01:40:19 +0000438void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
439 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000440 // Convert
441 // leal x@tlsgd(, %ebx, 1),
442 // call __tls_get_addr@plt
443 // to
444 // movl %gs:0, %eax
445 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000446 const uint8_t Inst[] = {
447 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
448 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
449 };
450 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola74f3dbe2016-05-20 20:09:35 +0000451 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000452}
453
George Rimar6f17e092015-12-17 09:32:21 +0000454// In some conditions, relocations can be optimized to avoid using GOT.
455// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000456void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
457 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000458 // Ulrich's document section 6.2 says that @gotntpoff can
459 // be used with MOVL or ADDL instructions.
460 // @indntpoff is similar to @gotntpoff, but for use in
461 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000462 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000463 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000464 uint8_t Reg = (Loc[-1] >> 3) & 7;
465 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000466 if (Type == R_386_TLS_IE) {
467 // For R_386_TLS_IE relocation we perform the next transformations:
468 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
469 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
470 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
471 // First one is special because when EAX is used the sequence is 5 bytes
472 // long, otherwise it is 6 bytes.
473 if (*Op == 0xa1) {
474 *Op = 0xb8;
475 } else {
476 *Inst = IsMov ? 0xc7 : 0x81;
477 *Op = 0xc0 | ((*Op >> 3) & 7);
478 }
479 } else {
480 // R_386_TLS_GOTIE relocation can be optimized to
481 // R_386_TLS_LE so that it does not use GOT.
482 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
483 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
484 // Note: gold converts to ADDL instead of LEAL.
485 *Inst = IsMov ? 0xc7 : 0x8d;
486 if (IsMov)
487 *Op = 0xc0 | ((*Op >> 3) & 7);
488 else
489 *Op = 0x80 | Reg | (Reg << 3);
490 }
Rafael Espindola8818ca62016-05-20 17:41:09 +0000491 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000492}
493
Rafael Espindola22ef9562016-04-13 01:40:19 +0000494void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
495 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000496 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000497 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000498 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000499 }
500
Rui Ueyama55274e32016-04-23 01:10:15 +0000501 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000502 // leal foo(%reg),%eax
503 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000504 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000505 // movl %gs:0,%eax
506 // nop
507 // leal 0(%esi,1),%esi
508 const uint8_t Inst[] = {
509 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
510 0x90, // nop
511 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
512 };
513 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000514}
515
Rafael Espindola7f074422015-09-22 21:35:51 +0000516X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000517 CopyRel = R_X86_64_COPY;
518 GotRel = R_X86_64_GLOB_DAT;
519 PltRel = R_X86_64_JUMP_SLOT;
520 RelativeRel = R_X86_64_RELATIVE;
521 IRelativeRel = R_X86_64_IRELATIVE;
522 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000523 TlsModuleIndexRel = R_X86_64_DTPMOD64;
524 TlsOffsetRel = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000525 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000526 PltZeroSize = 16;
Rafael Espindolaf807d472016-06-04 23:04:39 +0000527 TlsGdRelaxSkip = 2;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000528}
529
530RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
531 switch (Type) {
532 default:
533 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000534 case R_X86_64_TPOFF32:
535 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000536 case R_X86_64_TLSLD:
537 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000538 case R_X86_64_TLSGD:
539 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000540 case R_X86_64_SIZE32:
541 case R_X86_64_SIZE64:
542 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000543 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000544 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000545 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000546 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000547 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000548 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000549 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000550 case R_X86_64_GOTPCREL:
Rafael Espindoladba64b82016-05-24 11:53:15 +0000551 case R_X86_64_GOTPCRELX:
552 case R_X86_64_REX_GOTPCRELX:
Rafael Espindolafe3a2f12016-05-24 12:12:06 +0000553 case R_X86_64_GOTTPOFF:
554 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000555 }
George Rimar648a2c32015-10-20 08:54:27 +0000556}
557
Rui Ueyamac516ae12016-01-29 02:33:45 +0000558void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000559 // The first entry holds the value of _DYNAMIC. It is not clear why that is
560 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000561 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000562 // other program).
Igor Kudrin351b41d2015-11-16 17:44:08 +0000563 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
564}
565
Rui Ueyamac516ae12016-01-29 02:33:45 +0000566void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000567 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000568 write32le(Buf, Plt + 6);
569}
570
Rui Ueyama900e2d22016-01-29 03:51:49 +0000571void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000572 const uint8_t PltData[] = {
573 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
574 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
575 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
576 };
577 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000578 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
579 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
580 write32le(Buf + 2, Got - Plt + 2); // GOT+8
581 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000582}
Rafael Espindola01205f72015-09-22 18:19:46 +0000583
Rui Ueyama9398f862016-01-29 04:15:02 +0000584void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
585 uint64_t PltEntryAddr, int32_t Index,
586 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000587 const uint8_t Inst[] = {
588 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
589 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
590 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
591 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000592 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000593
George Rimar648a2c32015-10-20 08:54:27 +0000594 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
595 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000596 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000597}
598
George Rimar86971052016-03-29 08:35:42 +0000599uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
600 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
601 if (Config->Shared)
602 error(getELFRelocationTypeName(EM_X86_64, Type) +
603 " cannot be a dynamic relocation");
604 return Type;
605}
606
George Rimar98b060d2016-03-06 06:01:07 +0000607bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000608 return Type == R_X86_64_GOTTPOFF;
609}
610
George Rimar98b060d2016-03-06 06:01:07 +0000611bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000612 return Type == R_X86_64_TLSGD;
613}
614
George Rimar98b060d2016-03-06 06:01:07 +0000615bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000616 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
617 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000618}
619
Rafael Espindola22ef9562016-04-13 01:40:19 +0000620void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
621 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000622 // Convert
623 // .byte 0x66
624 // leaq x@tlsgd(%rip), %rdi
625 // .word 0x6666
626 // rex64
627 // call __tls_get_addr@plt
628 // to
629 // mov %fs:0x0,%rax
630 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000631 const uint8_t Inst[] = {
632 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
633 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
634 };
635 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000636 // The original code used a pc relative relocation and so we have to
637 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000638 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000639}
640
Rafael Espindola22ef9562016-04-13 01:40:19 +0000641void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
642 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000643 // Convert
644 // .byte 0x66
645 // leaq x@tlsgd(%rip), %rdi
646 // .word 0x6666
647 // rex64
648 // call __tls_get_addr@plt
649 // to
650 // mov %fs:0x0,%rax
651 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000652 const uint8_t Inst[] = {
653 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
654 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
655 };
656 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000657 // Both code sequences are PC relatives, but since we are moving the constant
658 // forward by 8 bytes we have to subtract the value by 8.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000659 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000660}
661
George Rimar77d1cb12015-11-24 09:00:06 +0000662// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000663// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000664void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
665 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000666 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
667 // used in MOVQ or ADDQ instructions only.
668 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
669 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
670 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
671 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
672 uint8_t *Prefix = Loc - 3;
673 uint8_t *Inst = Loc - 2;
674 uint8_t *RegSlot = Loc - 1;
675 uint8_t Reg = Loc[-1] >> 3;
676 bool IsMov = *Inst == 0x8b;
677 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000678
George Rimar77d1cb12015-11-24 09:00:06 +0000679 // r12 and rsp registers requires special handling.
680 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
681 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
682 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
683 // The same true for rsp. So we convert to addq for them, saving 1 byte that
684 // we dont have.
685 if (RspAdd)
686 *Inst = 0x81;
687 else
688 *Inst = IsMov ? 0xc7 : 0x8d;
689 if (*Prefix == 0x4c)
690 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
691 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000692 // The original code used a pc relative relocation and so we have to
693 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000694 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000695}
696
Rafael Espindola22ef9562016-04-13 01:40:19 +0000697void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
698 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000699 // Convert
700 // leaq bar@tlsld(%rip), %rdi
701 // callq __tls_get_addr@PLT
702 // leaq bar@dtpoff(%rax), %rcx
703 // to
704 // .word 0x6666
705 // .byte 0x66
706 // mov %fs:0,%rax
707 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000708 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000709 write64le(Loc, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000710 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000711 }
712 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000713 relocateOne(Loc, R_X86_64_TPOFF32, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000714 return;
George Rimar25411f252015-12-04 11:20:13 +0000715 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000716
717 const uint8_t Inst[] = {
Rui Ueyama02fcf112016-05-25 04:29:53 +0000718 0x66, 0x66, // .word 0x6666
719 0x66, // .byte 0x66
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000720 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
721 };
722 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000723}
724
Rafael Espindola22ef9562016-04-13 01:40:19 +0000725void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
726 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000727 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000728 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000729 checkUInt<32>(Val, Type);
730 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000731 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000732 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000733 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000734 case R_X86_64_GOT32:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000735 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000736 case R_X86_64_GOTPCRELX:
737 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000738 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000739 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000740 case R_X86_64_PLT32:
741 case R_X86_64_TLSGD:
742 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000743 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000744 case R_X86_64_SIZE32:
Rafael Espindolafb0ceb52016-05-20 20:02:27 +0000745 checkInt<32>(Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000746 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000747 break;
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000748 case R_X86_64_64:
749 case R_X86_64_DTPOFF64:
750 case R_X86_64_SIZE64:
751 case R_X86_64_PC64:
752 write64le(Loc, Val);
753 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000754 default:
George Rimar57610422016-03-11 14:43:02 +0000755 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000756 }
757}
758
Rafael Espindola5c66b822016-06-04 22:58:54 +0000759RelExpr X86_64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
760 RelExpr RelExpr) const {
George Rimar5c33b912016-05-25 14:31:37 +0000761 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
George Rimarf10c8292016-06-01 16:45:30 +0000762 return RelExpr;
George Rimara8f9cf12016-05-26 13:37:12 +0000763 const uint8_t Op = Data[-2];
764 const uint8_t ModRm = Data[-1];
George Rimarf10c8292016-06-01 16:45:30 +0000765 // FIXME: When PIC is disabled and foo is defined locally in the
766 // lower 32 bit address space, memory operand in mov can be converted into
767 // immediate operand. Otherwise, mov must be changed to lea. We support only
768 // latter relaxation at this moment.
George Rimar95433df2016-05-25 16:51:08 +0000769 if (Op == 0x8b)
George Rimarf10c8292016-06-01 16:45:30 +0000770 return R_RELAX_GOT_PC;
George Rimar95433df2016-05-25 16:51:08 +0000771 // Relax call and jmp.
George Rimarf10c8292016-06-01 16:45:30 +0000772 if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
773 return R_RELAX_GOT_PC;
774
775 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
776 // If PIC then no relaxation is available.
777 // We also don't relax test/binop instructions without REX byte,
778 // they are 32bit operations and not common to have.
779 assert(Type == R_X86_64_REX_GOTPCRELX);
780 return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
George Rimar5c33b912016-05-25 14:31:37 +0000781}
782
George Rimarb7204302016-06-02 09:22:00 +0000783// A subset of relaxations can only be applied for no-PIC. This method
784// handles such relaxations. Instructions encoding information was taken from:
785// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
786// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
787// 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
788void X86_64TargetInfo::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
789 uint8_t ModRm) const {
George Rimarf10c8292016-06-01 16:45:30 +0000790 const uint8_t Rex = Loc[-3];
791 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
792 if (Op == 0x85) {
793 // See "TEST-Logical Compare" (4-428 Vol. 2B),
794 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
795
796 // ModR/M byte has form XX YYY ZZZ, where
797 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
798 // XX has different meanings:
799 // 00: The operand's memory address is in reg1.
800 // 01: The operand's memory address is reg1 + a byte-sized displacement.
801 // 10: The operand's memory address is reg1 + a word-sized displacement.
802 // 11: The operand is reg1 itself.
803 // If an instruction requires only one operand, the unused reg2 field
804 // holds extra opcode bits rather than a register code
805 // 0xC0 == 11 000 000 binary.
806 // 0x38 == 00 111 000 binary.
807 // We transfer reg2 to reg1 here as operand.
808 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
809 *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
810
811 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
812 // See "TEST-Logical Compare" (4-428 Vol. 2B).
813 *(Loc - 2) = 0xf7;
814
815 // Move R bit to the B bit in REX byte.
816 // REX byte is encoded as 0100WRXB, where
817 // 0100 is 4bit fixed pattern.
818 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
819 // default operand size is used (which is 32-bit for most but not all
820 // instructions).
821 // REX.R This 1-bit value is an extension to the MODRM.reg field.
822 // REX.X This 1-bit value is an extension to the SIB.index field.
823 // REX.B This 1-bit value is an extension to the MODRM.rm field or the
824 // SIB.base field.
825 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
826 *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
827 relocateOne(Loc, R_X86_64_PC32, Val);
828 return;
829 }
830
831 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
832 // or xor operations.
833
834 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
835 // Logic is close to one for test instruction above, but we also
836 // write opcode extension here, see below for details.
837 *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
838
839 // Primary opcode is 0x81, opcode extension is one of:
840 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
841 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
842 // This value was wrote to MODRM.reg in a line above.
843 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
844 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
845 // descriptions about each operation.
846 *(Loc - 2) = 0x81;
847 *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
George Rimar5c33b912016-05-25 14:31:37 +0000848 relocateOne(Loc, R_X86_64_PC32, Val);
849}
850
George Rimarb7204302016-06-02 09:22:00 +0000851void X86_64TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
852 const uint8_t Op = Loc[-2];
853 const uint8_t ModRm = Loc[-1];
854
855 // Convert mov foo@GOTPCREL(%rip), %reg to lea foo(%rip), %reg.
856 if (Op == 0x8b) {
857 *(Loc - 2) = 0x8d;
858 relocateOne(Loc, R_X86_64_PC32, Val);
859 return;
860 }
861
862 // Convert call/jmp instructions.
863 if (Op == 0xff) {
864 if (ModRm == 0x15) {
865 // ABI says we can convert call *foo@GOTPCREL(%rip) to nop call foo.
866 // Instead we convert to addr32 call foo, where addr32 is instruction
867 // prefix. That makes result expression to be a single instruction.
868 *(Loc - 2) = 0x67; // addr32 prefix
869 *(Loc - 1) = 0xe8; // call
870 } else {
871 assert(ModRm == 0x25);
872 // Convert jmp *foo@GOTPCREL(%rip) to jmp foo nop.
873 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
874 *(Loc - 2) = 0xe9; // jmp
875 *(Loc + 3) = 0x90; // nop
876 Loc -= 1;
877 Val += 1;
878 }
879 relocateOne(Loc, R_X86_64_PC32, Val);
880 return;
881 }
882
883 assert(!Config->Pic);
884 // We are relaxing a rip relative to an absolute, so compensate
885 // for the old -4 addend.
886 relaxGotNoPic(Loc, Val + 4, Op, ModRm);
887}
888
Hal Finkel3c8cc672015-10-12 20:56:18 +0000889// Relocation masks following the #lo(value), #hi(value), #ha(value),
890// #higher(value), #highera(value), #highest(value), and #highesta(value)
891// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
892// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000893static uint16_t applyPPCLo(uint64_t V) { return V; }
894static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
895static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
896static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
897static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000898static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000899static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
900
Davide Italiano8c3444362016-01-11 19:45:33 +0000901PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000902
Rafael Espindola22ef9562016-04-13 01:40:19 +0000903void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
904 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000905 switch (Type) {
906 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000907 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000908 break;
909 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000910 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000911 break;
912 default:
George Rimar57610422016-03-11 14:43:02 +0000913 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000914 }
915}
916
Rafael Espindola22ef9562016-04-13 01:40:19 +0000917RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
918 return R_ABS;
919}
920
Rafael Espindolac4010882015-09-22 20:54:08 +0000921PPC64TargetInfo::PPC64TargetInfo() {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000922 PltRel = GotRel = R_PPC64_GLOB_DAT;
Rui Ueyama724d6252016-01-29 01:49:32 +0000923 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000924 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000925
926 // We need 64K pages (at least under glibc/Linux, the loader won't
927 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000928 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000929
930 // The PPC64 ELF ABI v1 spec, says:
931 //
932 // It is normally desirable to put segments with different characteristics
933 // in separate 256 Mbyte portions of the address space, to give the
934 // operating system full paging flexibility in the 64-bit address space.
935 //
936 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
937 // use 0x10000000 as the starting address.
938 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000939}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000940
Rafael Espindola15cec292016-04-27 12:25:22 +0000941static uint64_t PPC64TocOffset = 0x8000;
942
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000943uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +0000944 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
945 // TOC starts where the first of these sections starts. We always create a
946 // .got when we see a relocation that uses it, so for us the start is always
947 // the .got.
Hal Finkel3c8cc672015-10-12 20:56:18 +0000948 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +0000949
950 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
951 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
952 // code (crt1.o) assumes that you can get from the TOC base to the
953 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +0000954 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000955}
956
Rafael Espindola22ef9562016-04-13 01:40:19 +0000957RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
958 switch (Type) {
959 default:
960 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +0000961 case R_PPC64_TOC16:
962 case R_PPC64_TOC16_DS:
963 case R_PPC64_TOC16_HA:
964 case R_PPC64_TOC16_HI:
965 case R_PPC64_TOC16_LO:
966 case R_PPC64_TOC16_LO_DS:
967 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000968 case R_PPC64_TOC:
969 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000970 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000971 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000972 }
973}
974
Rui Ueyama9398f862016-01-29 04:15:02 +0000975void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
976 uint64_t PltEntryAddr, int32_t Index,
977 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000978 uint64_t Off = GotEntryAddr - getPPC64TocBase();
979
980 // FIXME: What we should do, in theory, is get the offset of the function
981 // descriptor in the .opd section, and use that as the offset from %r2 (the
982 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
983 // be a pointer to the function descriptor in the .opd section. Using
984 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
985
Hal Finkelfa92f682015-10-13 21:47:34 +0000986 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000987 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
988 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
989 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
990 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
991 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
992 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
993 write32be(Buf + 28, 0x4e800420); // bctr
994}
995
Rafael Espindola22ef9562016-04-13 01:40:19 +0000996void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
997 uint64_t Val) const {
Rafael Espindola15cec292016-04-27 12:25:22 +0000998 uint64_t TO = PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000999
Rafael Espindola15cec292016-04-27 12:25:22 +00001000 // For a TOC-relative relocation, proceed in terms of the corresponding
1001 // ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001002 switch (Type) {
Rafael Espindola15cec292016-04-27 12:25:22 +00001003 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break;
1004 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break;
1005 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break;
1006 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break;
1007 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break;
1008 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001009 default: break;
1010 }
1011
Hal Finkel3c8cc672015-10-12 20:56:18 +00001012 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001013 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001014 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001015 // Preserve the AA/LK bits in the branch instruction
1016 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001017 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001018 break;
1019 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001020 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001021 checkInt<16>(Val, Type);
1022 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001023 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001024 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001025 checkInt<16>(Val, Type);
1026 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001028 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001029 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 break;
1031 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001032 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001033 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001034 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001035 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001036 break;
1037 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001038 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001039 break;
1040 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001041 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001042 break;
1043 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001044 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001045 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001046 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001047 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001049 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001050 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 break;
1052 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001053 checkInt<32>(Val, Type);
1054 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001056 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001057 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001058 break;
1059 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001060 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001061 break;
1062 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001063 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001064 break;
1065 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001066 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001067 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 case R_PPC64_REL24: {
1069 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001070 checkInt<24>(Val, Type);
1071 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001072 break;
1073 }
1074 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001075 checkInt<32>(Val, Type);
1076 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001077 break;
1078 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001079 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001081 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001082 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001083 break;
1084 default:
George Rimar57610422016-03-11 14:43:02 +00001085 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001086 }
1087}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001088
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001089AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001090 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001091 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001092 IRelativeRel = R_AARCH64_IRELATIVE;
1093 GotRel = R_AARCH64_GLOB_DAT;
1094 PltRel = R_AARCH64_JUMP_SLOT;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001095 TlsDescRel = R_AARCH64_TLSDESC;
Rui Ueyama724d6252016-01-29 01:49:32 +00001096 TlsGotRel = R_AARCH64_TLS_TPREL64;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001097 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001098 PltZeroSize = 32;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001099
1100 // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
1101 // 1 of the tls structures and the tcb size is 16.
1102 TcbSize = 16;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001103}
George Rimar648a2c32015-10-20 08:54:27 +00001104
Rafael Espindola22ef9562016-04-13 01:40:19 +00001105RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1106 const SymbolBody &S) const {
1107 switch (Type) {
1108 default:
1109 return R_ABS;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001110
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001111 case R_AARCH64_TLSDESC_ADR_PAGE21:
1112 return R_TLSDESC_PAGE;
1113
1114 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1115 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1116 return R_TLSDESC;
1117
1118 case R_AARCH64_TLSDESC_CALL:
1119 return R_HINT;
1120
Rafael Espindola8818ca62016-05-20 17:41:09 +00001121 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1122 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1123 return R_TLS;
1124
Rafael Espindola22ef9562016-04-13 01:40:19 +00001125 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +00001126 case R_AARCH64_CONDBR19:
1127 case R_AARCH64_JUMP26:
1128 case R_AARCH64_TSTBR14:
1129 return R_PLT_PC;
1130
Rafael Espindola22ef9562016-04-13 01:40:19 +00001131 case R_AARCH64_PREL16:
1132 case R_AARCH64_PREL32:
1133 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001134 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001135 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001136 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001137 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001138 case R_AARCH64_LD64_GOT_LO12_NC:
1139 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1140 return R_GOT;
1141 case R_AARCH64_ADR_GOT_PAGE:
1142 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1143 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001144 }
1145}
1146
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001147RelExpr AArch64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
1148 RelExpr Expr) const {
1149 if (Expr == R_RELAX_TLS_GD_TO_IE) {
1150 if (Type == R_AARCH64_TLSDESC_ADR_PAGE21)
1151 return R_RELAX_TLS_GD_TO_IE_PAGE_PC;
1152 return R_RELAX_TLS_GD_TO_IE_ABS;
1153 }
1154 return Expr;
1155}
1156
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001157bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001158 switch (Type) {
1159 default:
1160 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001161 case R_AARCH64_ADD_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001162 case R_AARCH64_LD64_GOT_LO12_NC:
1163 case R_AARCH64_LDST128_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001164 case R_AARCH64_LDST16_ABS_LO12_NC:
1165 case R_AARCH64_LDST32_ABS_LO12_NC:
1166 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001167 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001168 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1169 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +00001170 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001171 return true;
1172 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001173}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001174
George Rimar98b060d2016-03-06 06:01:07 +00001175bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001176 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1177 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1178}
1179
George Rimar98b060d2016-03-06 06:01:07 +00001180uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001181 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1182 return Type;
1183 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001184 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001185 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001186 // Keep it going with a dummy value so that we can find more reloc errors.
1187 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001188}
1189
Rui Ueyamac516ae12016-01-29 02:33:45 +00001190void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001191 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1192}
1193
Rafael Espindola22ef9562016-04-13 01:40:19 +00001194static uint64_t getAArch64Page(uint64_t Expr) {
1195 return Expr & (~static_cast<uint64_t>(0xFFF));
1196}
1197
Rui Ueyama900e2d22016-01-29 03:51:49 +00001198void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001199 const uint8_t PltData[] = {
1200 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1201 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1202 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1203 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1204 0x20, 0x02, 0x1f, 0xd6, // br x17
1205 0x1f, 0x20, 0x03, 0xd5, // nop
1206 0x1f, 0x20, 0x03, 0xd5, // nop
1207 0x1f, 0x20, 0x03, 0xd5 // nop
1208 };
1209 memcpy(Buf, PltData, sizeof(PltData));
1210
Rui Ueyama900e2d22016-01-29 03:51:49 +00001211 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1212 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001213 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1214 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1215 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1216 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001217}
1218
Rui Ueyama9398f862016-01-29 04:15:02 +00001219void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1220 uint64_t PltEntryAddr, int32_t Index,
1221 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001222 const uint8_t Inst[] = {
1223 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1224 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1225 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1226 0x20, 0x02, 0x1f, 0xd6 // br x17
1227 };
1228 memcpy(Buf, Inst, sizeof(Inst));
1229
Rafael Espindola22ef9562016-04-13 01:40:19 +00001230 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1231 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1232 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1233 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001234}
1235
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001236static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001237 uint32_t ImmLo = (Imm & 0x3) << 29;
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001238 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
1239 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001240 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001241}
1242
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001243static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1244 or32le(L, (Imm & 0xFFF) << 10);
1245}
1246
Rafael Espindola22ef9562016-04-13 01:40:19 +00001247void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1248 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001249 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001250 case R_AARCH64_ABS16:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001251 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001252 checkIntUInt<16>(Val, Type);
1253 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001254 break;
1255 case R_AARCH64_ABS32:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001256 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001257 checkIntUInt<32>(Val, Type);
1258 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001259 break;
1260 case R_AARCH64_ABS64:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001261 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001262 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001263 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001264 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001265 // This relocation stores 12 bits and there's no instruction
1266 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001267 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1268 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001269 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001270 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001271 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001272 case R_AARCH64_ADR_PREL_PG_HI21:
1273 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001274 case R_AARCH64_TLSDESC_ADR_PAGE21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001275 checkInt<33>(Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001276 updateAArch64Addr(Loc, Val >> 12);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001277 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001278 case R_AARCH64_ADR_PREL_LO21:
1279 checkInt<21>(Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001280 updateAArch64Addr(Loc, Val);
Davide Italiano1d750a62015-09-27 08:45:38 +00001281 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001282 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001283 case R_AARCH64_JUMP26:
1284 checkInt<28>(Val, Type);
1285 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001286 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001287 case R_AARCH64_CONDBR19:
1288 checkInt<21>(Val, Type);
1289 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001290 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001291 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001292 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001293 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001294 checkAlignment<8>(Val, Type);
1295 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001296 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001297 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001298 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001299 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001300 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001301 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001302 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001303 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001304 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001305 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001306 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001307 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001308 break;
1309 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001310 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001311 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001312 case R_AARCH64_TSTBR14:
1313 checkInt<16>(Val, Type);
1314 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001315 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001316 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1317 checkInt<24>(Val, Type);
Rafael Espindola1016f192016-06-02 15:51:40 +00001318 updateAArch64Add(Loc, Val >> 12);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001319 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001320 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001321 case R_AARCH64_TLSDESC_ADD_LO12_NC:
Rafael Espindola1016f192016-06-02 15:51:40 +00001322 updateAArch64Add(Loc, Val);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001323 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001324 default:
George Rimar57610422016-03-11 14:43:02 +00001325 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001326 }
1327}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001328
Rafael Espindola22ef9562016-04-13 01:40:19 +00001329void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1330 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001331 // TLSDESC Global-Dynamic relocation are in the form:
1332 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1333 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1334 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1335 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001336 // blr x1
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001337 // And it can optimized to:
1338 // movz x0, #0x0, lsl #16
1339 // movk x0, #0x10
1340 // nop
1341 // nop
Rafael Espindola8818ca62016-05-20 17:41:09 +00001342 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001343
1344 uint32_t NewInst;
1345 switch (Type) {
1346 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1347 case R_AARCH64_TLSDESC_CALL:
1348 // nop
1349 NewInst = 0xd503201f;
1350 break;
1351 case R_AARCH64_TLSDESC_ADR_PAGE21:
1352 // movz
Rafael Espindola8818ca62016-05-20 17:41:09 +00001353 NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001354 break;
1355 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1356 // movk
Rafael Espindola8818ca62016-05-20 17:41:09 +00001357 NewInst = 0xf2800000 | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001358 break;
1359 default:
George Rimar777f9632016-03-12 08:31:34 +00001360 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001361 }
1362 write32le(Loc, NewInst);
1363}
1364
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001365void AArch64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
1366 uint64_t Val) const {
1367 // TLSDESC Global-Dynamic relocation are in the form:
1368 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1369 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1370 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1371 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1372 // blr x1
1373 // And it can optimized to:
1374 // adrp x0, :gottprel:v
1375 // ldr x0, [x0, :gottprel_lo12:v]
1376 // nop
1377 // nop
1378
1379 switch (Type) {
1380 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1381 case R_AARCH64_TLSDESC_CALL:
1382 write32le(Loc, 0xd503201f); // nop
1383 break;
1384 case R_AARCH64_TLSDESC_ADR_PAGE21:
1385 write32le(Loc, 0x90000000); // adrp
1386 relocateOne(Loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, Val);
1387 break;
1388 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1389 write32le(Loc, 0xf9400000); // ldr
1390 relocateOne(Loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, Val);
1391 break;
1392 default:
1393 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
1394 }
1395}
1396
Rafael Espindola22ef9562016-04-13 01:40:19 +00001397void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1398 uint64_t Val) const {
Rafael Espindola8818ca62016-05-20 17:41:09 +00001399 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001400
George Rimar4d1d16d2016-03-06 06:16:05 +00001401 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001402 uint32_t NewInst;
1403 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1404 // Generate movz.
1405 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001406 NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001407 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1408 // Generate movk
1409 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001410 NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001411 } else {
George Rimar777f9632016-03-12 08:31:34 +00001412 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001413 }
1414 write32le(Loc, NewInst);
1415}
1416
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001417// Implementing relocations for AMDGPU is low priority since most
1418// programs don't use relocations now. Thus, this function is not
1419// actually called (relocateOne is called for each relocation).
1420// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001421void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1422 uint64_t Val) const {
1423 llvm_unreachable("not implemented");
1424}
1425
1426RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001427 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001428}
1429
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001430template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001431 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001432 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001433 PltEntrySize = 16;
1434 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001435 ThunkSize = 16;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001436 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001437 PltRel = R_MIPS_JUMP_SLOT;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001438 if (ELFT::Is64Bits)
1439 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1440 else
1441 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001442}
1443
1444template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001445RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1446 const SymbolBody &S) const {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001447 if (ELFT::Is64Bits)
1448 // See comment in the calculateMips64RelChain.
1449 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001450 switch (Type) {
1451 default:
1452 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001453 case R_MIPS_JALR:
1454 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001455 case R_MIPS_GPREL16:
1456 case R_MIPS_GPREL32:
1457 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001458 case R_MIPS_26:
1459 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001460 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001461 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001462 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001463 // MIPS _gp_disp designates offset between start of function and 'gp'
1464 // pointer into GOT. __gnu_local_gp is equal to the current value of
1465 // the 'gp'. Therefore any relocations against them do not require
1466 // dynamic relocation.
1467 if (&S == ElfSym<ELFT>::MipsGpDisp)
1468 return R_PC;
1469 return R_ABS;
1470 case R_MIPS_PC32:
1471 case R_MIPS_PC16:
1472 case R_MIPS_PC19_S2:
1473 case R_MIPS_PC21_S2:
1474 case R_MIPS_PC26_S2:
1475 case R_MIPS_PCHI16:
1476 case R_MIPS_PCLO16:
1477 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001478 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001479 if (S.isLocal())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001480 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001481 // fallthrough
1482 case R_MIPS_CALL16:
1483 case R_MIPS_GOT_DISP:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001484 if (!S.isPreemptible())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001485 return R_MIPS_GOT_LOCAL;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001486 return R_GOT_OFF;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001487 case R_MIPS_GOT_PAGE:
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001488 return R_MIPS_GOT_LOCAL_PAGE;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001489 }
1490}
1491
1492template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001493uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001494 if (Type == R_MIPS_32 || Type == R_MIPS_64)
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001495 return RelativeRel;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001496 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001497 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001498 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001499 // Keep it going with a dummy value so that we can find more reloc errors.
1500 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001501}
1502
1503template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001504void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1505 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001506}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001507
Simon Atanasyan35031192015-12-15 06:06:34 +00001508static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001509
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001510template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001511static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001512 uint32_t Instr = read32<E>(Loc);
1513 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1514 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1515}
1516
1517template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001518static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001519 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001520 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001521 if (SHIFT > 0)
1522 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001523 checkInt<BSIZE + SHIFT>(V, Type);
1524 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001525}
1526
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001527template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001528static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001529 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001530 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001531}
1532
Simon Atanasyan3b377852016-03-04 10:55:20 +00001533template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001534static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1535 uint32_t Instr = read32<E>(Loc);
1536 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1537}
1538
Rafael Espindola666625b2016-04-01 14:36:09 +00001539template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001540 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1541}
1542
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001543template <class ELFT>
1544void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1545 const endianness E = ELFT::TargetEndianness;
1546 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1547 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1548 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1549 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1550 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1551 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1552 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1553 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1554 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001555 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001556 writeMipsLo16<E>(Buf + 4, Got);
1557 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001558}
1559
1560template <class ELFT>
1561void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1562 uint64_t PltEntryAddr, int32_t Index,
1563 unsigned RelOff) const {
1564 const endianness E = ELFT::TargetEndianness;
1565 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1566 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1567 write32<E>(Buf + 8, 0x03200008); // jr $25
1568 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001569 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001570 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1571 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001572}
1573
1574template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001575void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1576 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1577 // See MipsTargetInfo::writeThunk for details.
1578 const endianness E = ELFT::TargetEndianness;
1579 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1580 write32<E>(Buf + 4, 0x08000000); // j func
1581 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1582 write32<E>(Buf + 12, 0x00000000); // nop
1583 writeMipsHi16<E>(Buf, S);
1584 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1585 writeMipsLo16<E>(Buf + 8, S);
1586}
1587
1588template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001589bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1590 const SymbolBody &S) const {
1591 // Any MIPS PIC code function is invoked with its address in register $t9.
1592 // So if we have a branch instruction from non-PIC code to the PIC one
1593 // we cannot make the jump directly and need to create a small stubs
1594 // to save the target function address.
1595 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1596 if (Type != R_MIPS_26)
1597 return false;
1598 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1599 if (!F)
1600 return false;
1601 // If current file has PIC code, LA25 stub is not required.
1602 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1603 return false;
1604 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1605 if (!D || !D->Section)
1606 return false;
1607 // LA25 is required if target file has PIC code
1608 // or target symbol is a PIC symbol.
1609 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001610 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001611}
1612
1613template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001614uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001615 uint32_t Type) const {
1616 const endianness E = ELFT::TargetEndianness;
1617 switch (Type) {
1618 default:
1619 return 0;
1620 case R_MIPS_32:
1621 case R_MIPS_GPREL32:
1622 return read32<E>(Buf);
1623 case R_MIPS_26:
1624 // FIXME (simon): If the relocation target symbol is not a PLT entry
1625 // we should use another expression for calculation:
1626 // ((A << 2) | (P & 0xf0000000)) >> 2
1627 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1628 case R_MIPS_GPREL16:
1629 case R_MIPS_LO16:
1630 case R_MIPS_PCLO16:
1631 case R_MIPS_TLS_DTPREL_HI16:
1632 case R_MIPS_TLS_DTPREL_LO16:
1633 case R_MIPS_TLS_TPREL_HI16:
1634 case R_MIPS_TLS_TPREL_LO16:
1635 return readSignedLo16<E>(Buf);
1636 case R_MIPS_PC16:
1637 return getPcRelocAddend<E, 16, 2>(Buf);
1638 case R_MIPS_PC19_S2:
1639 return getPcRelocAddend<E, 19, 2>(Buf);
1640 case R_MIPS_PC21_S2:
1641 return getPcRelocAddend<E, 21, 2>(Buf);
1642 case R_MIPS_PC26_S2:
1643 return getPcRelocAddend<E, 26, 2>(Buf);
1644 case R_MIPS_PC32:
1645 return getPcRelocAddend<E, 32, 0>(Buf);
1646 }
1647}
1648
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001649static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1650 uint64_t Val) {
1651 // MIPS N64 ABI packs multiple relocations into the single relocation
1652 // record. In general, all up to three relocations can have arbitrary
1653 // types. In fact, Clang and GCC uses only a few combinations. For now,
1654 // we support two of them. That is allow to pass at least all LLVM
1655 // test suite cases.
1656 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1657 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1658 // The first relocation is a 'real' relocation which is calculated
1659 // using the corresponding symbol's value. The second and the third
1660 // relocations used to modify result of the first one: extend it to
1661 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1662 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1663 uint32_t Type2 = (Type >> 8) & 0xff;
1664 uint32_t Type3 = (Type >> 16) & 0xff;
1665 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1666 return std::make_pair(Type, Val);
1667 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1668 return std::make_pair(Type2, Val);
1669 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1670 return std::make_pair(Type3, -Val);
1671 error("unsupported relocations combination " + Twine(Type));
1672 return std::make_pair(Type & 0xff, Val);
1673}
1674
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001675template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001676void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1677 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001678 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001679 // Thread pointer and DRP offsets from the start of TLS data area.
1680 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001681 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1682 Val -= 0x8000;
1683 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1684 Val -= 0x7000;
1685 if (ELFT::Is64Bits)
1686 std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001687 switch (Type) {
1688 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001689 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001690 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001691 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001692 case R_MIPS_64:
1693 write64<E>(Loc, Val);
1694 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00001695 case R_MIPS_26:
1696 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001697 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001698 case R_MIPS_GOT_DISP:
1699 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001700 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001701 case R_MIPS_GPREL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001702 checkInt<16>(Val, Type);
1703 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001704 case R_MIPS_CALL16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001705 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001706 case R_MIPS_LO16:
1707 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001708 case R_MIPS_TLS_DTPREL_LO16:
1709 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001710 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001711 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001712 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001713 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001714 case R_MIPS_TLS_DTPREL_HI16:
1715 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001716 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001717 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001718 case R_MIPS_JALR:
1719 // Ignore this optimization relocation for now
1720 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001721 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001722 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001723 break;
1724 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001725 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001726 break;
1727 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001728 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001729 break;
1730 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001731 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001732 break;
1733 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001734 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001735 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001736 default:
George Rimar57610422016-03-11 14:43:02 +00001737 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001738 }
1739}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001740
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001741template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001742bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00001743 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001744}
Rafael Espindola01205f72015-09-22 18:19:46 +00001745}
1746}