blob: 0cf8de91accdd994b28c4a2ada3e1425af41d508 [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
303template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000304TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
305 const SymbolBody &S) const {
Rui Ueyama7ede5432016-03-13 04:40:14 +0000306 if (S.isGnuIfunc<ELFT>())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000307 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000308 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000309 return Plt_Explicit;
310
311 // This handles a non PIC program call to function in a shared library.
312 // In an ideal world, we could just report an error saying the relocation
313 // can overflow at runtime.
314 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
315 // libc.so.
316 //
317 // The general idea on how to handle such cases is to create a PLT entry
318 // and use that as the function value.
319 //
320 // For the static linking part, we just return true and everything else
321 // will use the the PLT entry as the address.
322 //
323 // The remaining problem is making sure pointer equality still works. We
324 // need the help of the dynamic linker for that. We let it know that we have
325 // a direct reference to a so symbol by creating an undefined symbol with a
326 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
327 // the value of the symbol we created. This is true even for got entries, so
328 // pointer equality is maintained. To avoid an infinite loop, the only entry
329 // that points to the real function is a dedicated got entry used by the
330 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
331 // R_386_JMP_SLOT, etc).
332 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
George Rimar786e8662016-03-17 05:57:33 +0000333 if (!Config->Pic && SS->Sym.getType() == STT_FUNC &&
Rafael Espindola005d8442016-03-03 18:44:38 +0000334 !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000335 return Plt_Implicit;
336
Rafael Espindola852860e2016-02-12 15:47:37 +0000337 return Plt_No;
338}
Rui Ueyama012eb782016-01-29 04:05:09 +0000339
George Rimar98b060d2016-03-06 06:01:07 +0000340bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000341
George Rimar98b060d2016-03-06 06:01:07 +0000342bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000343 return false;
344}
345
George Rimar98b060d2016-03-06 06:01:07 +0000346bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000347
George Rimar98b060d2016-03-06 06:01:07 +0000348bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000349 return false;
350}
351
George Rimar7b8850f2016-03-06 06:09:50 +0000352size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
353 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000354 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000355 if (isTlsGlobalDynamicRel(Type)) {
356 if (S.isPreemptible())
357 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000358 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000359 }
360 if (isTlsLocalDynamicRel(Type))
361 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
362 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000363 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000364}
365
366size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000367 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000368 llvm_unreachable("Should not have claimed to be relaxable");
369}
370
371size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
372 uint64_t P, uint64_t SA) const {
373 llvm_unreachable("Should not have claimed to be relaxable");
374}
375
376size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000377 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000378 llvm_unreachable("Should not have claimed to be relaxable");
379}
380
381size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
382 uint64_t P, uint64_t SA) const {
383 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000384}
George Rimar77d1cb12015-11-24 09:00:06 +0000385
Rafael Espindola7f074422015-09-22 21:35:51 +0000386X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000387 CopyRel = R_386_COPY;
388 GotRel = R_386_GLOB_DAT;
389 PltRel = R_386_JUMP_SLOT;
390 IRelativeRel = R_386_IRELATIVE;
391 RelativeRel = R_386_RELATIVE;
392 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000393 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
394 TlsOffsetRel = R_386_TLS_DTPOFF32;
395 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000396 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000397 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000398}
399
Rafael Espindolaffcad442016-03-23 14:58:25 +0000400bool X86TargetInfo::isRelRelative(uint32_t Type) const {
401 switch (Type) {
402 default:
403 return false;
404 case R_386_PC32:
405 case R_386_PLT32:
406 case R_386_TLS_LDO_32:
407 return true;
408 }
409}
410
Rui Ueyamac516ae12016-01-29 02:33:45 +0000411void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000412 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
413}
414
Rui Ueyamac516ae12016-01-29 02:33:45 +0000415void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000416 // Entries in .got.plt initially points back to the corresponding
417 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000418 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000419}
Rafael Espindola01205f72015-09-22 18:19:46 +0000420
George Rimar98b060d2016-03-06 06:01:07 +0000421uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000422 if (Type == R_386_TLS_LE)
423 return R_386_TLS_TPOFF;
424 if (Type == R_386_TLS_LE_32)
425 return R_386_TLS_TPOFF32;
426 return Type;
427}
428
George Rimar98b060d2016-03-06 06:01:07 +0000429uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000430 if (Type == R_386_TLS_IE)
431 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000432 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000433}
434
George Rimar98b060d2016-03-06 06:01:07 +0000435bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000436 return Type == R_386_TLS_GD;
437}
438
George Rimar98b060d2016-03-06 06:01:07 +0000439bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000440 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
441}
442
George Rimar98b060d2016-03-06 06:01:07 +0000443bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000444 return Type == R_386_TLS_LDM;
445}
446
George Rimar98b060d2016-03-06 06:01:07 +0000447bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000448 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
449}
450
Rui Ueyama900e2d22016-01-29 03:51:49 +0000451void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000452 // Executable files and shared object files have
453 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000454 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000455 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000456 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000457 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
458 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000459 };
460 memcpy(Buf, V, sizeof(V));
461 return;
462 }
George Rimar648a2c32015-10-20 08:54:27 +0000463
George Rimar77b77792015-11-25 22:15:01 +0000464 const uint8_t PltData[] = {
465 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000466 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
467 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000468 };
469 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000470 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000471 write32le(Buf + 2, Got + 4);
472 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000473}
474
Rui Ueyama9398f862016-01-29 04:15:02 +0000475void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
476 uint64_t PltEntryAddr, int32_t Index,
477 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000478 const uint8_t Inst[] = {
479 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
480 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
481 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
482 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000483 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000484
George Rimar77b77792015-11-25 22:15:01 +0000485 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000486 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000487 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
488 : Out<ELF32LE>::Got->getVA();
489 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000490 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000491 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000492}
493
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000494bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
495 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000496}
497
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000498bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000499 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000500 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000501 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000502 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000503 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000504}
505
Rafael Espindola993f0272016-02-26 14:27:47 +0000506bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
507 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000508}
509
George Rimarbfb7bf72015-12-21 10:00:12 +0000510bool X86TargetInfo::isGotRelative(uint32_t Type) const {
511 // This relocation does not require got entry,
512 // but it is relative to got and needs it to be created.
513 // Here we request for that.
514 return Type == R_386_GOTOFF;
515}
516
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000517bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
518 return Type == R_386_GOT32;
519}
520
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000521void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000522 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000523 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000524 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000525 case R_386_32:
526 add32le(Loc, SA);
527 break;
George Rimarffb67352016-01-19 11:00:48 +0000528 case R_386_GOT32: {
529 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
530 Out<ELF32LE>::Got->getNumEntries() * 4;
531 checkInt<32>(V, Type);
532 add32le(Loc, V);
533 break;
534 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000535 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000536 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000537 break;
George Rimar13934772015-11-25 20:20:31 +0000538 case R_386_GOTPC:
539 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
540 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000541 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000542 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000543 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000544 break;
George Rimar9db204a2015-12-02 09:58:20 +0000545 case R_386_TLS_GD:
546 case R_386_TLS_LDM:
547 case R_386_TLS_TPOFF: {
548 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
549 Out<ELF32LE>::Got->getNumEntries() * 4;
550 checkInt<32>(V, Type);
551 write32le(Loc, V);
552 break;
553 }
George Rimar6f17e092015-12-17 09:32:21 +0000554 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000555 case R_386_TLS_LDO_32:
556 write32le(Loc, SA);
557 break;
George Rimard23970f2015-11-25 20:41:53 +0000558 case R_386_TLS_LE:
559 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
560 break;
561 case R_386_TLS_LE_32:
562 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
563 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000564 default:
George Rimar57610422016-03-11 14:43:02 +0000565 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000566 }
567}
568
George Rimar98b060d2016-03-06 06:01:07 +0000569bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000570 return Config->Shared && Type == R_386_TLS_IE;
571}
572
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000573size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000574 uint32_t Type, uint64_t P,
575 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000576 // GD can be optimized to LE:
577 // leal x@tlsgd(, %ebx, 1),
578 // call __tls_get_addr@plt
579 // Can be converted to:
580 // movl %gs:0,%eax
581 // addl $x@ntpoff,%eax
582 // But gold emits subl $foo@tpoff,%eax instead of addl.
583 // These instructions are completely equal in behavior.
584 // This method generates subl to be consistent with gold.
585 const uint8_t Inst[] = {
586 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
587 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
588 };
589 memcpy(Loc - 3, Inst, sizeof(Inst));
590 relocateOne(Loc + 5, BufEnd, R_386_32, P,
591 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
592
593 // The next relocation should be against __tls_get_addr, so skip it
594 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000595}
596
597// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
598// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
599// how GD can be optimized to IE:
600// leal x@tlsgd(, %ebx, 1),
601// call __tls_get_addr@plt
602// Is converted to:
603// movl %gs:0, %eax
604// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000605size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
606 uint32_t Type, uint64_t P,
607 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000608 const uint8_t Inst[] = {
609 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
610 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
611 };
612 memcpy(Loc - 3, Inst, sizeof(Inst));
613 relocateOne(Loc + 5, BufEnd, R_386_32, P,
614 SA - Out<ELF32LE>::Got->getVA() -
615 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000616
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000617 // The next relocation should be against __tls_get_addr, so skip it
618 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000619}
620
George Rimar6f17e092015-12-17 09:32:21 +0000621// In some conditions, relocations can be optimized to avoid using GOT.
622// This function does that for Initial Exec to Local Exec case.
623// Read "ELF Handling For Thread-Local Storage, 5.1
624// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000625// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000626
627size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000628 uint32_t Type, uint64_t P,
629 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000630 // Ulrich's document section 6.2 says that @gotntpoff can
631 // be used with MOVL or ADDL instructions.
632 // @indntpoff is similar to @gotntpoff, but for use in
633 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000634 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000635 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000636 uint8_t Reg = (Loc[-1] >> 3) & 7;
637 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000638 if (Type == R_386_TLS_IE) {
639 // For R_386_TLS_IE relocation we perform the next transformations:
640 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
641 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
642 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
643 // First one is special because when EAX is used the sequence is 5 bytes
644 // long, otherwise it is 6 bytes.
645 if (*Op == 0xa1) {
646 *Op = 0xb8;
647 } else {
648 *Inst = IsMov ? 0xc7 : 0x81;
649 *Op = 0xc0 | ((*Op >> 3) & 7);
650 }
651 } else {
652 // R_386_TLS_GOTIE relocation can be optimized to
653 // R_386_TLS_LE so that it does not use GOT.
654 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
655 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
656 // Note: gold converts to ADDL instead of LEAL.
657 *Inst = IsMov ? 0xc7 : 0x8d;
658 if (IsMov)
659 *Op = 0xc0 | ((*Op >> 3) & 7);
660 else
661 *Op = 0x80 | Reg | (Reg << 3);
662 }
George Rimar2558e122015-12-09 09:55:54 +0000663 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000664
665 return 0;
666}
667
668size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
669 uint32_t Type, uint64_t P,
670 uint64_t SA) const {
671 if (Type == R_386_TLS_LDO_32) {
672 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
673 return 0;
674 }
675
676 // LD can be optimized to LE:
677 // leal foo(%reg),%eax
678 // call ___tls_get_addr
679 // Is converted to:
680 // movl %gs:0,%eax
681 // nop
682 // leal 0(%esi,1),%esi
683 const uint8_t Inst[] = {
684 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
685 0x90, // nop
686 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
687 };
688 memcpy(Loc - 2, Inst, sizeof(Inst));
689
690 // The next relocation should be against __tls_get_addr, so skip it
691 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000692}
693
Rafael Espindola7f074422015-09-22 21:35:51 +0000694X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000695 CopyRel = R_X86_64_COPY;
696 GotRel = R_X86_64_GLOB_DAT;
697 PltRel = R_X86_64_JUMP_SLOT;
698 RelativeRel = R_X86_64_RELATIVE;
699 IRelativeRel = R_X86_64_IRELATIVE;
700 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000701 TlsModuleIndexRel = R_X86_64_DTPMOD64;
702 TlsOffsetRel = R_X86_64_DTPOFF64;
703 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000704 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000705 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000706}
707
Rui Ueyamac516ae12016-01-29 02:33:45 +0000708void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000709 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
710}
711
Rui Ueyamac516ae12016-01-29 02:33:45 +0000712void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000713 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000714 write32le(Buf, Plt + 6);
715}
716
Rui Ueyama900e2d22016-01-29 03:51:49 +0000717void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000718 const uint8_t PltData[] = {
719 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
720 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
721 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
722 };
723 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000724 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
725 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
726 write32le(Buf + 2, Got - Plt + 2); // GOT+8
727 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000728}
Rafael Espindola01205f72015-09-22 18:19:46 +0000729
Rui Ueyama9398f862016-01-29 04:15:02 +0000730void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
731 uint64_t PltEntryAddr, int32_t Index,
732 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000733 const uint8_t Inst[] = {
734 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
735 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
736 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
737 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000738 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000739
George Rimar648a2c32015-10-20 08:54:27 +0000740 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
741 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000742 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000743}
744
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000745bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
746 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
747 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000748}
749
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000750bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000751 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
752 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000753}
754
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000755bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000756 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000757 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000758 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000759 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000760 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000761}
762
George Rimar98b060d2016-03-06 06:01:07 +0000763uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000764 // No other types of TLS relocations requiring GOT should
765 // reach here.
766 assert(Type == R_X86_64_GOTTPOFF);
767 return R_X86_64_PC32;
768}
769
George Rimar98b060d2016-03-06 06:01:07 +0000770bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000771 return Type == R_X86_64_GOTTPOFF;
772}
773
George Rimar98b060d2016-03-06 06:01:07 +0000774bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000775 return Type == R_X86_64_TLSGD;
776}
777
George Rimar98b060d2016-03-06 06:01:07 +0000778bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000779 return Type == R_X86_64_TLSLD;
780}
781
George Rimar98b060d2016-03-06 06:01:07 +0000782bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000783 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
784 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000785}
786
Rafael Espindola993f0272016-02-26 14:27:47 +0000787bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
788 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000789}
Rafael Espindolac4010882015-09-22 20:54:08 +0000790
Rafael Espindolaae244002015-10-05 19:30:12 +0000791bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
792 switch (Type) {
793 default:
794 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000795 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000796 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000797 case R_X86_64_PC8:
798 case R_X86_64_PC16:
799 case R_X86_64_PC32:
800 case R_X86_64_PC64:
801 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000802 return true;
803 }
804}
805
Rui Ueyamac516ae12016-01-29 02:33:45 +0000806bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000807 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000808}
809
George Rimar6713cf82015-11-25 21:46:05 +0000810// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
811// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000812// how GD can be optimized to LE:
813// .byte 0x66
814// leaq x@tlsgd(%rip), %rdi
815// .word 0x6666
816// rex64
817// call __tls_get_addr@plt
818// Is converted to:
819// mov %fs:0x0,%rax
820// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000821size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000822 uint32_t Type, uint64_t P,
823 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000824 const uint8_t Inst[] = {
825 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
826 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
827 };
828 memcpy(Loc - 4, Inst, sizeof(Inst));
829 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000830
831 // The next relocation should be against __tls_get_addr, so skip it
832 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000833}
834
George Rimar25411f252015-12-04 11:20:13 +0000835// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
836// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
837// how GD can be optimized to IE:
838// .byte 0x66
839// leaq x@tlsgd(%rip), %rdi
840// .word 0x6666
841// rex64
842// call __tls_get_addr@plt
843// Is converted to:
844// mov %fs:0x0,%rax
845// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000846size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
847 uint32_t Type, uint64_t P,
848 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000849 const uint8_t Inst[] = {
850 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
851 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
852 };
853 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000854 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000855
856 // The next relocation should be against __tls_get_addr, so skip it
857 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000858}
859
George Rimar77d1cb12015-11-24 09:00:06 +0000860// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000861// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000862// This function does that. Read "ELF Handling For Thread-Local Storage,
863// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
864// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000865size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000866 uint32_t Type, uint64_t P,
867 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000868 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
869 // used in MOVQ or ADDQ instructions only.
870 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
871 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
872 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
873 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
874 uint8_t *Prefix = Loc - 3;
875 uint8_t *Inst = Loc - 2;
876 uint8_t *RegSlot = Loc - 1;
877 uint8_t Reg = Loc[-1] >> 3;
878 bool IsMov = *Inst == 0x8b;
879 bool RspAdd = !IsMov && Reg == 4;
880 // r12 and rsp registers requires special handling.
881 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
882 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
883 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
884 // The same true for rsp. So we convert to addq for them, saving 1 byte that
885 // we dont have.
886 if (RspAdd)
887 *Inst = 0x81;
888 else
889 *Inst = IsMov ? 0xc7 : 0x8d;
890 if (*Prefix == 0x4c)
891 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
892 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
893 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000894 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000895}
896
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000897// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
898// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
899// how LD can be optimized to LE:
900// leaq bar@tlsld(%rip), %rdi
901// callq __tls_get_addr@PLT
902// leaq bar@dtpoff(%rax), %rcx
903// Is converted to:
904// .word 0x6666
905// .byte 0x66
906// mov %fs:0,%rax
907// leaq bar@tpoff(%rax), %rcx
908size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
909 uint32_t Type, uint64_t P,
910 uint64_t SA) const {
911 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000912 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
913 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000914 }
915 if (Type == R_X86_64_DTPOFF32) {
916 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000917 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000918 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000919
920 const uint8_t Inst[] = {
921 0x66, 0x66, //.word 0x6666
922 0x66, //.byte 0x66
923 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
924 };
925 memcpy(Loc - 3, Inst, sizeof(Inst));
926 // The next relocation should be against __tls_get_addr, so skip it
927 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000928}
929
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000930void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000931 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000932 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000933 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000934 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000935 checkUInt<32>(SA, Type);
936 write32le(Loc, SA);
937 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000938 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000939 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000940 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000941 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000942 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000943 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000944 write64le(Loc, SA);
945 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000946 case R_X86_64_DTPOFF32:
947 write32le(Loc, SA);
948 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000949 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000950 case R_X86_64_GOTPCRELX:
951 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000952 case R_X86_64_PC32:
953 case R_X86_64_PLT32:
954 case R_X86_64_TLSGD:
955 case R_X86_64_TLSLD:
956 write32le(Loc, SA - P);
957 break;
George Rimar48651482015-12-11 08:59:37 +0000958 case R_X86_64_SIZE32:
959 write32le(Loc, ZA);
960 break;
961 case R_X86_64_SIZE64:
962 write64le(Loc, ZA);
963 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000964 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000965 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000966 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000967 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000968 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000969 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000970 default:
George Rimar57610422016-03-11 14:43:02 +0000971 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000972 }
973}
974
Hal Finkel3c8cc672015-10-12 20:56:18 +0000975// Relocation masks following the #lo(value), #hi(value), #ha(value),
976// #higher(value), #highera(value), #highest(value), and #highesta(value)
977// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
978// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000979static uint16_t applyPPCLo(uint64_t V) { return V; }
980static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
981static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
982static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
983static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000984static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000985static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
986
Davide Italiano8c3444362016-01-11 19:45:33 +0000987PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000988bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
989
990void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
991 uint64_t P, uint64_t SA, uint64_t ZA,
992 uint8_t *PairedLoc) const {
993 switch (Type) {
994 case R_PPC_ADDR16_HA:
995 write16be(Loc, applyPPCHa(SA));
996 break;
997 case R_PPC_ADDR16_LO:
998 write16be(Loc, applyPPCLo(SA));
999 break;
1000 default:
George Rimar57610422016-03-11 14:43:02 +00001001 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +00001002 }
1003}
1004
Rafael Espindolac4010882015-09-22 20:54:08 +00001005PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001006 GotRel = R_PPC64_GLOB_DAT;
1007 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001008 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001009
1010 // We need 64K pages (at least under glibc/Linux, the loader won't
1011 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001012 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001013
1014 // The PPC64 ELF ABI v1 spec, says:
1015 //
1016 // It is normally desirable to put segments with different characteristics
1017 // in separate 256 Mbyte portions of the address space, to give the
1018 // operating system full paging flexibility in the 64-bit address space.
1019 //
1020 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1021 // use 0x10000000 as the starting address.
1022 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001023}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001024
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001025uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001026 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1027 // order. The TOC starts where the first of these sections starts.
1028
1029 // FIXME: This obviously does not do the right thing when there is no .got
1030 // section, but there is a .toc or .tocbss section.
1031 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1032 if (!TocVA)
1033 TocVA = Out<ELF64BE>::Plt->getVA();
1034
1035 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1036 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1037 // code (crt1.o) assumes that you can get from the TOC base to the
1038 // start of the .toc section with only a single (signed) 16-bit relocation.
1039 return TocVA + 0x8000;
1040}
1041
Rui Ueyama9398f862016-01-29 04:15:02 +00001042void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1043 uint64_t PltEntryAddr, int32_t Index,
1044 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001045 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1046
1047 // FIXME: What we should do, in theory, is get the offset of the function
1048 // descriptor in the .opd section, and use that as the offset from %r2 (the
1049 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1050 // be a pointer to the function descriptor in the .opd section. Using
1051 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1052
Hal Finkelfa92f682015-10-13 21:47:34 +00001053 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001054 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1055 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1056 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1057 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1058 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1059 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1060 write32be(Buf + 28, 0x4e800420); // bctr
1061}
1062
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001063bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001064 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 return true;
1066
1067 switch (Type) {
1068 default: return false;
1069 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001070 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001071 case R_PPC64_GOT16_HA:
1072 case R_PPC64_GOT16_HI:
1073 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001074 case R_PPC64_GOT16_LO_DS:
1075 return true;
1076 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001077}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078
Rafael Espindola993f0272016-02-26 14:27:47 +00001079bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001081 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001082}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001083
Hal Finkelbe0823d2015-10-12 20:58:52 +00001084bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1085 switch (Type) {
1086 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001087 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001088 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001089 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001090 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001091 }
1092}
1093
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001094void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001095 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001096 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001097 uint64_t TB = getPPC64TocBase();
1098
Hal Finkel3c8cc672015-10-12 20:56:18 +00001099 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1100 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001101 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001102 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1103 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001104 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1105 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001106 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1107 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001108 default: break;
1109 }
1110
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001112 case R_PPC64_ADDR14: {
1113 checkAlignment<4>(SA, Type);
1114 // Preserve the AA/LK bits in the branch instruction
1115 uint8_t AALK = Loc[3];
1116 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1117 break;
1118 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001120 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001121 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001122 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001124 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001127 case R_PPC64_ADDR16_HA:
1128 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001129 break;
1130 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001131 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001134 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001135 break;
1136 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001137 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001138 break;
1139 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001140 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001141 break;
1142 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001143 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001144 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001145 case R_PPC64_ADDR16_LO:
1146 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001147 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001148 case R_PPC64_ADDR16_LO_DS:
1149 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001150 break;
1151 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001152 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001153 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001155 case R_PPC64_ADDR64:
1156 write64be(Loc, SA);
1157 break;
1158 case R_PPC64_REL16_HA:
1159 write16be(Loc, applyPPCHa(SA - P));
1160 break;
1161 case R_PPC64_REL16_HI:
1162 write16be(Loc, applyPPCHi(SA - P));
1163 break;
1164 case R_PPC64_REL16_LO:
1165 write16be(Loc, applyPPCLo(SA - P));
1166 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001167 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001168 // If we have an undefined weak symbol, we might get here with a symbol
1169 // address of zero. That could overflow, but the code must be unreachable,
1170 // so don't bother doing anything at all.
1171 if (!SA)
1172 break;
1173
Hal Finkeldaedc122015-10-12 23:16:53 +00001174 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1175 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001176 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001177
1178 if (!InPlt && Out<ELF64BE>::Opd) {
1179 // If this is a local call, and we currently have the address of a
1180 // function-descriptor, get the underlying code address instead.
1181 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1182 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001183 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001184
1185 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001186 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001187 }
1188
Hal Finkel3c8cc672015-10-12 20:56:18 +00001189 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001190 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001191 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001192
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001193 uint32_t Nop = 0x60000000;
1194 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1195 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001196 break;
1197 }
1198 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001199 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001200 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001201 break;
1202 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001203 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001204 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001205 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001206 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001207 break;
1208 default:
George Rimar57610422016-03-11 14:43:02 +00001209 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001210 }
1211}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001212
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001213AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001214 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001215 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001216 IRelativeRel = R_AARCH64_IRELATIVE;
1217 GotRel = R_AARCH64_GLOB_DAT;
1218 PltRel = R_AARCH64_JUMP_SLOT;
1219 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001220 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1221 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001222 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001223 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001224 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001225}
George Rimar648a2c32015-10-20 08:54:27 +00001226
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001227bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001228 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001229 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
Rafael Espindola57ca2702016-02-24 20:52:58 +00001230 Type == R_AARCH64_LDST32_ABS_LO12_NC ||
Rafael Espindola47ed5422016-02-24 21:48:06 +00001231 Type == R_AARCH64_LDST64_ABS_LO12_NC ||
Rafael Espindolaa598a3a2016-02-24 22:07:12 +00001232 Type == R_AARCH64_ADD_ABS_LO12_NC || Type == R_AARCH64_CALL26;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001233}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001234
George Rimar98b060d2016-03-06 06:01:07 +00001235bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001236 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1237 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1238 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1239 Type == R_AARCH64_TLSDESC_CALL;
1240}
1241
George Rimar98b060d2016-03-06 06:01:07 +00001242bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001243 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1244 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1245}
1246
George Rimar98b060d2016-03-06 06:01:07 +00001247uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001248 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1249 return Type;
1250 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001251 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001252 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001253 // Keep it going with a dummy value so that we can find more reloc errors.
1254 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001255}
1256
Rui Ueyamac516ae12016-01-29 02:33:45 +00001257void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001258 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1259}
1260
Rui Ueyama900e2d22016-01-29 03:51:49 +00001261void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001262 const uint8_t PltData[] = {
1263 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1264 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1265 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1266 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1267 0x20, 0x02, 0x1f, 0xd6, // br x17
1268 0x1f, 0x20, 0x03, 0xd5, // nop
1269 0x1f, 0x20, 0x03, 0xd5, // nop
1270 0x1f, 0x20, 0x03, 0xd5 // nop
1271 };
1272 memcpy(Buf, PltData, sizeof(PltData));
1273
Rui Ueyama900e2d22016-01-29 03:51:49 +00001274 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1275 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1276 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1277 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1278 Got + 16);
1279 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1280 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001281}
1282
Rui Ueyama9398f862016-01-29 04:15:02 +00001283void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1284 uint64_t PltEntryAddr, int32_t Index,
1285 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001286 const uint8_t Inst[] = {
1287 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1288 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1289 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1290 0x20, 0x02, 0x1f, 0xd6 // br x17
1291 };
1292 memcpy(Buf, Inst, sizeof(Inst));
1293
1294 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1295 GotEntryAddr);
1296 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1297 GotEntryAddr);
1298 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1299 GotEntryAddr);
1300}
1301
George Rimar98b060d2016-03-06 06:01:07 +00001302uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001303 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001304 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1305 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001306}
1307
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001308bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001309 switch (Type) {
1310 default:
1311 return false;
1312 case R_AARCH64_ABS16:
1313 case R_AARCH64_ABS32:
1314 case R_AARCH64_ABS64:
1315 case R_AARCH64_ADD_ABS_LO12_NC:
1316 case R_AARCH64_ADR_PREL_LO21:
1317 case R_AARCH64_ADR_PREL_PG_HI21:
1318 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001319 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001320 case R_AARCH64_LDST32_ABS_LO12_NC:
1321 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001322 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001323 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001324 }
1325}
1326
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001327bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001328 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001329 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001330 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001331 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001332 case R_AARCH64_ADR_GOT_PAGE:
1333 case R_AARCH64_LD64_GOT_LO12_NC:
1334 return true;
1335 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001336 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001337 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001338}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001339
Rafael Espindola993f0272016-02-26 14:27:47 +00001340bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001341 switch (Type) {
1342 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001343 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001344 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001345 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001346 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001347 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001348 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001349 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001350}
Davide Italiano1d750a62015-09-27 08:45:38 +00001351
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001352static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001353 uint32_t ImmLo = (Imm & 0x3) << 29;
1354 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1355 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001356 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001357}
1358
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001359static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1360 or32le(L, (Imm & 0xFFF) << 10);
1361}
1362
Davide Italiano318ca222015-10-02 22:13:51 +00001363// Page(Expr) is the page address of the expression Expr, defined
1364// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001365// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001366static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001367 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001368}
1369
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001370void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001371 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001372 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001373 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001374 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001375 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001376 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001377 break;
1378 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001379 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001380 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001381 break;
1382 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001383 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001384 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001385 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001386 // This relocation stores 12 bits and there's no instruction
1387 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001388 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1389 // bits in Loc are zero.
1390 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001391 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001392 case R_AARCH64_ADR_GOT_PAGE: {
1393 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1394 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001395 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001396 break;
1397 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001398 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001399 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001400 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001401 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001402 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001403 }
George Rimar3d737e42016-01-13 13:04:46 +00001404 case R_AARCH64_ADR_PREL_PG_HI21:
1405 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001406 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001407 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001408 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001409 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001410 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001411 case R_AARCH64_CALL26:
1412 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001413 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001414 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001415 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1416 break;
1417 }
George Rimar4102bfb2016-01-11 14:22:00 +00001418 case R_AARCH64_CONDBR19: {
1419 uint64_t X = SA - P;
1420 checkInt<21>(X, Type);
1421 or32le(Loc, (X & 0x1FFFFC) << 3);
1422 break;
1423 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001424 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001425 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001426 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001427 or32le(Loc, (SA & 0xFF8) << 7);
1428 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001429 case R_AARCH64_LDST128_ABS_LO12_NC:
1430 or32le(Loc, (SA & 0x0FF8) << 6);
1431 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001432 case R_AARCH64_LDST16_ABS_LO12_NC:
1433 or32le(Loc, (SA & 0x0FFC) << 9);
1434 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001435 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001436 or32le(Loc, (SA & 0xFFF) << 10);
1437 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001438 case R_AARCH64_LDST32_ABS_LO12_NC:
1439 or32le(Loc, (SA & 0xFFC) << 8);
1440 break;
1441 case R_AARCH64_LDST64_ABS_LO12_NC:
1442 or32le(Loc, (SA & 0xFF8) << 7);
1443 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001444 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001445 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001446 write16le(Loc, SA - P);
1447 break;
1448 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001449 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001450 write32le(Loc, SA - P);
1451 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001452 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001453 write64le(Loc, SA - P);
1454 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001455 case R_AARCH64_TSTBR14: {
1456 uint64_t X = SA - P;
1457 checkInt<16>(X, Type);
1458 or32le(Loc, (X & 0xFFFC) << 3);
1459 break;
1460 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001461 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1462 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1463 checkInt<24>(V, Type);
1464 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1465 break;
1466 }
1467 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1468 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1469 updateAArch64Add(Loc, V & 0xFFF);
1470 break;
1471 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001472 default:
George Rimar57610422016-03-11 14:43:02 +00001473 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001474 }
1475}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001476
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001477size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001478 uint32_t Type, uint64_t P,
1479 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001480 // TLSDESC Global-Dynamic relocation are in the form:
1481 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1482 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1483 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1484 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1485 // And it can optimized to:
1486 // movz x0, #0x0, lsl #16
1487 // movk x0, #0x10
1488 // nop
1489 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001490 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1491 uint64_t X = SA + TPOff;
1492 checkUInt<32>(X, Type);
1493
1494 uint32_t NewInst;
1495 switch (Type) {
1496 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1497 case R_AARCH64_TLSDESC_CALL:
1498 // nop
1499 NewInst = 0xd503201f;
1500 break;
1501 case R_AARCH64_TLSDESC_ADR_PAGE21:
1502 // movz
1503 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1504 break;
1505 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1506 // movk
1507 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1508 break;
1509 default:
George Rimar777f9632016-03-12 08:31:34 +00001510 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001511 }
1512 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001513
1514 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001515}
1516
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001517size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001518 uint32_t Type, uint64_t P,
1519 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001520 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1521 uint64_t X = SA + TPOff;
1522 checkUInt<32>(X, Type);
1523
George Rimar4d1d16d2016-03-06 06:16:05 +00001524 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001525 uint32_t NewInst;
1526 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1527 // Generate movz.
1528 unsigned RegNo = (Inst & 0x1f);
1529 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1530 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1531 // Generate movk
1532 unsigned RegNo = (Inst & 0x1f);
1533 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1534 } else {
George Rimar777f9632016-03-12 08:31:34 +00001535 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001536 }
1537 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001538
1539 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001540}
1541
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001542// Implementing relocations for AMDGPU is low priority since most
1543// programs don't use relocations now. Thus, this function is not
1544// actually called (relocateOne is called for each relocation).
1545// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001546void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1547 uint64_t P, uint64_t SA, uint64_t ZA,
1548 uint8_t *PairedLoc) const {
George Rimar57610422016-03-11 14:43:02 +00001549 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001550}
1551
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001552template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001553 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001554 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001555 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001556 PltEntrySize = 16;
1557 PltZeroSize = 32;
1558 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001559 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001560 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001561 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001562}
1563
1564template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001565uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001566 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1567 return R_MIPS_REL32;
1568 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001569 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001570 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001571 // Keep it going with a dummy value so that we can find more reloc errors.
1572 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001573}
1574
1575template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001576void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001577 typedef typename ELFT::Off Elf_Off;
1578 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001579
1580 // Set the MSB of the second GOT slot. This is not required by any
1581 // MIPS ABI documentation, though.
1582 //
1583 // There is a comment in glibc saying that "The MSB of got[1] of a
1584 // gnu object is set to identify gnu objects," and in GNU gold it
1585 // says "the second entry will be used by some runtime loaders".
1586 // But how this field is being used is unclear.
1587 //
1588 // We are not really willing to mimic other linkers behaviors
1589 // without understanding why they do that, but because all files
1590 // generated by GNU tools have this special GOT value, and because
1591 // we've been doing this for years, it is probably a safe bet to
1592 // keep doing this for now. We really need to revisit this to see
1593 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001594 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001595 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001596}
1597
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001598template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001599void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1600 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001601}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001602
Simon Atanasyan35031192015-12-15 06:06:34 +00001603static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001604
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001605template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001606static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001607 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001608 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001609 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001610 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1611 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001612 checkAlignment<(1 << SHIFT)>(S + A, Type);
1613 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001614 checkInt<BSIZE + SHIFT>(V, Type);
1615 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001616}
1617
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001618template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001619static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001620 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001621 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001622}
1623
Simon Atanasyan3b377852016-03-04 10:55:20 +00001624template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001625static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1626 uint32_t Instr = read32<E>(Loc);
1627 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1628}
1629
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001630template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1631 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1632}
1633
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001634template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001635static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001636 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001637}
1638
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001639template <class ELFT>
1640void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1641 const endianness E = ELFT::TargetEndianness;
1642 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1643 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1644 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1645 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1646 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1647 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1648 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1649 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1650 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001651 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001652 writeMipsLo16<E>(Buf + 4, Got);
1653 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001654}
1655
1656template <class ELFT>
1657void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1658 uint64_t PltEntryAddr, int32_t Index,
1659 unsigned RelOff) const {
1660 const endianness E = ELFT::TargetEndianness;
1661 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1662 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1663 write32<E>(Buf + 8, 0x03200008); // jr $25
1664 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001665 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001666 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1667 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001668}
1669
1670template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001671bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001672 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001673}
1674
1675template <class ELFT>
1676bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyand040a582016-02-25 16:19:15 +00001677 return needsPlt<ELFT>(Type, S) || refersToGotEntry(Type);
1678}
1679
1680template <class ELFT>
1681bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1682 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001683}
1684
1685template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001686bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1687 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001688}
1689
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001690template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001691void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001692 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001693 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001694 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001695 // Thread pointer and DRP offsets from the start of TLS data area.
1696 // https://www.linux-mips.org/wiki/NPTL
1697 const uint32_t TPOffset = 0x7000;
1698 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001699 switch (Type) {
1700 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001701 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001702 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001703 case R_MIPS_26: {
1704 uint32_t Instr = read32<E>(Loc);
1705 // FIXME (simon): If the relocation target symbol is not a PLT entry
1706 // we should use another expression for calculation:
1707 // ((A << 2) | (P & 0xf0000000)) >> 2
1708 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1709 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1710 break;
1711 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001712 case R_MIPS_CALL16:
1713 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001714 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001715 if (Type == R_MIPS_GOT16)
1716 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001717 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001718 break;
1719 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001720 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001721 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001722 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001723 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001724 break;
1725 }
1726 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001727 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001728 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001729 case R_MIPS_HI16:
1730 if (PairedLoc)
1731 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1732 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001733 warning("can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001734 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001735 }
1736 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001737 case R_MIPS_JALR:
1738 // Ignore this optimization relocation for now
1739 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001740 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001741 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001742 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001743 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001744 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001745 break;
1746 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001747 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001748 break;
1749 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001750 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001751 break;
1752 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001753 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001754 break;
1755 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001756 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001757 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001758 case R_MIPS_PCHI16:
1759 if (PairedLoc)
1760 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1761 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001762 warning("can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001763 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001764 }
1765 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001766 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001767 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001768 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001769 case R_MIPS_TLS_DTPREL_HI16:
1770 writeMipsHi16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1771 break;
1772 case R_MIPS_TLS_DTPREL_LO16:
1773 writeMipsLo16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1774 break;
1775 case R_MIPS_TLS_TPREL_HI16:
1776 writeMipsHi16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1777 break;
1778 case R_MIPS_TLS_TPREL_LO16:
1779 writeMipsLo16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1780 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001781 default:
George Rimar57610422016-03-11 14:43:02 +00001782 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001783 }
1784}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001785
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001786template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001787bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001788 return Type == R_MIPS_JALR;
1789}
1790
1791template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001792bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1793 switch (Type) {
1794 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001795 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001796 case R_MIPS_26:
1797 case R_MIPS_32:
1798 case R_MIPS_64:
1799 case R_MIPS_HI16:
1800 case R_MIPS_LO16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001801 case R_MIPS_TLS_DTPREL_HI16:
1802 case R_MIPS_TLS_DTPREL_LO16:
1803 case R_MIPS_TLS_TPREL_HI16:
1804 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001805 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001806 }
1807}
1808
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001809// _gp is a MIPS-specific ABI-defined symbol which points to
1810// a location that is relative to GOT. This function returns
1811// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001812template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001813 unsigned GPOffset = 0x7ff0;
1814 if (uint64_t V = Out<ELFT>::Got->getVA())
1815 return V + GPOffset;
1816 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001817}
1818
1819template uint32_t getMipsGpAddr<ELF32LE>();
1820template uint32_t getMipsGpAddr<ELF32BE>();
1821template uint64_t getMipsGpAddr<ELF64LE>();
1822template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001823
1824template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1825 const SymbolBody &) const;
1826template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1827 const SymbolBody &) const;
1828template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1829 const SymbolBody &) const;
1830template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1831 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001832
1833template TargetInfo::PltNeed
1834TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1835template TargetInfo::PltNeed
1836TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1837template TargetInfo::PltNeed
1838TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1839template TargetInfo::PltNeed
1840TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001841}
1842}