blob: c4c93cb72aae1a4f9980f401fc2202132406ca35 [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"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000035
Rui Ueyamac1c282a2016-02-11 21:18:01 +000036TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000037
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000049 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050}
51
52template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
53 if (isUInt<N>(V))
54 return;
55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000056 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000057}
58
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
60 if (isInt<N>(V) || isUInt<N>(V))
61 return;
62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000063 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064}
65
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
67 if ((V & (N - 1)) == 0)
68 return;
69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000070 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
Rui Ueyamaefc23de2015-10-14 21:30:32 +000073namespace {
74class X86TargetInfo final : public TargetInfo {
75public:
76 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000077 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000078 uint32_t getDynRel(uint32_t Type) const override;
79 uint32_t getTlsGotRel(uint32_t Type) const override;
80 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
81 bool isTlsLocalDynamicRel(uint32_t Type) const override;
82 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
83 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000084 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000085 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000086 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
87 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000088 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000089 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000090 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000091 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000092 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000093 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000094 uint64_t SA, uint64_t ZA = 0,
95 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000096
97 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
98 uint64_t P, uint64_t SA) const override;
99 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000100 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000101 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000102 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000103 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
104 uint64_t P, uint64_t SA) const override;
105
George Rimarbfb7bf72015-12-21 10:00:12 +0000106 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000107 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000108};
109
110class X86_64TargetInfo final : public TargetInfo {
111public:
112 X86_64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000113 uint32_t getTlsGotRel(uint32_t Type) const override;
114 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
115 bool isTlsLocalDynamicRel(uint32_t Type) const override;
116 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
117 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000118 void writeGotPltHeader(uint8_t *Buf) const override;
119 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000120 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000121 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
122 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000123 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000124 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000125 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000126 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000127 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000128 uint64_t SA, uint64_t ZA = 0,
129 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000130 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000131 bool isSizeRel(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000132
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000133 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
134 uint64_t P, uint64_t SA) const override;
135 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000136 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000137 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000138 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000139 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
140 uint64_t P, uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000141};
142
Davide Italiano8c3444362016-01-11 19:45:33 +0000143class PPCTargetInfo final : public TargetInfo {
144public:
145 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000146 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
147 uint64_t SA, uint64_t ZA = 0,
148 uint8_t *PairedLoc = nullptr) const override;
149 bool isRelRelative(uint32_t Type) const override;
150};
151
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000152class PPC64TargetInfo final : public TargetInfo {
153public:
154 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000155 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
156 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000157 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000158 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000159 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000160 uint64_t SA, uint64_t ZA = 0,
161 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000162 bool isRelRelative(uint32_t Type) const override;
163};
164
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000165class AArch64TargetInfo final : public TargetInfo {
166public:
167 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000168 uint32_t getDynRel(uint32_t Type) const override;
169 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
170 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000171 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000172 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000173 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
174 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000175 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000176 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000177 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000178 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000179 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000180 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000181 uint64_t SA, uint64_t ZA = 0,
182 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000183
184 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000185 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000186 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000187 uint64_t P, uint64_t SA) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000188
189private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000190 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000191};
192
Tom Stellard80efb162016-01-07 03:59:08 +0000193class AMDGPUTargetInfo final : public TargetInfo {
194public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000195 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000196 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
197 uint64_t SA, uint64_t ZA = 0,
198 uint8_t *PairedLoc = nullptr) const override;
199};
200
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000201template <class ELFT> class MipsTargetInfo final : public TargetInfo {
202public:
203 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000204 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000205 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
206 void writePltZero(uint8_t *Buf) const override;
207 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
208 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000209 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000210 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000211 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000212 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000213 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000214 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000215 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000216 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000217 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000218 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000219};
220} // anonymous namespace
221
Rui Ueyama91004392015-10-13 16:08:15 +0000222TargetInfo *createTarget() {
223 switch (Config->EMachine) {
224 case EM_386:
225 return new X86TargetInfo();
226 case EM_AARCH64:
227 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000228 case EM_AMDGPU:
229 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000230 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000231 switch (Config->EKind) {
232 case ELF32LEKind:
233 return new MipsTargetInfo<ELF32LE>();
234 case ELF32BEKind:
235 return new MipsTargetInfo<ELF32BE>();
236 default:
George Rimar777f9632016-03-12 08:31:34 +0000237 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000238 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000239 case EM_PPC:
240 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000241 case EM_PPC64:
242 return new PPC64TargetInfo();
243 case EM_X86_64:
244 return new X86_64TargetInfo();
245 }
George Rimar777f9632016-03-12 08:31:34 +0000246 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000247}
248
Rafael Espindola01205f72015-09-22 18:19:46 +0000249TargetInfo::~TargetInfo() {}
250
George Rimar98b060d2016-03-06 06:01:07 +0000251bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000252 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000253 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000254
Rafael Espindolad405f472016-03-04 21:37:09 +0000255 // We know we are producing an executable.
256
257 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
258 // depending on the symbol being locally defined or not.
259 if (isTlsGlobalDynamicRel(Type))
260 return true;
261
262 // Local-Dynamic relocs can be relaxed to Local-Exec.
263 if (isTlsLocalDynamicRel(Type))
264 return true;
265
266 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
267 // defined.
268 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000269 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000270
George Rimar77d1cb12015-11-24 09:00:06 +0000271 return false;
272}
273
George Rimar786e8662016-03-17 05:57:33 +0000274uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000275
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000276bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
277
278template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
279 if (Config->Shared)
280 return false;
281 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
282 if (!SS)
283 return false;
284 return SS->Sym.getType() == STT_OBJECT;
285}
286
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000287template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000288bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000289 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000290}
291
George Rimarbfb7bf72015-12-21 10:00:12 +0000292bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000293bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000294bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000295bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000296
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000297bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000298
Rafael Espindola993f0272016-02-26 14:27:47 +0000299bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000300
301bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
302
Rafael Espindola852860e2016-02-12 15:47:37 +0000303TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
304 const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +0000305 if (S.IsGnuIFunc)
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000306 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000307 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000308 return Plt_Explicit;
309
310 // This handles a non PIC program call to function in a shared library.
311 // In an ideal world, we could just report an error saying the relocation
312 // can overflow at runtime.
313 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
314 // libc.so.
315 //
316 // The general idea on how to handle such cases is to create a PLT entry
317 // and use that as the function value.
318 //
319 // For the static linking part, we just return true and everything else
320 // will use the the PLT entry as the address.
321 //
322 // The remaining problem is making sure pointer equality still works. We
323 // need the help of the dynamic linker for that. We let it know that we have
324 // a direct reference to a so symbol by creating an undefined symbol with a
325 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
326 // the value of the symbol we created. This is true even for got entries, so
327 // pointer equality is maintained. To avoid an infinite loop, the only entry
328 // that points to the real function is a dedicated got entry used by the
329 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
330 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000331 if (S.isShared())
332 if (!Config->Pic && S.IsFunc && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000333 return Plt_Implicit;
334
Rafael Espindola852860e2016-02-12 15:47:37 +0000335 return Plt_No;
336}
Rui Ueyama012eb782016-01-29 04:05:09 +0000337
George Rimar98b060d2016-03-06 06:01:07 +0000338bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000339
George Rimar98b060d2016-03-06 06:01:07 +0000340bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000341 return false;
342}
343
George Rimar98b060d2016-03-06 06:01:07 +0000344bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000345
George Rimar98b060d2016-03-06 06:01:07 +0000346bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000347 return false;
348}
349
George Rimar7b8850f2016-03-06 06:09:50 +0000350size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
351 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000352 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000353 if (isTlsGlobalDynamicRel(Type)) {
354 if (S.isPreemptible())
355 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000356 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000357 }
358 if (isTlsLocalDynamicRel(Type))
359 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
360 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000361 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000362}
363
364size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000365 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000366 llvm_unreachable("Should not have claimed to be relaxable");
367}
368
369size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
370 uint64_t P, uint64_t SA) const {
371 llvm_unreachable("Should not have claimed to be relaxable");
372}
373
374size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000375 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000376 llvm_unreachable("Should not have claimed to be relaxable");
377}
378
379size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
380 uint64_t P, uint64_t SA) const {
381 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000382}
George Rimar77d1cb12015-11-24 09:00:06 +0000383
Rafael Espindola7f074422015-09-22 21:35:51 +0000384X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000385 CopyRel = R_386_COPY;
386 GotRel = R_386_GLOB_DAT;
387 PltRel = R_386_JUMP_SLOT;
388 IRelativeRel = R_386_IRELATIVE;
389 RelativeRel = R_386_RELATIVE;
390 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000391 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
392 TlsOffsetRel = R_386_TLS_DTPOFF32;
393 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000394 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000395 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000396}
397
Rafael Espindolaffcad442016-03-23 14:58:25 +0000398bool X86TargetInfo::isRelRelative(uint32_t Type) const {
399 switch (Type) {
400 default:
401 return false;
402 case R_386_PC32:
403 case R_386_PLT32:
404 case R_386_TLS_LDO_32:
405 return true;
406 }
407}
408
Rui Ueyamac516ae12016-01-29 02:33:45 +0000409void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000410 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
411}
412
Rui Ueyamac516ae12016-01-29 02:33:45 +0000413void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000414 // Entries in .got.plt initially points back to the corresponding
415 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000416 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000417}
Rafael Espindola01205f72015-09-22 18:19:46 +0000418
George Rimar98b060d2016-03-06 06:01:07 +0000419uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000420 if (Type == R_386_TLS_LE)
421 return R_386_TLS_TPOFF;
422 if (Type == R_386_TLS_LE_32)
423 return R_386_TLS_TPOFF32;
424 return Type;
425}
426
George Rimar98b060d2016-03-06 06:01:07 +0000427uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000428 if (Type == R_386_TLS_IE)
429 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000430 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000431}
432
George Rimar98b060d2016-03-06 06:01:07 +0000433bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000434 return Type == R_386_TLS_GD;
435}
436
George Rimar98b060d2016-03-06 06:01:07 +0000437bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000438 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
439}
440
George Rimar98b060d2016-03-06 06:01:07 +0000441bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000442 return Type == R_386_TLS_LDM;
443}
444
George Rimar98b060d2016-03-06 06:01:07 +0000445bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000446 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
447}
448
Rui Ueyama900e2d22016-01-29 03:51:49 +0000449void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000450 // Executable files and shared object files have
451 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000452 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000453 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000454 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000455 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
456 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000457 };
458 memcpy(Buf, V, sizeof(V));
459 return;
460 }
George Rimar648a2c32015-10-20 08:54:27 +0000461
George Rimar77b77792015-11-25 22:15:01 +0000462 const uint8_t PltData[] = {
463 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000464 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
465 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000466 };
467 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000468 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000469 write32le(Buf + 2, Got + 4);
470 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000471}
472
Rui Ueyama9398f862016-01-29 04:15:02 +0000473void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
474 uint64_t PltEntryAddr, int32_t Index,
475 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000476 const uint8_t Inst[] = {
477 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
478 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
479 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
480 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000481 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000482
George Rimar77b77792015-11-25 22:15:01 +0000483 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000484 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000485 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
486 : Out<ELF32LE>::Got->getVA();
487 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000488 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000489 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000490}
491
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000492bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
493 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000494}
495
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000496bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000497 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000498 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000499 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000500 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000501 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000502}
503
Rafael Espindola993f0272016-02-26 14:27:47 +0000504bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
505 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000506}
507
George Rimarbfb7bf72015-12-21 10:00:12 +0000508bool X86TargetInfo::isGotRelative(uint32_t Type) const {
509 // This relocation does not require got entry,
510 // but it is relative to got and needs it to be created.
511 // Here we request for that.
512 return Type == R_386_GOTOFF;
513}
514
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000515bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
516 return Type == R_386_GOT32;
517}
518
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000519void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000520 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000521 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000522 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000523 case R_386_32:
524 add32le(Loc, SA);
525 break;
George Rimarffb67352016-01-19 11:00:48 +0000526 case R_386_GOT32: {
527 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
528 Out<ELF32LE>::Got->getNumEntries() * 4;
529 checkInt<32>(V, Type);
530 add32le(Loc, V);
531 break;
532 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000533 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000534 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000535 break;
George Rimar13934772015-11-25 20:20:31 +0000536 case R_386_GOTPC:
537 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
538 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000539 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000540 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000541 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000542 break;
George Rimar9db204a2015-12-02 09:58:20 +0000543 case R_386_TLS_GD:
544 case R_386_TLS_LDM:
545 case R_386_TLS_TPOFF: {
546 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
547 Out<ELF32LE>::Got->getNumEntries() * 4;
548 checkInt<32>(V, Type);
549 write32le(Loc, V);
550 break;
551 }
George Rimar6f17e092015-12-17 09:32:21 +0000552 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000553 case R_386_TLS_LDO_32:
554 write32le(Loc, SA);
555 break;
George Rimard23970f2015-11-25 20:41:53 +0000556 case R_386_TLS_LE:
557 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
558 break;
559 case R_386_TLS_LE_32:
560 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
561 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000562 default:
George Rimar57610422016-03-11 14:43:02 +0000563 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000564 }
565}
566
George Rimar98b060d2016-03-06 06:01:07 +0000567bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000568 return Config->Shared && Type == R_386_TLS_IE;
569}
570
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000571size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000572 uint32_t Type, uint64_t P,
573 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000574 // GD can be optimized to LE:
575 // leal x@tlsgd(, %ebx, 1),
576 // call __tls_get_addr@plt
577 // Can be converted to:
578 // movl %gs:0,%eax
579 // addl $x@ntpoff,%eax
580 // But gold emits subl $foo@tpoff,%eax instead of addl.
581 // These instructions are completely equal in behavior.
582 // This method generates subl to be consistent with gold.
583 const uint8_t Inst[] = {
584 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
585 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
586 };
587 memcpy(Loc - 3, Inst, sizeof(Inst));
588 relocateOne(Loc + 5, BufEnd, R_386_32, P,
589 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
590
591 // The next relocation should be against __tls_get_addr, so skip it
592 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000593}
594
595// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
596// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
597// how GD can be optimized to IE:
598// leal x@tlsgd(, %ebx, 1),
599// call __tls_get_addr@plt
600// Is converted to:
601// movl %gs:0, %eax
602// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000603size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
604 uint32_t Type, uint64_t P,
605 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000606 const uint8_t Inst[] = {
607 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
608 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
609 };
610 memcpy(Loc - 3, Inst, sizeof(Inst));
611 relocateOne(Loc + 5, BufEnd, R_386_32, P,
612 SA - Out<ELF32LE>::Got->getVA() -
613 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000614
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000615 // The next relocation should be against __tls_get_addr, so skip it
616 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000617}
618
George Rimar6f17e092015-12-17 09:32:21 +0000619// In some conditions, relocations can be optimized to avoid using GOT.
620// This function does that for Initial Exec to Local Exec case.
621// Read "ELF Handling For Thread-Local Storage, 5.1
622// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000623// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000624
625size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000626 uint32_t Type, uint64_t P,
627 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000628 // Ulrich's document section 6.2 says that @gotntpoff can
629 // be used with MOVL or ADDL instructions.
630 // @indntpoff is similar to @gotntpoff, but for use in
631 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000632 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000633 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000634 uint8_t Reg = (Loc[-1] >> 3) & 7;
635 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000636 if (Type == R_386_TLS_IE) {
637 // For R_386_TLS_IE relocation we perform the next transformations:
638 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
639 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
640 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
641 // First one is special because when EAX is used the sequence is 5 bytes
642 // long, otherwise it is 6 bytes.
643 if (*Op == 0xa1) {
644 *Op = 0xb8;
645 } else {
646 *Inst = IsMov ? 0xc7 : 0x81;
647 *Op = 0xc0 | ((*Op >> 3) & 7);
648 }
649 } else {
650 // R_386_TLS_GOTIE relocation can be optimized to
651 // R_386_TLS_LE so that it does not use GOT.
652 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
653 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
654 // Note: gold converts to ADDL instead of LEAL.
655 *Inst = IsMov ? 0xc7 : 0x8d;
656 if (IsMov)
657 *Op = 0xc0 | ((*Op >> 3) & 7);
658 else
659 *Op = 0x80 | Reg | (Reg << 3);
660 }
George Rimar2558e122015-12-09 09:55:54 +0000661 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000662
663 return 0;
664}
665
666size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
667 uint32_t Type, uint64_t P,
668 uint64_t SA) const {
669 if (Type == R_386_TLS_LDO_32) {
670 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
671 return 0;
672 }
673
674 // LD can be optimized to LE:
675 // leal foo(%reg),%eax
676 // call ___tls_get_addr
677 // Is converted to:
678 // movl %gs:0,%eax
679 // nop
680 // leal 0(%esi,1),%esi
681 const uint8_t Inst[] = {
682 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
683 0x90, // nop
684 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
685 };
686 memcpy(Loc - 2, Inst, sizeof(Inst));
687
688 // The next relocation should be against __tls_get_addr, so skip it
689 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000690}
691
Rafael Espindola7f074422015-09-22 21:35:51 +0000692X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000693 CopyRel = R_X86_64_COPY;
694 GotRel = R_X86_64_GLOB_DAT;
695 PltRel = R_X86_64_JUMP_SLOT;
696 RelativeRel = R_X86_64_RELATIVE;
697 IRelativeRel = R_X86_64_IRELATIVE;
698 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000699 TlsModuleIndexRel = R_X86_64_DTPMOD64;
700 TlsOffsetRel = R_X86_64_DTPOFF64;
701 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000702 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000703 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000704}
705
Rui Ueyamac516ae12016-01-29 02:33:45 +0000706void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000707 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
708}
709
Rui Ueyamac516ae12016-01-29 02:33:45 +0000710void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000711 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000712 write32le(Buf, Plt + 6);
713}
714
Rui Ueyama900e2d22016-01-29 03:51:49 +0000715void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000716 const uint8_t PltData[] = {
717 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
718 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
719 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
720 };
721 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000722 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
723 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
724 write32le(Buf + 2, Got - Plt + 2); // GOT+8
725 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000726}
Rafael Espindola01205f72015-09-22 18:19:46 +0000727
Rui Ueyama9398f862016-01-29 04:15:02 +0000728void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
729 uint64_t PltEntryAddr, int32_t Index,
730 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000731 const uint8_t Inst[] = {
732 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
733 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
734 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
735 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000736 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000737
George Rimar648a2c32015-10-20 08:54:27 +0000738 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
739 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000740 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000741}
742
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000743bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
744 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
745 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000746}
747
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000748bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000749 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
750 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000751}
752
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000753bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000754 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000755 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000756 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000757 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000758 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000759}
760
George Rimar98b060d2016-03-06 06:01:07 +0000761uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000762 // No other types of TLS relocations requiring GOT should
763 // reach here.
764 assert(Type == R_X86_64_GOTTPOFF);
765 return R_X86_64_PC32;
766}
767
George Rimar98b060d2016-03-06 06:01:07 +0000768bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000769 return Type == R_X86_64_GOTTPOFF;
770}
771
George Rimar98b060d2016-03-06 06:01:07 +0000772bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000773 return Type == R_X86_64_TLSGD;
774}
775
George Rimar98b060d2016-03-06 06:01:07 +0000776bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000777 return Type == R_X86_64_TLSLD;
778}
779
George Rimar98b060d2016-03-06 06:01:07 +0000780bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000781 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
782 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000783}
784
Rafael Espindola993f0272016-02-26 14:27:47 +0000785bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
786 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000787}
Rafael Espindolac4010882015-09-22 20:54:08 +0000788
Rafael Espindolaae244002015-10-05 19:30:12 +0000789bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
790 switch (Type) {
791 default:
792 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000793 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000794 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000795 case R_X86_64_PC8:
796 case R_X86_64_PC16:
797 case R_X86_64_PC32:
798 case R_X86_64_PC64:
799 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000800 return true;
801 }
802}
803
Rui Ueyamac516ae12016-01-29 02:33:45 +0000804bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000805 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000806}
807
George Rimar6713cf82015-11-25 21:46:05 +0000808// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
809// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000810// how GD can be optimized to LE:
811// .byte 0x66
812// leaq x@tlsgd(%rip), %rdi
813// .word 0x6666
814// rex64
815// call __tls_get_addr@plt
816// Is converted to:
817// mov %fs:0x0,%rax
818// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000819size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000820 uint32_t Type, uint64_t P,
821 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000822 const uint8_t Inst[] = {
823 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
824 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
825 };
826 memcpy(Loc - 4, Inst, sizeof(Inst));
827 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000828
829 // The next relocation should be against __tls_get_addr, so skip it
830 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000831}
832
George Rimar25411f252015-12-04 11:20:13 +0000833// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
834// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
835// how GD can be optimized to IE:
836// .byte 0x66
837// leaq x@tlsgd(%rip), %rdi
838// .word 0x6666
839// rex64
840// call __tls_get_addr@plt
841// Is converted to:
842// mov %fs:0x0,%rax
843// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000844size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
845 uint32_t Type, uint64_t P,
846 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000847 const uint8_t Inst[] = {
848 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
849 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
850 };
851 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000852 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000853
854 // The next relocation should be against __tls_get_addr, so skip it
855 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000856}
857
George Rimar77d1cb12015-11-24 09:00:06 +0000858// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000859// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000860// This function does that. Read "ELF Handling For Thread-Local Storage,
861// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
862// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000863size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000864 uint32_t Type, uint64_t P,
865 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000866 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
867 // used in MOVQ or ADDQ instructions only.
868 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
869 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
870 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
871 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
872 uint8_t *Prefix = Loc - 3;
873 uint8_t *Inst = Loc - 2;
874 uint8_t *RegSlot = Loc - 1;
875 uint8_t Reg = Loc[-1] >> 3;
876 bool IsMov = *Inst == 0x8b;
877 bool RspAdd = !IsMov && Reg == 4;
878 // r12 and rsp registers requires special handling.
879 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
880 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
881 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
882 // The same true for rsp. So we convert to addq for them, saving 1 byte that
883 // we dont have.
884 if (RspAdd)
885 *Inst = 0x81;
886 else
887 *Inst = IsMov ? 0xc7 : 0x8d;
888 if (*Prefix == 0x4c)
889 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
890 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
891 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000892 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000893}
894
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000895// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
896// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
897// how LD can be optimized to LE:
898// leaq bar@tlsld(%rip), %rdi
899// callq __tls_get_addr@PLT
900// leaq bar@dtpoff(%rax), %rcx
901// Is converted to:
902// .word 0x6666
903// .byte 0x66
904// mov %fs:0,%rax
905// leaq bar@tpoff(%rax), %rcx
906size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
907 uint32_t Type, uint64_t P,
908 uint64_t SA) const {
909 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000910 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
911 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000912 }
913 if (Type == R_X86_64_DTPOFF32) {
914 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000915 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000916 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000917
918 const uint8_t Inst[] = {
919 0x66, 0x66, //.word 0x6666
920 0x66, //.byte 0x66
921 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
922 };
923 memcpy(Loc - 3, Inst, sizeof(Inst));
924 // The next relocation should be against __tls_get_addr, so skip it
925 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000926}
927
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000928void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000929 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000930 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000931 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000932 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000933 checkUInt<32>(SA, Type);
934 write32le(Loc, SA);
935 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000936 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000937 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000938 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000939 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000940 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000941 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000942 write64le(Loc, SA);
943 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000944 case R_X86_64_DTPOFF32:
945 write32le(Loc, SA);
946 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000947 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000948 case R_X86_64_GOTPCRELX:
949 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000950 case R_X86_64_PC32:
951 case R_X86_64_PLT32:
952 case R_X86_64_TLSGD:
953 case R_X86_64_TLSLD:
954 write32le(Loc, SA - P);
955 break;
George Rimar48651482015-12-11 08:59:37 +0000956 case R_X86_64_SIZE32:
957 write32le(Loc, ZA);
958 break;
959 case R_X86_64_SIZE64:
960 write64le(Loc, ZA);
961 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000962 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000963 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000964 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000965 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000966 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000967 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000968 default:
George Rimar57610422016-03-11 14:43:02 +0000969 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000970 }
971}
972
Hal Finkel3c8cc672015-10-12 20:56:18 +0000973// Relocation masks following the #lo(value), #hi(value), #ha(value),
974// #higher(value), #highera(value), #highest(value), and #highesta(value)
975// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
976// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000977static uint16_t applyPPCLo(uint64_t V) { return V; }
978static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
979static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
980static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
981static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000983static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
984
Davide Italiano8c3444362016-01-11 19:45:33 +0000985PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000986bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
987
988void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
989 uint64_t P, uint64_t SA, uint64_t ZA,
990 uint8_t *PairedLoc) const {
991 switch (Type) {
992 case R_PPC_ADDR16_HA:
993 write16be(Loc, applyPPCHa(SA));
994 break;
995 case R_PPC_ADDR16_LO:
996 write16be(Loc, applyPPCLo(SA));
997 break;
998 default:
George Rimar57610422016-03-11 14:43:02 +0000999 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +00001000 }
1001}
1002
Rafael Espindolac4010882015-09-22 20:54:08 +00001003PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001004 GotRel = R_PPC64_GLOB_DAT;
1005 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001006 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001007
1008 // We need 64K pages (at least under glibc/Linux, the loader won't
1009 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001010 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001011
1012 // The PPC64 ELF ABI v1 spec, says:
1013 //
1014 // It is normally desirable to put segments with different characteristics
1015 // in separate 256 Mbyte portions of the address space, to give the
1016 // operating system full paging flexibility in the 64-bit address space.
1017 //
1018 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1019 // use 0x10000000 as the starting address.
1020 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001021}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001022
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001023uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001024 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1025 // order. The TOC starts where the first of these sections starts.
1026
1027 // FIXME: This obviously does not do the right thing when there is no .got
1028 // section, but there is a .toc or .tocbss section.
1029 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1030 if (!TocVA)
1031 TocVA = Out<ELF64BE>::Plt->getVA();
1032
1033 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1034 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1035 // code (crt1.o) assumes that you can get from the TOC base to the
1036 // start of the .toc section with only a single (signed) 16-bit relocation.
1037 return TocVA + 0x8000;
1038}
1039
Rui Ueyama9398f862016-01-29 04:15:02 +00001040void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1041 uint64_t PltEntryAddr, int32_t Index,
1042 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1044
1045 // FIXME: What we should do, in theory, is get the offset of the function
1046 // descriptor in the .opd section, and use that as the offset from %r2 (the
1047 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1048 // be a pointer to the function descriptor in the .opd section. Using
1049 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1050
Hal Finkelfa92f682015-10-13 21:47:34 +00001051 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1053 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1054 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1055 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1056 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1057 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1058 write32be(Buf + 28, 0x4e800420); // bctr
1059}
1060
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001061bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001062 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 return true;
1064
1065 switch (Type) {
1066 default: return false;
1067 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001069 case R_PPC64_GOT16_HA:
1070 case R_PPC64_GOT16_HI:
1071 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001072 case R_PPC64_GOT16_LO_DS:
1073 return true;
1074 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001075}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076
Rafael Espindola993f0272016-02-26 14:27:47 +00001077bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001079 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001080}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001081
Hal Finkelbe0823d2015-10-12 20:58:52 +00001082bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1083 switch (Type) {
1084 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001085 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001086 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001087 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001088 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001089 }
1090}
1091
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001092void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001093 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001094 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001095 uint64_t TB = getPPC64TocBase();
1096
Hal Finkel3c8cc672015-10-12 20:56:18 +00001097 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1098 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001099 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001100 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1101 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001102 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1103 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001104 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1105 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 default: break;
1107 }
1108
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001110 case R_PPC64_ADDR14: {
1111 checkAlignment<4>(SA, Type);
1112 // Preserve the AA/LK bits in the branch instruction
1113 uint8_t AALK = Loc[3];
1114 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1115 break;
1116 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001118 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001120 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001121 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001122 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001123 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001125 case R_PPC64_ADDR16_HA:
1126 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001127 break;
1128 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001132 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
1137 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
1140 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001141 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001143 case R_PPC64_ADDR16_LO:
1144 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001146 case R_PPC64_ADDR16_LO_DS:
1147 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001148 break;
1149 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001150 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001151 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001152 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001153 case R_PPC64_ADDR64:
1154 write64be(Loc, SA);
1155 break;
1156 case R_PPC64_REL16_HA:
1157 write16be(Loc, applyPPCHa(SA - P));
1158 break;
1159 case R_PPC64_REL16_HI:
1160 write16be(Loc, applyPPCHi(SA - P));
1161 break;
1162 case R_PPC64_REL16_LO:
1163 write16be(Loc, applyPPCLo(SA - P));
1164 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001165 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001166 // If we have an undefined weak symbol, we might get here with a symbol
1167 // address of zero. That could overflow, but the code must be unreachable,
1168 // so don't bother doing anything at all.
1169 if (!SA)
1170 break;
1171
Hal Finkeldaedc122015-10-12 23:16:53 +00001172 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1173 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001174 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001175
1176 if (!InPlt && Out<ELF64BE>::Opd) {
1177 // If this is a local call, and we currently have the address of a
1178 // function-descriptor, get the underlying code address instead.
1179 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1180 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001181 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001182
1183 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001184 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001185 }
1186
Hal Finkel3c8cc672015-10-12 20:56:18 +00001187 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001188 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001189 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001190
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001191 uint32_t Nop = 0x60000000;
1192 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1193 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001194 break;
1195 }
1196 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001197 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001198 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001199 break;
1200 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001201 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001202 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001203 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001204 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001205 break;
1206 default:
George Rimar57610422016-03-11 14:43:02 +00001207 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001208 }
1209}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001210
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001211AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001212 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001213 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001214 IRelativeRel = R_AARCH64_IRELATIVE;
1215 GotRel = R_AARCH64_GLOB_DAT;
1216 PltRel = R_AARCH64_JUMP_SLOT;
1217 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001218 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1219 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001220 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001221 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001222 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001223}
George Rimar648a2c32015-10-20 08:54:27 +00001224
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001225bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001226 switch (Type) {
1227 default:
1228 return false;
1229 case R_AARCH64_PREL32:
1230 case R_AARCH64_ADR_PREL_LO21:
1231 case R_AARCH64_ADR_PREL_PG_HI21:
1232 case R_AARCH64_ADR_GOT_PAGE:
1233 case R_AARCH64_LDST8_ABS_LO12_NC:
1234 case R_AARCH64_LDST16_ABS_LO12_NC:
1235 case R_AARCH64_LDST32_ABS_LO12_NC:
1236 case R_AARCH64_LDST64_ABS_LO12_NC:
1237 case R_AARCH64_LDST128_ABS_LO12_NC:
1238 case R_AARCH64_ADD_ABS_LO12_NC:
1239 case R_AARCH64_CALL26:
1240 case R_AARCH64_JUMP26:
1241 case R_AARCH64_CONDBR19:
1242 case R_AARCH64_TSTBR14:
1243 return true;
1244 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001245}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001246
George Rimar98b060d2016-03-06 06:01:07 +00001247bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001248 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1249 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1250 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1251 Type == R_AARCH64_TLSDESC_CALL;
1252}
1253
George Rimar98b060d2016-03-06 06:01:07 +00001254bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001255 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1256 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1257}
1258
George Rimar98b060d2016-03-06 06:01:07 +00001259uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001260 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1261 return Type;
1262 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001263 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001264 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001265 // Keep it going with a dummy value so that we can find more reloc errors.
1266 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001267}
1268
Rui Ueyamac516ae12016-01-29 02:33:45 +00001269void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001270 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1271}
1272
Rui Ueyama900e2d22016-01-29 03:51:49 +00001273void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001274 const uint8_t PltData[] = {
1275 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1276 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1277 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1278 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1279 0x20, 0x02, 0x1f, 0xd6, // br x17
1280 0x1f, 0x20, 0x03, 0xd5, // nop
1281 0x1f, 0x20, 0x03, 0xd5, // nop
1282 0x1f, 0x20, 0x03, 0xd5 // nop
1283 };
1284 memcpy(Buf, PltData, sizeof(PltData));
1285
Rui Ueyama900e2d22016-01-29 03:51:49 +00001286 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1287 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1288 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1289 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1290 Got + 16);
1291 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1292 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001293}
1294
Rui Ueyama9398f862016-01-29 04:15:02 +00001295void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1296 uint64_t PltEntryAddr, int32_t Index,
1297 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001298 const uint8_t Inst[] = {
1299 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1300 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1301 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1302 0x20, 0x02, 0x1f, 0xd6 // br x17
1303 };
1304 memcpy(Buf, Inst, sizeof(Inst));
1305
1306 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1307 GotEntryAddr);
1308 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1309 GotEntryAddr);
1310 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1311 GotEntryAddr);
1312}
1313
George Rimar98b060d2016-03-06 06:01:07 +00001314uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001315 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001316 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1317 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001318}
1319
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001320bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001321 switch (Type) {
1322 default:
1323 return false;
1324 case R_AARCH64_ABS16:
1325 case R_AARCH64_ABS32:
1326 case R_AARCH64_ABS64:
1327 case R_AARCH64_ADD_ABS_LO12_NC:
1328 case R_AARCH64_ADR_PREL_LO21:
1329 case R_AARCH64_ADR_PREL_PG_HI21:
1330 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001331 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001332 case R_AARCH64_LDST32_ABS_LO12_NC:
1333 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001334 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001335 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001336 }
1337}
1338
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001339bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001340 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001341 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001342 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001343 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001344 case R_AARCH64_ADR_GOT_PAGE:
1345 case R_AARCH64_LD64_GOT_LO12_NC:
1346 return true;
1347 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001348 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001349 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001350}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001351
Rafael Espindola993f0272016-02-26 14:27:47 +00001352bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001353 switch (Type) {
1354 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001355 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001356 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001357 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001358 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001359 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001360 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001361 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001362}
Davide Italiano1d750a62015-09-27 08:45:38 +00001363
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001364static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001365 uint32_t ImmLo = (Imm & 0x3) << 29;
1366 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1367 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001368 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001369}
1370
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001371static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1372 or32le(L, (Imm & 0xFFF) << 10);
1373}
1374
Davide Italiano318ca222015-10-02 22:13:51 +00001375// Page(Expr) is the page address of the expression Expr, defined
1376// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001377// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001378static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001379 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001380}
1381
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001382void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001383 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001384 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001385 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001386 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001387 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001388 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001389 break;
1390 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001391 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001392 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001393 break;
1394 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001395 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001396 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001397 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001398 // This relocation stores 12 bits and there's no instruction
1399 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001400 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1401 // bits in Loc are zero.
1402 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001403 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001404 case R_AARCH64_ADR_GOT_PAGE: {
1405 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1406 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001407 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001408 break;
1409 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001410 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001411 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001412 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001413 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001414 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001415 }
George Rimar3d737e42016-01-13 13:04:46 +00001416 case R_AARCH64_ADR_PREL_PG_HI21:
1417 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001418 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001419 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001420 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001421 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001422 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001423 case R_AARCH64_CALL26:
1424 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001425 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001426 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001427 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1428 break;
1429 }
George Rimar4102bfb2016-01-11 14:22:00 +00001430 case R_AARCH64_CONDBR19: {
1431 uint64_t X = SA - P;
1432 checkInt<21>(X, Type);
1433 or32le(Loc, (X & 0x1FFFFC) << 3);
1434 break;
1435 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001436 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001437 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001438 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001439 or32le(Loc, (SA & 0xFF8) << 7);
1440 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001441 case R_AARCH64_LDST128_ABS_LO12_NC:
1442 or32le(Loc, (SA & 0x0FF8) << 6);
1443 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001444 case R_AARCH64_LDST16_ABS_LO12_NC:
1445 or32le(Loc, (SA & 0x0FFC) << 9);
1446 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001447 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001448 or32le(Loc, (SA & 0xFFF) << 10);
1449 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001450 case R_AARCH64_LDST32_ABS_LO12_NC:
1451 or32le(Loc, (SA & 0xFFC) << 8);
1452 break;
1453 case R_AARCH64_LDST64_ABS_LO12_NC:
1454 or32le(Loc, (SA & 0xFF8) << 7);
1455 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001456 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001457 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001458 write16le(Loc, SA - P);
1459 break;
1460 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001461 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001462 write32le(Loc, SA - P);
1463 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001464 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001465 write64le(Loc, SA - P);
1466 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001467 case R_AARCH64_TSTBR14: {
1468 uint64_t X = SA - P;
1469 checkInt<16>(X, Type);
1470 or32le(Loc, (X & 0xFFFC) << 3);
1471 break;
1472 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001473 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1474 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1475 checkInt<24>(V, Type);
1476 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1477 break;
1478 }
1479 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1480 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1481 updateAArch64Add(Loc, V & 0xFFF);
1482 break;
1483 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001484 default:
George Rimar57610422016-03-11 14:43:02 +00001485 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001486 }
1487}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001488
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001489size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001490 uint32_t Type, uint64_t P,
1491 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001492 // TLSDESC Global-Dynamic relocation are in the form:
1493 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1494 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1495 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1496 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1497 // And it can optimized to:
1498 // movz x0, #0x0, lsl #16
1499 // movk x0, #0x10
1500 // nop
1501 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001502 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1503 uint64_t X = SA + TPOff;
1504 checkUInt<32>(X, Type);
1505
1506 uint32_t NewInst;
1507 switch (Type) {
1508 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1509 case R_AARCH64_TLSDESC_CALL:
1510 // nop
1511 NewInst = 0xd503201f;
1512 break;
1513 case R_AARCH64_TLSDESC_ADR_PAGE21:
1514 // movz
1515 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1516 break;
1517 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1518 // movk
1519 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1520 break;
1521 default:
George Rimar777f9632016-03-12 08:31:34 +00001522 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001523 }
1524 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001525
1526 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001527}
1528
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001529size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001530 uint32_t Type, uint64_t P,
1531 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001532 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1533 uint64_t X = SA + TPOff;
1534 checkUInt<32>(X, Type);
1535
George Rimar4d1d16d2016-03-06 06:16:05 +00001536 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001537 uint32_t NewInst;
1538 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1539 // Generate movz.
1540 unsigned RegNo = (Inst & 0x1f);
1541 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1542 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1543 // Generate movk
1544 unsigned RegNo = (Inst & 0x1f);
1545 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1546 } else {
George Rimar777f9632016-03-12 08:31:34 +00001547 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001548 }
1549 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001550
1551 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001552}
1553
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001554// Implementing relocations for AMDGPU is low priority since most
1555// programs don't use relocations now. Thus, this function is not
1556// actually called (relocateOne is called for each relocation).
1557// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001558void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1559 uint64_t P, uint64_t SA, uint64_t ZA,
1560 uint8_t *PairedLoc) const {
George Rimar57610422016-03-11 14:43:02 +00001561 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001562}
1563
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001564template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001565 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001566 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001567 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001568 PltEntrySize = 16;
1569 PltZeroSize = 32;
1570 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001571 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001572 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001573 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001574}
1575
1576template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001577uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001578 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1579 return R_MIPS_REL32;
1580 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001581 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001582 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001583 // Keep it going with a dummy value so that we can find more reloc errors.
1584 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001585}
1586
1587template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001588void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001589 typedef typename ELFT::Off Elf_Off;
1590 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001591
1592 // Set the MSB of the second GOT slot. This is not required by any
1593 // MIPS ABI documentation, though.
1594 //
1595 // There is a comment in glibc saying that "The MSB of got[1] of a
1596 // gnu object is set to identify gnu objects," and in GNU gold it
1597 // says "the second entry will be used by some runtime loaders".
1598 // But how this field is being used is unclear.
1599 //
1600 // We are not really willing to mimic other linkers behaviors
1601 // without understanding why they do that, but because all files
1602 // generated by GNU tools have this special GOT value, and because
1603 // we've been doing this for years, it is probably a safe bet to
1604 // keep doing this for now. We really need to revisit this to see
1605 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001606 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001607 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001608}
1609
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001610template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001611void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1612 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001613}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001614
Simon Atanasyan35031192015-12-15 06:06:34 +00001615static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001616
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001617template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001618static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001619 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001620 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001621 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001622 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1623 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001624 checkAlignment<(1 << SHIFT)>(S + A, Type);
1625 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001626 checkInt<BSIZE + SHIFT>(V, Type);
1627 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001628}
1629
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001630template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001631static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001632 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001633 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001634}
1635
Simon Atanasyan3b377852016-03-04 10:55:20 +00001636template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001637static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1638 uint32_t Instr = read32<E>(Loc);
1639 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1640}
1641
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001642template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1643 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1644}
1645
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001646template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001647static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001648 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001649}
1650
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001651template <class ELFT>
1652void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1653 const endianness E = ELFT::TargetEndianness;
1654 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1655 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1656 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1657 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1658 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1659 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1660 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1661 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1662 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001663 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001664 writeMipsLo16<E>(Buf + 4, Got);
1665 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001666}
1667
1668template <class ELFT>
1669void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1670 uint64_t PltEntryAddr, int32_t Index,
1671 unsigned RelOff) const {
1672 const endianness E = ELFT::TargetEndianness;
1673 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1674 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1675 write32<E>(Buf + 8, 0x03200008); // jr $25
1676 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001677 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001678 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1679 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001680}
1681
1682template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001683bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001684 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001685}
1686
1687template <class ELFT>
1688bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001689 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001690}
1691
1692template <class ELFT>
1693bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1694 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001695}
1696
1697template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001698bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1699 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001700}
1701
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001702template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001703void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001704 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001705 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001706 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001707 // Thread pointer and DRP offsets from the start of TLS data area.
1708 // https://www.linux-mips.org/wiki/NPTL
1709 const uint32_t TPOffset = 0x7000;
1710 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001711 switch (Type) {
1712 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001713 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001714 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001715 case R_MIPS_26: {
1716 uint32_t Instr = read32<E>(Loc);
1717 // FIXME (simon): If the relocation target symbol is not a PLT entry
1718 // we should use another expression for calculation:
1719 // ((A << 2) | (P & 0xf0000000)) >> 2
1720 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1721 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1722 break;
1723 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001724 case R_MIPS_CALL16:
1725 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001726 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001727 if (Type == R_MIPS_GOT16)
1728 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001729 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001730 break;
1731 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001732 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001733 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001734 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001735 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001736 break;
1737 }
1738 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001739 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001740 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001741 case R_MIPS_HI16:
1742 if (PairedLoc)
1743 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1744 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001745 warning("can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001746 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001747 }
1748 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001749 case R_MIPS_JALR:
1750 // Ignore this optimization relocation for now
1751 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001752 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001753 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001754 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001755 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001756 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001757 break;
1758 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001759 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001760 break;
1761 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001762 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001763 break;
1764 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001765 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001766 break;
1767 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001768 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001769 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001770 case R_MIPS_PCHI16:
1771 if (PairedLoc)
1772 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1773 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001774 warning("can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001775 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001776 }
1777 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001778 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001779 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001781 case R_MIPS_TLS_DTPREL_HI16:
1782 writeMipsHi16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1783 break;
1784 case R_MIPS_TLS_DTPREL_LO16:
1785 writeMipsLo16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1786 break;
1787 case R_MIPS_TLS_TPREL_HI16:
1788 writeMipsHi16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1789 break;
1790 case R_MIPS_TLS_TPREL_LO16:
1791 writeMipsLo16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1792 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001793 default:
George Rimar57610422016-03-11 14:43:02 +00001794 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001795 }
1796}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001797
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001798template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001799bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001800 return Type == R_MIPS_JALR;
1801}
1802
1803template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001804bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1805 switch (Type) {
1806 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001807 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001808 case R_MIPS_26:
1809 case R_MIPS_32:
1810 case R_MIPS_64:
1811 case R_MIPS_HI16:
1812 case R_MIPS_LO16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001813 case R_MIPS_TLS_DTPREL_HI16:
1814 case R_MIPS_TLS_DTPREL_LO16:
1815 case R_MIPS_TLS_TPREL_HI16:
1816 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001817 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001818 }
1819}
1820
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001821// _gp is a MIPS-specific ABI-defined symbol which points to
1822// a location that is relative to GOT. This function returns
1823// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001824template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001825 unsigned GPOffset = 0x7ff0;
1826 if (uint64_t V = Out<ELFT>::Got->getVA())
1827 return V + GPOffset;
1828 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001829}
1830
1831template uint32_t getMipsGpAddr<ELF32LE>();
1832template uint32_t getMipsGpAddr<ELF32BE>();
1833template uint64_t getMipsGpAddr<ELF64LE>();
1834template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001835
1836template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1837 const SymbolBody &) const;
1838template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1839 const SymbolBody &) const;
1840template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1841 const SymbolBody &) const;
1842template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1843 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001844}
1845}