blob: 4ec8a9176666fa2b980b7cc151f7795f7c113d6e [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
Rui Ueyama12ebff22016-06-07 18:10:12 +000050static StringRef getRelName(uint32_t Type) {
51 return getELFRelocationTypeName(Config->EMachine, Type);
52}
53
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000054template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
55 if (isInt<N>(V))
56 return;
Rui Ueyama12ebff22016-06-07 18:10:12 +000057 error("relocation " + getRelName(Type) + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000058}
59
60template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
61 if (isUInt<N>(V))
62 return;
Rui Ueyama12ebff22016-06-07 18:10:12 +000063 error("relocation " + getRelName(Type) + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000064}
65
Igor Kudrinfea8ed52015-11-26 10:05:24 +000066template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
67 if (isInt<N>(V) || isUInt<N>(V))
68 return;
Rui Ueyama12ebff22016-06-07 18:10:12 +000069 error("relocation " + getRelName(Type) + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000070}
71
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000072template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
73 if ((V & (N - 1)) == 0)
74 return;
Rui Ueyama12ebff22016-06-07 18:10:12 +000075 error("improper alignment for relocation " + getRelName(Type));
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000076}
77
Rui Ueyama45a873d2016-06-07 18:03:05 +000078static void warnDynRel(uint32_t Type) {
Rui Ueyama12ebff22016-06-07 18:10:12 +000079 error("relocation " + getRelName(Type) +
Rui Ueyama45a873d2016-06-07 18:03:05 +000080 " cannot be used when making a shared object; recompile with -fPIC.");
81}
82
Rui Ueyamaefc23de2015-10-14 21:30:32 +000083namespace {
84class X86TargetInfo final : public TargetInfo {
85public:
86 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000087 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000088 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000089 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000090 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000091 bool isTlsLocalDynamicRel(uint32_t Type) const override;
92 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
93 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000094 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000095 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000096 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
97 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000098 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000099
Rafael Espindola69f54022016-06-04 23:22:34 +0000100 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
101 RelExpr Expr) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000102 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
103 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
104 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
105 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000106};
107
108class X86_64TargetInfo final : public TargetInfo {
109public:
110 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000111 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000112 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000113 bool isTlsLocalDynamicRel(uint32_t Type) const override;
114 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
115 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000116 void writeGotPltHeader(uint8_t *Buf) const override;
117 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000118 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000119 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
120 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000121 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000122
Rafael Espindola5c66b822016-06-04 22:58:54 +0000123 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
124 RelExpr Expr) const override;
George Rimar5c33b912016-05-25 14:31:37 +0000125 void relaxGot(uint8_t *Loc, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000126 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
127 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
128 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
129 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
George Rimarb7204302016-06-02 09:22:00 +0000130
131private:
132 void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
133 uint8_t ModRm) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134};
135
Davide Italiano8c3444362016-01-11 19:45:33 +0000136class PPCTargetInfo final : public TargetInfo {
137public:
138 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000139 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000140 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000141};
142
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000143class PPC64TargetInfo final : public TargetInfo {
144public:
145 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000146 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000147 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
148 int32_t Index, unsigned RelOff) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000149 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000150};
151
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000152class AArch64TargetInfo final : public TargetInfo {
153public:
154 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000155 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000156 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000157 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000158 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000159 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000160 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
161 int32_t Index, unsigned RelOff) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000162 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000163 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000164 RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
165 RelExpr Expr) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000166 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolae1979ae2016-06-04 23:33:31 +0000167 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000168 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000169};
170
Tom Stellard80efb162016-01-07 03:59:08 +0000171class AMDGPUTargetInfo final : public TargetInfo {
172public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000173 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000174 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
175 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000176};
177
Peter Smith8646ced2016-06-07 09:31:52 +0000178class ARMTargetInfo final : public TargetInfo {
179public:
180 ARMTargetInfo();
181 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
182 uint32_t getDynRel(uint32_t Type) const override;
183 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
184 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
185 void writePltZero(uint8_t *Buf) const override;
186 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
187 int32_t Index, unsigned RelOff) const override;
188 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
189};
190
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000191template <class ELFT> class MipsTargetInfo final : public TargetInfo {
192public:
193 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000194 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000195 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000196 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000197 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
198 void writePltZero(uint8_t *Buf) const override;
199 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
200 int32_t Index, unsigned RelOff) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000201 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000202 bool needsThunk(uint32_t Type, const InputFile &File,
203 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000204 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000205 bool usesOnlyLowPageBits(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000206};
207} // anonymous namespace
208
Rui Ueyama91004392015-10-13 16:08:15 +0000209TargetInfo *createTarget() {
210 switch (Config->EMachine) {
211 case EM_386:
212 return new X86TargetInfo();
213 case EM_AARCH64:
214 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000215 case EM_AMDGPU:
216 return new AMDGPUTargetInfo();
Peter Smith8646ced2016-06-07 09:31:52 +0000217 case EM_ARM:
218 return new ARMTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000219 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000220 switch (Config->EKind) {
221 case ELF32LEKind:
222 return new MipsTargetInfo<ELF32LE>();
223 case ELF32BEKind:
224 return new MipsTargetInfo<ELF32BE>();
Simon Atanasyanae77ab72016-04-29 10:39:17 +0000225 case ELF64LEKind:
226 return new MipsTargetInfo<ELF64LE>();
227 case ELF64BEKind:
228 return new MipsTargetInfo<ELF64BE>();
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000229 default:
George Rimar777f9632016-03-12 08:31:34 +0000230 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000231 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000232 case EM_PPC:
233 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000234 case EM_PPC64:
235 return new PPC64TargetInfo();
236 case EM_X86_64:
237 return new X86_64TargetInfo();
238 }
George Rimar777f9632016-03-12 08:31:34 +0000239 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000240}
241
Rafael Espindola01205f72015-09-22 18:19:46 +0000242TargetInfo::~TargetInfo() {}
243
Rafael Espindola666625b2016-04-01 14:36:09 +0000244uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
245 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000246 return 0;
247}
248
George Rimar786e8662016-03-17 05:57:33 +0000249uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000250
Rafael Espindolab8ff59a2016-04-28 14:34:39 +0000251bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000252
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000253bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
254 const SymbolBody &S) const {
255 return false;
256}
257
George Rimar98b060d2016-03-06 06:01:07 +0000258bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000259
George Rimar98b060d2016-03-06 06:01:07 +0000260bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000261
George Rimar98b060d2016-03-06 06:01:07 +0000262bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000263 return false;
264}
265
Rafael Espindola5c66b822016-06-04 22:58:54 +0000266RelExpr TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
267 RelExpr Expr) const {
George Rimarf10c8292016-06-01 16:45:30 +0000268 return Expr;
George Rimar5c33b912016-05-25 14:31:37 +0000269}
270
271void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
272 llvm_unreachable("Should not have claimed to be relaxable");
273}
274
Rafael Espindola22ef9562016-04-13 01:40:19 +0000275void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
276 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000277 llvm_unreachable("Should not have claimed to be relaxable");
278}
279
Rafael Espindola22ef9562016-04-13 01:40:19 +0000280void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
281 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000282 llvm_unreachable("Should not have claimed to be relaxable");
283}
284
Rafael Espindola22ef9562016-04-13 01:40:19 +0000285void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
286 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000287 llvm_unreachable("Should not have claimed to be relaxable");
288}
289
Rafael Espindola22ef9562016-04-13 01:40:19 +0000290void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
291 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000292 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000293}
George Rimar77d1cb12015-11-24 09:00:06 +0000294
Rafael Espindola7f074422015-09-22 21:35:51 +0000295X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000296 CopyRel = R_386_COPY;
297 GotRel = R_386_GLOB_DAT;
298 PltRel = R_386_JUMP_SLOT;
299 IRelativeRel = R_386_IRELATIVE;
300 RelativeRel = R_386_RELATIVE;
301 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000302 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
303 TlsOffsetRel = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000304 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000305 PltZeroSize = 16;
Rafael Espindolaf807d472016-06-04 23:04:39 +0000306 TlsGdRelaxSkip = 2;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000307}
308
309RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
310 switch (Type) {
311 default:
312 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000313 case R_386_TLS_GD:
314 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000315 case R_386_TLS_LDM:
316 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000317 case R_386_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000318 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000319 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000320 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000321 case R_386_GOTPC:
322 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000323 case R_386_TLS_IE:
324 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000325 case R_386_GOT32:
326 case R_386_TLS_GOTIE:
327 return R_GOT_FROM_END;
328 case R_386_GOTOFF:
329 return R_GOTREL;
330 case R_386_TLS_LE:
331 return R_TLS;
332 case R_386_TLS_LE_32:
333 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000334 }
George Rimar77b77792015-11-25 22:15:01 +0000335}
336
Rafael Espindola69f54022016-06-04 23:22:34 +0000337RelExpr X86TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
338 RelExpr Expr) const {
339 switch (Expr) {
340 default:
341 return Expr;
342 case R_RELAX_TLS_GD_TO_IE:
343 return R_RELAX_TLS_GD_TO_IE_END;
344 case R_RELAX_TLS_GD_TO_LE:
345 return R_RELAX_TLS_GD_TO_LE_NEG;
346 }
347}
348
Rui Ueyamac516ae12016-01-29 02:33:45 +0000349void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000350 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
351}
352
Rui Ueyamac516ae12016-01-29 02:33:45 +0000353void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000354 // Entries in .got.plt initially points back to the corresponding
355 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000356 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000357}
Rafael Espindola01205f72015-09-22 18:19:46 +0000358
George Rimar98b060d2016-03-06 06:01:07 +0000359uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000360 if (Type == R_386_TLS_LE)
361 return R_386_TLS_TPOFF;
362 if (Type == R_386_TLS_LE_32)
363 return R_386_TLS_TPOFF32;
364 return Type;
365}
366
George Rimar98b060d2016-03-06 06:01:07 +0000367bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000368 return Type == R_386_TLS_GD;
369}
370
George Rimar98b060d2016-03-06 06:01:07 +0000371bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000372 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
373}
374
George Rimar98b060d2016-03-06 06:01:07 +0000375bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000376 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
377}
378
Rui Ueyama900e2d22016-01-29 03:51:49 +0000379void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000380 // Executable files and shared object files have
381 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000382 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000383 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000384 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000385 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
386 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000387 };
388 memcpy(Buf, V, sizeof(V));
389 return;
390 }
George Rimar648a2c32015-10-20 08:54:27 +0000391
George Rimar77b77792015-11-25 22:15:01 +0000392 const uint8_t PltData[] = {
393 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000394 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
395 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000396 };
397 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000398 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000399 write32le(Buf + 2, Got + 4);
400 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000401}
402
Rui Ueyama9398f862016-01-29 04:15:02 +0000403void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
404 uint64_t PltEntryAddr, int32_t Index,
405 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000406 const uint8_t Inst[] = {
407 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
408 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
409 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
410 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000411 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000412
George Rimar77b77792015-11-25 22:15:01 +0000413 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000414 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rafael Espindolae2f43772016-05-18 20:44:24 +0000415 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyama9398f862016-01-29 04:15:02 +0000416 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000417 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000418 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000419}
420
Rafael Espindola666625b2016-04-01 14:36:09 +0000421uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
422 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000423 switch (Type) {
424 default:
425 return 0;
426 case R_386_32:
427 case R_386_GOT32:
428 case R_386_GOTOFF:
429 case R_386_GOTPC:
430 case R_386_PC32:
431 case R_386_PLT32:
432 return read32le(Buf);
433 }
434}
435
Rafael Espindola22ef9562016-04-13 01:40:19 +0000436void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
437 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000438 checkInt<32>(Val, Type);
439 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000440}
441
Rafael Espindola22ef9562016-04-13 01:40:19 +0000442void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
443 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000444 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000445 // leal x@tlsgd(, %ebx, 1),
446 // call __tls_get_addr@plt
Rui Ueyama55274e32016-04-23 01:10:15 +0000447 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000448 // movl %gs:0,%eax
Rui Ueyama55274e32016-04-23 01:10:15 +0000449 // subl $x@ntpoff,%eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000450 const uint8_t Inst[] = {
451 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
452 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
453 };
454 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindolaebed1fe2016-05-20 21:23:52 +0000455 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000456}
457
Rafael Espindola22ef9562016-04-13 01:40:19 +0000458void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
459 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000460 // Convert
461 // leal x@tlsgd(, %ebx, 1),
462 // call __tls_get_addr@plt
463 // to
464 // movl %gs:0, %eax
465 // addl x@gotntpoff(%ebx), %eax
George Rimar2558e122015-12-09 09:55:54 +0000466 const uint8_t Inst[] = {
467 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
468 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
469 };
470 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola74f3dbe2016-05-20 20:09:35 +0000471 relocateOne(Loc + 5, R_386_32, Val);
George Rimar2558e122015-12-09 09:55:54 +0000472}
473
George Rimar6f17e092015-12-17 09:32:21 +0000474// In some conditions, relocations can be optimized to avoid using GOT.
475// This function does that for Initial Exec to Local Exec case.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000476void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
477 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000478 // Ulrich's document section 6.2 says that @gotntpoff can
479 // be used with MOVL or ADDL instructions.
480 // @indntpoff is similar to @gotntpoff, but for use in
481 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000482 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000483 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000484 uint8_t Reg = (Loc[-1] >> 3) & 7;
485 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000486 if (Type == R_386_TLS_IE) {
487 // For R_386_TLS_IE relocation we perform the next transformations:
488 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
489 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
490 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
491 // First one is special because when EAX is used the sequence is 5 bytes
492 // long, otherwise it is 6 bytes.
493 if (*Op == 0xa1) {
494 *Op = 0xb8;
495 } else {
496 *Inst = IsMov ? 0xc7 : 0x81;
497 *Op = 0xc0 | ((*Op >> 3) & 7);
498 }
499 } else {
500 // R_386_TLS_GOTIE relocation can be optimized to
501 // R_386_TLS_LE so that it does not use GOT.
502 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
503 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
504 // Note: gold converts to ADDL instead of LEAL.
505 *Inst = IsMov ? 0xc7 : 0x8d;
506 if (IsMov)
507 *Op = 0xc0 | ((*Op >> 3) & 7);
508 else
509 *Op = 0x80 | Reg | (Reg << 3);
510 }
Rafael Espindola8818ca62016-05-20 17:41:09 +0000511 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000512}
513
Rafael Espindola22ef9562016-04-13 01:40:19 +0000514void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
515 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000516 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000517 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000518 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000519 }
520
Rui Ueyama55274e32016-04-23 01:10:15 +0000521 // Convert
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000522 // leal foo(%reg),%eax
523 // call ___tls_get_addr
Rui Ueyama55274e32016-04-23 01:10:15 +0000524 // to
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000525 // movl %gs:0,%eax
526 // nop
527 // leal 0(%esi,1),%esi
528 const uint8_t Inst[] = {
529 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
530 0x90, // nop
531 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
532 };
533 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000534}
535
Rafael Espindola7f074422015-09-22 21:35:51 +0000536X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000537 CopyRel = R_X86_64_COPY;
538 GotRel = R_X86_64_GLOB_DAT;
539 PltRel = R_X86_64_JUMP_SLOT;
540 RelativeRel = R_X86_64_RELATIVE;
541 IRelativeRel = R_X86_64_IRELATIVE;
542 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000543 TlsModuleIndexRel = R_X86_64_DTPMOD64;
544 TlsOffsetRel = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000545 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000546 PltZeroSize = 16;
Rafael Espindolaf807d472016-06-04 23:04:39 +0000547 TlsGdRelaxSkip = 2;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000548}
549
550RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
551 switch (Type) {
552 default:
553 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000554 case R_X86_64_TPOFF32:
555 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000556 case R_X86_64_TLSLD:
557 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000558 case R_X86_64_TLSGD:
559 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000560 case R_X86_64_SIZE32:
561 case R_X86_64_SIZE64:
562 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000563 case R_X86_64_PLT32:
Rafael Espindolab312a742016-04-21 17:30:24 +0000564 return R_PLT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000565 case R_X86_64_PC32:
Rafael Espindola926bff82016-04-25 14:05:44 +0000566 case R_X86_64_PC64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000567 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000568 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000569 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000570 case R_X86_64_GOTPCREL:
Rafael Espindoladba64b82016-05-24 11:53:15 +0000571 case R_X86_64_GOTPCRELX:
572 case R_X86_64_REX_GOTPCRELX:
Rafael Espindolafe3a2f12016-05-24 12:12:06 +0000573 case R_X86_64_GOTTPOFF:
574 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000575 }
George Rimar648a2c32015-10-20 08:54:27 +0000576}
577
Rui Ueyamac516ae12016-01-29 02:33:45 +0000578void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000579 // The first entry holds the value of _DYNAMIC. It is not clear why that is
580 // required, but it is documented in the psabi and the glibc dynamic linker
Rafael Espindolae5027512016-05-10 16:23:46 +0000581 // seems to use it (note that this is relevant for linking ld.so, not any
Rafael Espindola4ee6cb32016-05-09 18:12:15 +0000582 // other program).
Igor Kudrin351b41d2015-11-16 17:44:08 +0000583 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
584}
585
Rui Ueyamac516ae12016-01-29 02:33:45 +0000586void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000587 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000588 write32le(Buf, Plt + 6);
589}
590
Rui Ueyama900e2d22016-01-29 03:51:49 +0000591void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000592 const uint8_t PltData[] = {
593 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
594 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
595 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
596 };
597 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000598 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
599 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
600 write32le(Buf + 2, Got - Plt + 2); // GOT+8
601 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000602}
Rafael Espindola01205f72015-09-22 18:19:46 +0000603
Rui Ueyama9398f862016-01-29 04:15:02 +0000604void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
605 uint64_t PltEntryAddr, int32_t Index,
606 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000607 const uint8_t Inst[] = {
608 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
609 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
610 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
611 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000612 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000613
George Rimar648a2c32015-10-20 08:54:27 +0000614 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
615 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000616 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000617}
618
George Rimar86971052016-03-29 08:35:42 +0000619uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
Rui Ueyama12ebff22016-06-07 18:10:12 +0000620 if (Config->Shared && (Type == R_X86_64_PC32 || Type == R_X86_64_32))
621 error(getRelName(Type) + " cannot be a dynamic relocation");
George Rimar86971052016-03-29 08:35:42 +0000622 return Type;
623}
624
George Rimar98b060d2016-03-06 06:01:07 +0000625bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000626 return Type == R_X86_64_GOTTPOFF;
627}
628
George Rimar98b060d2016-03-06 06:01:07 +0000629bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000630 return Type == R_X86_64_TLSGD;
631}
632
George Rimar98b060d2016-03-06 06:01:07 +0000633bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000634 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
635 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000636}
637
Rafael Espindola22ef9562016-04-13 01:40:19 +0000638void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
639 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000640 // Convert
641 // .byte 0x66
642 // leaq x@tlsgd(%rip), %rdi
643 // .word 0x6666
644 // rex64
645 // call __tls_get_addr@plt
646 // to
647 // mov %fs:0x0,%rax
648 // lea x@tpoff,%rax
George Rimar6713cf82015-11-25 21:46:05 +0000649 const uint8_t Inst[] = {
650 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
651 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
652 };
653 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000654 // The original code used a pc relative relocation and so we have to
655 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000656 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000657}
658
Rafael Espindola22ef9562016-04-13 01:40:19 +0000659void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
660 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000661 // Convert
662 // .byte 0x66
663 // leaq x@tlsgd(%rip), %rdi
664 // .word 0x6666
665 // rex64
666 // call __tls_get_addr@plt
667 // to
668 // mov %fs:0x0,%rax
669 // addq x@tpoff,%rax
George Rimar25411f252015-12-04 11:20:13 +0000670 const uint8_t Inst[] = {
671 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
672 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
673 };
674 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000675 // Both code sequences are PC relatives, but since we are moving the constant
676 // forward by 8 bytes we have to subtract the value by 8.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000677 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000678}
679
George Rimar77d1cb12015-11-24 09:00:06 +0000680// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000681// R_X86_64_TPOFF32 so that it does not use GOT.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000682void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
683 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000684 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
685 // used in MOVQ or ADDQ instructions only.
686 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
687 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
688 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
689 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
690 uint8_t *Prefix = Loc - 3;
691 uint8_t *Inst = Loc - 2;
692 uint8_t *RegSlot = Loc - 1;
693 uint8_t Reg = Loc[-1] >> 3;
694 bool IsMov = *Inst == 0x8b;
695 bool RspAdd = !IsMov && Reg == 4;
Rui Ueyama55274e32016-04-23 01:10:15 +0000696
George Rimar77d1cb12015-11-24 09:00:06 +0000697 // r12 and rsp registers requires special handling.
698 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
699 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
700 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
701 // The same true for rsp. So we convert to addq for them, saving 1 byte that
702 // we dont have.
703 if (RspAdd)
704 *Inst = 0x81;
705 else
706 *Inst = IsMov ? 0xc7 : 0x8d;
707 if (*Prefix == 0x4c)
708 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
709 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola91e9fc02016-05-20 21:09:59 +0000710 // The original code used a pc relative relocation and so we have to
711 // compensate for the -4 in had in the addend.
Rafael Espindola8818ca62016-05-20 17:41:09 +0000712 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000713}
714
Rafael Espindola22ef9562016-04-13 01:40:19 +0000715void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
716 uint64_t Val) const {
Rui Ueyama55274e32016-04-23 01:10:15 +0000717 // Convert
718 // leaq bar@tlsld(%rip), %rdi
719 // callq __tls_get_addr@PLT
720 // leaq bar@dtpoff(%rax), %rcx
721 // to
722 // .word 0x6666
723 // .byte 0x66
724 // mov %fs:0,%rax
725 // leaq bar@tpoff(%rax), %rcx
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000726 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000727 write64le(Loc, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000728 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000729 }
730 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola8818ca62016-05-20 17:41:09 +0000731 relocateOne(Loc, R_X86_64_TPOFF32, Val);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000732 return;
George Rimar25411f252015-12-04 11:20:13 +0000733 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000734
735 const uint8_t Inst[] = {
Rui Ueyama02fcf112016-05-25 04:29:53 +0000736 0x66, 0x66, // .word 0x6666
737 0x66, // .byte 0x66
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000738 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
739 };
740 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000741}
742
Rafael Espindola22ef9562016-04-13 01:40:19 +0000743void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
744 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000745 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000746 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000747 checkUInt<32>(Val, Type);
748 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000749 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000750 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000751 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000752 case R_X86_64_GOT32:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000753 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000754 case R_X86_64_GOTPCRELX:
755 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000756 case R_X86_64_PC32:
Rafael Espindola38bd2172016-05-04 15:51:23 +0000757 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000758 case R_X86_64_PLT32:
759 case R_X86_64_TLSGD:
760 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000761 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000762 case R_X86_64_SIZE32:
Rafael Espindolafb0ceb52016-05-20 20:02:27 +0000763 checkInt<32>(Val, Type);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000764 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000765 break;
Rui Ueyamae66f45c2016-05-25 04:10:14 +0000766 case R_X86_64_64:
767 case R_X86_64_DTPOFF64:
768 case R_X86_64_SIZE64:
769 case R_X86_64_PC64:
770 write64le(Loc, Val);
771 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000772 default:
George Rimar57610422016-03-11 14:43:02 +0000773 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000774 }
775}
776
Rafael Espindola5c66b822016-06-04 22:58:54 +0000777RelExpr X86_64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
778 RelExpr RelExpr) const {
George Rimar5c33b912016-05-25 14:31:37 +0000779 if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX)
George Rimarf10c8292016-06-01 16:45:30 +0000780 return RelExpr;
George Rimara8f9cf12016-05-26 13:37:12 +0000781 const uint8_t Op = Data[-2];
782 const uint8_t ModRm = Data[-1];
George Rimarf10c8292016-06-01 16:45:30 +0000783 // FIXME: When PIC is disabled and foo is defined locally in the
784 // lower 32 bit address space, memory operand in mov can be converted into
785 // immediate operand. Otherwise, mov must be changed to lea. We support only
786 // latter relaxation at this moment.
George Rimar95433df2016-05-25 16:51:08 +0000787 if (Op == 0x8b)
George Rimarf10c8292016-06-01 16:45:30 +0000788 return R_RELAX_GOT_PC;
George Rimar95433df2016-05-25 16:51:08 +0000789 // Relax call and jmp.
George Rimarf10c8292016-06-01 16:45:30 +0000790 if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25))
791 return R_RELAX_GOT_PC;
792
793 // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor.
794 // If PIC then no relaxation is available.
795 // We also don't relax test/binop instructions without REX byte,
796 // they are 32bit operations and not common to have.
797 assert(Type == R_X86_64_REX_GOTPCRELX);
798 return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC;
George Rimar5c33b912016-05-25 14:31:37 +0000799}
800
George Rimarb7204302016-06-02 09:22:00 +0000801// A subset of relaxations can only be applied for no-PIC. This method
802// handles such relaxations. Instructions encoding information was taken from:
803// "Intel 64 and IA-32 Architectures Software Developer's Manual V2"
804// (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
805// 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
806void X86_64TargetInfo::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
807 uint8_t ModRm) const {
George Rimarf10c8292016-06-01 16:45:30 +0000808 const uint8_t Rex = Loc[-3];
809 // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg".
810 if (Op == 0x85) {
811 // See "TEST-Logical Compare" (4-428 Vol. 2B),
812 // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension).
813
814 // ModR/M byte has form XX YYY ZZZ, where
815 // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1).
816 // XX has different meanings:
817 // 00: The operand's memory address is in reg1.
818 // 01: The operand's memory address is reg1 + a byte-sized displacement.
819 // 10: The operand's memory address is reg1 + a word-sized displacement.
820 // 11: The operand is reg1 itself.
821 // If an instruction requires only one operand, the unused reg2 field
822 // holds extra opcode bits rather than a register code
823 // 0xC0 == 11 000 000 binary.
824 // 0x38 == 00 111 000 binary.
825 // We transfer reg2 to reg1 here as operand.
826 // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3).
827 *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte.
828
829 // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32
830 // See "TEST-Logical Compare" (4-428 Vol. 2B).
831 *(Loc - 2) = 0xf7;
832
833 // Move R bit to the B bit in REX byte.
834 // REX byte is encoded as 0100WRXB, where
835 // 0100 is 4bit fixed pattern.
836 // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the
837 // default operand size is used (which is 32-bit for most but not all
838 // instructions).
839 // REX.R This 1-bit value is an extension to the MODRM.reg field.
840 // REX.X This 1-bit value is an extension to the SIB.index field.
841 // REX.B This 1-bit value is an extension to the MODRM.rm field or the
842 // SIB.base field.
843 // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A).
844 *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
845 relocateOne(Loc, R_X86_64_PC32, Val);
846 return;
847 }
848
849 // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub
850 // or xor operations.
851
852 // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg".
853 // Logic is close to one for test instruction above, but we also
854 // write opcode extension here, see below for details.
855 *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte.
856
857 // Primary opcode is 0x81, opcode extension is one of:
858 // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB,
859 // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP.
860 // This value was wrote to MODRM.reg in a line above.
861 // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15),
862 // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for
863 // descriptions about each operation.
864 *(Loc - 2) = 0x81;
865 *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2;
George Rimar5c33b912016-05-25 14:31:37 +0000866 relocateOne(Loc, R_X86_64_PC32, Val);
867}
868
George Rimarb7204302016-06-02 09:22:00 +0000869void X86_64TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const {
870 const uint8_t Op = Loc[-2];
871 const uint8_t ModRm = Loc[-1];
872
873 // Convert mov foo@GOTPCREL(%rip), %reg to lea foo(%rip), %reg.
874 if (Op == 0x8b) {
875 *(Loc - 2) = 0x8d;
876 relocateOne(Loc, R_X86_64_PC32, Val);
877 return;
878 }
879
880 // Convert call/jmp instructions.
881 if (Op == 0xff) {
882 if (ModRm == 0x15) {
883 // ABI says we can convert call *foo@GOTPCREL(%rip) to nop call foo.
884 // Instead we convert to addr32 call foo, where addr32 is instruction
885 // prefix. That makes result expression to be a single instruction.
886 *(Loc - 2) = 0x67; // addr32 prefix
887 *(Loc - 1) = 0xe8; // call
888 } else {
889 assert(ModRm == 0x25);
890 // Convert jmp *foo@GOTPCREL(%rip) to jmp foo nop.
891 // jmp doesn't return, so it is fine to use nop here, it is just a stub.
892 *(Loc - 2) = 0xe9; // jmp
893 *(Loc + 3) = 0x90; // nop
894 Loc -= 1;
895 Val += 1;
896 }
897 relocateOne(Loc, R_X86_64_PC32, Val);
898 return;
899 }
900
901 assert(!Config->Pic);
902 // We are relaxing a rip relative to an absolute, so compensate
903 // for the old -4 addend.
904 relaxGotNoPic(Loc, Val + 4, Op, ModRm);
905}
906
Hal Finkel3c8cc672015-10-12 20:56:18 +0000907// Relocation masks following the #lo(value), #hi(value), #ha(value),
908// #higher(value), #highera(value), #highest(value), and #highesta(value)
909// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
910// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000911static uint16_t applyPPCLo(uint64_t V) { return V; }
912static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
913static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
914static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
915static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000916static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000917static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
918
Davide Italiano8c3444362016-01-11 19:45:33 +0000919PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000920
Rafael Espindola22ef9562016-04-13 01:40:19 +0000921void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
922 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000923 switch (Type) {
924 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000925 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000926 break;
927 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000928 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000929 break;
930 default:
George Rimar57610422016-03-11 14:43:02 +0000931 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000932 }
933}
934
Rafael Espindola22ef9562016-04-13 01:40:19 +0000935RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
936 return R_ABS;
937}
938
Rafael Espindolac4010882015-09-22 20:54:08 +0000939PPC64TargetInfo::PPC64TargetInfo() {
Rafael Espindolae4c86d832016-05-18 21:03:36 +0000940 PltRel = GotRel = R_PPC64_GLOB_DAT;
Rui Ueyama724d6252016-01-29 01:49:32 +0000941 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000942 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000943
944 // We need 64K pages (at least under glibc/Linux, the loader won't
945 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000946 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000947
948 // The PPC64 ELF ABI v1 spec, says:
949 //
950 // It is normally desirable to put segments with different characteristics
951 // in separate 256 Mbyte portions of the address space, to give the
952 // operating system full paging flexibility in the 64-bit address space.
953 //
954 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
955 // use 0x10000000 as the starting address.
956 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000957}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000958
Rafael Espindola15cec292016-04-27 12:25:22 +0000959static uint64_t PPC64TocOffset = 0x8000;
960
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000961uint64_t getPPC64TocBase() {
Rafael Espindola520ed3a2016-04-27 12:21:27 +0000962 // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
963 // TOC starts where the first of these sections starts. We always create a
964 // .got when we see a relocation that uses it, so for us the start is always
965 // the .got.
Hal Finkel3c8cc672015-10-12 20:56:18 +0000966 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
Hal Finkel3c8cc672015-10-12 20:56:18 +0000967
968 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
969 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
970 // code (crt1.o) assumes that you can get from the TOC base to the
971 // start of the .toc section with only a single (signed) 16-bit relocation.
Rafael Espindola15cec292016-04-27 12:25:22 +0000972 return TocVA + PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000973}
974
Rafael Espindola22ef9562016-04-13 01:40:19 +0000975RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
976 switch (Type) {
977 default:
978 return R_ABS;
Rafael Espindola15cec292016-04-27 12:25:22 +0000979 case R_PPC64_TOC16:
980 case R_PPC64_TOC16_DS:
981 case R_PPC64_TOC16_HA:
982 case R_PPC64_TOC16_HI:
983 case R_PPC64_TOC16_LO:
984 case R_PPC64_TOC16_LO_DS:
985 return R_GOTREL;
Rafael Espindola365e5f62016-04-27 11:54:07 +0000986 case R_PPC64_TOC:
987 return R_PPC_TOC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000988 case R_PPC64_REL24:
Rafael Espindolab312a742016-04-21 17:30:24 +0000989 return R_PPC_PLT_OPD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000990 }
991}
992
Rui Ueyama9398f862016-01-29 04:15:02 +0000993void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
994 uint64_t PltEntryAddr, int32_t Index,
995 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000996 uint64_t Off = GotEntryAddr - getPPC64TocBase();
997
998 // FIXME: What we should do, in theory, is get the offset of the function
999 // descriptor in the .opd section, and use that as the offset from %r2 (the
1000 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1001 // be a pointer to the function descriptor in the .opd section. Using
1002 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1003
Hal Finkelfa92f682015-10-13 21:47:34 +00001004 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001005 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1006 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1007 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1008 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1009 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1010 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1011 write32be(Buf + 28, 0x4e800420); // bctr
1012}
1013
Rafael Espindola22ef9562016-04-13 01:40:19 +00001014void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1015 uint64_t Val) const {
Rafael Espindola15cec292016-04-27 12:25:22 +00001016 uint64_t TO = PPC64TocOffset;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001017
Rafael Espindola15cec292016-04-27 12:25:22 +00001018 // For a TOC-relative relocation, proceed in terms of the corresponding
1019 // ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001020 switch (Type) {
Rafael Espindola15cec292016-04-27 12:25:22 +00001021 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break;
1022 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break;
1023 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break;
1024 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break;
1025 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break;
1026 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 default: break;
1028 }
1029
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001031 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001032 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001033 // Preserve the AA/LK bits in the branch instruction
1034 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001035 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001036 break;
1037 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001039 checkInt<16>(Val, Type);
1040 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001041 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001042 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001043 checkInt<16>(Val, Type);
1044 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001045 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001046 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001047 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048 break;
1049 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001050 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001053 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001054 break;
1055 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001056 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001057 break;
1058 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001059 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060 break;
1061 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001062 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001064 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001065 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001066 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001067 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001068 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001069 break;
1070 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001071 checkInt<32>(Val, Type);
1072 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001074 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001075 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001076 break;
1077 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001078 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001079 break;
1080 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001081 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001082 break;
1083 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001084 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001085 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001086 case R_PPC64_REL24: {
1087 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001088 checkInt<24>(Val, Type);
1089 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001090 break;
1091 }
1092 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001093 checkInt<32>(Val, Type);
1094 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001095 break;
1096 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001097 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001098 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001099 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001100 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001101 break;
1102 default:
George Rimar57610422016-03-11 14:43:02 +00001103 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001104 }
1105}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001106
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001107AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001108 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001109 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001110 IRelativeRel = R_AARCH64_IRELATIVE;
1111 GotRel = R_AARCH64_GLOB_DAT;
1112 PltRel = R_AARCH64_JUMP_SLOT;
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001113 TlsDescRel = R_AARCH64_TLSDESC;
Rui Ueyama724d6252016-01-29 01:49:32 +00001114 TlsGotRel = R_AARCH64_TLS_TPREL64;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001115 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001116 PltZeroSize = 32;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001117
1118 // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant
1119 // 1 of the tls structures and the tcb size is 16.
1120 TcbSize = 16;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001121}
George Rimar648a2c32015-10-20 08:54:27 +00001122
Rafael Espindola22ef9562016-04-13 01:40:19 +00001123RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1124 const SymbolBody &S) const {
1125 switch (Type) {
1126 default:
1127 return R_ABS;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001128
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001129 case R_AARCH64_TLSDESC_ADR_PAGE21:
1130 return R_TLSDESC_PAGE;
1131
1132 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1133 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1134 return R_TLSDESC;
1135
1136 case R_AARCH64_TLSDESC_CALL:
1137 return R_HINT;
1138
Rafael Espindola8818ca62016-05-20 17:41:09 +00001139 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1140 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1141 return R_TLS;
1142
Rafael Espindola22ef9562016-04-13 01:40:19 +00001143 case R_AARCH64_CALL26:
Rafael Espindolab312a742016-04-21 17:30:24 +00001144 case R_AARCH64_CONDBR19:
1145 case R_AARCH64_JUMP26:
1146 case R_AARCH64_TSTBR14:
1147 return R_PLT_PC;
1148
Rafael Espindola22ef9562016-04-13 01:40:19 +00001149 case R_AARCH64_PREL16:
1150 case R_AARCH64_PREL32:
1151 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001152 case R_AARCH64_ADR_PREL_LO21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001153 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001154 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001155 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001156 case R_AARCH64_LD64_GOT_LO12_NC:
1157 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1158 return R_GOT;
1159 case R_AARCH64_ADR_GOT_PAGE:
1160 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1161 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001162 }
1163}
1164
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001165RelExpr AArch64TargetInfo::adjustRelaxExpr(uint32_t Type, const uint8_t *Data,
1166 RelExpr Expr) const {
1167 if (Expr == R_RELAX_TLS_GD_TO_IE) {
1168 if (Type == R_AARCH64_TLSDESC_ADR_PAGE21)
1169 return R_RELAX_TLS_GD_TO_IE_PAGE_PC;
1170 return R_RELAX_TLS_GD_TO_IE_ABS;
1171 }
1172 return Expr;
1173}
1174
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001175bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001176 switch (Type) {
1177 default:
1178 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001179 case R_AARCH64_ADD_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001180 case R_AARCH64_LD64_GOT_LO12_NC:
1181 case R_AARCH64_LDST128_ABS_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001182 case R_AARCH64_LDST16_ABS_LO12_NC:
1183 case R_AARCH64_LDST32_ABS_LO12_NC:
1184 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola53d0a9f2016-06-02 15:24:52 +00001185 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001186 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1187 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindolade17d282016-05-04 21:40:07 +00001188 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001189 return true;
1190 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001191}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001192
George Rimar98b060d2016-03-06 06:01:07 +00001193bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001194 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1195 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1196}
1197
George Rimar98b060d2016-03-06 06:01:07 +00001198uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001199 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1200 return Type;
Rui Ueyama21923992016-02-01 23:28:21 +00001201 // Keep it going with a dummy value so that we can find more reloc errors.
Rui Ueyama45a873d2016-06-07 18:03:05 +00001202 warnDynRel(Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001203 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001204}
1205
Rui Ueyamac516ae12016-01-29 02:33:45 +00001206void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001207 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1208}
1209
Rafael Espindola22ef9562016-04-13 01:40:19 +00001210static uint64_t getAArch64Page(uint64_t Expr) {
1211 return Expr & (~static_cast<uint64_t>(0xFFF));
1212}
1213
Rui Ueyama900e2d22016-01-29 03:51:49 +00001214void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001215 const uint8_t PltData[] = {
1216 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1217 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1218 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1219 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1220 0x20, 0x02, 0x1f, 0xd6, // br x17
1221 0x1f, 0x20, 0x03, 0xd5, // nop
1222 0x1f, 0x20, 0x03, 0xd5, // nop
1223 0x1f, 0x20, 0x03, 0xd5 // nop
1224 };
1225 memcpy(Buf, PltData, sizeof(PltData));
1226
Rui Ueyama900e2d22016-01-29 03:51:49 +00001227 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1228 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001229 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1230 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1231 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1232 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001233}
1234
Rui Ueyama9398f862016-01-29 04:15:02 +00001235void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1236 uint64_t PltEntryAddr, int32_t Index,
1237 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001238 const uint8_t Inst[] = {
1239 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1240 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1241 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1242 0x20, 0x02, 0x1f, 0xd6 // br x17
1243 };
1244 memcpy(Buf, Inst, sizeof(Inst));
1245
Rafael Espindola22ef9562016-04-13 01:40:19 +00001246 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1247 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1248 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1249 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001250}
1251
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001252static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001253 uint32_t ImmLo = (Imm & 0x3) << 29;
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001254 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
1255 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001256 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001257}
1258
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001259static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1260 or32le(L, (Imm & 0xFFF) << 10);
1261}
1262
Rafael Espindola22ef9562016-04-13 01:40:19 +00001263void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1264 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001265 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001266 case R_AARCH64_ABS16:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001267 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001268 checkIntUInt<16>(Val, Type);
1269 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001270 break;
1271 case R_AARCH64_ABS32:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001272 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001273 checkIntUInt<32>(Val, Type);
1274 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001275 break;
1276 case R_AARCH64_ABS64:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001277 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001278 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001279 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001280 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001281 // This relocation stores 12 bits and there's no instruction
1282 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001283 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1284 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001285 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001286 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001287 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamae66f45c2016-05-25 04:10:14 +00001288 case R_AARCH64_ADR_PREL_PG_HI21:
1289 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001290 case R_AARCH64_TLSDESC_ADR_PAGE21:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001291 checkInt<33>(Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001292 updateAArch64Addr(Loc, Val >> 12);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001293 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001294 case R_AARCH64_ADR_PREL_LO21:
1295 checkInt<21>(Val, Type);
Rafael Espindola1c0eb972016-06-02 16:00:25 +00001296 updateAArch64Addr(Loc, Val);
Davide Italiano1d750a62015-09-27 08:45:38 +00001297 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001298 case R_AARCH64_CALL26:
Rafael Espindolad79073d2016-04-25 12:32:19 +00001299 case R_AARCH64_JUMP26:
1300 checkInt<28>(Val, Type);
1301 or32le(Loc, (Val & 0x0FFFFFFC) >> 2);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001302 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001303 case R_AARCH64_CONDBR19:
1304 checkInt<21>(Val, Type);
1305 or32le(Loc, (Val & 0x1FFFFC) << 3);
George Rimar4102bfb2016-01-11 14:22:00 +00001306 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001307 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001308 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001309 case R_AARCH64_TLSDESC_LD64_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001310 checkAlignment<8>(Val, Type);
1311 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001312 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001313 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001314 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001315 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001316 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001317 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001318 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001319 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001320 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001321 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001322 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001323 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001324 break;
1325 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001326 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001327 break;
Rafael Espindolad79073d2016-04-25 12:32:19 +00001328 case R_AARCH64_TSTBR14:
1329 checkInt<16>(Val, Type);
1330 or32le(Loc, (Val & 0xFFFC) << 3);
George Rimar1395dbd2016-01-11 14:27:05 +00001331 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001332 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1333 checkInt<24>(Val, Type);
Rafael Espindola1016f192016-06-02 15:51:40 +00001334 updateAArch64Add(Loc, Val >> 12);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001335 break;
Rafael Espindola8818ca62016-05-20 17:41:09 +00001336 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
Rafael Espindolae37d13b2016-06-02 19:49:53 +00001337 case R_AARCH64_TLSDESC_ADD_LO12_NC:
Rafael Espindola1016f192016-06-02 15:51:40 +00001338 updateAArch64Add(Loc, Val);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001339 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001340 default:
George Rimar57610422016-03-11 14:43:02 +00001341 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001342 }
1343}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001344
Rafael Espindola22ef9562016-04-13 01:40:19 +00001345void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1346 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001347 // TLSDESC Global-Dynamic relocation are in the form:
1348 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1349 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1350 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1351 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001352 // blr x1
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001353 // And it can optimized to:
1354 // movz x0, #0x0, lsl #16
1355 // movk x0, #0x10
1356 // nop
1357 // nop
Rafael Espindola8818ca62016-05-20 17:41:09 +00001358 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001359
1360 uint32_t NewInst;
1361 switch (Type) {
1362 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1363 case R_AARCH64_TLSDESC_CALL:
1364 // nop
1365 NewInst = 0xd503201f;
1366 break;
1367 case R_AARCH64_TLSDESC_ADR_PAGE21:
1368 // movz
Rafael Espindola8818ca62016-05-20 17:41:09 +00001369 NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001370 break;
1371 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1372 // movk
Rafael Espindola8818ca62016-05-20 17:41:09 +00001373 NewInst = 0xf2800000 | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001374 break;
1375 default:
George Rimar777f9632016-03-12 08:31:34 +00001376 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001377 }
1378 write32le(Loc, NewInst);
1379}
1380
Rafael Espindolae1979ae2016-06-04 23:33:31 +00001381void AArch64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
1382 uint64_t Val) const {
1383 // TLSDESC Global-Dynamic relocation are in the form:
1384 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1385 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1386 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1387 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1388 // blr x1
1389 // And it can optimized to:
1390 // adrp x0, :gottprel:v
1391 // ldr x0, [x0, :gottprel_lo12:v]
1392 // nop
1393 // nop
1394
1395 switch (Type) {
1396 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1397 case R_AARCH64_TLSDESC_CALL:
1398 write32le(Loc, 0xd503201f); // nop
1399 break;
1400 case R_AARCH64_TLSDESC_ADR_PAGE21:
1401 write32le(Loc, 0x90000000); // adrp
1402 relocateOne(Loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, Val);
1403 break;
1404 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1405 write32le(Loc, 0xf9400000); // ldr
1406 relocateOne(Loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, Val);
1407 break;
1408 default:
1409 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
1410 }
1411}
1412
Rafael Espindola22ef9562016-04-13 01:40:19 +00001413void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1414 uint64_t Val) const {
Rafael Espindola8818ca62016-05-20 17:41:09 +00001415 checkUInt<32>(Val, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001416
George Rimar4d1d16d2016-03-06 06:16:05 +00001417 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001418 uint32_t NewInst;
1419 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1420 // Generate movz.
1421 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001422 NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001423 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1424 // Generate movk
1425 unsigned RegNo = (Inst & 0x1f);
Rafael Espindola8818ca62016-05-20 17:41:09 +00001426 NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001427 } else {
George Rimar777f9632016-03-12 08:31:34 +00001428 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001429 }
1430 write32le(Loc, NewInst);
1431}
1432
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001433// Implementing relocations for AMDGPU is low priority since most
1434// programs don't use relocations now. Thus, this function is not
1435// actually called (relocateOne is called for each relocation).
1436// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001437void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1438 uint64_t Val) const {
1439 llvm_unreachable("not implemented");
1440}
1441
1442RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001443 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001444}
1445
Peter Smith8646ced2016-06-07 09:31:52 +00001446ARMTargetInfo::ARMTargetInfo() {
1447 CopyRel = R_ARM_COPY;
1448 RelativeRel = R_ARM_RELATIVE;
1449 IRelativeRel = R_ARM_IRELATIVE;
1450 GotRel = R_ARM_GLOB_DAT;
1451 PltRel = R_ARM_JUMP_SLOT;
1452 TlsGotRel = R_ARM_TLS_TPOFF32;
1453 TlsModuleIndexRel = R_ARM_TLS_DTPMOD32;
1454 TlsOffsetRel = R_ARM_TLS_DTPOFF32;
1455 PltEntrySize = 16;
1456 PltZeroSize = 20;
1457}
1458
1459RelExpr ARMTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1460 switch (Type) {
1461 default:
1462 return R_ABS;
1463 case R_ARM_CALL:
1464 case R_ARM_JUMP24:
1465 case R_ARM_PC24:
1466 case R_ARM_PLT32:
1467 return R_PLT_PC;
1468 case R_ARM_GOTOFF32:
1469 // (S + A) - GOT_ORG
1470 return R_GOTREL;
1471 case R_ARM_GOT_BREL:
1472 // GOT(S) + A - GOT_ORG
1473 return R_GOT_OFF;
1474 case R_ARM_GOT_PREL:
1475 // GOT(S) + - GOT_ORG
1476 return R_GOT_PC;
1477 case R_ARM_BASE_PREL:
1478 // B(S) + A - P
1479 // FIXME: currently B(S) assumed to be .got, this may not hold for all
1480 // platforms.
1481 return R_GOTONLY_PC;
1482 case R_ARM_PREL31:
1483 case R_ARM_REL32:
1484 return R_PC;
1485 }
1486}
1487
1488uint32_t ARMTargetInfo::getDynRel(uint32_t Type) const {
1489 if (Type == R_ARM_ABS32)
1490 return Type;
Peter Smith8646ced2016-06-07 09:31:52 +00001491 // Keep it going with a dummy value so that we can find more reloc errors.
Rui Ueyama45a873d2016-06-07 18:03:05 +00001492 warnDynRel(Type);
Peter Smith8646ced2016-06-07 09:31:52 +00001493 return R_ARM_ABS32;
1494}
1495
1496void ARMTargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1497 write32le(Buf, Out<ELF32LE>::Plt->getVA());
1498}
1499
1500void ARMTargetInfo::writePltZero(uint8_t *Buf) const {
1501 const uint8_t PltData[] = {
1502 0x04, 0xe0, 0x2d, 0xe5, // str lr, [sp,#-4]!
1503 0x04, 0xe0, 0x9f, 0xe5, // ldr lr, L2
1504 0x0e, 0xe0, 0x8f, 0xe0, // L1: add lr, pc, lr
1505 0x08, 0xf0, 0xbe, 0xe5, // ldr pc, [lr, #8]
1506 0x00, 0x00, 0x00, 0x00, // L2: .word &(.got.plt) - L1 - 8
1507 };
1508 memcpy(Buf, PltData, sizeof(PltData));
1509 uint64_t GotPlt = Out<ELF32LE>::GotPlt->getVA();
1510 uint64_t L1 = Out<ELF32LE>::Plt->getVA() + 8;
1511 write32le(Buf + 16, GotPlt - L1 - 8);
1512}
1513
1514void ARMTargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1515 uint64_t PltEntryAddr, int32_t Index,
1516 unsigned RelOff) const {
1517 // FIXME: Using simple code sequence with simple relocations.
1518 // There is a more optimal sequence but it requires support for the group
1519 // relocations. See ELF for the ARM Architecture Appendix A.3
1520 const uint8_t PltData[] = {
1521 0x04, 0xc0, 0x9f, 0xe5, // ldr ip, L2
1522 0x0f, 0xc0, 0x8c, 0xe0, // L1: add ip, ip, pc
1523 0x00, 0xf0, 0x9c, 0xe5, // ldr pc, [ip]
1524 0x00, 0x00, 0x00, 0x00, // L2: .word Offset(&(.plt.got) - L1 - 8
1525 };
1526 memcpy(Buf, PltData, sizeof(PltData));
1527 uint64_t L1 = PltEntryAddr + 4;
1528 write32le(Buf + 12, GotEntryAddr - L1 - 8);
1529}
1530
1531void ARMTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1532 uint64_t Val) const {
1533 switch (Type) {
1534 case R_ARM_NONE:
1535 break;
1536 case R_ARM_ABS32:
1537 case R_ARM_BASE_PREL:
1538 case R_ARM_GOTOFF32:
1539 case R_ARM_GOT_BREL:
1540 case R_ARM_GOT_PREL:
1541 case R_ARM_REL32:
1542 write32le(Loc, Val);
1543 break;
1544 case R_ARM_PREL31:
1545 checkInt<31>(Val, Type);
1546 write32le(Loc, (read32le(Loc) & 0x80000000) | (Val & ~0x80000000));
1547 break;
1548 case R_ARM_CALL:
1549 case R_ARM_JUMP24:
1550 case R_ARM_PC24:
1551 case R_ARM_PLT32:
1552 checkInt<26>(Val, Type);
1553 write32le(Loc, (read32le(Loc) & ~0x00ffffff) | ((Val >> 2) & 0x00ffffff));
1554 break;
1555 case R_ARM_MOVW_ABS_NC:
1556 write32le(Loc, (read32le(Loc) & ~0x000f0fff) | ((Val & 0xf000) << 4) |
1557 (Val & 0x0fff));
1558 break;
1559 case R_ARM_MOVT_ABS:
1560 checkUInt<32>(Val, Type);
1561 write32le(Loc, (read32le(Loc) & ~0x000f0fff) |
1562 (((Val >> 16) & 0xf000) << 4) | ((Val >> 16) & 0xfff));
1563 break;
1564 default:
1565 fatal("unrecognized reloc " + Twine(Type));
1566 }
1567}
1568
1569uint64_t ARMTargetInfo::getImplicitAddend(const uint8_t *Buf,
1570 uint32_t Type) const {
1571 switch (Type) {
1572 default:
1573 return 0;
1574 case R_ARM_ABS32:
1575 case R_ARM_BASE_PREL:
1576 case R_ARM_GOTOFF32:
1577 case R_ARM_GOT_BREL:
1578 case R_ARM_GOT_PREL:
1579 case R_ARM_REL32:
1580 return SignExtend64<32>(read32le(Buf));
1581 break;
1582 case R_ARM_PREL31:
1583 return SignExtend64<31>(read32le(Buf));
1584 break;
1585 case R_ARM_CALL:
1586 case R_ARM_JUMP24:
1587 case R_ARM_PC24:
1588 case R_ARM_PLT32:
1589 return SignExtend64<26>((read32le(Buf) & 0x00ffffff) << 2);
1590 case R_ARM_MOVW_ABS_NC:
1591 case R_ARM_MOVT_ABS: {
1592 // ELF for the ARM Architecture 4.6.1.1 the implicit addend for MOVW and
1593 // MOVT is in the range -32768 <= A < 32768
1594 uint64_t Val = read32le(Buf) & 0x000f0fff;
1595 return SignExtend64<16>(((Val & 0x000f0000) >> 4) | (Val & 0x00fff));
1596 }
1597 }
1598}
1599
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001600template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001601 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001602 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001603 PltEntrySize = 16;
1604 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001605 ThunkSize = 16;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001606 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001607 PltRel = R_MIPS_JUMP_SLOT;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001608 if (ELFT::Is64Bits)
1609 RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32;
1610 else
1611 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001612}
1613
1614template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001615RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1616 const SymbolBody &S) const {
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001617 if (ELFT::Is64Bits)
1618 // See comment in the calculateMips64RelChain.
1619 Type &= 0xff;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001620 switch (Type) {
1621 default:
1622 return R_ABS;
Rafael Espindolaebb04b92016-05-04 14:44:22 +00001623 case R_MIPS_JALR:
1624 return R_HINT;
Rafael Espindola1763dc42016-04-26 22:00:04 +00001625 case R_MIPS_GPREL16:
1626 case R_MIPS_GPREL32:
1627 return R_GOTREL;
Rafael Espindolab312a742016-04-21 17:30:24 +00001628 case R_MIPS_26:
1629 return R_PLT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001630 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001631 case R_MIPS_LO16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001632 case R_MIPS_GOT_OFST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001633 // MIPS _gp_disp designates offset between start of function and 'gp'
1634 // pointer into GOT. __gnu_local_gp is equal to the current value of
1635 // the 'gp'. Therefore any relocations against them do not require
1636 // dynamic relocation.
1637 if (&S == ElfSym<ELFT>::MipsGpDisp)
1638 return R_PC;
1639 return R_ABS;
1640 case R_MIPS_PC32:
1641 case R_MIPS_PC16:
1642 case R_MIPS_PC19_S2:
1643 case R_MIPS_PC21_S2:
1644 case R_MIPS_PC26_S2:
1645 case R_MIPS_PCHI16:
1646 case R_MIPS_PCLO16:
1647 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001648 case R_MIPS_GOT16:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001649 if (S.isLocal())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001650 return R_MIPS_GOT_LOCAL_PAGE;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001651 // fallthrough
1652 case R_MIPS_CALL16:
1653 case R_MIPS_GOT_DISP:
Rafael Espindola5628ee72016-04-15 19:14:18 +00001654 if (!S.isPreemptible())
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001655 return R_MIPS_GOT_LOCAL;
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001656 return R_GOT_OFF;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001657 case R_MIPS_GOT_PAGE:
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +00001658 return R_MIPS_GOT_LOCAL_PAGE;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001659 }
1660}
1661
1662template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001663uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001664 if (Type == R_MIPS_32 || Type == R_MIPS_64)
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001665 return RelativeRel;
Rui Ueyama21923992016-02-01 23:28:21 +00001666 // Keep it going with a dummy value so that we can find more reloc errors.
Rui Ueyama45a873d2016-06-07 18:03:05 +00001667 warnDynRel(Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001668 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001669}
1670
1671template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001672void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1673 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001674}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001675
Simon Atanasyan35031192015-12-15 06:06:34 +00001676static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001677
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001678template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001679static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001680 uint32_t Instr = read32<E>(Loc);
1681 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1682 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1683}
1684
1685template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001686static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001687 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001688 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001689 if (SHIFT > 0)
1690 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001691 checkInt<BSIZE + SHIFT>(V, Type);
1692 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001693}
1694
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001695template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001696static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001697 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001698 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001699}
1700
Simon Atanasyan3b377852016-03-04 10:55:20 +00001701template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001702static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1703 uint32_t Instr = read32<E>(Loc);
1704 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1705}
1706
Rafael Espindola666625b2016-04-01 14:36:09 +00001707template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001708 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1709}
1710
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001711template <class ELFT>
1712void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1713 const endianness E = ELFT::TargetEndianness;
1714 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1715 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1716 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1717 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1718 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1719 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1720 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1721 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1722 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001723 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001724 writeMipsLo16<E>(Buf + 4, Got);
1725 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001726}
1727
1728template <class ELFT>
1729void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1730 uint64_t PltEntryAddr, int32_t Index,
1731 unsigned RelOff) const {
1732 const endianness E = ELFT::TargetEndianness;
1733 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1734 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1735 write32<E>(Buf + 8, 0x03200008); // jr $25
1736 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001737 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001738 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1739 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001740}
1741
1742template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001743void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1744 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1745 // See MipsTargetInfo::writeThunk for details.
1746 const endianness E = ELFT::TargetEndianness;
1747 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1748 write32<E>(Buf + 4, 0x08000000); // j func
1749 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1750 write32<E>(Buf + 12, 0x00000000); // nop
1751 writeMipsHi16<E>(Buf, S);
1752 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1753 writeMipsLo16<E>(Buf + 8, S);
1754}
1755
1756template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001757bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1758 const SymbolBody &S) const {
1759 // Any MIPS PIC code function is invoked with its address in register $t9.
1760 // So if we have a branch instruction from non-PIC code to the PIC one
1761 // we cannot make the jump directly and need to create a small stubs
1762 // to save the target function address.
1763 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1764 if (Type != R_MIPS_26)
1765 return false;
1766 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1767 if (!F)
1768 return false;
1769 // If current file has PIC code, LA25 stub is not required.
1770 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1771 return false;
1772 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1773 if (!D || !D->Section)
1774 return false;
1775 // LA25 is required if target file has PIC code
1776 // or target symbol is a PIC symbol.
1777 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001778 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001779}
1780
1781template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001782uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001783 uint32_t Type) const {
1784 const endianness E = ELFT::TargetEndianness;
1785 switch (Type) {
1786 default:
1787 return 0;
1788 case R_MIPS_32:
1789 case R_MIPS_GPREL32:
1790 return read32<E>(Buf);
1791 case R_MIPS_26:
1792 // FIXME (simon): If the relocation target symbol is not a PLT entry
1793 // we should use another expression for calculation:
1794 // ((A << 2) | (P & 0xf0000000)) >> 2
1795 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1796 case R_MIPS_GPREL16:
1797 case R_MIPS_LO16:
1798 case R_MIPS_PCLO16:
1799 case R_MIPS_TLS_DTPREL_HI16:
1800 case R_MIPS_TLS_DTPREL_LO16:
1801 case R_MIPS_TLS_TPREL_HI16:
1802 case R_MIPS_TLS_TPREL_LO16:
1803 return readSignedLo16<E>(Buf);
1804 case R_MIPS_PC16:
1805 return getPcRelocAddend<E, 16, 2>(Buf);
1806 case R_MIPS_PC19_S2:
1807 return getPcRelocAddend<E, 19, 2>(Buf);
1808 case R_MIPS_PC21_S2:
1809 return getPcRelocAddend<E, 21, 2>(Buf);
1810 case R_MIPS_PC26_S2:
1811 return getPcRelocAddend<E, 26, 2>(Buf);
1812 case R_MIPS_PC32:
1813 return getPcRelocAddend<E, 32, 0>(Buf);
1814 }
1815}
1816
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001817static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type,
1818 uint64_t Val) {
1819 // MIPS N64 ABI packs multiple relocations into the single relocation
1820 // record. In general, all up to three relocations can have arbitrary
1821 // types. In fact, Clang and GCC uses only a few combinations. For now,
1822 // we support two of them. That is allow to pass at least all LLVM
1823 // test suite cases.
1824 // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16
1825 // <any relocation> / R_MIPS_64 / R_MIPS_NONE
1826 // The first relocation is a 'real' relocation which is calculated
1827 // using the corresponding symbol's value. The second and the third
1828 // relocations used to modify result of the first one: extend it to
1829 // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation
1830 // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf
1831 uint32_t Type2 = (Type >> 8) & 0xff;
1832 uint32_t Type3 = (Type >> 16) & 0xff;
1833 if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE)
1834 return std::make_pair(Type, Val);
1835 if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE)
1836 return std::make_pair(Type2, Val);
1837 if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
1838 return std::make_pair(Type3, -Val);
1839 error("unsupported relocations combination " + Twine(Type));
1840 return std::make_pair(Type & 0xff, Val);
1841}
1842
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001843template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001844void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1845 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001846 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001847 // Thread pointer and DRP offsets from the start of TLS data area.
1848 // https://www.linux-mips.org/wiki/NPTL
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001849 if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16)
1850 Val -= 0x8000;
1851 else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16)
1852 Val -= 0x7000;
1853 if (ELFT::Is64Bits)
1854 std::tie(Type, Val) = calculateMips64RelChain(Type, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001855 switch (Type) {
1856 case R_MIPS_32:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001857 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001858 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001859 break;
Simon Atanasyanda83bbc2016-05-04 22:39:40 +00001860 case R_MIPS_64:
1861 write64<E>(Loc, Val);
1862 break;
Simon Atanasyan10296c22016-05-07 07:36:47 +00001863 case R_MIPS_26:
1864 write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001865 break;
Simon Atanasyanbe804552016-05-04 17:47:11 +00001866 case R_MIPS_GOT_DISP:
1867 case R_MIPS_GOT_PAGE:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001868 case R_MIPS_GOT16:
Simon Atanasyan9ac81982016-05-06 15:02:50 +00001869 case R_MIPS_GPREL16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001870 checkInt<16>(Val, Type);
1871 // fallthrough
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001872 case R_MIPS_CALL16:
Simon Atanasyanbe804552016-05-04 17:47:11 +00001873 case R_MIPS_GOT_OFST:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001874 case R_MIPS_LO16:
1875 case R_MIPS_PCLO16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001876 case R_MIPS_TLS_DTPREL_LO16:
1877 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola58cd5db2016-04-19 22:46:03 +00001878 writeMipsLo16<E>(Loc, Val);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001879 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001880 case R_MIPS_HI16:
Simon Atanasyan5b9ac412016-05-06 15:02:54 +00001881 case R_MIPS_PCHI16:
Simon Atanasyan8c8a5b52016-05-08 14:08:40 +00001882 case R_MIPS_TLS_DTPREL_HI16:
1883 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001884 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001885 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001886 case R_MIPS_JALR:
1887 // Ignore this optimization relocation for now
1888 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001889 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001890 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001891 break;
1892 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001893 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001894 break;
1895 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001896 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001897 break;
1898 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001899 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001900 break;
1901 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001902 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001903 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001904 default:
George Rimar57610422016-03-11 14:43:02 +00001905 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001906 }
1907}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001908
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001909template <class ELFT>
Rafael Espindolab8ff59a2016-04-28 14:34:39 +00001910bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const {
Simon Atanasyanbe804552016-05-04 17:47:11 +00001911 return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001912}
Rafael Espindola01205f72015-09-22 18:19:46 +00001913}
1914}