blob: 7549b5597c3512d56f3fdb3a1f7ae56f1672c663 [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 Espindolafb1533b2016-02-22 21:23:29 +000088 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000089 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000090 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000091 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000092 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000093 uint64_t SA, uint64_t ZA = 0,
94 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000095
96 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
97 uint64_t P, uint64_t SA) const override;
98 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
99 uint64_t P, uint64_t SA,
100 const SymbolBody &S) const override;
101 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
102 uint64_t P, uint64_t SA,
103 const SymbolBody &S) const override;
104 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
105 uint64_t P, uint64_t SA) const override;
106
George Rimarbfb7bf72015-12-21 10:00:12 +0000107 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000108 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000109};
110
111class X86_64TargetInfo final : public TargetInfo {
112public:
113 X86_64TargetInfo();
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,
137 uint64_t P, uint64_t SA,
138 const SymbolBody &S) const override;
139 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
140 uint64_t P, uint64_t SA,
141 const SymbolBody &S) const override;
142 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
143 uint64_t P, uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000144};
145
Davide Italiano8c3444362016-01-11 19:45:33 +0000146class PPCTargetInfo final : public TargetInfo {
147public:
148 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000149 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
150 uint64_t SA, uint64_t ZA = 0,
151 uint8_t *PairedLoc = nullptr) const override;
152 bool isRelRelative(uint32_t Type) const override;
153};
154
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155class PPC64TargetInfo final : public TargetInfo {
156public:
157 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000158 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
159 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000160 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000161 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000162 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000163 uint64_t SA, uint64_t ZA = 0,
164 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000165 bool isRelRelative(uint32_t Type) const override;
166};
167
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000168class AArch64TargetInfo final : public TargetInfo {
169public:
170 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000171 uint32_t getDynRel(uint32_t Type) const override;
172 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
173 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000174 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000175 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000176 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
177 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000178 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000179 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000180 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000181 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000182 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000183 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000184 uint64_t SA, uint64_t ZA = 0,
185 uint8_t *PairedLoc = nullptr) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000186
187 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
188 uint64_t P, uint64_t SA,
189 const SymbolBody &S) const override;
190 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
191 uint64_t P, uint64_t SA,
192 const SymbolBody &S) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000193
194private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000195 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000196};
197
Tom Stellard80efb162016-01-07 03:59:08 +0000198class AMDGPUTargetInfo final : public TargetInfo {
199public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000200 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000201 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
202 uint64_t SA, uint64_t ZA = 0,
203 uint8_t *PairedLoc = nullptr) const override;
204};
205
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000206template <class ELFT> class MipsTargetInfo final : public TargetInfo {
207public:
208 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000209 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000210 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
211 void writePltZero(uint8_t *Buf) const override;
212 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
213 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000214 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000215 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000216 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000217 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000218 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000219 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000220 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000221 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000222 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000223 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000224};
225} // anonymous namespace
226
Rui Ueyama91004392015-10-13 16:08:15 +0000227TargetInfo *createTarget() {
228 switch (Config->EMachine) {
229 case EM_386:
230 return new X86TargetInfo();
231 case EM_AARCH64:
232 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000233 case EM_AMDGPU:
234 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000235 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000236 switch (Config->EKind) {
237 case ELF32LEKind:
238 return new MipsTargetInfo<ELF32LE>();
239 case ELF32BEKind:
240 return new MipsTargetInfo<ELF32BE>();
241 default:
George Rimar777f9632016-03-12 08:31:34 +0000242 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000243 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000244 case EM_PPC:
245 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000246 case EM_PPC64:
247 return new PPC64TargetInfo();
248 case EM_X86_64:
249 return new X86_64TargetInfo();
250 }
George Rimar777f9632016-03-12 08:31:34 +0000251 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000252}
253
Rafael Espindola01205f72015-09-22 18:19:46 +0000254TargetInfo::~TargetInfo() {}
255
George Rimar98b060d2016-03-06 06:01:07 +0000256bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000257 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000258 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000259
Rafael Espindolad405f472016-03-04 21:37:09 +0000260 // We know we are producing an executable.
261
262 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
263 // depending on the symbol being locally defined or not.
264 if (isTlsGlobalDynamicRel(Type))
265 return true;
266
267 // Local-Dynamic relocs can be relaxed to Local-Exec.
268 if (isTlsLocalDynamicRel(Type))
269 return true;
270
271 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
272 // defined.
273 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000274 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000275
George Rimar77d1cb12015-11-24 09:00:06 +0000276 return false;
277}
278
Igor Kudrinf6f45472015-11-10 08:39:27 +0000279uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
280
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000281bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
282
283template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
284 if (Config->Shared)
285 return false;
286 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
287 if (!SS)
288 return false;
289 return SS->Sym.getType() == STT_OBJECT;
290}
291
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000292template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000293bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000294 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000295}
296
George Rimarbfb7bf72015-12-21 10:00:12 +0000297bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000298bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000299bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000300bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000301
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000302bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000303
Rafael Espindola993f0272016-02-26 14:27:47 +0000304bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000305
306bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
307
308template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000309TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
310 const SymbolBody &S) const {
Rui Ueyama7ede5432016-03-13 04:40:14 +0000311 if (S.isGnuIfunc<ELFT>())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000312 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000313 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000314 return Plt_Explicit;
315
316 // This handles a non PIC program call to function in a shared library.
317 // In an ideal world, we could just report an error saying the relocation
318 // can overflow at runtime.
319 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
320 // libc.so.
321 //
322 // The general idea on how to handle such cases is to create a PLT entry
323 // and use that as the function value.
324 //
325 // For the static linking part, we just return true and everything else
326 // will use the the PLT entry as the address.
327 //
328 // The remaining problem is making sure pointer equality still works. We
329 // need the help of the dynamic linker for that. We let it know that we have
330 // a direct reference to a so symbol by creating an undefined symbol with a
331 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
332 // the value of the symbol we created. This is true even for got entries, so
333 // pointer equality is maintained. To avoid an infinite loop, the only entry
334 // that points to the real function is a dedicated got entry used by the
335 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
336 // R_386_JMP_SLOT, etc).
337 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
Rafael Espindola005d8442016-03-03 18:44:38 +0000338 if (!Config->Shared && SS->Sym.getType() == STT_FUNC &&
339 !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000340 return Plt_Implicit;
341
Rafael Espindola852860e2016-02-12 15:47:37 +0000342 return Plt_No;
343}
Rui Ueyama012eb782016-01-29 04:05:09 +0000344
George Rimar98b060d2016-03-06 06:01:07 +0000345bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000346
George Rimar98b060d2016-03-06 06:01:07 +0000347bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000348 return false;
349}
350
George Rimar98b060d2016-03-06 06:01:07 +0000351bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000352
George Rimar98b060d2016-03-06 06:01:07 +0000353bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000354 return false;
355}
356
George Rimar7b8850f2016-03-06 06:09:50 +0000357size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
358 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000359 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000360 if (isTlsGlobalDynamicRel(Type)) {
361 if (S.isPreemptible())
362 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
363 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA, S);
364 }
365 if (isTlsLocalDynamicRel(Type))
366 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
367 assert(isTlsInitialExecRel(Type));
368 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA, S);
369}
370
371size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
372 uint64_t P, uint64_t SA,
373 const SymbolBody &S) const {
374 llvm_unreachable("Should not have claimed to be relaxable");
375}
376
377size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
378 uint64_t P, uint64_t SA) const {
379 llvm_unreachable("Should not have claimed to be relaxable");
380}
381
382size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
383 uint64_t P, uint64_t SA,
384 const SymbolBody &S) const {
385 llvm_unreachable("Should not have claimed to be relaxable");
386}
387
388size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
389 uint64_t P, uint64_t SA) const {
390 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000391}
George Rimar77d1cb12015-11-24 09:00:06 +0000392
Rafael Espindola7f074422015-09-22 21:35:51 +0000393X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000394 CopyRel = R_386_COPY;
395 GotRel = R_386_GLOB_DAT;
396 PltRel = R_386_JUMP_SLOT;
397 IRelativeRel = R_386_IRELATIVE;
398 RelativeRel = R_386_RELATIVE;
399 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000400 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
401 TlsOffsetRel = R_386_TLS_DTPOFF32;
402 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000403 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000404 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000405}
406
Rui Ueyamac516ae12016-01-29 02:33:45 +0000407void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000408 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
409}
410
Rui Ueyamac516ae12016-01-29 02:33:45 +0000411void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000412 // Entries in .got.plt initially points back to the corresponding
413 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000414 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000415}
Rafael Espindola01205f72015-09-22 18:19:46 +0000416
George Rimar98b060d2016-03-06 06:01:07 +0000417uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000418 if (Type == R_386_TLS_LE)
419 return R_386_TLS_TPOFF;
420 if (Type == R_386_TLS_LE_32)
421 return R_386_TLS_TPOFF32;
422 return Type;
423}
424
George Rimar98b060d2016-03-06 06:01:07 +0000425uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000426 if (Type == R_386_TLS_IE)
427 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000428 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000429}
430
George Rimar98b060d2016-03-06 06:01:07 +0000431bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000432 return Type == R_386_TLS_GD;
433}
434
George Rimar98b060d2016-03-06 06:01:07 +0000435bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000436 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
437}
438
George Rimar98b060d2016-03-06 06:01:07 +0000439bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000440 return Type == R_386_TLS_LDM;
441}
442
George Rimar98b060d2016-03-06 06:01:07 +0000443bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000444 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
445}
446
Rui Ueyama900e2d22016-01-29 03:51:49 +0000447void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000448 // Executable files and shared object files have
449 // separate procedure linkage tables.
450 if (Config->Shared) {
451 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000452 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000453 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
454 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000455 };
456 memcpy(Buf, V, sizeof(V));
457 return;
458 }
George Rimar648a2c32015-10-20 08:54:27 +0000459
George Rimar77b77792015-11-25 22:15:01 +0000460 const uint8_t PltData[] = {
461 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000462 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
463 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000464 };
465 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000466 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000467 write32le(Buf + 2, Got + 4);
468 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000469}
470
Rui Ueyama9398f862016-01-29 04:15:02 +0000471void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
472 uint64_t PltEntryAddr, int32_t Index,
473 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000474 const uint8_t Inst[] = {
475 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
476 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
477 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
478 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000479 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000480
George Rimar77b77792015-11-25 22:15:01 +0000481 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
482 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000483 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
484 : Out<ELF32LE>::Got->getVA();
485 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000486 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000487 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000488}
489
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000490bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
491 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000492}
493
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000494bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000495 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000496 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000497 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000498 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000499 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000500}
501
Rafael Espindola993f0272016-02-26 14:27:47 +0000502bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
503 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000504}
505
George Rimarbfb7bf72015-12-21 10:00:12 +0000506bool X86TargetInfo::isGotRelative(uint32_t Type) const {
507 // This relocation does not require got entry,
508 // but it is relative to got and needs it to be created.
509 // Here we request for that.
510 return Type == R_386_GOTOFF;
511}
512
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000513bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
514 return Type == R_386_GOT32;
515}
516
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000517void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000518 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000519 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000520 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000521 case R_386_32:
522 add32le(Loc, SA);
523 break;
George Rimarffb67352016-01-19 11:00:48 +0000524 case R_386_GOT32: {
525 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
526 Out<ELF32LE>::Got->getNumEntries() * 4;
527 checkInt<32>(V, Type);
528 add32le(Loc, V);
529 break;
530 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000531 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000532 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000533 break;
George Rimar13934772015-11-25 20:20:31 +0000534 case R_386_GOTPC:
535 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
536 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000537 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000538 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000539 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000540 break;
George Rimar9db204a2015-12-02 09:58:20 +0000541 case R_386_TLS_GD:
542 case R_386_TLS_LDM:
543 case R_386_TLS_TPOFF: {
544 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
545 Out<ELF32LE>::Got->getNumEntries() * 4;
546 checkInt<32>(V, Type);
547 write32le(Loc, V);
548 break;
549 }
George Rimar6f17e092015-12-17 09:32:21 +0000550 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000551 case R_386_TLS_LDO_32:
552 write32le(Loc, SA);
553 break;
George Rimard23970f2015-11-25 20:41:53 +0000554 case R_386_TLS_LE:
555 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
556 break;
557 case R_386_TLS_LE_32:
558 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
559 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000560 default:
George Rimar57610422016-03-11 14:43:02 +0000561 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000562 }
563}
564
George Rimar98b060d2016-03-06 06:01:07 +0000565bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000566 return Config->Shared && Type == R_386_TLS_IE;
567}
568
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000569size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
570 uint32_t Type, uint64_t P, uint64_t SA,
571 const SymbolBody &S) const {
572 // GD can be optimized to LE:
573 // leal x@tlsgd(, %ebx, 1),
574 // call __tls_get_addr@plt
575 // Can be converted to:
576 // movl %gs:0,%eax
577 // addl $x@ntpoff,%eax
578 // But gold emits subl $foo@tpoff,%eax instead of addl.
579 // These instructions are completely equal in behavior.
580 // This method generates subl to be consistent with gold.
581 const uint8_t Inst[] = {
582 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
583 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
584 };
585 memcpy(Loc - 3, Inst, sizeof(Inst));
586 relocateOne(Loc + 5, BufEnd, R_386_32, P,
587 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
588
589 // The next relocation should be against __tls_get_addr, so skip it
590 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000591}
592
593// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
594// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
595// how GD can be optimized to IE:
596// leal x@tlsgd(, %ebx, 1),
597// call __tls_get_addr@plt
598// Is converted to:
599// movl %gs:0, %eax
600// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000601size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
602 uint32_t Type, uint64_t P,
603 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000604 const uint8_t Inst[] = {
605 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
606 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
607 };
608 memcpy(Loc - 3, Inst, sizeof(Inst));
609 relocateOne(Loc + 5, BufEnd, R_386_32, P,
610 SA - Out<ELF32LE>::Got->getVA() -
611 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000612
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000613 // The next relocation should be against __tls_get_addr, so skip it
614 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000615}
616
George Rimar6f17e092015-12-17 09:32:21 +0000617// In some conditions, relocations can be optimized to avoid using GOT.
618// This function does that for Initial Exec to Local Exec case.
619// Read "ELF Handling For Thread-Local Storage, 5.1
620// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000621// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000622
623size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
624 uint32_t Type, uint64_t P, uint64_t SA,
625 const SymbolBody &S) const {
George Rimar6f17e092015-12-17 09:32:21 +0000626 // Ulrich's document section 6.2 says that @gotntpoff can
627 // be used with MOVL or ADDL instructions.
628 // @indntpoff is similar to @gotntpoff, but for use in
629 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000630 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000631 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000632 uint8_t Reg = (Loc[-1] >> 3) & 7;
633 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000634 if (Type == R_386_TLS_IE) {
635 // For R_386_TLS_IE relocation we perform the next transformations:
636 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
637 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
638 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
639 // First one is special because when EAX is used the sequence is 5 bytes
640 // long, otherwise it is 6 bytes.
641 if (*Op == 0xa1) {
642 *Op = 0xb8;
643 } else {
644 *Inst = IsMov ? 0xc7 : 0x81;
645 *Op = 0xc0 | ((*Op >> 3) & 7);
646 }
647 } else {
648 // R_386_TLS_GOTIE relocation can be optimized to
649 // R_386_TLS_LE so that it does not use GOT.
650 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
651 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
652 // Note: gold converts to ADDL instead of LEAL.
653 *Inst = IsMov ? 0xc7 : 0x8d;
654 if (IsMov)
655 *Op = 0xc0 | ((*Op >> 3) & 7);
656 else
657 *Op = 0x80 | Reg | (Reg << 3);
658 }
George Rimar2558e122015-12-09 09:55:54 +0000659 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000660
661 return 0;
662}
663
664size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
665 uint32_t Type, uint64_t P,
666 uint64_t SA) const {
667 if (Type == R_386_TLS_LDO_32) {
668 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
669 return 0;
670 }
671
672 // LD can be optimized to LE:
673 // leal foo(%reg),%eax
674 // call ___tls_get_addr
675 // Is converted to:
676 // movl %gs:0,%eax
677 // nop
678 // leal 0(%esi,1),%esi
679 const uint8_t Inst[] = {
680 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
681 0x90, // nop
682 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
683 };
684 memcpy(Loc - 2, Inst, sizeof(Inst));
685
686 // The next relocation should be against __tls_get_addr, so skip it
687 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000688}
689
Rafael Espindola7f074422015-09-22 21:35:51 +0000690X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000691 CopyRel = R_X86_64_COPY;
692 GotRel = R_X86_64_GLOB_DAT;
693 PltRel = R_X86_64_JUMP_SLOT;
694 RelativeRel = R_X86_64_RELATIVE;
695 IRelativeRel = R_X86_64_IRELATIVE;
696 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000697 TlsModuleIndexRel = R_X86_64_DTPMOD64;
698 TlsOffsetRel = R_X86_64_DTPOFF64;
699 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000700 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000701 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000702}
703
Rui Ueyamac516ae12016-01-29 02:33:45 +0000704void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000705 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
706}
707
Rui Ueyamac516ae12016-01-29 02:33:45 +0000708void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000709 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000710 write32le(Buf, Plt + 6);
711}
712
Rui Ueyama900e2d22016-01-29 03:51:49 +0000713void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000714 const uint8_t PltData[] = {
715 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
716 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
717 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
718 };
719 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000720 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
721 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
722 write32le(Buf + 2, Got - Plt + 2); // GOT+8
723 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000724}
Rafael Espindola01205f72015-09-22 18:19:46 +0000725
Rui Ueyama9398f862016-01-29 04:15:02 +0000726void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
727 uint64_t PltEntryAddr, int32_t Index,
728 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000729 const uint8_t Inst[] = {
730 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
731 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
732 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
733 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000734 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000735
George Rimar648a2c32015-10-20 08:54:27 +0000736 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
737 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000738 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000739}
740
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000741bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
742 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
743 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000744}
745
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000746bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
747 return Type == R_X86_64_GOTPCREL;
748}
749
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000750bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000751 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000752 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000753 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000754 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000755 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000756}
757
George Rimar98b060d2016-03-06 06:01:07 +0000758uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000759 // No other types of TLS relocations requiring GOT should
760 // reach here.
761 assert(Type == R_X86_64_GOTTPOFF);
762 return R_X86_64_PC32;
763}
764
George Rimar98b060d2016-03-06 06:01:07 +0000765bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000766 return Type == R_X86_64_GOTTPOFF;
767}
768
George Rimar98b060d2016-03-06 06:01:07 +0000769bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000770 return Type == R_X86_64_TLSGD;
771}
772
George Rimar98b060d2016-03-06 06:01:07 +0000773bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000774 return Type == R_X86_64_TLSLD;
775}
776
George Rimar98b060d2016-03-06 06:01:07 +0000777bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000778 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
779 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000780}
781
Rafael Espindola993f0272016-02-26 14:27:47 +0000782bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
783 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000784}
Rafael Espindolac4010882015-09-22 20:54:08 +0000785
Rafael Espindolaae244002015-10-05 19:30:12 +0000786bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
787 switch (Type) {
788 default:
789 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000790 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000791 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000792 case R_X86_64_PC8:
793 case R_X86_64_PC16:
794 case R_X86_64_PC32:
795 case R_X86_64_PC64:
796 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000797 return true;
798 }
799}
800
Rui Ueyamac516ae12016-01-29 02:33:45 +0000801bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000802 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000803}
804
George Rimar6713cf82015-11-25 21:46:05 +0000805// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
806// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000807// how GD can be optimized to LE:
808// .byte 0x66
809// leaq x@tlsgd(%rip), %rdi
810// .word 0x6666
811// rex64
812// call __tls_get_addr@plt
813// Is converted to:
814// mov %fs:0x0,%rax
815// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000816size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
817 uint32_t Type, uint64_t P, uint64_t SA,
818 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000819 const uint8_t Inst[] = {
820 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
821 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
822 };
823 memcpy(Loc - 4, Inst, sizeof(Inst));
824 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000825
826 // The next relocation should be against __tls_get_addr, so skip it
827 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000828}
829
George Rimar25411f252015-12-04 11:20:13 +0000830// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
831// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
832// how GD can be optimized to IE:
833// .byte 0x66
834// leaq x@tlsgd(%rip), %rdi
835// .word 0x6666
836// rex64
837// call __tls_get_addr@plt
838// Is converted to:
839// mov %fs:0x0,%rax
840// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000841size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
842 uint32_t Type, uint64_t P,
843 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000844 const uint8_t Inst[] = {
845 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
846 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
847 };
848 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000849 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000850
851 // The next relocation should be against __tls_get_addr, so skip it
852 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000853}
854
George Rimar77d1cb12015-11-24 09:00:06 +0000855// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000856// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000857// This function does that. Read "ELF Handling For Thread-Local Storage,
858// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
859// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000860size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
861 uint32_t Type, uint64_t P, uint64_t SA,
862 const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000863 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
864 // used in MOVQ or ADDQ instructions only.
865 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
866 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
867 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
868 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
869 uint8_t *Prefix = Loc - 3;
870 uint8_t *Inst = Loc - 2;
871 uint8_t *RegSlot = Loc - 1;
872 uint8_t Reg = Loc[-1] >> 3;
873 bool IsMov = *Inst == 0x8b;
874 bool RspAdd = !IsMov && Reg == 4;
875 // r12 and rsp registers requires special handling.
876 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
877 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
878 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
879 // The same true for rsp. So we convert to addq for them, saving 1 byte that
880 // we dont have.
881 if (RspAdd)
882 *Inst = 0x81;
883 else
884 *Inst = IsMov ? 0xc7 : 0x8d;
885 if (*Prefix == 0x4c)
886 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
887 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
888 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000889 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000890}
891
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000892// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
893// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
894// how LD can be optimized to LE:
895// leaq bar@tlsld(%rip), %rdi
896// callq __tls_get_addr@PLT
897// leaq bar@dtpoff(%rax), %rcx
898// Is converted to:
899// .word 0x6666
900// .byte 0x66
901// mov %fs:0,%rax
902// leaq bar@tpoff(%rax), %rcx
903size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
904 uint32_t Type, uint64_t P,
905 uint64_t SA) const {
906 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000907 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
908 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000909 }
910 if (Type == R_X86_64_DTPOFF32) {
911 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000912 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000913 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000914
915 const uint8_t Inst[] = {
916 0x66, 0x66, //.word 0x6666
917 0x66, //.byte 0x66
918 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
919 };
920 memcpy(Loc - 3, Inst, sizeof(Inst));
921 // The next relocation should be against __tls_get_addr, so skip it
922 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000923}
924
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000925void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000926 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000927 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000928 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000929 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000930 checkUInt<32>(SA, Type);
931 write32le(Loc, SA);
932 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000933 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000934 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000935 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000936 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000937 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000938 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000939 write64le(Loc, SA);
940 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000941 case R_X86_64_DTPOFF32:
942 write32le(Loc, SA);
943 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000944 case R_X86_64_GOTPCREL:
945 case R_X86_64_PC32:
946 case R_X86_64_PLT32:
947 case R_X86_64_TLSGD:
948 case R_X86_64_TLSLD:
949 write32le(Loc, SA - P);
950 break;
George Rimar48651482015-12-11 08:59:37 +0000951 case R_X86_64_SIZE32:
952 write32le(Loc, ZA);
953 break;
954 case R_X86_64_SIZE64:
955 write64le(Loc, ZA);
956 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000957 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000958 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000959 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000960 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000961 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000962 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000963 default:
George Rimar57610422016-03-11 14:43:02 +0000964 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000965 }
966}
967
Hal Finkel3c8cc672015-10-12 20:56:18 +0000968// Relocation masks following the #lo(value), #hi(value), #ha(value),
969// #higher(value), #highera(value), #highest(value), and #highesta(value)
970// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
971// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000972static uint16_t applyPPCLo(uint64_t V) { return V; }
973static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
974static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
975static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
976static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000977static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000978static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
979
Davide Italiano8c3444362016-01-11 19:45:33 +0000980PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000981bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
982
983void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
984 uint64_t P, uint64_t SA, uint64_t ZA,
985 uint8_t *PairedLoc) const {
986 switch (Type) {
987 case R_PPC_ADDR16_HA:
988 write16be(Loc, applyPPCHa(SA));
989 break;
990 case R_PPC_ADDR16_LO:
991 write16be(Loc, applyPPCLo(SA));
992 break;
993 default:
George Rimar57610422016-03-11 14:43:02 +0000994 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000995 }
996}
997
Rafael Espindolac4010882015-09-22 20:54:08 +0000998PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000999 GotRel = R_PPC64_GLOB_DAT;
1000 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001001 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001002
1003 // We need 64K pages (at least under glibc/Linux, the loader won't
1004 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001005 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001006
1007 // The PPC64 ELF ABI v1 spec, says:
1008 //
1009 // It is normally desirable to put segments with different characteristics
1010 // in separate 256 Mbyte portions of the address space, to give the
1011 // operating system full paging flexibility in the 64-bit address space.
1012 //
1013 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1014 // use 0x10000000 as the starting address.
1015 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001016}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001017
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001018uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1020 // order. The TOC starts where the first of these sections starts.
1021
1022 // FIXME: This obviously does not do the right thing when there is no .got
1023 // section, but there is a .toc or .tocbss section.
1024 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1025 if (!TocVA)
1026 TocVA = Out<ELF64BE>::Plt->getVA();
1027
1028 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1029 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1030 // code (crt1.o) assumes that you can get from the TOC base to the
1031 // start of the .toc section with only a single (signed) 16-bit relocation.
1032 return TocVA + 0x8000;
1033}
1034
Rui Ueyama9398f862016-01-29 04:15:02 +00001035void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1036 uint64_t PltEntryAddr, int32_t Index,
1037 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1039
1040 // FIXME: What we should do, in theory, is get the offset of the function
1041 // descriptor in the .opd section, and use that as the offset from %r2 (the
1042 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1043 // be a pointer to the function descriptor in the .opd section. Using
1044 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1045
Hal Finkelfa92f682015-10-13 21:47:34 +00001046 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1048 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1049 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1050 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1051 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1052 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1053 write32be(Buf + 28, 0x4e800420); // bctr
1054}
1055
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001056bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001057 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 return true;
1059
1060 switch (Type) {
1061 default: return false;
1062 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001064 case R_PPC64_GOT16_HA:
1065 case R_PPC64_GOT16_HI:
1066 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001067 case R_PPC64_GOT16_LO_DS:
1068 return true;
1069 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001070}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071
Rafael Espindola993f0272016-02-26 14:27:47 +00001072bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001074 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001075}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076
Hal Finkelbe0823d2015-10-12 20:58:52 +00001077bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1078 switch (Type) {
1079 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001080 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001081 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001082 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001083 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001084 }
1085}
1086
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001087void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001088 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001089 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001090 uint64_t TB = getPPC64TocBase();
1091
Hal Finkel3c8cc672015-10-12 20:56:18 +00001092 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1093 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001094 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001095 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1096 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001097 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1098 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001099 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1100 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001101 default: break;
1102 }
1103
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001105 case R_PPC64_ADDR14: {
1106 checkAlignment<4>(SA, Type);
1107 // Preserve the AA/LK bits in the branch instruction
1108 uint8_t AALK = Loc[3];
1109 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1110 break;
1111 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001112 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001113 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001114 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001115 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001116 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001117 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001118 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001120 case R_PPC64_ADDR16_HA:
1121 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001122 break;
1123 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001124 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001125 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001127 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001128 break;
1129 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001130 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 break;
1132 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001133 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001134 break;
1135 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001136 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001137 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001138 case R_PPC64_ADDR16_LO:
1139 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001140 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001141 case R_PPC64_ADDR16_LO_DS:
1142 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001143 break;
1144 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001145 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001146 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001147 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001148 case R_PPC64_ADDR64:
1149 write64be(Loc, SA);
1150 break;
1151 case R_PPC64_REL16_HA:
1152 write16be(Loc, applyPPCHa(SA - P));
1153 break;
1154 case R_PPC64_REL16_HI:
1155 write16be(Loc, applyPPCHi(SA - P));
1156 break;
1157 case R_PPC64_REL16_LO:
1158 write16be(Loc, applyPPCLo(SA - P));
1159 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001160 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001161 // If we have an undefined weak symbol, we might get here with a symbol
1162 // address of zero. That could overflow, but the code must be unreachable,
1163 // so don't bother doing anything at all.
1164 if (!SA)
1165 break;
1166
Hal Finkeldaedc122015-10-12 23:16:53 +00001167 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1168 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001169 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001170
1171 if (!InPlt && Out<ELF64BE>::Opd) {
1172 // If this is a local call, and we currently have the address of a
1173 // function-descriptor, get the underlying code address instead.
1174 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1175 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001176 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001177
1178 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001179 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001180 }
1181
Hal Finkel3c8cc672015-10-12 20:56:18 +00001182 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001183 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001184 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001185
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001186 uint32_t Nop = 0x60000000;
1187 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1188 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001189 break;
1190 }
1191 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001192 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001193 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001194 break;
1195 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001196 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001197 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001198 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001199 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001200 break;
1201 default:
George Rimar57610422016-03-11 14:43:02 +00001202 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001203 }
1204}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001205
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001206AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001207 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001208 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001209 IRelativeRel = R_AARCH64_IRELATIVE;
1210 GotRel = R_AARCH64_GLOB_DAT;
1211 PltRel = R_AARCH64_JUMP_SLOT;
1212 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001213 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1214 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001215 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001216 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001217 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001218}
George Rimar648a2c32015-10-20 08:54:27 +00001219
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001220bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001221 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001222 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
Rafael Espindola57ca2702016-02-24 20:52:58 +00001223 Type == R_AARCH64_LDST32_ABS_LO12_NC ||
Rafael Espindola47ed5422016-02-24 21:48:06 +00001224 Type == R_AARCH64_LDST64_ABS_LO12_NC ||
Rafael Espindolaa598a3a2016-02-24 22:07:12 +00001225 Type == R_AARCH64_ADD_ABS_LO12_NC || Type == R_AARCH64_CALL26;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001226}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001227
George Rimar98b060d2016-03-06 06:01:07 +00001228bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001229 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1230 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1231 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1232 Type == R_AARCH64_TLSDESC_CALL;
1233}
1234
George Rimar98b060d2016-03-06 06:01:07 +00001235bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001236 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1237 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1238}
1239
George Rimar98b060d2016-03-06 06:01:07 +00001240uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001241 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1242 return Type;
1243 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001244 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001245 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001246 // Keep it going with a dummy value so that we can find more reloc errors.
1247 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001248}
1249
Rui Ueyamac516ae12016-01-29 02:33:45 +00001250void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001251 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1252}
1253
Rui Ueyama900e2d22016-01-29 03:51:49 +00001254void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001255 const uint8_t PltData[] = {
1256 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1257 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1258 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1259 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1260 0x20, 0x02, 0x1f, 0xd6, // br x17
1261 0x1f, 0x20, 0x03, 0xd5, // nop
1262 0x1f, 0x20, 0x03, 0xd5, // nop
1263 0x1f, 0x20, 0x03, 0xd5 // nop
1264 };
1265 memcpy(Buf, PltData, sizeof(PltData));
1266
Rui Ueyama900e2d22016-01-29 03:51:49 +00001267 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1268 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1269 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1270 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1271 Got + 16);
1272 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1273 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001274}
1275
Rui Ueyama9398f862016-01-29 04:15:02 +00001276void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1277 uint64_t PltEntryAddr, int32_t Index,
1278 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001279 const uint8_t Inst[] = {
1280 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1281 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1282 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1283 0x20, 0x02, 0x1f, 0xd6 // br x17
1284 };
1285 memcpy(Buf, Inst, sizeof(Inst));
1286
1287 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1288 GotEntryAddr);
1289 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1290 GotEntryAddr);
1291 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1292 GotEntryAddr);
1293}
1294
George Rimar98b060d2016-03-06 06:01:07 +00001295uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001296 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001297 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1298 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001299}
1300
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001301bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001302 switch (Type) {
1303 default:
1304 return false;
1305 case R_AARCH64_ABS16:
1306 case R_AARCH64_ABS32:
1307 case R_AARCH64_ABS64:
1308 case R_AARCH64_ADD_ABS_LO12_NC:
1309 case R_AARCH64_ADR_PREL_LO21:
1310 case R_AARCH64_ADR_PREL_PG_HI21:
1311 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001312 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001313 case R_AARCH64_LDST32_ABS_LO12_NC:
1314 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001315 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001316 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001317 }
1318}
1319
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001320bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001321 switch (Type) {
1322 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1323 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1324 case R_AARCH64_ADR_GOT_PAGE:
1325 case R_AARCH64_LD64_GOT_LO12_NC:
1326 return true;
1327 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001328 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001329 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001330}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001331
Rafael Espindola993f0272016-02-26 14:27:47 +00001332bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001333 switch (Type) {
1334 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001335 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001336 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001337 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001338 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001339 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001340 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001341 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001342}
Davide Italiano1d750a62015-09-27 08:45:38 +00001343
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001344static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001345 uint32_t ImmLo = (Imm & 0x3) << 29;
1346 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1347 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001348 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001349}
1350
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001351static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1352 or32le(L, (Imm & 0xFFF) << 10);
1353}
1354
Davide Italiano318ca222015-10-02 22:13:51 +00001355// Page(Expr) is the page address of the expression Expr, defined
1356// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001357// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001358static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001359 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001360}
1361
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001362void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001363 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001364 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001365 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001366 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001367 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001368 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001369 break;
1370 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001371 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001372 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001373 break;
1374 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001375 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001376 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001377 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001378 // This relocation stores 12 bits and there's no instruction
1379 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001380 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1381 // bits in Loc are zero.
1382 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001383 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001384 case R_AARCH64_ADR_GOT_PAGE: {
1385 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1386 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001387 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001388 break;
1389 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001390 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001391 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001392 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001393 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001394 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001395 }
George Rimar3d737e42016-01-13 13:04:46 +00001396 case R_AARCH64_ADR_PREL_PG_HI21:
1397 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001398 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001399 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001400 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001401 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001402 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001403 case R_AARCH64_CALL26:
1404 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001405 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001406 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001407 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1408 break;
1409 }
George Rimar4102bfb2016-01-11 14:22:00 +00001410 case R_AARCH64_CONDBR19: {
1411 uint64_t X = SA - P;
1412 checkInt<21>(X, Type);
1413 or32le(Loc, (X & 0x1FFFFC) << 3);
1414 break;
1415 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001416 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001417 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001418 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001419 or32le(Loc, (SA & 0xFF8) << 7);
1420 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001421 case R_AARCH64_LDST128_ABS_LO12_NC:
1422 or32le(Loc, (SA & 0x0FF8) << 6);
1423 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001424 case R_AARCH64_LDST16_ABS_LO12_NC:
1425 or32le(Loc, (SA & 0x0FFC) << 9);
1426 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001427 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001428 or32le(Loc, (SA & 0xFFF) << 10);
1429 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001430 case R_AARCH64_LDST32_ABS_LO12_NC:
1431 or32le(Loc, (SA & 0xFFC) << 8);
1432 break;
1433 case R_AARCH64_LDST64_ABS_LO12_NC:
1434 or32le(Loc, (SA & 0xFF8) << 7);
1435 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001436 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001437 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001438 write16le(Loc, SA - P);
1439 break;
1440 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001441 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001442 write32le(Loc, SA - P);
1443 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001444 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001445 write64le(Loc, SA - P);
1446 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001447 case R_AARCH64_TSTBR14: {
1448 uint64_t X = SA - P;
1449 checkInt<16>(X, Type);
1450 or32le(Loc, (X & 0xFFFC) << 3);
1451 break;
1452 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001453 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1454 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1455 checkInt<24>(V, Type);
1456 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1457 break;
1458 }
1459 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1460 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1461 updateAArch64Add(Loc, V & 0xFFF);
1462 break;
1463 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001464 default:
George Rimar57610422016-03-11 14:43:02 +00001465 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001466 }
1467}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001468
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001469size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
1470 uint32_t Type, uint64_t P, uint64_t SA,
1471 const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001472 // TLSDESC Global-Dynamic relocation are in the form:
1473 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1474 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1475 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1476 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1477 // And it can optimized to:
1478 // movz x0, #0x0, lsl #16
1479 // movk x0, #0x10
1480 // nop
1481 // nop
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001482 SA = S.getVA<ELF64LE>();
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001483 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1484 uint64_t X = SA + TPOff;
1485 checkUInt<32>(X, Type);
1486
1487 uint32_t NewInst;
1488 switch (Type) {
1489 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1490 case R_AARCH64_TLSDESC_CALL:
1491 // nop
1492 NewInst = 0xd503201f;
1493 break;
1494 case R_AARCH64_TLSDESC_ADR_PAGE21:
1495 // movz
1496 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1497 break;
1498 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1499 // movk
1500 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1501 break;
1502 default:
George Rimar777f9632016-03-12 08:31:34 +00001503 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001504 }
1505 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001506
1507 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001508}
1509
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001510size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
1511 uint32_t Type, uint64_t P, uint64_t SA,
1512 const SymbolBody &S) const {
1513 SA = S.getVA<ELF64LE>();
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001514 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1515 uint64_t X = SA + TPOff;
1516 checkUInt<32>(X, Type);
1517
George Rimar4d1d16d2016-03-06 06:16:05 +00001518 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001519 uint32_t NewInst;
1520 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1521 // Generate movz.
1522 unsigned RegNo = (Inst & 0x1f);
1523 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1524 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1525 // Generate movk
1526 unsigned RegNo = (Inst & 0x1f);
1527 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1528 } else {
George Rimar777f9632016-03-12 08:31:34 +00001529 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001530 }
1531 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001532
1533 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001534}
1535
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001536// Implementing relocations for AMDGPU is low priority since most
1537// programs don't use relocations now. Thus, this function is not
1538// actually called (relocateOne is called for each relocation).
1539// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001540void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1541 uint64_t P, uint64_t SA, uint64_t ZA,
1542 uint8_t *PairedLoc) const {
George Rimar57610422016-03-11 14:43:02 +00001543 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001544}
1545
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001546template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001547 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001548 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001549 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001550 PltEntrySize = 16;
1551 PltZeroSize = 32;
1552 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001553 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001554 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001555 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001556}
1557
1558template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001559uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001560 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1561 return R_MIPS_REL32;
1562 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001563 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001564 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001565 // Keep it going with a dummy value so that we can find more reloc errors.
1566 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001567}
1568
1569template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001570void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001571 typedef typename ELFT::Off Elf_Off;
1572 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001573
1574 // Set the MSB of the second GOT slot. This is not required by any
1575 // MIPS ABI documentation, though.
1576 //
1577 // There is a comment in glibc saying that "The MSB of got[1] of a
1578 // gnu object is set to identify gnu objects," and in GNU gold it
1579 // says "the second entry will be used by some runtime loaders".
1580 // But how this field is being used is unclear.
1581 //
1582 // We are not really willing to mimic other linkers behaviors
1583 // without understanding why they do that, but because all files
1584 // generated by GNU tools have this special GOT value, and because
1585 // we've been doing this for years, it is probably a safe bet to
1586 // keep doing this for now. We really need to revisit this to see
1587 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001588 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001589 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001590}
1591
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001592template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001593void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1594 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001595}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001596
Simon Atanasyan35031192015-12-15 06:06:34 +00001597static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001598
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001599template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001600static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001601 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001602 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001603 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001604 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1605 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001606 checkAlignment<(1 << SHIFT)>(S + A, Type);
1607 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001608 checkInt<BSIZE + SHIFT>(V, Type);
1609 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001610}
1611
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001612template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001613static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001614 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001615 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001616}
1617
Simon Atanasyan3b377852016-03-04 10:55:20 +00001618template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001619static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1620 uint32_t Instr = read32<E>(Loc);
1621 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1622}
1623
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001624template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1625 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1626}
1627
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001628template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001629static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001630 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001631}
1632
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001633template <class ELFT>
1634void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1635 const endianness E = ELFT::TargetEndianness;
1636 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1637 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1638 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1639 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1640 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1641 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1642 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1643 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1644 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001645 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001646 writeMipsLo16<E>(Buf + 4, Got);
1647 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001648}
1649
1650template <class ELFT>
1651void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1652 uint64_t PltEntryAddr, int32_t Index,
1653 unsigned RelOff) const {
1654 const endianness E = ELFT::TargetEndianness;
1655 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1656 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1657 write32<E>(Buf + 8, 0x03200008); // jr $25
1658 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001659 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001660 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1661 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001662}
1663
1664template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001665bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001666 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001667}
1668
1669template <class ELFT>
1670bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyand040a582016-02-25 16:19:15 +00001671 return needsPlt<ELFT>(Type, S) || refersToGotEntry(Type);
1672}
1673
1674template <class ELFT>
1675bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1676 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001677}
1678
1679template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001680bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1681 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001682}
1683
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001684template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001685void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001686 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001687 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001688 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001689 switch (Type) {
1690 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001691 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001692 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001693 case R_MIPS_26: {
1694 uint32_t Instr = read32<E>(Loc);
1695 // FIXME (simon): If the relocation target symbol is not a PLT entry
1696 // we should use another expression for calculation:
1697 // ((A << 2) | (P & 0xf0000000)) >> 2
1698 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1699 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1700 break;
1701 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001702 case R_MIPS_CALL16:
1703 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001704 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001705 if (Type == R_MIPS_GOT16)
1706 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001707 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001708 break;
1709 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001710 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001711 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001712 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001713 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001714 break;
1715 }
1716 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001717 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001718 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001719 case R_MIPS_HI16:
1720 if (PairedLoc)
1721 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1722 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001723 warning("can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001724 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001725 }
1726 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001727 case R_MIPS_JALR:
1728 // Ignore this optimization relocation for now
1729 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001730 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001731 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001732 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001733 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001734 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001735 break;
1736 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001737 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001738 break;
1739 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001740 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001741 break;
1742 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001743 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001744 break;
1745 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001746 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001747 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001748 case R_MIPS_PCHI16:
1749 if (PairedLoc)
1750 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1751 else {
George Rimarca1d1fb2016-03-15 14:00:22 +00001752 warning("can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001753 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001754 }
1755 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001756 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001757 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001758 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001759 default:
George Rimar57610422016-03-11 14:43:02 +00001760 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001761 }
1762}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001763
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001764template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001765bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001766 return Type == R_MIPS_JALR;
1767}
1768
1769template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001770bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1771 switch (Type) {
1772 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001773 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001774 case R_MIPS_26:
1775 case R_MIPS_32:
1776 case R_MIPS_64:
1777 case R_MIPS_HI16:
1778 case R_MIPS_LO16:
1779 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780 }
1781}
1782
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001783// _gp is a MIPS-specific ABI-defined symbol which points to
1784// a location that is relative to GOT. This function returns
1785// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001786template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001787 unsigned GPOffset = 0x7ff0;
1788 if (uint64_t V = Out<ELFT>::Got->getVA())
1789 return V + GPOffset;
1790 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001791}
1792
1793template uint32_t getMipsGpAddr<ELF32LE>();
1794template uint32_t getMipsGpAddr<ELF32BE>();
1795template uint64_t getMipsGpAddr<ELF64LE>();
1796template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001797
1798template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1799 const SymbolBody &) const;
1800template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1801 const SymbolBody &) const;
1802template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1803 const SymbolBody &) const;
1804template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1805 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001806
1807template TargetInfo::PltNeed
1808TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1809template TargetInfo::PltNeed
1810TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1811template TargetInfo::PltNeed
1812TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1813template TargetInfo::PltNeed
1814TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001815}
1816}