blob: 4f87aac261eff1bfeee518424a74481974f58b39 [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 Rimar86971052016-03-29 08:35:42 +0000113 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000114 uint32_t getTlsGotRel(uint32_t Type) const override;
115 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
116 bool isTlsLocalDynamicRel(uint32_t Type) const override;
117 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
118 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000119 void writeGotPltHeader(uint8_t *Buf) const override;
120 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000121 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000122 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
123 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000124 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000125 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000126 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000127 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000128 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000129 uint64_t SA, uint64_t ZA = 0,
130 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000131 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000132 bool isSizeRel(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000133
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000134 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
135 uint64_t P, uint64_t SA) const override;
136 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000137 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000138 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000139 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000140 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
141 uint64_t P, uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000142};
143
Davide Italiano8c3444362016-01-11 19:45:33 +0000144class PPCTargetInfo final : public TargetInfo {
145public:
146 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000147 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
148 uint64_t SA, uint64_t ZA = 0,
149 uint8_t *PairedLoc = nullptr) const override;
150 bool isRelRelative(uint32_t Type) const override;
151};
152
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000153class PPC64TargetInfo final : public TargetInfo {
154public:
155 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000156 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
157 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000158 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000159 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000160 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000161 uint64_t SA, uint64_t ZA = 0,
162 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000163 bool isRelRelative(uint32_t Type) const override;
164};
165
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000166class AArch64TargetInfo final : public TargetInfo {
167public:
168 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000169 uint32_t getDynRel(uint32_t Type) const override;
170 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
171 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000172 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000173 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000174 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
175 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000176 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000177 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000178 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000179 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000180 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000181 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000182 uint64_t SA, uint64_t ZA = 0,
183 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000184
185 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000186 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000187 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000188 uint64_t P, uint64_t SA) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000189
190private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000191 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000192};
193
Tom Stellard80efb162016-01-07 03:59:08 +0000194class AMDGPUTargetInfo final : public TargetInfo {
195public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000196 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000197 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
198 uint64_t SA, uint64_t ZA = 0,
199 uint8_t *PairedLoc = nullptr) const override;
200};
201
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000202template <class ELFT> class MipsTargetInfo final : public TargetInfo {
203public:
204 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000205 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000206 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
207 void writePltZero(uint8_t *Buf) const override;
208 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
209 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000210 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000211 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000212 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000213 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000214 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000215 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000216 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000217 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000218 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000219 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000220};
221} // anonymous namespace
222
Rui Ueyama91004392015-10-13 16:08:15 +0000223TargetInfo *createTarget() {
224 switch (Config->EMachine) {
225 case EM_386:
226 return new X86TargetInfo();
227 case EM_AARCH64:
228 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000229 case EM_AMDGPU:
230 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000231 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000232 switch (Config->EKind) {
233 case ELF32LEKind:
234 return new MipsTargetInfo<ELF32LE>();
235 case ELF32BEKind:
236 return new MipsTargetInfo<ELF32BE>();
237 default:
George Rimar777f9632016-03-12 08:31:34 +0000238 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000239 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000240 case EM_PPC:
241 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000242 case EM_PPC64:
243 return new PPC64TargetInfo();
244 case EM_X86_64:
245 return new X86_64TargetInfo();
246 }
George Rimar777f9632016-03-12 08:31:34 +0000247 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000248}
249
Rafael Espindola01205f72015-09-22 18:19:46 +0000250TargetInfo::~TargetInfo() {}
251
George Rimar98b060d2016-03-06 06:01:07 +0000252bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000253 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000254 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000255
Rafael Espindolad405f472016-03-04 21:37:09 +0000256 // We know we are producing an executable.
257
258 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
259 // depending on the symbol being locally defined or not.
260 if (isTlsGlobalDynamicRel(Type))
261 return true;
262
263 // Local-Dynamic relocs can be relaxed to Local-Exec.
264 if (isTlsLocalDynamicRel(Type))
265 return true;
266
267 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
268 // defined.
269 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000270 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000271
George Rimar77d1cb12015-11-24 09:00:06 +0000272 return false;
273}
274
George Rimar786e8662016-03-17 05:57:33 +0000275uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000276
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000277bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
278
279template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
280 if (Config->Shared)
281 return false;
282 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
283 if (!SS)
284 return false;
285 return SS->Sym.getType() == STT_OBJECT;
286}
287
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000288template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000289bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000290 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000291}
292
George Rimarbfb7bf72015-12-21 10:00:12 +0000293bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000294bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000295bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000296bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000297
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000298bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000299
Rafael Espindola993f0272016-02-26 14:27:47 +0000300bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000301
302bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
303
Rafael Espindola852860e2016-02-12 15:47:37 +0000304TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
305 const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +0000306 if (S.IsGnuIFunc)
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000307 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000308 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000309 return Plt_Explicit;
310
311 // This handles a non PIC program call to function in a shared library.
312 // In an ideal world, we could just report an error saying the relocation
313 // can overflow at runtime.
314 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
315 // libc.so.
316 //
317 // The general idea on how to handle such cases is to create a PLT entry
318 // and use that as the function value.
319 //
320 // For the static linking part, we just return true and everything else
321 // will use the the PLT entry as the address.
322 //
323 // The remaining problem is making sure pointer equality still works. We
324 // need the help of the dynamic linker for that. We let it know that we have
325 // a direct reference to a so symbol by creating an undefined symbol with a
326 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
327 // the value of the symbol we created. This is true even for got entries, so
328 // pointer equality is maintained. To avoid an infinite loop, the only entry
329 // that points to the real function is a dedicated got entry used by the
330 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
331 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000332 if (S.isShared())
333 if (!Config->Pic && S.IsFunc && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000334 return Plt_Implicit;
335
Rafael Espindola852860e2016-02-12 15:47:37 +0000336 return Plt_No;
337}
Rui Ueyama012eb782016-01-29 04:05:09 +0000338
George Rimar98b060d2016-03-06 06:01:07 +0000339bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000340
George Rimar98b060d2016-03-06 06:01:07 +0000341bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000342 return false;
343}
344
George Rimar98b060d2016-03-06 06:01:07 +0000345bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000346
George Rimar98b060d2016-03-06 06:01:07 +0000347bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000348 return false;
349}
350
George Rimar7b8850f2016-03-06 06:09:50 +0000351size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
352 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000353 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000354 if (isTlsGlobalDynamicRel(Type)) {
355 if (S.isPreemptible())
356 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000357 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000358 }
359 if (isTlsLocalDynamicRel(Type))
360 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
361 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000362 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000363}
364
365size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000366 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000367 llvm_unreachable("Should not have claimed to be relaxable");
368}
369
370size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
371 uint64_t P, uint64_t SA) const {
372 llvm_unreachable("Should not have claimed to be relaxable");
373}
374
375size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000376 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000377 llvm_unreachable("Should not have claimed to be relaxable");
378}
379
380size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
381 uint64_t P, uint64_t SA) const {
382 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000383}
George Rimar77d1cb12015-11-24 09:00:06 +0000384
Rafael Espindola7f074422015-09-22 21:35:51 +0000385X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000386 CopyRel = R_386_COPY;
387 GotRel = R_386_GLOB_DAT;
388 PltRel = R_386_JUMP_SLOT;
389 IRelativeRel = R_386_IRELATIVE;
390 RelativeRel = R_386_RELATIVE;
391 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000392 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
393 TlsOffsetRel = R_386_TLS_DTPOFF32;
394 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000395 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000396 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000397}
398
Rafael Espindolaffcad442016-03-23 14:58:25 +0000399bool X86TargetInfo::isRelRelative(uint32_t Type) const {
400 switch (Type) {
401 default:
402 return false;
403 case R_386_PC32:
404 case R_386_PLT32:
405 case R_386_TLS_LDO_32:
406 return true;
407 }
408}
409
Rui Ueyamac516ae12016-01-29 02:33:45 +0000410void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000411 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
412}
413
Rui Ueyamac516ae12016-01-29 02:33:45 +0000414void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000415 // Entries in .got.plt initially points back to the corresponding
416 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000417 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000418}
Rafael Espindola01205f72015-09-22 18:19:46 +0000419
George Rimar98b060d2016-03-06 06:01:07 +0000420uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000421 if (Type == R_386_TLS_LE)
422 return R_386_TLS_TPOFF;
423 if (Type == R_386_TLS_LE_32)
424 return R_386_TLS_TPOFF32;
425 return Type;
426}
427
George Rimar98b060d2016-03-06 06:01:07 +0000428uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000429 if (Type == R_386_TLS_IE)
430 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000431 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000432}
433
George Rimar98b060d2016-03-06 06:01:07 +0000434bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000435 return Type == R_386_TLS_GD;
436}
437
George Rimar98b060d2016-03-06 06:01:07 +0000438bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000439 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
440}
441
George Rimar98b060d2016-03-06 06:01:07 +0000442bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000443 return Type == R_386_TLS_LDM;
444}
445
George Rimar98b060d2016-03-06 06:01:07 +0000446bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000447 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
448}
449
Rui Ueyama900e2d22016-01-29 03:51:49 +0000450void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000451 // Executable files and shared object files have
452 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000453 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000454 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000455 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000456 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
457 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000458 };
459 memcpy(Buf, V, sizeof(V));
460 return;
461 }
George Rimar648a2c32015-10-20 08:54:27 +0000462
George Rimar77b77792015-11-25 22:15:01 +0000463 const uint8_t PltData[] = {
464 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000465 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
466 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000467 };
468 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000469 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000470 write32le(Buf + 2, Got + 4);
471 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000472}
473
Rui Ueyama9398f862016-01-29 04:15:02 +0000474void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
475 uint64_t PltEntryAddr, int32_t Index,
476 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000477 const uint8_t Inst[] = {
478 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
479 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
480 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
481 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000482 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000483
George Rimar77b77792015-11-25 22:15:01 +0000484 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000485 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000486 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
487 : Out<ELF32LE>::Got->getVA();
488 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000489 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000490 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000491}
492
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000493bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
494 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000495}
496
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000497bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000498 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000499 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000500 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000501 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000502 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000503}
504
Rafael Espindola993f0272016-02-26 14:27:47 +0000505bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
506 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000507}
508
George Rimarbfb7bf72015-12-21 10:00:12 +0000509bool X86TargetInfo::isGotRelative(uint32_t Type) const {
510 // This relocation does not require got entry,
511 // but it is relative to got and needs it to be created.
512 // Here we request for that.
513 return Type == R_386_GOTOFF;
514}
515
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000516bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
517 return Type == R_386_GOT32;
518}
519
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000520void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000521 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000522 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000523 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000524 case R_386_32:
525 add32le(Loc, SA);
526 break;
George Rimarffb67352016-01-19 11:00:48 +0000527 case R_386_GOT32: {
528 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
529 Out<ELF32LE>::Got->getNumEntries() * 4;
530 checkInt<32>(V, Type);
531 add32le(Loc, V);
532 break;
533 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000534 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000535 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000536 break;
George Rimar13934772015-11-25 20:20:31 +0000537 case R_386_GOTPC:
538 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
539 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000540 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000541 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000542 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000543 break;
George Rimar9db204a2015-12-02 09:58:20 +0000544 case R_386_TLS_GD:
545 case R_386_TLS_LDM:
546 case R_386_TLS_TPOFF: {
547 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
548 Out<ELF32LE>::Got->getNumEntries() * 4;
549 checkInt<32>(V, Type);
550 write32le(Loc, V);
551 break;
552 }
George Rimar6f17e092015-12-17 09:32:21 +0000553 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000554 case R_386_TLS_LDO_32:
555 write32le(Loc, SA);
556 break;
George Rimard23970f2015-11-25 20:41:53 +0000557 case R_386_TLS_LE:
558 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
559 break;
560 case R_386_TLS_LE_32:
561 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
562 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000563 default:
George Rimar57610422016-03-11 14:43:02 +0000564 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000565 }
566}
567
George Rimar98b060d2016-03-06 06:01:07 +0000568bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000569 return Config->Shared && Type == R_386_TLS_IE;
570}
571
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000572size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000573 uint32_t Type, uint64_t P,
574 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000575 // GD can be optimized to LE:
576 // leal x@tlsgd(, %ebx, 1),
577 // call __tls_get_addr@plt
578 // Can be converted to:
579 // movl %gs:0,%eax
580 // addl $x@ntpoff,%eax
581 // But gold emits subl $foo@tpoff,%eax instead of addl.
582 // These instructions are completely equal in behavior.
583 // This method generates subl to be consistent with gold.
584 const uint8_t Inst[] = {
585 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
586 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
587 };
588 memcpy(Loc - 3, Inst, sizeof(Inst));
589 relocateOne(Loc + 5, BufEnd, R_386_32, P,
590 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
591
592 // The next relocation should be against __tls_get_addr, so skip it
593 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000594}
595
596// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
597// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
598// how GD can be optimized to IE:
599// leal x@tlsgd(, %ebx, 1),
600// call __tls_get_addr@plt
601// Is converted to:
602// movl %gs:0, %eax
603// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000604size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
605 uint32_t Type, uint64_t P,
606 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000607 const uint8_t Inst[] = {
608 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
609 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
610 };
611 memcpy(Loc - 3, Inst, sizeof(Inst));
612 relocateOne(Loc + 5, BufEnd, R_386_32, P,
613 SA - Out<ELF32LE>::Got->getVA() -
614 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000615
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000616 // The next relocation should be against __tls_get_addr, so skip it
617 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000618}
619
George Rimar6f17e092015-12-17 09:32:21 +0000620// In some conditions, relocations can be optimized to avoid using GOT.
621// This function does that for Initial Exec to Local Exec case.
622// Read "ELF Handling For Thread-Local Storage, 5.1
623// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000624// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000625
626size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000627 uint32_t Type, uint64_t P,
628 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000629 // Ulrich's document section 6.2 says that @gotntpoff can
630 // be used with MOVL or ADDL instructions.
631 // @indntpoff is similar to @gotntpoff, but for use in
632 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000633 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000634 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000635 uint8_t Reg = (Loc[-1] >> 3) & 7;
636 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000637 if (Type == R_386_TLS_IE) {
638 // For R_386_TLS_IE relocation we perform the next transformations:
639 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
640 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
641 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
642 // First one is special because when EAX is used the sequence is 5 bytes
643 // long, otherwise it is 6 bytes.
644 if (*Op == 0xa1) {
645 *Op = 0xb8;
646 } else {
647 *Inst = IsMov ? 0xc7 : 0x81;
648 *Op = 0xc0 | ((*Op >> 3) & 7);
649 }
650 } else {
651 // R_386_TLS_GOTIE relocation can be optimized to
652 // R_386_TLS_LE so that it does not use GOT.
653 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
654 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
655 // Note: gold converts to ADDL instead of LEAL.
656 *Inst = IsMov ? 0xc7 : 0x8d;
657 if (IsMov)
658 *Op = 0xc0 | ((*Op >> 3) & 7);
659 else
660 *Op = 0x80 | Reg | (Reg << 3);
661 }
George Rimar2558e122015-12-09 09:55:54 +0000662 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000663
664 return 0;
665}
666
667size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
668 uint32_t Type, uint64_t P,
669 uint64_t SA) const {
670 if (Type == R_386_TLS_LDO_32) {
671 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
672 return 0;
673 }
674
675 // LD can be optimized to LE:
676 // leal foo(%reg),%eax
677 // call ___tls_get_addr
678 // Is converted to:
679 // movl %gs:0,%eax
680 // nop
681 // leal 0(%esi,1),%esi
682 const uint8_t Inst[] = {
683 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
684 0x90, // nop
685 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
686 };
687 memcpy(Loc - 2, Inst, sizeof(Inst));
688
689 // The next relocation should be against __tls_get_addr, so skip it
690 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000691}
692
Rafael Espindola7f074422015-09-22 21:35:51 +0000693X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000694 CopyRel = R_X86_64_COPY;
695 GotRel = R_X86_64_GLOB_DAT;
696 PltRel = R_X86_64_JUMP_SLOT;
697 RelativeRel = R_X86_64_RELATIVE;
698 IRelativeRel = R_X86_64_IRELATIVE;
699 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000700 TlsModuleIndexRel = R_X86_64_DTPMOD64;
701 TlsOffsetRel = R_X86_64_DTPOFF64;
702 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000703 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000704 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000705}
706
Rui Ueyamac516ae12016-01-29 02:33:45 +0000707void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000708 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
709}
710
Rui Ueyamac516ae12016-01-29 02:33:45 +0000711void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000712 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000713 write32le(Buf, Plt + 6);
714}
715
Rui Ueyama900e2d22016-01-29 03:51:49 +0000716void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000717 const uint8_t PltData[] = {
718 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
719 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
720 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
721 };
722 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000723 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
724 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
725 write32le(Buf + 2, Got - Plt + 2); // GOT+8
726 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000727}
Rafael Espindola01205f72015-09-22 18:19:46 +0000728
Rui Ueyama9398f862016-01-29 04:15:02 +0000729void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
730 uint64_t PltEntryAddr, int32_t Index,
731 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000732 const uint8_t Inst[] = {
733 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
734 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
735 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
736 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000737 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000738
George Rimar648a2c32015-10-20 08:54:27 +0000739 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
740 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000741 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000742}
743
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000744bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
745 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
746 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000747}
748
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000749bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000750 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
751 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000752}
753
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000754bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000755 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000756 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000757 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000758 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000759 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000760}
761
George Rimar86971052016-03-29 08:35:42 +0000762uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
763 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
764 if (Config->Shared)
765 error(getELFRelocationTypeName(EM_X86_64, Type) +
766 " cannot be a dynamic relocation");
767 return Type;
768}
769
George Rimar98b060d2016-03-06 06:01:07 +0000770uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000771 // No other types of TLS relocations requiring GOT should
772 // reach here.
773 assert(Type == R_X86_64_GOTTPOFF);
774 return R_X86_64_PC32;
775}
776
George Rimar98b060d2016-03-06 06:01:07 +0000777bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000778 return Type == R_X86_64_GOTTPOFF;
779}
780
George Rimar98b060d2016-03-06 06:01:07 +0000781bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000782 return Type == R_X86_64_TLSGD;
783}
784
George Rimar98b060d2016-03-06 06:01:07 +0000785bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000786 return Type == R_X86_64_TLSLD;
787}
788
George Rimar98b060d2016-03-06 06:01:07 +0000789bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000790 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
791 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000792}
793
Rafael Espindola993f0272016-02-26 14:27:47 +0000794bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
795 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000796}
Rafael Espindolac4010882015-09-22 20:54:08 +0000797
Rafael Espindolaae244002015-10-05 19:30:12 +0000798bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
799 switch (Type) {
800 default:
801 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000802 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000803 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000804 case R_X86_64_PC8:
805 case R_X86_64_PC16:
806 case R_X86_64_PC32:
807 case R_X86_64_PC64:
808 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000809 return true;
810 }
811}
812
Rui Ueyamac516ae12016-01-29 02:33:45 +0000813bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000814 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000815}
816
George Rimar6713cf82015-11-25 21:46:05 +0000817// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
818// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000819// how GD can be optimized to LE:
820// .byte 0x66
821// leaq x@tlsgd(%rip), %rdi
822// .word 0x6666
823// rex64
824// call __tls_get_addr@plt
825// Is converted to:
826// mov %fs:0x0,%rax
827// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000828size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000829 uint32_t Type, uint64_t P,
830 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000831 const uint8_t Inst[] = {
832 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
833 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
834 };
835 memcpy(Loc - 4, Inst, sizeof(Inst));
836 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000837
838 // The next relocation should be against __tls_get_addr, so skip it
839 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000840}
841
George Rimar25411f252015-12-04 11:20:13 +0000842// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
843// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
844// how GD can be optimized to IE:
845// .byte 0x66
846// leaq x@tlsgd(%rip), %rdi
847// .word 0x6666
848// rex64
849// call __tls_get_addr@plt
850// Is converted to:
851// mov %fs:0x0,%rax
852// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000853size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
854 uint32_t Type, uint64_t P,
855 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000856 const uint8_t Inst[] = {
857 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
858 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
859 };
860 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000861 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000862
863 // The next relocation should be against __tls_get_addr, so skip it
864 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000865}
866
George Rimar77d1cb12015-11-24 09:00:06 +0000867// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000868// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000869// This function does that. Read "ELF Handling For Thread-Local Storage,
870// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
871// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000872size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000873 uint32_t Type, uint64_t P,
874 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000875 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
876 // used in MOVQ or ADDQ instructions only.
877 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
878 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
879 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
880 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
881 uint8_t *Prefix = Loc - 3;
882 uint8_t *Inst = Loc - 2;
883 uint8_t *RegSlot = Loc - 1;
884 uint8_t Reg = Loc[-1] >> 3;
885 bool IsMov = *Inst == 0x8b;
886 bool RspAdd = !IsMov && Reg == 4;
887 // r12 and rsp registers requires special handling.
888 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
889 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
890 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
891 // The same true for rsp. So we convert to addq for them, saving 1 byte that
892 // we dont have.
893 if (RspAdd)
894 *Inst = 0x81;
895 else
896 *Inst = IsMov ? 0xc7 : 0x8d;
897 if (*Prefix == 0x4c)
898 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
899 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
900 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000901 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000902}
903
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000904// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
905// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
906// how LD can be optimized to LE:
907// leaq bar@tlsld(%rip), %rdi
908// callq __tls_get_addr@PLT
909// leaq bar@dtpoff(%rax), %rcx
910// Is converted to:
911// .word 0x6666
912// .byte 0x66
913// mov %fs:0,%rax
914// leaq bar@tpoff(%rax), %rcx
915size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
916 uint32_t Type, uint64_t P,
917 uint64_t SA) const {
918 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000919 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
920 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000921 }
922 if (Type == R_X86_64_DTPOFF32) {
923 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000924 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000925 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000926
927 const uint8_t Inst[] = {
928 0x66, 0x66, //.word 0x6666
929 0x66, //.byte 0x66
930 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
931 };
932 memcpy(Loc - 3, Inst, sizeof(Inst));
933 // The next relocation should be against __tls_get_addr, so skip it
934 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000935}
936
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000937void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000938 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000939 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000940 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000941 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000942 checkUInt<32>(SA, Type);
943 write32le(Loc, SA);
944 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000945 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000946 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000947 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000948 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000949 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000950 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000951 write64le(Loc, SA);
952 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000953 case R_X86_64_DTPOFF32:
954 write32le(Loc, SA);
955 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000956 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000957 case R_X86_64_GOTPCRELX:
958 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000959 case R_X86_64_PC32:
960 case R_X86_64_PLT32:
961 case R_X86_64_TLSGD:
962 case R_X86_64_TLSLD:
963 write32le(Loc, SA - P);
964 break;
George Rimar48651482015-12-11 08:59:37 +0000965 case R_X86_64_SIZE32:
966 write32le(Loc, ZA);
967 break;
968 case R_X86_64_SIZE64:
969 write64le(Loc, ZA);
970 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000971 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000972 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000973 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000974 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000975 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000976 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000977 default:
George Rimar57610422016-03-11 14:43:02 +0000978 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000979 }
980}
981
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982// Relocation masks following the #lo(value), #hi(value), #ha(value),
983// #higher(value), #highera(value), #highest(value), and #highesta(value)
984// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
985// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000986static uint16_t applyPPCLo(uint64_t V) { return V; }
987static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
988static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
989static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
990static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000991static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000992static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
993
Davide Italiano8c3444362016-01-11 19:45:33 +0000994PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000995bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
996
997void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
998 uint64_t P, uint64_t SA, uint64_t ZA,
999 uint8_t *PairedLoc) const {
1000 switch (Type) {
1001 case R_PPC_ADDR16_HA:
1002 write16be(Loc, applyPPCHa(SA));
1003 break;
1004 case R_PPC_ADDR16_LO:
1005 write16be(Loc, applyPPCLo(SA));
1006 break;
1007 default:
George Rimar57610422016-03-11 14:43:02 +00001008 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +00001009 }
1010}
1011
Rafael Espindolac4010882015-09-22 20:54:08 +00001012PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001013 GotRel = R_PPC64_GLOB_DAT;
1014 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001015 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001016
1017 // We need 64K pages (at least under glibc/Linux, the loader won't
1018 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001019 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001020
1021 // The PPC64 ELF ABI v1 spec, says:
1022 //
1023 // It is normally desirable to put segments with different characteristics
1024 // in separate 256 Mbyte portions of the address space, to give the
1025 // operating system full paging flexibility in the 64-bit address space.
1026 //
1027 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1028 // use 0x10000000 as the starting address.
1029 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001030}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001031
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001032uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001033 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1034 // order. The TOC starts where the first of these sections starts.
1035
1036 // FIXME: This obviously does not do the right thing when there is no .got
1037 // section, but there is a .toc or .tocbss section.
1038 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1039 if (!TocVA)
1040 TocVA = Out<ELF64BE>::Plt->getVA();
1041
1042 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1043 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1044 // code (crt1.o) assumes that you can get from the TOC base to the
1045 // start of the .toc section with only a single (signed) 16-bit relocation.
1046 return TocVA + 0x8000;
1047}
1048
Rui Ueyama9398f862016-01-29 04:15:02 +00001049void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1050 uint64_t PltEntryAddr, int32_t Index,
1051 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1053
1054 // FIXME: What we should do, in theory, is get the offset of the function
1055 // descriptor in the .opd section, and use that as the offset from %r2 (the
1056 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1057 // be a pointer to the function descriptor in the .opd section. Using
1058 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1059
Hal Finkelfa92f682015-10-13 21:47:34 +00001060 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1062 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1063 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1064 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1065 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1066 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1067 write32be(Buf + 28, 0x4e800420); // bctr
1068}
1069
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001070bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001071 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001072 return true;
1073
1074 switch (Type) {
1075 default: return false;
1076 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001077 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001078 case R_PPC64_GOT16_HA:
1079 case R_PPC64_GOT16_HI:
1080 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001081 case R_PPC64_GOT16_LO_DS:
1082 return true;
1083 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001084}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001085
Rafael Espindola993f0272016-02-26 14:27:47 +00001086bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001088 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001089}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001090
Hal Finkelbe0823d2015-10-12 20:58:52 +00001091bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1092 switch (Type) {
1093 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001094 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001095 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001096 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001097 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001098 }
1099}
1100
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001101void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001102 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001103 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104 uint64_t TB = getPPC64TocBase();
1105
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1107 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001108 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001109 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1110 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001111 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1112 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001113 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1114 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 default: break;
1116 }
1117
Hal Finkel3c8cc672015-10-12 20:56:18 +00001118 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001119 case R_PPC64_ADDR14: {
1120 checkAlignment<4>(SA, Type);
1121 // Preserve the AA/LK bits in the branch instruction
1122 uint8_t AALK = Loc[3];
1123 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1124 break;
1125 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001127 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001129 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001131 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001132 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001134 case R_PPC64_ADDR16_HA:
1135 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
1137 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001140 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001141 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
1143 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001144 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
1146 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001147 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001148 break;
1149 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001150 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001151 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001152 case R_PPC64_ADDR16_LO:
1153 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001155 case R_PPC64_ADDR16_LO_DS:
1156 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001157 break;
1158 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001159 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001160 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001161 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001162 case R_PPC64_ADDR64:
1163 write64be(Loc, SA);
1164 break;
1165 case R_PPC64_REL16_HA:
1166 write16be(Loc, applyPPCHa(SA - P));
1167 break;
1168 case R_PPC64_REL16_HI:
1169 write16be(Loc, applyPPCHi(SA - P));
1170 break;
1171 case R_PPC64_REL16_LO:
1172 write16be(Loc, applyPPCLo(SA - P));
1173 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001174 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001175 // If we have an undefined weak symbol, we might get here with a symbol
1176 // address of zero. That could overflow, but the code must be unreachable,
1177 // so don't bother doing anything at all.
1178 if (!SA)
1179 break;
1180
Hal Finkeldaedc122015-10-12 23:16:53 +00001181 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1182 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001183 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001184
1185 if (!InPlt && Out<ELF64BE>::Opd) {
1186 // If this is a local call, and we currently have the address of a
1187 // function-descriptor, get the underlying code address instead.
1188 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1189 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001190 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001191
1192 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001193 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001194 }
1195
Hal Finkel3c8cc672015-10-12 20:56:18 +00001196 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001197 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001198 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001199
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001200 uint32_t Nop = 0x60000000;
1201 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1202 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001203 break;
1204 }
1205 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001206 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001207 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001208 break;
1209 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001210 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001211 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001212 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001213 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001214 break;
1215 default:
George Rimar57610422016-03-11 14:43:02 +00001216 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001217 }
1218}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001219
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001220AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001221 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001222 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001223 IRelativeRel = R_AARCH64_IRELATIVE;
1224 GotRel = R_AARCH64_GLOB_DAT;
1225 PltRel = R_AARCH64_JUMP_SLOT;
1226 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001227 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1228 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001229 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001230 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001231 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001232}
George Rimar648a2c32015-10-20 08:54:27 +00001233
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001234bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001235 switch (Type) {
1236 default:
1237 return false;
1238 case R_AARCH64_PREL32:
1239 case R_AARCH64_ADR_PREL_LO21:
1240 case R_AARCH64_ADR_PREL_PG_HI21:
1241 case R_AARCH64_ADR_GOT_PAGE:
1242 case R_AARCH64_LDST8_ABS_LO12_NC:
1243 case R_AARCH64_LDST16_ABS_LO12_NC:
1244 case R_AARCH64_LDST32_ABS_LO12_NC:
1245 case R_AARCH64_LDST64_ABS_LO12_NC:
1246 case R_AARCH64_LDST128_ABS_LO12_NC:
1247 case R_AARCH64_ADD_ABS_LO12_NC:
1248 case R_AARCH64_CALL26:
1249 case R_AARCH64_JUMP26:
1250 case R_AARCH64_CONDBR19:
1251 case R_AARCH64_TSTBR14:
Rafael Espindola07275532016-03-28 01:31:11 +00001252 case R_AARCH64_PREL64:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001253 return true;
1254 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001255}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001256
George Rimar98b060d2016-03-06 06:01:07 +00001257bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001258 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1259 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1260 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1261 Type == R_AARCH64_TLSDESC_CALL;
1262}
1263
George Rimar98b060d2016-03-06 06:01:07 +00001264bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001265 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1266 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1267}
1268
George Rimar98b060d2016-03-06 06:01:07 +00001269uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001270 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1271 return Type;
1272 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001273 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001274 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001275 // Keep it going with a dummy value so that we can find more reloc errors.
1276 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001277}
1278
Rui Ueyamac516ae12016-01-29 02:33:45 +00001279void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001280 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1281}
1282
Rui Ueyama900e2d22016-01-29 03:51:49 +00001283void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001284 const uint8_t PltData[] = {
1285 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1286 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1287 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1288 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1289 0x20, 0x02, 0x1f, 0xd6, // br x17
1290 0x1f, 0x20, 0x03, 0xd5, // nop
1291 0x1f, 0x20, 0x03, 0xd5, // nop
1292 0x1f, 0x20, 0x03, 0xd5 // nop
1293 };
1294 memcpy(Buf, PltData, sizeof(PltData));
1295
Rui Ueyama900e2d22016-01-29 03:51:49 +00001296 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1297 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1298 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1299 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1300 Got + 16);
1301 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1302 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001303}
1304
Rui Ueyama9398f862016-01-29 04:15:02 +00001305void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1306 uint64_t PltEntryAddr, int32_t Index,
1307 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001308 const uint8_t Inst[] = {
1309 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1310 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1311 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1312 0x20, 0x02, 0x1f, 0xd6 // br x17
1313 };
1314 memcpy(Buf, Inst, sizeof(Inst));
1315
1316 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1317 GotEntryAddr);
1318 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1319 GotEntryAddr);
1320 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1321 GotEntryAddr);
1322}
1323
George Rimar98b060d2016-03-06 06:01:07 +00001324uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001325 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001326 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1327 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001328}
1329
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001330bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001331 switch (Type) {
1332 default:
1333 return false;
1334 case R_AARCH64_ABS16:
1335 case R_AARCH64_ABS32:
1336 case R_AARCH64_ABS64:
1337 case R_AARCH64_ADD_ABS_LO12_NC:
1338 case R_AARCH64_ADR_PREL_LO21:
1339 case R_AARCH64_ADR_PREL_PG_HI21:
1340 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001341 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001342 case R_AARCH64_LDST32_ABS_LO12_NC:
1343 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001344 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001345 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001346 }
1347}
1348
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001349bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001350 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001351 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001352 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001353 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001354 case R_AARCH64_ADR_GOT_PAGE:
1355 case R_AARCH64_LD64_GOT_LO12_NC:
1356 return true;
1357 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001358 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001359 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001360}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001361
Rafael Espindola993f0272016-02-26 14:27:47 +00001362bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001363 switch (Type) {
1364 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001365 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001366 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001367 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001368 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001369 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001370 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001371 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001372}
Davide Italiano1d750a62015-09-27 08:45:38 +00001373
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001374static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001375 uint32_t ImmLo = (Imm & 0x3) << 29;
1376 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1377 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001378 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001379}
1380
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001381static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1382 or32le(L, (Imm & 0xFFF) << 10);
1383}
1384
Davide Italiano318ca222015-10-02 22:13:51 +00001385// Page(Expr) is the page address of the expression Expr, defined
1386// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001387// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001388static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001389 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001390}
1391
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001392void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001393 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001394 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001395 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001396 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001397 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001398 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001399 break;
1400 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001401 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001402 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001403 break;
1404 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001405 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001406 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001407 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001408 // This relocation stores 12 bits and there's no instruction
1409 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001410 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1411 // bits in Loc are zero.
1412 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001413 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001414 case R_AARCH64_ADR_GOT_PAGE: {
1415 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1416 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001417 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001418 break;
1419 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001420 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001421 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001422 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001423 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001424 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001425 }
George Rimar3d737e42016-01-13 13:04:46 +00001426 case R_AARCH64_ADR_PREL_PG_HI21:
1427 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001428 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001429 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001430 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001431 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001432 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001433 case R_AARCH64_CALL26:
1434 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001435 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001436 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001437 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1438 break;
1439 }
George Rimar4102bfb2016-01-11 14:22:00 +00001440 case R_AARCH64_CONDBR19: {
1441 uint64_t X = SA - P;
1442 checkInt<21>(X, Type);
1443 or32le(Loc, (X & 0x1FFFFC) << 3);
1444 break;
1445 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001446 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001447 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001448 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001449 or32le(Loc, (SA & 0xFF8) << 7);
1450 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001451 case R_AARCH64_LDST128_ABS_LO12_NC:
1452 or32le(Loc, (SA & 0x0FF8) << 6);
1453 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001454 case R_AARCH64_LDST16_ABS_LO12_NC:
1455 or32le(Loc, (SA & 0x0FFC) << 9);
1456 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001457 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001458 or32le(Loc, (SA & 0xFFF) << 10);
1459 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001460 case R_AARCH64_LDST32_ABS_LO12_NC:
1461 or32le(Loc, (SA & 0xFFC) << 8);
1462 break;
1463 case R_AARCH64_LDST64_ABS_LO12_NC:
1464 or32le(Loc, (SA & 0xFF8) << 7);
1465 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001466 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001467 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001468 write16le(Loc, SA - P);
1469 break;
1470 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001471 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001472 write32le(Loc, SA - P);
1473 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001474 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001475 write64le(Loc, SA - P);
1476 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001477 case R_AARCH64_TSTBR14: {
1478 uint64_t X = SA - P;
1479 checkInt<16>(X, Type);
1480 or32le(Loc, (X & 0xFFFC) << 3);
1481 break;
1482 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001483 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1484 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1485 checkInt<24>(V, Type);
1486 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1487 break;
1488 }
1489 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1490 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1491 updateAArch64Add(Loc, V & 0xFFF);
1492 break;
1493 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001494 default:
George Rimar57610422016-03-11 14:43:02 +00001495 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001496 }
1497}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001498
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001499size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001500 uint32_t Type, uint64_t P,
1501 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001502 // TLSDESC Global-Dynamic relocation are in the form:
1503 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1504 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1505 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1506 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1507 // And it can optimized to:
1508 // movz x0, #0x0, lsl #16
1509 // movk x0, #0x10
1510 // nop
1511 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001512 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1513 uint64_t X = SA + TPOff;
1514 checkUInt<32>(X, Type);
1515
1516 uint32_t NewInst;
1517 switch (Type) {
1518 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1519 case R_AARCH64_TLSDESC_CALL:
1520 // nop
1521 NewInst = 0xd503201f;
1522 break;
1523 case R_AARCH64_TLSDESC_ADR_PAGE21:
1524 // movz
1525 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1526 break;
1527 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1528 // movk
1529 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1530 break;
1531 default:
George Rimar777f9632016-03-12 08:31:34 +00001532 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001533 }
1534 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001535
1536 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001537}
1538
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001539size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001540 uint32_t Type, uint64_t P,
1541 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001542 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1543 uint64_t X = SA + TPOff;
1544 checkUInt<32>(X, Type);
1545
George Rimar4d1d16d2016-03-06 06:16:05 +00001546 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001547 uint32_t NewInst;
1548 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1549 // Generate movz.
1550 unsigned RegNo = (Inst & 0x1f);
1551 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1552 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1553 // Generate movk
1554 unsigned RegNo = (Inst & 0x1f);
1555 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1556 } else {
George Rimar777f9632016-03-12 08:31:34 +00001557 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001558 }
1559 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001560
1561 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001562}
1563
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001564// Implementing relocations for AMDGPU is low priority since most
1565// programs don't use relocations now. Thus, this function is not
1566// actually called (relocateOne is called for each relocation).
1567// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001568void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1569 uint64_t P, uint64_t SA, uint64_t ZA,
1570 uint8_t *PairedLoc) const {
George Rimar57610422016-03-11 14:43:02 +00001571 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001572}
1573
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001574template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001575 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001576 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001577 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001578 PltEntrySize = 16;
1579 PltZeroSize = 32;
1580 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001581 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001582 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001583 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001584}
1585
1586template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001587uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001588 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1589 return R_MIPS_REL32;
1590 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001591 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001592 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001593 // Keep it going with a dummy value so that we can find more reloc errors.
1594 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001595}
1596
1597template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001598void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001599 typedef typename ELFT::Off Elf_Off;
1600 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001601
1602 // Set the MSB of the second GOT slot. This is not required by any
1603 // MIPS ABI documentation, though.
1604 //
1605 // There is a comment in glibc saying that "The MSB of got[1] of a
1606 // gnu object is set to identify gnu objects," and in GNU gold it
1607 // says "the second entry will be used by some runtime loaders".
1608 // But how this field is being used is unclear.
1609 //
1610 // We are not really willing to mimic other linkers behaviors
1611 // without understanding why they do that, but because all files
1612 // generated by GNU tools have this special GOT value, and because
1613 // we've been doing this for years, it is probably a safe bet to
1614 // keep doing this for now. We really need to revisit this to see
1615 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001616 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001617 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001618}
1619
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001620template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001621void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1622 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001623}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001624
Simon Atanasyan35031192015-12-15 06:06:34 +00001625static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001626
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001627template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001628static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001629 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001630 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001631 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001632 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1633 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001634 checkAlignment<(1 << SHIFT)>(S + A, Type);
1635 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001636 checkInt<BSIZE + SHIFT>(V, Type);
1637 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001638}
1639
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001640template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001641static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001642 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001643 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001644}
1645
Simon Atanasyan3b377852016-03-04 10:55:20 +00001646template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001647static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1648 uint32_t Instr = read32<E>(Loc);
1649 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1650}
1651
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001652template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1653 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1654}
1655
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001656template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001657static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001658 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001659}
1660
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001661template <class ELFT>
1662void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1663 const endianness E = ELFT::TargetEndianness;
1664 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1665 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1666 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1667 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1668 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1669 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1670 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1671 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1672 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001673 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001674 writeMipsLo16<E>(Buf + 4, Got);
1675 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001676}
1677
1678template <class ELFT>
1679void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1680 uint64_t PltEntryAddr, int32_t Index,
1681 unsigned RelOff) const {
1682 const endianness E = ELFT::TargetEndianness;
1683 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1684 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1685 write32<E>(Buf + 8, 0x03200008); // jr $25
1686 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001687 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001688 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1689 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001690}
1691
1692template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001693bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001694 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001695}
1696
1697template <class ELFT>
1698bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001699 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001700}
1701
1702template <class ELFT>
1703bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1704 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001705}
1706
1707template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001708bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1709 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001710}
1711
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001712template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001713void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001714 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001715 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001716 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001717 // Thread pointer and DRP offsets from the start of TLS data area.
1718 // https://www.linux-mips.org/wiki/NPTL
1719 const uint32_t TPOffset = 0x7000;
1720 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001721 switch (Type) {
1722 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001723 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001724 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001725 case R_MIPS_26: {
1726 uint32_t Instr = read32<E>(Loc);
1727 // FIXME (simon): If the relocation target symbol is not a PLT entry
1728 // we should use another expression for calculation:
1729 // ((A << 2) | (P & 0xf0000000)) >> 2
1730 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1731 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1732 break;
1733 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001734 case R_MIPS_CALL16:
1735 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001736 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001737 if (Type == R_MIPS_GOT16)
1738 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001739 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001740 break;
1741 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001742 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001743 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001744 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001745 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001746 break;
1747 }
1748 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001749 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001750 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001751 case R_MIPS_HI16:
1752 if (PairedLoc)
1753 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1754 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001755 warning("can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001756 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001757 }
1758 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001759 case R_MIPS_JALR:
1760 // Ignore this optimization relocation for now
1761 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001762 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001763 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001764 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001765 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001766 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001767 break;
1768 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001769 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001770 break;
1771 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001772 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001773 break;
1774 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001775 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001776 break;
1777 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001778 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001779 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001780 case R_MIPS_PCHI16:
1781 if (PairedLoc)
1782 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1783 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001784 warning("can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001785 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001786 }
1787 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001788 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001789 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001790 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001791 case R_MIPS_TLS_DTPREL_HI16:
1792 writeMipsHi16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1793 break;
1794 case R_MIPS_TLS_DTPREL_LO16:
1795 writeMipsLo16<E>(Loc, S - DTPOffset + readSignedLo16<E>(Loc));
1796 break;
1797 case R_MIPS_TLS_TPREL_HI16:
1798 writeMipsHi16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1799 break;
1800 case R_MIPS_TLS_TPREL_LO16:
1801 writeMipsLo16<E>(Loc, S - TPOffset + readSignedLo16<E>(Loc));
1802 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001803 default:
George Rimar57610422016-03-11 14:43:02 +00001804 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001805 }
1806}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001807
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001808template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001809bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001810 return Type == R_MIPS_JALR;
1811}
1812
1813template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001814bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1815 switch (Type) {
1816 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001817 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001818 case R_MIPS_26:
1819 case R_MIPS_32:
1820 case R_MIPS_64:
1821 case R_MIPS_HI16:
1822 case R_MIPS_LO16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001823 case R_MIPS_TLS_DTPREL_HI16:
1824 case R_MIPS_TLS_DTPREL_LO16:
1825 case R_MIPS_TLS_TPREL_HI16:
1826 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001827 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001828 }
1829}
1830
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001831// _gp is a MIPS-specific ABI-defined symbol which points to
1832// a location that is relative to GOT. This function returns
1833// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001834template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001835 unsigned GPOffset = 0x7ff0;
1836 if (uint64_t V = Out<ELFT>::Got->getVA())
1837 return V + GPOffset;
1838 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001839}
1840
1841template uint32_t getMipsGpAddr<ELF32LE>();
1842template uint32_t getMipsGpAddr<ELF32BE>();
1843template uint64_t getMipsGpAddr<ELF64LE>();
1844template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001845
1846template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1847 const SymbolBody &) const;
1848template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1849 const SymbolBody &) const;
1850template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1851 const SymbolBody &) const;
1852template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1853 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001854}
1855}