blob: 0e07fae502b9ada590ce7f19e6d439257bf2c103 [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//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Simon Atanasyan13f6da12016-03-31 21:26:23 +000020#include "InputFiles.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000021#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000022#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000023
24#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000025#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000026#include "llvm/Support/Endian.h"
27#include "llvm/Support/ELF.h"
28
29using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000030using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000031using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000032using namespace llvm::ELF;
33
34namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000035namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000036
Rui Ueyamac1c282a2016-02-11 21:18:01 +000037TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000038
Rafael Espindolae7e57b22015-11-09 21:43:00 +000039static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000040
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000041template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
42 if (isInt<N>(V))
43 return;
44 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000045 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000046}
47
48template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
49 if (isUInt<N>(V))
50 return;
51 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000052 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000053}
54
Igor Kudrinfea8ed52015-11-26 10:05:24 +000055template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
56 if (isInt<N>(V) || isUInt<N>(V))
57 return;
58 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000059 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000060}
61
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000062template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
63 if ((V & (N - 1)) == 0)
64 return;
65 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000066 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000067}
68
Rui Ueyamaefc23de2015-10-14 21:30:32 +000069namespace {
70class X86TargetInfo final : public TargetInfo {
71public:
72 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000073 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000074 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000075 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000076 uint32_t getDynRel(uint32_t Type) const override;
77 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000078 bool isTlsLocalDynamicRel(uint32_t Type) const override;
79 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
80 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000081 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000082 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000083 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
84 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000085 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000086 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000087 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000088 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000089 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000090
Rafael Espindola22ef9562016-04-13 01:40:19 +000091 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
92 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
93 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
94 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000095
Rafael Espindola795dc5a2016-02-24 18:24:23 +000096 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000097};
98
99class X86_64TargetInfo final : public TargetInfo {
100public:
101 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000102 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000103 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000104 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000105 bool isTlsLocalDynamicRel(uint32_t Type) const override;
106 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
107 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000108 void writeGotPltHeader(uint8_t *Buf) const override;
109 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000110 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000111 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
112 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000113 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000114 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000115 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000116 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000117 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000118
Rafael Espindola22ef9562016-04-13 01:40:19 +0000119 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
120 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
121 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
122 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000123};
124
Davide Italiano8c3444362016-01-11 19:45:33 +0000125class PPCTargetInfo final : public TargetInfo {
126public:
127 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000128 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000129 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000130 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000131};
132
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000133class PPC64TargetInfo final : public TargetInfo {
134public:
135 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000136 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000137 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
138 int32_t Index, unsigned RelOff) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000139 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000140 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000141 bool isRelRelative(uint32_t Type) const override;
142};
143
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000144class AArch64TargetInfo final : public TargetInfo {
145public:
146 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000147 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000148 uint32_t getDynRel(uint32_t Type) const override;
149 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
150 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000151 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000152 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000153 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
154 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000155 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000156 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000157 bool needsCopyRelImpl(uint32_t Type) const override;
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +0000158 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000159 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000160 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
161 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
162 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000163
164private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000165 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000166};
167
Tom Stellard80efb162016-01-07 03:59:08 +0000168class AMDGPUTargetInfo final : public TargetInfo {
169public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000170 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000171 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
172 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000173};
174
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000175template <class ELFT> class MipsTargetInfo final : public TargetInfo {
176public:
177 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000178 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000179 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000180 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000181 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
182 void writePltZero(uint8_t *Buf) const override;
183 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
184 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000185 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000186 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000187 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000188 bool needsPltImpl(uint32_t Type) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000189 bool needsThunk(uint32_t Type, const InputFile &File,
190 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000191 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000192 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000193 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000194 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000195};
196} // anonymous namespace
197
Rui Ueyama91004392015-10-13 16:08:15 +0000198TargetInfo *createTarget() {
199 switch (Config->EMachine) {
200 case EM_386:
201 return new X86TargetInfo();
202 case EM_AARCH64:
203 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000204 case EM_AMDGPU:
205 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000206 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000207 switch (Config->EKind) {
208 case ELF32LEKind:
209 return new MipsTargetInfo<ELF32LE>();
210 case ELF32BEKind:
211 return new MipsTargetInfo<ELF32BE>();
212 default:
George Rimar777f9632016-03-12 08:31:34 +0000213 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000214 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000215 case EM_PPC:
216 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000217 case EM_PPC64:
218 return new PPC64TargetInfo();
219 case EM_X86_64:
220 return new X86_64TargetInfo();
221 }
George Rimar777f9632016-03-12 08:31:34 +0000222 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000223}
224
Rafael Espindola01205f72015-09-22 18:19:46 +0000225TargetInfo::~TargetInfo() {}
226
Rafael Espindola666625b2016-04-01 14:36:09 +0000227uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
228 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000229 return 0;
230}
231
George Rimar98b060d2016-03-06 06:01:07 +0000232bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000233 if (Config->Shared || (S && !S->isTls()))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000234 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000235
Rafael Espindolad405f472016-03-04 21:37:09 +0000236 // We know we are producing an executable.
237
238 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
239 // depending on the symbol being locally defined or not.
240 if (isTlsGlobalDynamicRel(Type))
241 return true;
242
243 // Local-Dynamic relocs can be relaxed to Local-Exec.
244 if (isTlsLocalDynamicRel(Type))
245 return true;
246
247 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
248 // defined.
249 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000250 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000251
George Rimar77d1cb12015-11-24 09:00:06 +0000252 return false;
253}
254
George Rimar786e8662016-03-17 05:57:33 +0000255uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000256
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000257bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
258
259template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
260 if (Config->Shared)
261 return false;
262 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
263 if (!SS)
264 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000265 return SS->isObject();
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000266}
267
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000268template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000269bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000270 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000271}
272
Rui Ueyamac516ae12016-01-29 02:33:45 +0000273bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000274bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
George Rimar48651482015-12-11 08:59:37 +0000275
Rafael Espindola993f0272016-02-26 14:27:47 +0000276bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000277
278bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
279
Rafael Espindola852860e2016-02-12 15:47:37 +0000280TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
281 const SymbolBody &S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000282 if (S.isGnuIFunc())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000283 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000284 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000285 return Plt_Explicit;
286
287 // This handles a non PIC program call to function in a shared library.
288 // In an ideal world, we could just report an error saying the relocation
289 // can overflow at runtime.
290 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
291 // libc.so.
292 //
293 // The general idea on how to handle such cases is to create a PLT entry
294 // and use that as the function value.
295 //
296 // For the static linking part, we just return true and everything else
297 // will use the the PLT entry as the address.
298 //
299 // The remaining problem is making sure pointer equality still works. We
300 // need the help of the dynamic linker for that. We let it know that we have
301 // a direct reference to a so symbol by creating an undefined symbol with a
302 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
303 // the value of the symbol we created. This is true even for got entries, so
304 // pointer equality is maintained. To avoid an infinite loop, the only entry
305 // that points to the real function is a dedicated got entry used by the
306 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
307 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000308 if (S.isShared())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000309 if (!Config->Pic && S.isFunc() && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000310 return Plt_Implicit;
311
Rafael Espindola852860e2016-02-12 15:47:37 +0000312 return Plt_No;
313}
Rui Ueyama012eb782016-01-29 04:05:09 +0000314
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000315bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
316 const SymbolBody &S) const {
317 return false;
318}
319
George Rimar98b060d2016-03-06 06:01:07 +0000320bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000321
George Rimar98b060d2016-03-06 06:01:07 +0000322bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000323
George Rimar98b060d2016-03-06 06:01:07 +0000324bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000325 return false;
326}
327
Rafael Espindola22ef9562016-04-13 01:40:19 +0000328void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
329 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000330 llvm_unreachable("Should not have claimed to be relaxable");
331}
332
Rafael Espindola22ef9562016-04-13 01:40:19 +0000333void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
334 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000335 llvm_unreachable("Should not have claimed to be relaxable");
336}
337
Rafael Espindola22ef9562016-04-13 01:40:19 +0000338void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
339 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000340 llvm_unreachable("Should not have claimed to be relaxable");
341}
342
Rafael Espindola22ef9562016-04-13 01:40:19 +0000343void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
344 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000345 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000346}
George Rimar77d1cb12015-11-24 09:00:06 +0000347
Rafael Espindola7f074422015-09-22 21:35:51 +0000348X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000349 CopyRel = R_386_COPY;
350 GotRel = R_386_GLOB_DAT;
351 PltRel = R_386_JUMP_SLOT;
352 IRelativeRel = R_386_IRELATIVE;
353 RelativeRel = R_386_RELATIVE;
354 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000355 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
356 TlsOffsetRel = R_386_TLS_DTPOFF32;
357 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000358 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000359 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000360 TlsGdToLeSkip = 2;
361}
362
363RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
364 switch (Type) {
365 default:
366 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000367 case R_386_TLS_GD:
368 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000369 case R_386_TLS_LDM:
370 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000371 case R_386_PLT32:
372 case R_386_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000373 return R_PC;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000374 case R_386_GOTPC:
375 return R_GOTONLY_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000376 case R_386_TLS_IE:
377 return R_GOT;
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000378 case R_386_GOT32:
379 case R_386_TLS_GOTIE:
380 return R_GOT_FROM_END;
381 case R_386_GOTOFF:
382 return R_GOTREL;
383 case R_386_TLS_LE:
384 return R_TLS;
385 case R_386_TLS_LE_32:
386 return R_NEG_TLS;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000387 }
George Rimar77b77792015-11-25 22:15:01 +0000388}
389
Rafael Espindolaffcad442016-03-23 14:58:25 +0000390bool X86TargetInfo::isRelRelative(uint32_t Type) const {
391 switch (Type) {
392 default:
393 return false;
394 case R_386_PC32:
395 case R_386_PLT32:
396 case R_386_TLS_LDO_32:
397 return true;
398 }
399}
400
Rui Ueyamac516ae12016-01-29 02:33:45 +0000401void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000402 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
403}
404
Rui Ueyamac516ae12016-01-29 02:33:45 +0000405void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000406 // Entries in .got.plt initially points back to the corresponding
407 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000408 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000409}
Rafael Espindola01205f72015-09-22 18:19:46 +0000410
George Rimar98b060d2016-03-06 06:01:07 +0000411uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000412 if (Type == R_386_TLS_LE)
413 return R_386_TLS_TPOFF;
414 if (Type == R_386_TLS_LE_32)
415 return R_386_TLS_TPOFF32;
416 return Type;
417}
418
George Rimar98b060d2016-03-06 06:01:07 +0000419uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000420 if (Type == R_386_TLS_IE)
421 return Type;
Rafael Espindolae149b482016-04-14 16:05:42 +0000422 return R_386_GOT32;
George Rimar6f17e092015-12-17 09:32:21 +0000423}
424
George Rimar98b060d2016-03-06 06:01:07 +0000425bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000426 return Type == R_386_TLS_GD;
427}
428
George Rimar98b060d2016-03-06 06:01:07 +0000429bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000430 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
431}
432
George Rimar98b060d2016-03-06 06:01:07 +0000433bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000434 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
435}
436
Rui Ueyama900e2d22016-01-29 03:51:49 +0000437void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000438 // Executable files and shared object files have
439 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000440 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000441 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000442 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000443 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
444 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000445 };
446 memcpy(Buf, V, sizeof(V));
447 return;
448 }
George Rimar648a2c32015-10-20 08:54:27 +0000449
George Rimar77b77792015-11-25 22:15:01 +0000450 const uint8_t PltData[] = {
451 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000452 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
453 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000454 };
455 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000456 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000457 write32le(Buf + 2, Got + 4);
458 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000459}
460
Rui Ueyama9398f862016-01-29 04:15:02 +0000461void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
462 uint64_t PltEntryAddr, int32_t Index,
463 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000464 const uint8_t Inst[] = {
465 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
466 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
467 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
468 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000469 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000470
George Rimar77b77792015-11-25 22:15:01 +0000471 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000472 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000473 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
474 : Out<ELF32LE>::Got->getVA();
475 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000476 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000477 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000478}
479
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000480bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
481 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000482}
483
Rafael Espindola993f0272016-02-26 14:27:47 +0000484bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
485 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000486}
487
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000488bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
489 return Type == R_386_GOT32;
490}
491
Rafael Espindola666625b2016-04-01 14:36:09 +0000492uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
493 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000494 switch (Type) {
495 default:
496 return 0;
497 case R_386_32:
498 case R_386_GOT32:
499 case R_386_GOTOFF:
500 case R_386_GOTPC:
501 case R_386_PC32:
502 case R_386_PLT32:
503 return read32le(Buf);
504 }
505}
506
Rafael Espindola22ef9562016-04-13 01:40:19 +0000507void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
508 uint64_t Val) const {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000509 checkInt<32>(Val, Type);
510 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000511}
512
George Rimar98b060d2016-03-06 06:01:07 +0000513bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000514 return Config->Shared && Type == R_386_TLS_IE;
515}
516
Rafael Espindola22ef9562016-04-13 01:40:19 +0000517void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
518 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000519 // GD can be optimized to LE:
520 // leal x@tlsgd(, %ebx, 1),
521 // call __tls_get_addr@plt
522 // Can be converted to:
523 // movl %gs:0,%eax
524 // addl $x@ntpoff,%eax
525 // But gold emits subl $foo@tpoff,%eax instead of addl.
526 // These instructions are completely equal in behavior.
527 // This method generates subl to be consistent with gold.
528 const uint8_t Inst[] = {
529 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
530 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
531 };
532 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000533 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000534}
535
536// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
537// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
538// how GD can be optimized to IE:
539// leal x@tlsgd(, %ebx, 1),
540// call __tls_get_addr@plt
541// Is converted to:
542// movl %gs:0, %eax
543// addl x@gotntpoff(%ebx), %eax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000544void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
545 uint64_t Val) const {
George Rimar2558e122015-12-09 09:55:54 +0000546 const uint8_t Inst[] = {
547 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
548 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
549 };
550 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000551 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
552 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000553}
554
George Rimar6f17e092015-12-17 09:32:21 +0000555// In some conditions, relocations can be optimized to avoid using GOT.
556// This function does that for Initial Exec to Local Exec case.
557// Read "ELF Handling For Thread-Local Storage, 5.1
558// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000559// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000560void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
561 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000562 // Ulrich's document section 6.2 says that @gotntpoff can
563 // be used with MOVL or ADDL instructions.
564 // @indntpoff is similar to @gotntpoff, but for use in
565 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000566 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000567 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000568 uint8_t Reg = (Loc[-1] >> 3) & 7;
569 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000570 if (Type == R_386_TLS_IE) {
571 // For R_386_TLS_IE relocation we perform the next transformations:
572 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
573 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
574 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
575 // First one is special because when EAX is used the sequence is 5 bytes
576 // long, otherwise it is 6 bytes.
577 if (*Op == 0xa1) {
578 *Op = 0xb8;
579 } else {
580 *Inst = IsMov ? 0xc7 : 0x81;
581 *Op = 0xc0 | ((*Op >> 3) & 7);
582 }
583 } else {
584 // R_386_TLS_GOTIE relocation can be optimized to
585 // R_386_TLS_LE so that it does not use GOT.
586 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
587 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
588 // Note: gold converts to ADDL instead of LEAL.
589 *Inst = IsMov ? 0xc7 : 0x8d;
590 if (IsMov)
591 *Op = 0xc0 | ((*Op >> 3) & 7);
592 else
593 *Op = 0x80 | Reg | (Reg << 3);
594 }
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000595 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000596}
597
Rafael Espindola22ef9562016-04-13 01:40:19 +0000598void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
599 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000600 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola3f5d6342016-04-18 12:07:13 +0000601 relocateOne(Loc, R_386_TLS_LE, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000602 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000603 }
604
605 // LD can be optimized to LE:
606 // leal foo(%reg),%eax
607 // call ___tls_get_addr
608 // Is converted to:
609 // movl %gs:0,%eax
610 // nop
611 // leal 0(%esi,1),%esi
612 const uint8_t Inst[] = {
613 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
614 0x90, // nop
615 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
616 };
617 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000618}
619
Rafael Espindola7f074422015-09-22 21:35:51 +0000620X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000621 CopyRel = R_X86_64_COPY;
622 GotRel = R_X86_64_GLOB_DAT;
623 PltRel = R_X86_64_JUMP_SLOT;
624 RelativeRel = R_X86_64_RELATIVE;
625 IRelativeRel = R_X86_64_IRELATIVE;
626 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000627 TlsModuleIndexRel = R_X86_64_DTPMOD64;
628 TlsOffsetRel = R_X86_64_DTPOFF64;
629 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000630 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000631 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000632 TlsGdToLeSkip = 2;
633}
634
635RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
636 switch (Type) {
637 default:
638 return R_ABS;
Rafael Espindolaece62b92016-04-18 12:44:33 +0000639 case R_X86_64_TPOFF32:
640 return R_TLS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000641 case R_X86_64_TLSLD:
642 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000643 case R_X86_64_TLSGD:
644 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000645 case R_X86_64_SIZE32:
646 case R_X86_64_SIZE64:
647 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000648 case R_X86_64_PLT32:
649 case R_X86_64_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000650 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000651 case R_X86_64_GOT32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000652 return R_GOT_FROM_END;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000653 case R_X86_64_GOTPCREL:
Rafael Espindolaf350d252016-04-19 20:18:52 +0000654 case R_X86_64_GOTPCRELX:
655 case R_X86_64_REX_GOTPCRELX:
Rafael Espindola5628ee72016-04-15 19:14:18 +0000656 case R_X86_64_GOTTPOFF:
657 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000658 }
George Rimar648a2c32015-10-20 08:54:27 +0000659}
660
Rui Ueyamac516ae12016-01-29 02:33:45 +0000661void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000662 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
663}
664
Rui Ueyamac516ae12016-01-29 02:33:45 +0000665void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000666 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000667 write32le(Buf, Plt + 6);
668}
669
Rui Ueyama900e2d22016-01-29 03:51:49 +0000670void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000671 const uint8_t PltData[] = {
672 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
673 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
674 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
675 };
676 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000677 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
678 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
679 write32le(Buf + 2, Got - Plt + 2); // GOT+8
680 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000681}
Rafael Espindola01205f72015-09-22 18:19:46 +0000682
Rui Ueyama9398f862016-01-29 04:15:02 +0000683void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
684 uint64_t PltEntryAddr, int32_t Index,
685 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000686 const uint8_t Inst[] = {
687 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
688 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
689 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
690 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000691 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000692
George Rimar648a2c32015-10-20 08:54:27 +0000693 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
694 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000695 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000696}
697
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000698bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
699 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
700 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000701}
702
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000703bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000704 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
705 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000706}
707
George Rimar86971052016-03-29 08:35:42 +0000708uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
709 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
710 if (Config->Shared)
711 error(getELFRelocationTypeName(EM_X86_64, Type) +
712 " cannot be a dynamic relocation");
713 return Type;
714}
715
George Rimar98b060d2016-03-06 06:01:07 +0000716uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000717 // No other types of TLS relocations requiring GOT should
718 // reach here.
719 assert(Type == R_X86_64_GOTTPOFF);
720 return R_X86_64_PC32;
721}
722
George Rimar98b060d2016-03-06 06:01:07 +0000723bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000724 return Type == R_X86_64_GOTTPOFF;
725}
726
George Rimar98b060d2016-03-06 06:01:07 +0000727bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000728 return Type == R_X86_64_TLSGD;
729}
730
George Rimar98b060d2016-03-06 06:01:07 +0000731bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000732 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
733 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000734}
735
Rafael Espindola993f0272016-02-26 14:27:47 +0000736bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
737 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000738}
Rafael Espindolac4010882015-09-22 20:54:08 +0000739
Rafael Espindolaae244002015-10-05 19:30:12 +0000740bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
741 switch (Type) {
742 default:
743 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000744 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000745 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000746 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000747 case R_X86_64_PC8:
748 case R_X86_64_PC16:
749 case R_X86_64_PC32:
750 case R_X86_64_PC64:
751 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000752 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000753 return true;
754 }
755}
756
George Rimar6713cf82015-11-25 21:46:05 +0000757// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
758// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000759// how GD can be optimized to LE:
760// .byte 0x66
761// leaq x@tlsgd(%rip), %rdi
762// .word 0x6666
763// rex64
764// call __tls_get_addr@plt
765// Is converted to:
766// mov %fs:0x0,%rax
767// lea x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000768void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
769 uint64_t Val) const {
George Rimar6713cf82015-11-25 21:46:05 +0000770 const uint8_t Inst[] = {
771 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
772 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
773 };
774 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000775 relocateOne(Loc + 8, R_X86_64_TPOFF32,
776 Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000777}
778
George Rimar25411f252015-12-04 11:20:13 +0000779// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
780// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
781// how GD can be optimized to IE:
782// .byte 0x66
783// leaq x@tlsgd(%rip), %rdi
784// .word 0x6666
785// rex64
786// call __tls_get_addr@plt
787// Is converted to:
788// mov %fs:0x0,%rax
789// addq x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000790void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
791 uint64_t Val) const {
George Rimar25411f252015-12-04 11:20:13 +0000792 const uint8_t Inst[] = {
793 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
794 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
795 };
796 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000797 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000798}
799
George Rimar77d1cb12015-11-24 09:00:06 +0000800// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000801// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000802// This function does that. Read "ELF Handling For Thread-Local Storage,
803// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
804// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000805void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
806 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000807 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
808 // used in MOVQ or ADDQ instructions only.
809 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
810 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
811 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
812 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
813 uint8_t *Prefix = Loc - 3;
814 uint8_t *Inst = Loc - 2;
815 uint8_t *RegSlot = Loc - 1;
816 uint8_t Reg = Loc[-1] >> 3;
817 bool IsMov = *Inst == 0x8b;
818 bool RspAdd = !IsMov && Reg == 4;
819 // r12 and rsp registers requires special handling.
820 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
821 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
822 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
823 // The same true for rsp. So we convert to addq for them, saving 1 byte that
824 // we dont have.
825 if (RspAdd)
826 *Inst = 0x81;
827 else
828 *Inst = IsMov ? 0xc7 : 0x8d;
829 if (*Prefix == 0x4c)
830 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
831 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindolaece62b92016-04-18 12:44:33 +0000832 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4 - Out<ELF64LE>::TlsPhdr->p_memsz);
George Rimar77d1cb12015-11-24 09:00:06 +0000833}
834
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000835// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
836// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
837// how LD can be optimized to LE:
838// leaq bar@tlsld(%rip), %rdi
839// callq __tls_get_addr@PLT
840// leaq bar@dtpoff(%rax), %rcx
841// Is converted to:
842// .word 0x6666
843// .byte 0x66
844// mov %fs:0,%rax
845// leaq bar@tpoff(%rax), %rcx
Rafael Espindola22ef9562016-04-13 01:40:19 +0000846void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
847 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000848 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000849 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
850 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000851 }
852 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindolaece62b92016-04-18 12:44:33 +0000853 relocateOne(Loc, R_X86_64_TPOFF32, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
Rafael Espindola22ef9562016-04-13 01:40:19 +0000854 return;
George Rimar25411f252015-12-04 11:20:13 +0000855 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000856
857 const uint8_t Inst[] = {
858 0x66, 0x66, //.word 0x6666
859 0x66, //.byte 0x66
860 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
861 };
862 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000863}
864
Rafael Espindola22ef9562016-04-13 01:40:19 +0000865void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
866 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000867 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000868 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000869 checkUInt<32>(Val, Type);
870 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000871 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000872 case R_X86_64_32S:
Rafael Espindolaece62b92016-04-18 12:44:33 +0000873 case R_X86_64_TPOFF32:
Rafael Espindolaf4c1cd42016-04-18 12:58:59 +0000874 case R_X86_64_GOT32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000875 checkInt<32>(Val, Type);
876 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000877 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000878 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000879 case R_X86_64_DTPOFF64:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000880 case R_X86_64_SIZE64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000881 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000882 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000883 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000884 case R_X86_64_GOTPCRELX:
885 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000886 case R_X86_64_PC32:
887 case R_X86_64_PLT32:
888 case R_X86_64_TLSGD:
889 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000890 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000891 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000892 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000893 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000894 default:
George Rimar57610422016-03-11 14:43:02 +0000895 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000896 }
897}
898
Hal Finkel3c8cc672015-10-12 20:56:18 +0000899// Relocation masks following the #lo(value), #hi(value), #ha(value),
900// #higher(value), #highera(value), #highest(value), and #highesta(value)
901// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
902// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000903static uint16_t applyPPCLo(uint64_t V) { return V; }
904static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
905static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
906static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
907static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000908static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000909static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
910
Davide Italiano8c3444362016-01-11 19:45:33 +0000911PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000912bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
913
Rafael Espindola22ef9562016-04-13 01:40:19 +0000914void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
915 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000916 switch (Type) {
917 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000918 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000919 break;
920 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000921 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000922 break;
923 default:
George Rimar57610422016-03-11 14:43:02 +0000924 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000925 }
926}
927
Rafael Espindola22ef9562016-04-13 01:40:19 +0000928RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
929 return R_ABS;
930}
931
Rafael Espindolac4010882015-09-22 20:54:08 +0000932PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000933 GotRel = R_PPC64_GLOB_DAT;
934 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000935 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000936
937 // We need 64K pages (at least under glibc/Linux, the loader won't
938 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000939 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000940
941 // The PPC64 ELF ABI v1 spec, says:
942 //
943 // It is normally desirable to put segments with different characteristics
944 // in separate 256 Mbyte portions of the address space, to give the
945 // operating system full paging flexibility in the 64-bit address space.
946 //
947 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
948 // use 0x10000000 as the starting address.
949 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000950}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000951
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000952uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000953 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
954 // order. The TOC starts where the first of these sections starts.
955
956 // FIXME: This obviously does not do the right thing when there is no .got
957 // section, but there is a .toc or .tocbss section.
958 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
959 if (!TocVA)
960 TocVA = Out<ELF64BE>::Plt->getVA();
961
962 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
963 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
964 // code (crt1.o) assumes that you can get from the TOC base to the
965 // start of the .toc section with only a single (signed) 16-bit relocation.
966 return TocVA + 0x8000;
967}
968
Rafael Espindola22ef9562016-04-13 01:40:19 +0000969RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
970 switch (Type) {
971 default:
972 return R_ABS;
973 case R_PPC64_REL24:
974 return R_PPC_OPD;
975 }
976}
977
Rui Ueyama9398f862016-01-29 04:15:02 +0000978void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
979 uint64_t PltEntryAddr, int32_t Index,
980 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000981 uint64_t Off = GotEntryAddr - getPPC64TocBase();
982
983 // FIXME: What we should do, in theory, is get the offset of the function
984 // descriptor in the .opd section, and use that as the offset from %r2 (the
985 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
986 // be a pointer to the function descriptor in the .opd section. Using
987 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
988
Hal Finkelfa92f682015-10-13 21:47:34 +0000989 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000990 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
991 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
992 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
993 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
994 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
995 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
996 write32be(Buf + 28, 0x4e800420); // bctr
997}
998
Rafael Espindola993f0272016-02-26 14:27:47 +0000999bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001000 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001001 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001002}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001003
Hal Finkelbe0823d2015-10-12 20:58:52 +00001004bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1005 switch (Type) {
1006 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001007 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001008 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001009 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001010 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001011 }
1012}
1013
Rafael Espindola22ef9562016-04-13 01:40:19 +00001014void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1015 uint64_t Val) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001016 uint64_t TB = getPPC64TocBase();
1017
Hal Finkel3c8cc672015-10-12 20:56:18 +00001018 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1019 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001020 switch (Type) {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001021 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TB; break;
1022 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TB; break;
1023 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TB; break;
1024 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TB; break;
1025 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TB; break;
1026 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TB; 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;
1113 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001114 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1115 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001116 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001117 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001118 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001119}
George Rimar648a2c32015-10-20 08:54:27 +00001120
Rafael Espindola22ef9562016-04-13 01:40:19 +00001121RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1122 const SymbolBody &S) const {
1123 switch (Type) {
1124 default:
1125 return R_ABS;
1126 case R_AARCH64_JUMP26:
1127 case R_AARCH64_CALL26:
1128 case R_AARCH64_PREL16:
1129 case R_AARCH64_PREL32:
1130 case R_AARCH64_PREL64:
1131 case R_AARCH64_CONDBR19:
1132 case R_AARCH64_ADR_PREL_LO21:
1133 case R_AARCH64_TSTBR14:
1134 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001135 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001136 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001137 case R_AARCH64_LD64_GOT_LO12_NC:
1138 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1139 return R_GOT;
1140 case R_AARCH64_ADR_GOT_PAGE:
1141 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1142 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001143 }
1144}
1145
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001146bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001147 switch (Type) {
1148 default:
1149 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001150 case R_AARCH64_ADD_ABS_LO12_NC:
1151 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001152 case R_AARCH64_ADR_PREL_LO21:
1153 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001154 case R_AARCH64_CALL26:
1155 case R_AARCH64_CONDBR19:
1156 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001157 case R_AARCH64_LDST8_ABS_LO12_NC:
1158 case R_AARCH64_LDST16_ABS_LO12_NC:
1159 case R_AARCH64_LDST32_ABS_LO12_NC:
1160 case R_AARCH64_LDST64_ABS_LO12_NC:
1161 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001162 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001163 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001164 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1165 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1166 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1167 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1168 case R_AARCH64_TSTBR14:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001169 return true;
1170 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001171}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001172
George Rimar98b060d2016-03-06 06:01:07 +00001173bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001174 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1175 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1176 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1177 Type == R_AARCH64_TLSDESC_CALL;
1178}
1179
George Rimar98b060d2016-03-06 06:01:07 +00001180bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001181 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1182 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1183}
1184
George Rimar98b060d2016-03-06 06:01:07 +00001185uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001186 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1187 return Type;
1188 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001189 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001190 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001191 // Keep it going with a dummy value so that we can find more reloc errors.
1192 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001193}
1194
Rui Ueyamac516ae12016-01-29 02:33:45 +00001195void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001196 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1197}
1198
Rafael Espindola22ef9562016-04-13 01:40:19 +00001199static uint64_t getAArch64Page(uint64_t Expr) {
1200 return Expr & (~static_cast<uint64_t>(0xFFF));
1201}
1202
Rui Ueyama900e2d22016-01-29 03:51:49 +00001203void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001204 const uint8_t PltData[] = {
1205 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1206 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1207 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1208 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1209 0x20, 0x02, 0x1f, 0xd6, // br x17
1210 0x1f, 0x20, 0x03, 0xd5, // nop
1211 0x1f, 0x20, 0x03, 0xd5, // nop
1212 0x1f, 0x20, 0x03, 0xd5 // nop
1213 };
1214 memcpy(Buf, PltData, sizeof(PltData));
1215
Rui Ueyama900e2d22016-01-29 03:51:49 +00001216 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1217 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001218 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1219 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1220 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1221 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001222}
1223
Rui Ueyama9398f862016-01-29 04:15:02 +00001224void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1225 uint64_t PltEntryAddr, int32_t Index,
1226 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001227 const uint8_t Inst[] = {
1228 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1229 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1230 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1231 0x20, 0x02, 0x1f, 0xd6 // br x17
1232 };
1233 memcpy(Buf, Inst, sizeof(Inst));
1234
Rafael Espindola22ef9562016-04-13 01:40:19 +00001235 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1236 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1237 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1238 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001239}
1240
George Rimar98b060d2016-03-06 06:01:07 +00001241uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001242 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001243 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1244 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001245}
1246
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001247bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001248 switch (Type) {
1249 default:
1250 return false;
1251 case R_AARCH64_ABS16:
1252 case R_AARCH64_ABS32:
1253 case R_AARCH64_ABS64:
1254 case R_AARCH64_ADD_ABS_LO12_NC:
1255 case R_AARCH64_ADR_PREL_LO21:
1256 case R_AARCH64_ADR_PREL_PG_HI21:
1257 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001258 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001259 case R_AARCH64_LDST32_ABS_LO12_NC:
1260 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001261 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001262 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001263 }
1264}
1265
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +00001266bool AArch64TargetInfo::refersToGotEntry(uint32_t Type) const {
1267 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC;
1268}
1269
Rafael Espindola993f0272016-02-26 14:27:47 +00001270bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001271 switch (Type) {
1272 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001273 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001274 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001275 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001276 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001277 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001278 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001279 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001280}
Davide Italiano1d750a62015-09-27 08:45:38 +00001281
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001282static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001283 uint32_t ImmLo = (Imm & 0x3) << 29;
1284 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1285 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001286 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001287}
1288
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001289static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1290 or32le(L, (Imm & 0xFFF) << 10);
1291}
1292
Rafael Espindola22ef9562016-04-13 01:40:19 +00001293void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1294 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001295 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001296 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001297 checkIntUInt<16>(Val, Type);
1298 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001299 break;
1300 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001301 checkIntUInt<32>(Val, Type);
1302 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001303 break;
1304 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001305 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001306 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001307 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001308 // This relocation stores 12 bits and there's no instruction
1309 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001310 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1311 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001312 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001313 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001314 case R_AARCH64_ADR_GOT_PAGE: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001315 uint64_t X = Val;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001316 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001317 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001318 break;
1319 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001320 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001321 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001322 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001323 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001324 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001325 }
George Rimar3d737e42016-01-13 13:04:46 +00001326 case R_AARCH64_ADR_PREL_PG_HI21:
1327 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001328 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001329 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001330 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001331 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001332 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001333 case R_AARCH64_CALL26:
1334 case R_AARCH64_JUMP26: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001335 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001336 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001337 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1338 break;
1339 }
George Rimar4102bfb2016-01-11 14:22:00 +00001340 case R_AARCH64_CONDBR19: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001341 uint64_t X = Val;
George Rimar4102bfb2016-01-11 14:22:00 +00001342 checkInt<21>(X, Type);
1343 or32le(Loc, (X & 0x1FFFFC) << 3);
1344 break;
1345 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001346 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001347 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001348 checkAlignment<8>(Val, Type);
1349 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001350 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001351 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001352 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001353 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001354 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001355 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001356 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001357 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001358 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001359 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001360 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001361 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001362 break;
1363 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001364 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001365 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001366 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001367 checkIntUInt<16>(Val, Type);
1368 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001369 break;
1370 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001371 checkIntUInt<32>(Val, Type);
1372 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001373 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001374 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001375 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001376 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001377 case R_AARCH64_TSTBR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001378 uint64_t X = Val;
George Rimar1395dbd2016-01-11 14:27:05 +00001379 checkInt<16>(X, Type);
1380 or32le(Loc, (X & 0xFFFC) << 3);
1381 break;
1382 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001383 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001384 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001385 checkInt<24>(V, Type);
1386 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1387 break;
1388 }
1389 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001390 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001391 updateAArch64Add(Loc, V & 0xFFF);
1392 break;
1393 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001394 default:
George Rimar57610422016-03-11 14:43:02 +00001395 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001396 }
1397}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001398
Rafael Espindola22ef9562016-04-13 01:40:19 +00001399void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1400 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001401 // TLSDESC Global-Dynamic relocation are in the form:
1402 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1403 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1404 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1405 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1406 // And it can optimized to:
1407 // movz x0, #0x0, lsl #16
1408 // movk x0, #0x10
1409 // nop
1410 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001411 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001412 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001413 checkUInt<32>(X, Type);
1414
1415 uint32_t NewInst;
1416 switch (Type) {
1417 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1418 case R_AARCH64_TLSDESC_CALL:
1419 // nop
1420 NewInst = 0xd503201f;
1421 break;
1422 case R_AARCH64_TLSDESC_ADR_PAGE21:
1423 // movz
1424 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1425 break;
1426 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1427 // movk
1428 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1429 break;
1430 default:
George Rimar777f9632016-03-12 08:31:34 +00001431 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001432 }
1433 write32le(Loc, NewInst);
1434}
1435
Rafael Espindola22ef9562016-04-13 01:40:19 +00001436void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1437 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001438 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001439 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001440 checkUInt<32>(X, Type);
1441
George Rimar4d1d16d2016-03-06 06:16:05 +00001442 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001443 uint32_t NewInst;
1444 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1445 // Generate movz.
1446 unsigned RegNo = (Inst & 0x1f);
1447 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1448 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1449 // Generate movk
1450 unsigned RegNo = (Inst & 0x1f);
1451 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1452 } else {
George Rimar777f9632016-03-12 08:31:34 +00001453 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001454 }
1455 write32le(Loc, NewInst);
1456}
1457
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001458// Implementing relocations for AMDGPU is low priority since most
1459// programs don't use relocations now. Thus, this function is not
1460// actually called (relocateOne is called for each relocation).
1461// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001462void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1463 uint64_t Val) const {
1464 llvm_unreachable("not implemented");
1465}
1466
1467RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001468 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001469}
1470
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001471template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001472 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001473 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001474 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001475 PltEntrySize = 16;
1476 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001477 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001478 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001479 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001480 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001481 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001482}
1483
1484template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001485RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1486 const SymbolBody &S) const {
1487 switch (Type) {
1488 default:
1489 return R_ABS;
1490 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001491 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001492 // MIPS _gp_disp designates offset between start of function and 'gp'
1493 // pointer into GOT. __gnu_local_gp is equal to the current value of
1494 // the 'gp'. Therefore any relocations against them do not require
1495 // dynamic relocation.
1496 if (&S == ElfSym<ELFT>::MipsGpDisp)
1497 return R_PC;
1498 return R_ABS;
1499 case R_MIPS_PC32:
1500 case R_MIPS_PC16:
1501 case R_MIPS_PC19_S2:
1502 case R_MIPS_PC21_S2:
1503 case R_MIPS_PC26_S2:
1504 case R_MIPS_PCHI16:
1505 case R_MIPS_PCLO16:
1506 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001507 case R_MIPS_GOT16:
1508 case R_MIPS_CALL16:
1509 if (S.isLocal())
1510 return R_MIPS_GOT_LOCAL;
1511 if (!S.isPreemptible())
1512 return R_MIPS_GOT;
1513 return R_GOT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001514 }
1515}
1516
1517template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001518uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001519 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1520 return R_MIPS_REL32;
1521 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001522 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001523 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001524 // Keep it going with a dummy value so that we can find more reloc errors.
1525 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001526}
1527
1528template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001529void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001530 typedef typename ELFT::Off Elf_Off;
1531 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001532
1533 // Set the MSB of the second GOT slot. This is not required by any
1534 // MIPS ABI documentation, though.
1535 //
1536 // There is a comment in glibc saying that "The MSB of got[1] of a
1537 // gnu object is set to identify gnu objects," and in GNU gold it
1538 // says "the second entry will be used by some runtime loaders".
1539 // But how this field is being used is unclear.
1540 //
1541 // We are not really willing to mimic other linkers behaviors
1542 // without understanding why they do that, but because all files
1543 // generated by GNU tools have this special GOT value, and because
1544 // we've been doing this for years, it is probably a safe bet to
1545 // keep doing this for now. We really need to revisit this to see
1546 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001547 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001548 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001549}
1550
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001551template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001552void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1553 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001554}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001555
Simon Atanasyan35031192015-12-15 06:06:34 +00001556static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001557
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001558template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001559static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001560 uint32_t Instr = read32<E>(Loc);
1561 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1562 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1563}
1564
1565template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001566static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001567 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001568 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001569 if (SHIFT > 0)
1570 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001571 checkInt<BSIZE + SHIFT>(V, Type);
1572 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001573}
1574
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001575template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001576static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001577 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001578 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001579}
1580
Simon Atanasyan3b377852016-03-04 10:55:20 +00001581template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001582static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1583 uint32_t Instr = read32<E>(Loc);
1584 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1585}
1586
Rafael Espindola666625b2016-04-01 14:36:09 +00001587template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001588 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1589}
1590
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001591template <class ELFT>
1592void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1593 const endianness E = ELFT::TargetEndianness;
1594 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1595 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1596 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1597 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1598 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1599 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1600 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1601 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1602 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001603 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001604 writeMipsLo16<E>(Buf + 4, Got);
1605 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001606}
1607
1608template <class ELFT>
1609void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1610 uint64_t PltEntryAddr, int32_t Index,
1611 unsigned RelOff) const {
1612 const endianness E = ELFT::TargetEndianness;
1613 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1614 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1615 write32<E>(Buf + 8, 0x03200008); // jr $25
1616 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001617 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001618 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1619 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001620}
1621
1622template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001623void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1624 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1625 // See MipsTargetInfo::writeThunk for details.
1626 const endianness E = ELFT::TargetEndianness;
1627 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1628 write32<E>(Buf + 4, 0x08000000); // j func
1629 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1630 write32<E>(Buf + 12, 0x00000000); // nop
1631 writeMipsHi16<E>(Buf, S);
1632 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1633 writeMipsLo16<E>(Buf + 8, S);
1634}
1635
1636template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001637bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Rafael Espindola790db9c2016-04-01 17:00:36 +00001638 return !isRelRelative(Type) || Type == R_MIPS_LO16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001639}
1640
1641template <class ELFT>
Simon Atanasyand040a582016-02-25 16:19:15 +00001642bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1643 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001644}
1645
1646template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001647bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1648 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001649}
1650
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001651template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001652bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1653 const SymbolBody &S) const {
1654 // Any MIPS PIC code function is invoked with its address in register $t9.
1655 // So if we have a branch instruction from non-PIC code to the PIC one
1656 // we cannot make the jump directly and need to create a small stubs
1657 // to save the target function address.
1658 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1659 if (Type != R_MIPS_26)
1660 return false;
1661 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1662 if (!F)
1663 return false;
1664 // If current file has PIC code, LA25 stub is not required.
1665 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1666 return false;
1667 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1668 if (!D || !D->Section)
1669 return false;
1670 // LA25 is required if target file has PIC code
1671 // or target symbol is a PIC symbol.
1672 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001673 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001674}
1675
1676template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001677uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001678 uint32_t Type) const {
1679 const endianness E = ELFT::TargetEndianness;
1680 switch (Type) {
1681 default:
1682 return 0;
1683 case R_MIPS_32:
1684 case R_MIPS_GPREL32:
1685 return read32<E>(Buf);
1686 case R_MIPS_26:
1687 // FIXME (simon): If the relocation target symbol is not a PLT entry
1688 // we should use another expression for calculation:
1689 // ((A << 2) | (P & 0xf0000000)) >> 2
1690 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1691 case R_MIPS_GPREL16:
1692 case R_MIPS_LO16:
1693 case R_MIPS_PCLO16:
1694 case R_MIPS_TLS_DTPREL_HI16:
1695 case R_MIPS_TLS_DTPREL_LO16:
1696 case R_MIPS_TLS_TPREL_HI16:
1697 case R_MIPS_TLS_TPREL_LO16:
1698 return readSignedLo16<E>(Buf);
1699 case R_MIPS_PC16:
1700 return getPcRelocAddend<E, 16, 2>(Buf);
1701 case R_MIPS_PC19_S2:
1702 return getPcRelocAddend<E, 19, 2>(Buf);
1703 case R_MIPS_PC21_S2:
1704 return getPcRelocAddend<E, 21, 2>(Buf);
1705 case R_MIPS_PC26_S2:
1706 return getPcRelocAddend<E, 26, 2>(Buf);
1707 case R_MIPS_PC32:
1708 return getPcRelocAddend<E, 32, 0>(Buf);
1709 }
1710}
1711
1712template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001713void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1714 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001715 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001716 // Thread pointer and DRP offsets from the start of TLS data area.
1717 // https://www.linux-mips.org/wiki/NPTL
1718 const uint32_t TPOffset = 0x7000;
1719 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001720 switch (Type) {
1721 case R_MIPS_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001722 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001723 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001724 case R_MIPS_26: {
1725 uint32_t Instr = read32<E>(Loc);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001726 write32<E>(Loc, (Instr & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001727 break;
1728 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001729 case R_MIPS_CALL16:
1730 case R_MIPS_GOT16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001731 int64_t V = Val - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001732 if (Type == R_MIPS_GOT16)
1733 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001734 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001735 break;
1736 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001737 case R_MIPS_GPREL16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001738 int64_t V = Val - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001739 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001740 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001741 break;
1742 }
1743 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001744 write32<E>(Loc, Val - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001745 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001746 case R_MIPS_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001747 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001748 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001749 case R_MIPS_JALR:
1750 // Ignore this optimization relocation for now
1751 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001752 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001753 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001754 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001755 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001756 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001757 break;
1758 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001759 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001760 break;
1761 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001762 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001763 break;
1764 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001765 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001766 break;
1767 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001768 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001769 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001770 case R_MIPS_PCHI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001771 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001772 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001773 case R_MIPS_PCLO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001774 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001775 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001776 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001777 writeMipsHi16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001778 break;
1779 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001780 writeMipsLo16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001781 break;
1782 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001783 writeMipsHi16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001784 break;
1785 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001786 writeMipsLo16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001787 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001788 default:
George Rimar57610422016-03-11 14:43:02 +00001789 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001790 }
1791}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001792
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001793template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001794bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001795 return Type == R_MIPS_JALR;
1796}
1797
1798template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001799bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1800 switch (Type) {
1801 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001802 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001803 case R_MIPS_26:
1804 case R_MIPS_32:
1805 case R_MIPS_64:
1806 case R_MIPS_HI16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001807 case R_MIPS_TLS_DTPREL_HI16:
1808 case R_MIPS_TLS_DTPREL_LO16:
1809 case R_MIPS_TLS_TPREL_HI16:
1810 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001811 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001812 }
1813}
1814
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001815// _gp is a MIPS-specific ABI-defined symbol which points to
1816// a location that is relative to GOT. This function returns
1817// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001818template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rafael Espindola6f92e142016-04-12 13:26:51 +00001819 return Out<ELFT>::Got->getVA() + MipsGPOffset;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001820}
1821
1822template uint32_t getMipsGpAddr<ELF32LE>();
1823template uint32_t getMipsGpAddr<ELF32BE>();
1824template uint64_t getMipsGpAddr<ELF64LE>();
1825template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001826
1827template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1828 const SymbolBody &) const;
1829template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1830 const SymbolBody &) const;
1831template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1832 const SymbolBody &) const;
1833template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1834 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001835}
1836}