blob: b5f5dd187453fda502a3a7ae8604aad64c969362 [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
14// in this file. SA is S+A.
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 Espindola666625b2016-04-01 14:36:09 +000073 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000074 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000075 uint32_t getDynRel(uint32_t Type) const override;
76 uint32_t getTlsGotRel(uint32_t Type) const override;
77 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
78 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 Espindola4e5ab422016-03-31 13:38:28 +000088 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000089 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000090 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +000091 uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000092
93 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
94 uint64_t P, uint64_t SA) const override;
95 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +000096 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000097 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +000098 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000099 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
100 uint64_t P, uint64_t SA) const override;
101
George Rimarbfb7bf72015-12-21 10:00:12 +0000102 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000103 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000104};
105
106class X86_64TargetInfo final : public TargetInfo {
107public:
108 X86_64TargetInfo();
George Rimar86971052016-03-29 08:35:42 +0000109 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000110 uint32_t getTlsGotRel(uint32_t Type) const override;
111 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
112 bool isTlsLocalDynamicRel(uint32_t Type) const override;
113 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
114 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000115 void writeGotPltHeader(uint8_t *Buf) const override;
116 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000117 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000118 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
119 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000120 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000121 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000122 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000123 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000124 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000125 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000126 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000127 bool isSizeRel(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000128
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000129 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
130 uint64_t P, uint64_t SA) const override;
131 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000132 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000133 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000134 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000135 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
136 uint64_t P, uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000137};
138
Davide Italiano8c3444362016-01-11 19:45:33 +0000139class PPCTargetInfo final : public TargetInfo {
140public:
141 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000142 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000143 uint64_t SA) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000144 bool isRelRelative(uint32_t Type) const override;
145};
146
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000147class PPC64TargetInfo final : public TargetInfo {
148public:
149 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000150 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
151 int32_t Index, unsigned RelOff) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000152 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000153 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000155 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000156 bool isRelRelative(uint32_t Type) const override;
157};
158
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000159class AArch64TargetInfo final : public TargetInfo {
160public:
161 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000162 uint32_t getDynRel(uint32_t Type) const override;
163 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
164 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000165 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000166 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000167 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
168 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000169 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000170 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000171 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000172 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000173 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000174 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000175 uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000176 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000177 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000178 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000179 uint64_t P, uint64_t SA) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000180
181private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000182 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000183};
184
Tom Stellard80efb162016-01-07 03:59:08 +0000185class AMDGPUTargetInfo final : public TargetInfo {
186public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000187 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000188 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000189 uint64_t SA) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000190};
191
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000192template <class ELFT> class MipsTargetInfo final : public TargetInfo {
193public:
194 MipsTargetInfo();
Rafael Espindola666625b2016-04-01 14:36:09 +0000195 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000196 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000197 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
198 void writePltZero(uint8_t *Buf) const override;
199 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
200 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000201 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000202 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000203 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000204 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000205 bool needsPltImpl(uint32_t Type) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000206 bool needsThunk(uint32_t Type, const InputFile &File,
207 const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000208 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000209 uint64_t SA) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000210 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000211 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000212 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000213};
214} // anonymous namespace
215
Rui Ueyama91004392015-10-13 16:08:15 +0000216TargetInfo *createTarget() {
217 switch (Config->EMachine) {
218 case EM_386:
219 return new X86TargetInfo();
220 case EM_AARCH64:
221 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000222 case EM_AMDGPU:
223 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000224 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000225 switch (Config->EKind) {
226 case ELF32LEKind:
227 return new MipsTargetInfo<ELF32LE>();
228 case ELF32BEKind:
229 return new MipsTargetInfo<ELF32BE>();
230 default:
George Rimar777f9632016-03-12 08:31:34 +0000231 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000232 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000233 case EM_PPC:
234 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000235 case EM_PPC64:
236 return new PPC64TargetInfo();
237 case EM_X86_64:
238 return new X86_64TargetInfo();
239 }
George Rimar777f9632016-03-12 08:31:34 +0000240 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000241}
242
Rafael Espindola01205f72015-09-22 18:19:46 +0000243TargetInfo::~TargetInfo() {}
244
Rafael Espindola666625b2016-04-01 14:36:09 +0000245uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
246 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000247 return 0;
248}
249
George Rimar98b060d2016-03-06 06:01:07 +0000250bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000251 if (Config->Shared || (S && !S->isTls()))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000252 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000253
Rafael Espindolad405f472016-03-04 21:37:09 +0000254 // We know we are producing an executable.
255
256 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
257 // depending on the symbol being locally defined or not.
258 if (isTlsGlobalDynamicRel(Type))
259 return true;
260
261 // Local-Dynamic relocs can be relaxed to Local-Exec.
262 if (isTlsLocalDynamicRel(Type))
263 return true;
264
265 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
266 // defined.
267 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000268 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000269
George Rimar77d1cb12015-11-24 09:00:06 +0000270 return false;
271}
272
George Rimar786e8662016-03-17 05:57:33 +0000273uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000274
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000275bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
276
277template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
278 if (Config->Shared)
279 return false;
280 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
281 if (!SS)
282 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000283 return SS->isObject();
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000284}
285
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000286template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000287bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000288 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000289}
290
George Rimarbfb7bf72015-12-21 10:00:12 +0000291bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000292bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000293bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000294bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000295
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000296bool TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
297 return false;
298}
Rui Ueyama012eb782016-01-29 04:05:09 +0000299
Rafael Espindola993f0272016-02-26 14:27:47 +0000300bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000301
302bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
303
Rafael Espindola852860e2016-02-12 15:47:37 +0000304TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
305 const SymbolBody &S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000306 if (S.isGnuIFunc())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000307 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000308 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000309 return Plt_Explicit;
310
311 // This handles a non PIC program call to function in a shared library.
312 // In an ideal world, we could just report an error saying the relocation
313 // can overflow at runtime.
314 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
315 // libc.so.
316 //
317 // The general idea on how to handle such cases is to create a PLT entry
318 // and use that as the function value.
319 //
320 // For the static linking part, we just return true and everything else
321 // will use the the PLT entry as the address.
322 //
323 // The remaining problem is making sure pointer equality still works. We
324 // need the help of the dynamic linker for that. We let it know that we have
325 // a direct reference to a so symbol by creating an undefined symbol with a
326 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
327 // the value of the symbol we created. This is true even for got entries, so
328 // pointer equality is maintained. To avoid an infinite loop, the only entry
329 // that points to the real function is a dedicated got entry used by the
330 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
331 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000332 if (S.isShared())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000333 if (!Config->Pic && S.isFunc() && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000334 return Plt_Implicit;
335
Rafael Espindola852860e2016-02-12 15:47:37 +0000336 return Plt_No;
337}
Rui Ueyama012eb782016-01-29 04:05:09 +0000338
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000339bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
340 const SymbolBody &S) const {
341 return false;
342}
343
George Rimar98b060d2016-03-06 06:01:07 +0000344bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000345
George Rimar98b060d2016-03-06 06:01:07 +0000346bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000347 return false;
348}
349
George Rimar98b060d2016-03-06 06:01:07 +0000350bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000351
George Rimar98b060d2016-03-06 06:01:07 +0000352bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000353 return false;
354}
355
George Rimar7b8850f2016-03-06 06:09:50 +0000356size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
357 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000358 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000359 if (isTlsGlobalDynamicRel(Type)) {
360 if (S.isPreemptible())
361 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000362 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000363 }
364 if (isTlsLocalDynamicRel(Type))
365 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
366 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000367 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000368}
369
370size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000371 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000372 llvm_unreachable("Should not have claimed to be relaxable");
373}
374
375size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
376 uint64_t P, uint64_t SA) const {
377 llvm_unreachable("Should not have claimed to be relaxable");
378}
379
380size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000381 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000382 llvm_unreachable("Should not have claimed to be relaxable");
383}
384
385size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
386 uint64_t P, uint64_t SA) const {
387 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000388}
George Rimar77d1cb12015-11-24 09:00:06 +0000389
Rafael Espindola7f074422015-09-22 21:35:51 +0000390X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000391 CopyRel = R_386_COPY;
392 GotRel = R_386_GLOB_DAT;
393 PltRel = R_386_JUMP_SLOT;
394 IRelativeRel = R_386_IRELATIVE;
395 RelativeRel = R_386_RELATIVE;
396 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000397 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
398 TlsOffsetRel = R_386_TLS_DTPOFF32;
399 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000400 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000401 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000402}
403
Rafael Espindolaffcad442016-03-23 14:58:25 +0000404bool X86TargetInfo::isRelRelative(uint32_t Type) const {
405 switch (Type) {
406 default:
407 return false;
408 case R_386_PC32:
409 case R_386_PLT32:
410 case R_386_TLS_LDO_32:
411 return true;
412 }
413}
414
Rui Ueyamac516ae12016-01-29 02:33:45 +0000415void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000416 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
417}
418
Rui Ueyamac516ae12016-01-29 02:33:45 +0000419void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000420 // Entries in .got.plt initially points back to the corresponding
421 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000422 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000423}
Rafael Espindola01205f72015-09-22 18:19:46 +0000424
George Rimar98b060d2016-03-06 06:01:07 +0000425uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000426 if (Type == R_386_TLS_LE)
427 return R_386_TLS_TPOFF;
428 if (Type == R_386_TLS_LE_32)
429 return R_386_TLS_TPOFF32;
430 return Type;
431}
432
George Rimar98b060d2016-03-06 06:01:07 +0000433uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000434 if (Type == R_386_TLS_IE)
435 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000436 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000437}
438
George Rimar98b060d2016-03-06 06:01:07 +0000439bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000440 return Type == R_386_TLS_GD;
441}
442
George Rimar98b060d2016-03-06 06:01:07 +0000443bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000444 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
445}
446
George Rimar98b060d2016-03-06 06:01:07 +0000447bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000448 return Type == R_386_TLS_LDM;
449}
450
George Rimar98b060d2016-03-06 06:01:07 +0000451bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000452 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
453}
454
Rui Ueyama900e2d22016-01-29 03:51:49 +0000455void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000456 // Executable files and shared object files have
457 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000458 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000459 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000460 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000461 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
462 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000463 };
464 memcpy(Buf, V, sizeof(V));
465 return;
466 }
George Rimar648a2c32015-10-20 08:54:27 +0000467
George Rimar77b77792015-11-25 22:15:01 +0000468 const uint8_t PltData[] = {
469 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000470 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
471 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000472 };
473 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000474 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000475 write32le(Buf + 2, Got + 4);
476 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000477}
478
Rui Ueyama9398f862016-01-29 04:15:02 +0000479void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
480 uint64_t PltEntryAddr, int32_t Index,
481 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000482 const uint8_t Inst[] = {
483 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
484 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
485 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
486 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000487 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000488
George Rimar77b77792015-11-25 22:15:01 +0000489 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000490 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000491 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
492 : Out<ELF32LE>::Got->getVA();
493 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000494 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000495 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000496}
497
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000498bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
499 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000500}
501
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000502bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000503 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000504 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000505 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000506 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000507 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000508}
509
Rafael Espindola993f0272016-02-26 14:27:47 +0000510bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
511 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000512}
513
George Rimarbfb7bf72015-12-21 10:00:12 +0000514bool X86TargetInfo::isGotRelative(uint32_t Type) const {
515 // This relocation does not require got entry,
516 // but it is relative to got and needs it to be created.
517 // Here we request for that.
518 return Type == R_386_GOTOFF;
519}
520
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000521bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
522 return Type == R_386_GOT32;
523}
524
Rafael Espindola666625b2016-04-01 14:36:09 +0000525uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
526 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000527 switch (Type) {
528 default:
529 return 0;
530 case R_386_32:
531 case R_386_GOT32:
532 case R_386_GOTOFF:
533 case R_386_GOTPC:
534 case R_386_PC32:
535 case R_386_PLT32:
536 return read32le(Buf);
537 }
538}
539
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000540void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +0000541 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000542 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000543 case R_386_32:
Rafael Espindolada99df32016-03-30 12:40:38 +0000544 write32le(Loc, SA);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000545 break;
George Rimarffb67352016-01-19 11:00:48 +0000546 case R_386_GOT32: {
547 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
548 Out<ELF32LE>::Got->getNumEntries() * 4;
549 checkInt<32>(V, Type);
Rafael Espindolada99df32016-03-30 12:40:38 +0000550 write32le(Loc, V);
George Rimarffb67352016-01-19 11:00:48 +0000551 break;
552 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000553 case R_386_GOTOFF:
Rafael Espindolada99df32016-03-30 12:40:38 +0000554 write32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000555 break;
George Rimar13934772015-11-25 20:20:31 +0000556 case R_386_GOTPC:
Rafael Espindolada99df32016-03-30 12:40:38 +0000557 write32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
George Rimar13934772015-11-25 20:20:31 +0000558 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000559 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000560 case R_386_PLT32:
Rafael Espindolada99df32016-03-30 12:40:38 +0000561 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000562 break;
George Rimar9db204a2015-12-02 09:58:20 +0000563 case R_386_TLS_GD:
564 case R_386_TLS_LDM:
565 case R_386_TLS_TPOFF: {
566 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
567 Out<ELF32LE>::Got->getNumEntries() * 4;
568 checkInt<32>(V, Type);
569 write32le(Loc, V);
570 break;
571 }
George Rimar6f17e092015-12-17 09:32:21 +0000572 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000573 case R_386_TLS_LDO_32:
574 write32le(Loc, SA);
575 break;
George Rimard23970f2015-11-25 20:41:53 +0000576 case R_386_TLS_LE:
577 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
578 break;
579 case R_386_TLS_LE_32:
580 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
581 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000582 default:
George Rimar57610422016-03-11 14:43:02 +0000583 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000584 }
585}
586
George Rimar98b060d2016-03-06 06:01:07 +0000587bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000588 return Config->Shared && Type == R_386_TLS_IE;
589}
590
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000591size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000592 uint32_t Type, uint64_t P,
593 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000594 // GD can be optimized to LE:
595 // leal x@tlsgd(, %ebx, 1),
596 // call __tls_get_addr@plt
597 // Can be converted to:
598 // movl %gs:0,%eax
599 // addl $x@ntpoff,%eax
600 // But gold emits subl $foo@tpoff,%eax instead of addl.
601 // These instructions are completely equal in behavior.
602 // This method generates subl to be consistent with gold.
603 const uint8_t Inst[] = {
604 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
605 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
606 };
607 memcpy(Loc - 3, Inst, sizeof(Inst));
608 relocateOne(Loc + 5, BufEnd, R_386_32, P,
609 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
610
611 // The next relocation should be against __tls_get_addr, so skip it
612 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000613}
614
615// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
616// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
617// how GD can be optimized to IE:
618// leal x@tlsgd(, %ebx, 1),
619// call __tls_get_addr@plt
620// Is converted to:
621// movl %gs:0, %eax
622// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000623size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
624 uint32_t Type, uint64_t P,
625 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000626 const uint8_t Inst[] = {
627 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
628 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
629 };
630 memcpy(Loc - 3, Inst, sizeof(Inst));
631 relocateOne(Loc + 5, BufEnd, R_386_32, P,
632 SA - Out<ELF32LE>::Got->getVA() -
633 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000634
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000635 // The next relocation should be against __tls_get_addr, so skip it
636 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000637}
638
George Rimar6f17e092015-12-17 09:32:21 +0000639// In some conditions, relocations can be optimized to avoid using GOT.
640// This function does that for Initial Exec to Local Exec case.
641// Read "ELF Handling For Thread-Local Storage, 5.1
642// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000643// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000644
645size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000646 uint32_t Type, uint64_t P,
647 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000648 // Ulrich's document section 6.2 says that @gotntpoff can
649 // be used with MOVL or ADDL instructions.
650 // @indntpoff is similar to @gotntpoff, but for use in
651 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000652 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000653 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000654 uint8_t Reg = (Loc[-1] >> 3) & 7;
655 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000656 if (Type == R_386_TLS_IE) {
657 // For R_386_TLS_IE relocation we perform the next transformations:
658 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
659 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
660 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
661 // First one is special because when EAX is used the sequence is 5 bytes
662 // long, otherwise it is 6 bytes.
663 if (*Op == 0xa1) {
664 *Op = 0xb8;
665 } else {
666 *Inst = IsMov ? 0xc7 : 0x81;
667 *Op = 0xc0 | ((*Op >> 3) & 7);
668 }
669 } else {
670 // R_386_TLS_GOTIE relocation can be optimized to
671 // R_386_TLS_LE so that it does not use GOT.
672 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
673 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
674 // Note: gold converts to ADDL instead of LEAL.
675 *Inst = IsMov ? 0xc7 : 0x8d;
676 if (IsMov)
677 *Op = 0xc0 | ((*Op >> 3) & 7);
678 else
679 *Op = 0x80 | Reg | (Reg << 3);
680 }
George Rimar2558e122015-12-09 09:55:54 +0000681 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000682
683 return 0;
684}
685
686size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
687 uint32_t Type, uint64_t P,
688 uint64_t SA) const {
689 if (Type == R_386_TLS_LDO_32) {
690 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
691 return 0;
692 }
693
694 // LD can be optimized to LE:
695 // leal foo(%reg),%eax
696 // call ___tls_get_addr
697 // Is converted to:
698 // movl %gs:0,%eax
699 // nop
700 // leal 0(%esi,1),%esi
701 const uint8_t Inst[] = {
702 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
703 0x90, // nop
704 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
705 };
706 memcpy(Loc - 2, Inst, sizeof(Inst));
707
708 // The next relocation should be against __tls_get_addr, so skip it
709 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000710}
711
Rafael Espindola7f074422015-09-22 21:35:51 +0000712X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000713 CopyRel = R_X86_64_COPY;
714 GotRel = R_X86_64_GLOB_DAT;
715 PltRel = R_X86_64_JUMP_SLOT;
716 RelativeRel = R_X86_64_RELATIVE;
717 IRelativeRel = R_X86_64_IRELATIVE;
718 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000719 TlsModuleIndexRel = R_X86_64_DTPMOD64;
720 TlsOffsetRel = R_X86_64_DTPOFF64;
721 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000722 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000723 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000724}
725
Rui Ueyamac516ae12016-01-29 02:33:45 +0000726void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000727 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
728}
729
Rui Ueyamac516ae12016-01-29 02:33:45 +0000730void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000731 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000732 write32le(Buf, Plt + 6);
733}
734
Rui Ueyama900e2d22016-01-29 03:51:49 +0000735void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000736 const uint8_t PltData[] = {
737 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
738 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
739 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
740 };
741 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000742 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
743 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
744 write32le(Buf + 2, Got - Plt + 2); // GOT+8
745 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000746}
Rafael Espindola01205f72015-09-22 18:19:46 +0000747
Rui Ueyama9398f862016-01-29 04:15:02 +0000748void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
749 uint64_t PltEntryAddr, int32_t Index,
750 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000751 const uint8_t Inst[] = {
752 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
753 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
754 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
755 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000756 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000757
George Rimar648a2c32015-10-20 08:54:27 +0000758 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
759 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000760 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000761}
762
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000763bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
764 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
765 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000766}
767
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000768bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000769 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
770 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000771}
772
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000773bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000774 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000775 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000776 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000777 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000778 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000779}
780
George Rimar86971052016-03-29 08:35:42 +0000781uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
782 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
783 if (Config->Shared)
784 error(getELFRelocationTypeName(EM_X86_64, Type) +
785 " cannot be a dynamic relocation");
786 return Type;
787}
788
George Rimar98b060d2016-03-06 06:01:07 +0000789uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000790 // No other types of TLS relocations requiring GOT should
791 // reach here.
792 assert(Type == R_X86_64_GOTTPOFF);
793 return R_X86_64_PC32;
794}
795
George Rimar98b060d2016-03-06 06:01:07 +0000796bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000797 return Type == R_X86_64_GOTTPOFF;
798}
799
George Rimar98b060d2016-03-06 06:01:07 +0000800bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000801 return Type == R_X86_64_TLSGD;
802}
803
George Rimar98b060d2016-03-06 06:01:07 +0000804bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000805 return Type == R_X86_64_TLSLD;
806}
807
George Rimar98b060d2016-03-06 06:01:07 +0000808bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000809 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
810 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000811}
812
Rafael Espindola993f0272016-02-26 14:27:47 +0000813bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
814 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000815}
Rafael Espindolac4010882015-09-22 20:54:08 +0000816
Rafael Espindolaae244002015-10-05 19:30:12 +0000817bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
818 switch (Type) {
819 default:
820 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000821 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000822 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000823 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000824 case R_X86_64_PC8:
825 case R_X86_64_PC16:
826 case R_X86_64_PC32:
827 case R_X86_64_PC64:
828 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000829 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000830 return true;
831 }
832}
833
Rui Ueyamac516ae12016-01-29 02:33:45 +0000834bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000835 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000836}
837
George Rimar6713cf82015-11-25 21:46:05 +0000838// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
839// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000840// how GD can be optimized to LE:
841// .byte 0x66
842// leaq x@tlsgd(%rip), %rdi
843// .word 0x6666
844// rex64
845// call __tls_get_addr@plt
846// Is converted to:
847// mov %fs:0x0,%rax
848// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000849size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000850 uint32_t Type, uint64_t P,
851 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000852 const uint8_t Inst[] = {
853 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
854 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
855 };
856 memcpy(Loc - 4, Inst, sizeof(Inst));
857 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000858
859 // The next relocation should be against __tls_get_addr, so skip it
860 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000861}
862
George Rimar25411f252015-12-04 11:20:13 +0000863// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
864// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
865// how GD can be optimized to IE:
866// .byte 0x66
867// leaq x@tlsgd(%rip), %rdi
868// .word 0x6666
869// rex64
870// call __tls_get_addr@plt
871// Is converted to:
872// mov %fs:0x0,%rax
873// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000874size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
875 uint32_t Type, uint64_t P,
876 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000877 const uint8_t Inst[] = {
878 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
879 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
880 };
881 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000882 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000883
884 // The next relocation should be against __tls_get_addr, so skip it
885 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000886}
887
George Rimar77d1cb12015-11-24 09:00:06 +0000888// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000889// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000890// This function does that. Read "ELF Handling For Thread-Local Storage,
891// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
892// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000893size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000894 uint32_t Type, uint64_t P,
895 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000896 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
897 // used in MOVQ or ADDQ instructions only.
898 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
899 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
900 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
901 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
902 uint8_t *Prefix = Loc - 3;
903 uint8_t *Inst = Loc - 2;
904 uint8_t *RegSlot = Loc - 1;
905 uint8_t Reg = Loc[-1] >> 3;
906 bool IsMov = *Inst == 0x8b;
907 bool RspAdd = !IsMov && Reg == 4;
908 // r12 and rsp registers requires special handling.
909 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
910 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
911 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
912 // The same true for rsp. So we convert to addq for them, saving 1 byte that
913 // we dont have.
914 if (RspAdd)
915 *Inst = 0x81;
916 else
917 *Inst = IsMov ? 0xc7 : 0x8d;
918 if (*Prefix == 0x4c)
919 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
920 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
921 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000922 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000923}
924
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000925// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
926// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
927// how LD can be optimized to LE:
928// leaq bar@tlsld(%rip), %rdi
929// callq __tls_get_addr@PLT
930// leaq bar@dtpoff(%rax), %rcx
931// Is converted to:
932// .word 0x6666
933// .byte 0x66
934// mov %fs:0,%rax
935// leaq bar@tpoff(%rax), %rcx
936size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
937 uint32_t Type, uint64_t P,
938 uint64_t SA) const {
939 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000940 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
941 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000942 }
943 if (Type == R_X86_64_DTPOFF32) {
944 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000945 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000946 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000947
948 const uint8_t Inst[] = {
949 0x66, 0x66, //.word 0x6666
950 0x66, //.byte 0x66
951 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
952 };
953 memcpy(Loc - 3, Inst, sizeof(Inst));
954 // The next relocation should be against __tls_get_addr, so skip it
955 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000956}
957
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000958void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +0000959 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000960 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000961 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000962 checkUInt<32>(SA, Type);
963 write32le(Loc, SA);
964 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000965 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000966 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000967 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000968 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000969 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000970 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000971 write64le(Loc, SA);
972 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000973 case R_X86_64_DTPOFF32:
974 write32le(Loc, SA);
975 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000976 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000977 case R_X86_64_GOTPCRELX:
978 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000979 case R_X86_64_PC32:
980 case R_X86_64_PLT32:
981 case R_X86_64_TLSGD:
982 case R_X86_64_TLSLD:
983 write32le(Loc, SA - P);
984 break;
George Rimar48651482015-12-11 08:59:37 +0000985 case R_X86_64_SIZE32:
Rafael Espindola287e1002016-03-30 13:27:50 +0000986 write32le(Loc, SA);
George Rimar48651482015-12-11 08:59:37 +0000987 break;
988 case R_X86_64_SIZE64:
Rafael Espindola287e1002016-03-30 13:27:50 +0000989 write64le(Loc, SA);
George Rimar48651482015-12-11 08:59:37 +0000990 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000991 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000992 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000993 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000994 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000995 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000996 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000997 default:
George Rimar57610422016-03-11 14:43:02 +0000998 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000999 }
1000}
1001
Hal Finkel3c8cc672015-10-12 20:56:18 +00001002// Relocation masks following the #lo(value), #hi(value), #ha(value),
1003// #higher(value), #highera(value), #highest(value), and #highesta(value)
1004// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
1005// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +00001006static uint16_t applyPPCLo(uint64_t V) { return V; }
1007static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
1008static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
1009static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
1010static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001011static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001012static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
1013
Davide Italiano8c3444362016-01-11 19:45:33 +00001014PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +00001015bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
1016
1017void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +00001018 uint64_t P, uint64_t SA) const {
Davide Italiano8c3444362016-01-11 19:45:33 +00001019 switch (Type) {
1020 case R_PPC_ADDR16_HA:
1021 write16be(Loc, applyPPCHa(SA));
1022 break;
1023 case R_PPC_ADDR16_LO:
1024 write16be(Loc, applyPPCLo(SA));
1025 break;
1026 default:
George Rimar57610422016-03-11 14:43:02 +00001027 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +00001028 }
1029}
1030
Rafael Espindolac4010882015-09-22 20:54:08 +00001031PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001032 GotRel = R_PPC64_GLOB_DAT;
1033 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001034 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001035
1036 // We need 64K pages (at least under glibc/Linux, the loader won't
1037 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001038 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001039
1040 // The PPC64 ELF ABI v1 spec, says:
1041 //
1042 // It is normally desirable to put segments with different characteristics
1043 // in separate 256 Mbyte portions of the address space, to give the
1044 // operating system full paging flexibility in the 64-bit address space.
1045 //
1046 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1047 // use 0x10000000 as the starting address.
1048 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001049}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001050
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001051uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1053 // order. The TOC starts where the first of these sections starts.
1054
1055 // FIXME: This obviously does not do the right thing when there is no .got
1056 // section, but there is a .toc or .tocbss section.
1057 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1058 if (!TocVA)
1059 TocVA = Out<ELF64BE>::Plt->getVA();
1060
1061 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1062 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1063 // code (crt1.o) assumes that you can get from the TOC base to the
1064 // start of the .toc section with only a single (signed) 16-bit relocation.
1065 return TocVA + 0x8000;
1066}
1067
Rui Ueyama9398f862016-01-29 04:15:02 +00001068void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1069 uint64_t PltEntryAddr, int32_t Index,
1070 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1072
1073 // FIXME: What we should do, in theory, is get the offset of the function
1074 // descriptor in the .opd section, and use that as the offset from %r2 (the
1075 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1076 // be a pointer to the function descriptor in the .opd section. Using
1077 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1078
Hal Finkelfa92f682015-10-13 21:47:34 +00001079 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1081 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1082 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1083 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1084 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1085 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1086 write32be(Buf + 28, 0x4e800420); // bctr
1087}
1088
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001089bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001090 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001091 return true;
1092
1093 switch (Type) {
1094 default: return false;
1095 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001096 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001097 case R_PPC64_GOT16_HA:
1098 case R_PPC64_GOT16_HI:
1099 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001100 case R_PPC64_GOT16_LO_DS:
1101 return true;
1102 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001103}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104
Rafael Espindola993f0272016-02-26 14:27:47 +00001105bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001107 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001108}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109
Hal Finkelbe0823d2015-10-12 20:58:52 +00001110bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1111 switch (Type) {
1112 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001113 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001114 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001115 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001116 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001117 }
1118}
1119
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001120void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +00001121 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001122 uint64_t TB = getPPC64TocBase();
1123
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1125 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001126 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001127 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1128 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001129 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1130 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001131 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1132 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 default: break;
1134 }
1135
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001137 case R_PPC64_ADDR14: {
1138 checkAlignment<4>(SA, Type);
1139 // Preserve the AA/LK bits in the branch instruction
1140 uint8_t AALK = Loc[3];
1141 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1142 break;
1143 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001144 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001145 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001146 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001147 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001148 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001149 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001150 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001151 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001152 case R_PPC64_ADDR16_HA:
1153 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 break;
1155 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001156 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001157 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001158 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001159 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001160 break;
1161 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001162 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001163 break;
1164 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001165 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001166 break;
1167 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001168 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001169 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001170 case R_PPC64_ADDR16_LO:
1171 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001172 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001173 case R_PPC64_ADDR16_LO_DS:
1174 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001175 break;
1176 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001177 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001178 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001179 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001180 case R_PPC64_ADDR64:
1181 write64be(Loc, SA);
1182 break;
1183 case R_PPC64_REL16_HA:
1184 write16be(Loc, applyPPCHa(SA - P));
1185 break;
1186 case R_PPC64_REL16_HI:
1187 write16be(Loc, applyPPCHi(SA - P));
1188 break;
1189 case R_PPC64_REL16_LO:
1190 write16be(Loc, applyPPCLo(SA - P));
1191 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001192 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001193 // If we have an undefined weak symbol, we might get here with a symbol
1194 // address of zero. That could overflow, but the code must be unreachable,
1195 // so don't bother doing anything at all.
1196 if (!SA)
1197 break;
1198
Hal Finkeldaedc122015-10-12 23:16:53 +00001199 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1200 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001201 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001202
1203 if (!InPlt && Out<ELF64BE>::Opd) {
1204 // If this is a local call, and we currently have the address of a
1205 // function-descriptor, get the underlying code address instead.
1206 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1207 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001208 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001209
1210 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001211 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001212 }
1213
Hal Finkel3c8cc672015-10-12 20:56:18 +00001214 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001215 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001216 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001217
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001218 uint32_t Nop = 0x60000000;
1219 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1220 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001221 break;
1222 }
1223 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001224 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001225 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001226 break;
1227 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001228 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001229 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001230 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001231 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001232 break;
1233 default:
George Rimar57610422016-03-11 14:43:02 +00001234 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001235 }
1236}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001237
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001238AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001239 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001240 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001241 IRelativeRel = R_AARCH64_IRELATIVE;
1242 GotRel = R_AARCH64_GLOB_DAT;
1243 PltRel = R_AARCH64_JUMP_SLOT;
1244 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001245 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1246 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001247 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001248 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001249 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001250}
George Rimar648a2c32015-10-20 08:54:27 +00001251
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001252bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001253 switch (Type) {
1254 default:
1255 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001256 case R_AARCH64_ADD_ABS_LO12_NC:
1257 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001258 case R_AARCH64_ADR_PREL_LO21:
1259 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001260 case R_AARCH64_CALL26:
1261 case R_AARCH64_CONDBR19:
1262 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001263 case R_AARCH64_LDST8_ABS_LO12_NC:
1264 case R_AARCH64_LDST16_ABS_LO12_NC:
1265 case R_AARCH64_LDST32_ABS_LO12_NC:
1266 case R_AARCH64_LDST64_ABS_LO12_NC:
1267 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001268 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001269 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001270 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1271 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1272 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1273 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1274 case R_AARCH64_TSTBR14:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001275 return true;
1276 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001277}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001278
George Rimar98b060d2016-03-06 06:01:07 +00001279bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001280 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1281 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1282 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1283 Type == R_AARCH64_TLSDESC_CALL;
1284}
1285
George Rimar98b060d2016-03-06 06:01:07 +00001286bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001287 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1288 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1289}
1290
George Rimar98b060d2016-03-06 06:01:07 +00001291uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001292 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1293 return Type;
1294 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001295 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001296 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001297 // Keep it going with a dummy value so that we can find more reloc errors.
1298 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001299}
1300
Rui Ueyamac516ae12016-01-29 02:33:45 +00001301void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001302 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1303}
1304
Rui Ueyama900e2d22016-01-29 03:51:49 +00001305void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001306 const uint8_t PltData[] = {
1307 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1308 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1309 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1310 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1311 0x20, 0x02, 0x1f, 0xd6, // br x17
1312 0x1f, 0x20, 0x03, 0xd5, // nop
1313 0x1f, 0x20, 0x03, 0xd5, // nop
1314 0x1f, 0x20, 0x03, 0xd5 // nop
1315 };
1316 memcpy(Buf, PltData, sizeof(PltData));
1317
Rui Ueyama900e2d22016-01-29 03:51:49 +00001318 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1319 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1320 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1321 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1322 Got + 16);
1323 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1324 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001325}
1326
Rui Ueyama9398f862016-01-29 04:15:02 +00001327void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1328 uint64_t PltEntryAddr, int32_t Index,
1329 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001330 const uint8_t Inst[] = {
1331 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1332 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1333 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1334 0x20, 0x02, 0x1f, 0xd6 // br x17
1335 };
1336 memcpy(Buf, Inst, sizeof(Inst));
1337
1338 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1339 GotEntryAddr);
1340 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1341 GotEntryAddr);
1342 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1343 GotEntryAddr);
1344}
1345
George Rimar98b060d2016-03-06 06:01:07 +00001346uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001347 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001348 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1349 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001350}
1351
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001352bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001353 switch (Type) {
1354 default:
1355 return false;
1356 case R_AARCH64_ABS16:
1357 case R_AARCH64_ABS32:
1358 case R_AARCH64_ABS64:
1359 case R_AARCH64_ADD_ABS_LO12_NC:
1360 case R_AARCH64_ADR_PREL_LO21:
1361 case R_AARCH64_ADR_PREL_PG_HI21:
1362 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001363 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001364 case R_AARCH64_LDST32_ABS_LO12_NC:
1365 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001366 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001367 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001368 }
1369}
1370
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001371bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001372 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001373 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001374 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001375 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001376 case R_AARCH64_ADR_GOT_PAGE:
1377 case R_AARCH64_LD64_GOT_LO12_NC:
1378 return true;
1379 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001380 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001381 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001382}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001383
Rafael Espindola993f0272016-02-26 14:27:47 +00001384bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001385 switch (Type) {
1386 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001387 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001388 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001389 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001390 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001391 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001392 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001393 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001394}
Davide Italiano1d750a62015-09-27 08:45:38 +00001395
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001396static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001397 uint32_t ImmLo = (Imm & 0x3) << 29;
1398 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1399 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001400 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001401}
1402
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001403static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1404 or32le(L, (Imm & 0xFFF) << 10);
1405}
1406
Davide Italiano318ca222015-10-02 22:13:51 +00001407// Page(Expr) is the page address of the expression Expr, defined
1408// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001409// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001410static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001411 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001412}
1413
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001414void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola287e1002016-03-30 13:27:50 +00001415 uint32_t Type, uint64_t P,
1416 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001417 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001418 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001419 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001420 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001421 break;
1422 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001423 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001424 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001425 break;
1426 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001427 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001428 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001429 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001430 // This relocation stores 12 bits and there's no instruction
1431 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001432 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1433 // bits in Loc are zero.
1434 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001435 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001436 case R_AARCH64_ADR_GOT_PAGE: {
1437 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1438 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001439 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001440 break;
1441 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001442 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001443 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001444 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001445 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001446 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001447 }
George Rimar3d737e42016-01-13 13:04:46 +00001448 case R_AARCH64_ADR_PREL_PG_HI21:
1449 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001450 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001451 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001452 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001453 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001454 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001455 case R_AARCH64_CALL26:
1456 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001457 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001458 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001459 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1460 break;
1461 }
George Rimar4102bfb2016-01-11 14:22:00 +00001462 case R_AARCH64_CONDBR19: {
1463 uint64_t X = SA - P;
1464 checkInt<21>(X, Type);
1465 or32le(Loc, (X & 0x1FFFFC) << 3);
1466 break;
1467 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001468 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001469 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001470 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001471 or32le(Loc, (SA & 0xFF8) << 7);
1472 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001473 case R_AARCH64_LDST128_ABS_LO12_NC:
1474 or32le(Loc, (SA & 0x0FF8) << 6);
1475 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001476 case R_AARCH64_LDST16_ABS_LO12_NC:
1477 or32le(Loc, (SA & 0x0FFC) << 9);
1478 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001479 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001480 or32le(Loc, (SA & 0xFFF) << 10);
1481 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001482 case R_AARCH64_LDST32_ABS_LO12_NC:
1483 or32le(Loc, (SA & 0xFFC) << 8);
1484 break;
1485 case R_AARCH64_LDST64_ABS_LO12_NC:
1486 or32le(Loc, (SA & 0xFF8) << 7);
1487 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001488 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001489 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001490 write16le(Loc, SA - P);
1491 break;
1492 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001493 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001494 write32le(Loc, SA - P);
1495 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001496 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001497 write64le(Loc, SA - P);
1498 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001499 case R_AARCH64_TSTBR14: {
1500 uint64_t X = SA - P;
1501 checkInt<16>(X, Type);
1502 or32le(Loc, (X & 0xFFFC) << 3);
1503 break;
1504 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001505 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1506 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1507 checkInt<24>(V, Type);
1508 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1509 break;
1510 }
1511 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1512 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1513 updateAArch64Add(Loc, V & 0xFFF);
1514 break;
1515 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001516 default:
George Rimar57610422016-03-11 14:43:02 +00001517 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001518 }
1519}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001520
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001521size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001522 uint32_t Type, uint64_t P,
1523 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001524 // TLSDESC Global-Dynamic relocation are in the form:
1525 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1526 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1527 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1528 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1529 // And it can optimized to:
1530 // movz x0, #0x0, lsl #16
1531 // movk x0, #0x10
1532 // nop
1533 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001534 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1535 uint64_t X = SA + TPOff;
1536 checkUInt<32>(X, Type);
1537
1538 uint32_t NewInst;
1539 switch (Type) {
1540 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1541 case R_AARCH64_TLSDESC_CALL:
1542 // nop
1543 NewInst = 0xd503201f;
1544 break;
1545 case R_AARCH64_TLSDESC_ADR_PAGE21:
1546 // movz
1547 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1548 break;
1549 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1550 // movk
1551 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1552 break;
1553 default:
George Rimar777f9632016-03-12 08:31:34 +00001554 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001555 }
1556 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001557
1558 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001559}
1560
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001561size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001562 uint32_t Type, uint64_t P,
1563 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001564 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1565 uint64_t X = SA + TPOff;
1566 checkUInt<32>(X, Type);
1567
George Rimar4d1d16d2016-03-06 06:16:05 +00001568 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001569 uint32_t NewInst;
1570 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1571 // Generate movz.
1572 unsigned RegNo = (Inst & 0x1f);
1573 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1574 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1575 // Generate movk
1576 unsigned RegNo = (Inst & 0x1f);
1577 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1578 } else {
George Rimar777f9632016-03-12 08:31:34 +00001579 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001580 }
1581 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001582
1583 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001584}
1585
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001586// Implementing relocations for AMDGPU is low priority since most
1587// programs don't use relocations now. Thus, this function is not
1588// actually called (relocateOne is called for each relocation).
1589// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001590void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +00001591 uint64_t P, uint64_t SA) const {
George Rimar57610422016-03-11 14:43:02 +00001592 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001593}
1594
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001595template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001596 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001597 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001598 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001599 PltEntrySize = 16;
1600 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001601 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001602 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001603 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001604 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001605 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001606}
1607
1608template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001609uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001610 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1611 return R_MIPS_REL32;
1612 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001613 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001614 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001615 // Keep it going with a dummy value so that we can find more reloc errors.
1616 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001617}
1618
1619template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001620void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001621 typedef typename ELFT::Off Elf_Off;
1622 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001623
1624 // Set the MSB of the second GOT slot. This is not required by any
1625 // MIPS ABI documentation, though.
1626 //
1627 // There is a comment in glibc saying that "The MSB of got[1] of a
1628 // gnu object is set to identify gnu objects," and in GNU gold it
1629 // says "the second entry will be used by some runtime loaders".
1630 // But how this field is being used is unclear.
1631 //
1632 // We are not really willing to mimic other linkers behaviors
1633 // without understanding why they do that, but because all files
1634 // generated by GNU tools have this special GOT value, and because
1635 // we've been doing this for years, it is probably a safe bet to
1636 // keep doing this for now. We really need to revisit this to see
1637 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001638 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001639 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001640}
1641
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001642template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001643void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1644 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001645}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001646
Simon Atanasyan35031192015-12-15 06:06:34 +00001647static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001648
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001649template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001650static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001651 uint32_t Instr = read32<E>(Loc);
1652 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1653 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1654}
1655
1656template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001657static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001658 uint64_t SA) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001659 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001660 uint32_t Instr = read32<E>(Loc);
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001661 int64_t V = SA - P;
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001662 if (SHIFT > 0)
1663 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001664 checkInt<BSIZE + SHIFT>(V, Type);
1665 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001666}
1667
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001668template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001669static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001670 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001671 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001672}
1673
Simon Atanasyan3b377852016-03-04 10:55:20 +00001674template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001675static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1676 uint32_t Instr = read32<E>(Loc);
1677 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1678}
1679
Rafael Espindola666625b2016-04-01 14:36:09 +00001680template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001681 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1682}
1683
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001684template <class ELFT>
1685void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1686 const endianness E = ELFT::TargetEndianness;
1687 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1688 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1689 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1690 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1691 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1692 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1693 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1694 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1695 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001696 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001697 writeMipsLo16<E>(Buf + 4, Got);
1698 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001699}
1700
1701template <class ELFT>
1702void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1703 uint64_t PltEntryAddr, int32_t Index,
1704 unsigned RelOff) const {
1705 const endianness E = ELFT::TargetEndianness;
1706 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1707 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1708 write32<E>(Buf + 8, 0x03200008); // jr $25
1709 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001710 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001711 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1712 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001713}
1714
1715template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001716void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1717 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1718 // See MipsTargetInfo::writeThunk for details.
1719 const endianness E = ELFT::TargetEndianness;
1720 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1721 write32<E>(Buf + 4, 0x08000000); // j func
1722 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1723 write32<E>(Buf + 12, 0x00000000); // nop
1724 writeMipsHi16<E>(Buf, S);
1725 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1726 writeMipsLo16<E>(Buf + 8, S);
1727}
1728
1729template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001730bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Rafael Espindola790db9c2016-04-01 17:00:36 +00001731 return !isRelRelative(Type) || Type == R_MIPS_LO16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001732}
1733
1734template <class ELFT>
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001735bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001736 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001737}
1738
1739template <class ELFT>
1740bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1741 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001742}
1743
1744template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001745bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1746 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001747}
1748
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001749template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001750bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1751 const SymbolBody &S) const {
1752 // Any MIPS PIC code function is invoked with its address in register $t9.
1753 // So if we have a branch instruction from non-PIC code to the PIC one
1754 // we cannot make the jump directly and need to create a small stubs
1755 // to save the target function address.
1756 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1757 if (Type != R_MIPS_26)
1758 return false;
1759 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1760 if (!F)
1761 return false;
1762 // If current file has PIC code, LA25 stub is not required.
1763 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1764 return false;
1765 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1766 if (!D || !D->Section)
1767 return false;
1768 // LA25 is required if target file has PIC code
1769 // or target symbol is a PIC symbol.
1770 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001771 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001772}
1773
1774template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001775uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001776 uint32_t Type) const {
1777 const endianness E = ELFT::TargetEndianness;
1778 switch (Type) {
1779 default:
1780 return 0;
1781 case R_MIPS_32:
1782 case R_MIPS_GPREL32:
1783 return read32<E>(Buf);
1784 case R_MIPS_26:
1785 // FIXME (simon): If the relocation target symbol is not a PLT entry
1786 // we should use another expression for calculation:
1787 // ((A << 2) | (P & 0xf0000000)) >> 2
1788 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1789 case R_MIPS_GPREL16:
1790 case R_MIPS_LO16:
1791 case R_MIPS_PCLO16:
1792 case R_MIPS_TLS_DTPREL_HI16:
1793 case R_MIPS_TLS_DTPREL_LO16:
1794 case R_MIPS_TLS_TPREL_HI16:
1795 case R_MIPS_TLS_TPREL_LO16:
1796 return readSignedLo16<E>(Buf);
1797 case R_MIPS_PC16:
1798 return getPcRelocAddend<E, 16, 2>(Buf);
1799 case R_MIPS_PC19_S2:
1800 return getPcRelocAddend<E, 19, 2>(Buf);
1801 case R_MIPS_PC21_S2:
1802 return getPcRelocAddend<E, 21, 2>(Buf);
1803 case R_MIPS_PC26_S2:
1804 return getPcRelocAddend<E, 26, 2>(Buf);
1805 case R_MIPS_PC32:
1806 return getPcRelocAddend<E, 32, 0>(Buf);
1807 }
1808}
1809
1810template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001811void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola287e1002016-03-30 13:27:50 +00001812 uint32_t Type, uint64_t P,
1813 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001814 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001815 // Thread pointer and DRP offsets from the start of TLS data area.
1816 // https://www.linux-mips.org/wiki/NPTL
1817 const uint32_t TPOffset = 0x7000;
1818 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001819 switch (Type) {
1820 case R_MIPS_32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001821 write32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001822 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001823 case R_MIPS_26: {
1824 uint32_t Instr = read32<E>(Loc);
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001825 write32<E>(Loc, (Instr & ~0x3ffffff) | (SA >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001826 break;
1827 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001828 case R_MIPS_CALL16:
1829 case R_MIPS_GOT16: {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001830 int64_t V = SA - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001831 if (Type == R_MIPS_GOT16)
1832 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001833 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001834 break;
1835 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001836 case R_MIPS_GPREL16: {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001837 int64_t V = SA - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001838 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001839 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001840 break;
1841 }
1842 case R_MIPS_GPREL32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001843 write32<E>(Loc, SA - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001844 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001845 case R_MIPS_HI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001846 writeMipsHi16<E>(Loc, SA);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001847 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001848 case R_MIPS_JALR:
1849 // Ignore this optimization relocation for now
1850 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001851 case R_MIPS_LO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001852 writeMipsLo16<E>(Loc, SA);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001853 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001854 case R_MIPS_PC16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001855 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001856 break;
1857 case R_MIPS_PC19_S2:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001858 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001859 break;
1860 case R_MIPS_PC21_S2:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001861 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001862 break;
1863 case R_MIPS_PC26_S2:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001864 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, SA);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001865 break;
1866 case R_MIPS_PC32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001867 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001868 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001869 case R_MIPS_PCHI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001870 writeMipsHi16<E>(Loc, SA - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001871 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001872 case R_MIPS_PCLO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001873 writeMipsLo16<E>(Loc, SA - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001874 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001875 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001876 writeMipsHi16<E>(Loc, SA - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001877 break;
1878 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001879 writeMipsLo16<E>(Loc, SA - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001880 break;
1881 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001882 writeMipsHi16<E>(Loc, SA - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001883 break;
1884 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001885 writeMipsLo16<E>(Loc, SA - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001886 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001887 default:
George Rimar57610422016-03-11 14:43:02 +00001888 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001889 }
1890}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001891
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001892template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001893bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001894 return Type == R_MIPS_JALR;
1895}
1896
1897template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001898bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1899 switch (Type) {
1900 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001901 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001902 case R_MIPS_26:
1903 case R_MIPS_32:
1904 case R_MIPS_64:
1905 case R_MIPS_HI16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001906 case R_MIPS_TLS_DTPREL_HI16:
1907 case R_MIPS_TLS_DTPREL_LO16:
1908 case R_MIPS_TLS_TPREL_HI16:
1909 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001910 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001911 }
1912}
1913
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001914// _gp is a MIPS-specific ABI-defined symbol which points to
1915// a location that is relative to GOT. This function returns
1916// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001917template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001918 unsigned GPOffset = 0x7ff0;
1919 if (uint64_t V = Out<ELFT>::Got->getVA())
1920 return V + GPOffset;
1921 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001922}
1923
1924template uint32_t getMipsGpAddr<ELF32LE>();
1925template uint32_t getMipsGpAddr<ELF32BE>();
1926template uint64_t getMipsGpAddr<ELF64LE>();
1927template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001928
1929template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1930 const SymbolBody &) const;
1931template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1932 const SymbolBody &) const;
1933template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1934 const SymbolBody &) const;
1935template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1936 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001937}
1938}