blob: 83cfef70dfe5882f265489823c0a406cfcb2c67f [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);
Rui Ueyama21923992016-02-01 23:28:21 +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);
Rui Ueyama21923992016-02-01 23:28:21 +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);
Rui Ueyama21923992016-02-01 23:28:21 +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);
Rui Ueyama21923992016-02-01 23:28:21 +000070 error("Improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
George Rimara07ff662015-12-21 10:12:06 +000073template <class ELFT> bool isGnuIFunc(const SymbolBody &S) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000074 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S))
George Rimara07ff662015-12-21 10:12:06 +000075 return SS->Sym.getType() == STT_GNU_IFUNC;
76 return false;
77}
78
Rui Ueyamaefc23de2015-10-14 21:30:32 +000079namespace {
80class X86TargetInfo final : public TargetInfo {
81public:
82 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000083 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000084 uint32_t getDynRel(uint32_t Type) const override;
85 uint32_t getTlsGotRel(uint32_t Type) const override;
86 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
87 bool isTlsLocalDynamicRel(uint32_t Type) const override;
88 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
89 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000090 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000091 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000092 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
93 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000094 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000095 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000096 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000097 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000098 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000099 uint64_t SA, uint64_t ZA = 0,
100 uint8_t *PairedLoc = nullptr) const override;
George Rimar7b8850f2016-03-06 06:09:50 +0000101 size_t relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
102 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000103 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000104 bool refersToGotEntry(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000105
106private:
107 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
108 uint64_t SA) const;
109 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
110 uint64_t SA) const;
111 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
112 uint64_t SA) const;
George Rimar98b060d2016-03-06 06:01:07 +0000113 void relocateTlsIeToLe(uint32_t Type, uint8_t *Loc, uint8_t *BufEnd,
George Rimar6f17e092015-12-17 09:32:21 +0000114 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000115};
116
117class X86_64TargetInfo final : public TargetInfo {
118public:
119 X86_64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000120 uint32_t getTlsGotRel(uint32_t Type) const override;
121 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
122 bool isTlsLocalDynamicRel(uint32_t Type) const override;
123 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
124 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000125 void writeGotPltHeader(uint8_t *Buf) const override;
126 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000127 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000128 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
129 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000130 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000131 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000132 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000133 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000134 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000135 uint64_t SA, uint64_t ZA = 0,
136 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000137 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000138 bool isSizeRel(uint32_t Type) const override;
George Rimar7b8850f2016-03-06 06:09:50 +0000139 size_t relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
140 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000141
142private:
143 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
145 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000147 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000149 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
150 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000151};
152
Davide Italiano8c3444362016-01-11 19:45:33 +0000153class PPCTargetInfo final : public TargetInfo {
154public:
155 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000156 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
157 uint64_t SA, uint64_t ZA = 0,
158 uint8_t *PairedLoc = nullptr) const override;
159 bool isRelRelative(uint32_t Type) const override;
160};
161
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000162class PPC64TargetInfo final : public TargetInfo {
163public:
164 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000165 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
166 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000167 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000168 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000169 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000170 uint64_t SA, uint64_t ZA = 0,
171 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172 bool isRelRelative(uint32_t Type) const override;
173};
174
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000175class AArch64TargetInfo final : public TargetInfo {
176public:
177 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000178 uint32_t getDynRel(uint32_t Type) const override;
179 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
180 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000181 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000182 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000183 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
184 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000185 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000186 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000187 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000188 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000189 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000190 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000191 uint64_t SA, uint64_t ZA = 0,
192 uint8_t *PairedLoc = nullptr) const override;
George Rimar7b8850f2016-03-06 06:09:50 +0000193 size_t relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
194 uint64_t SA, const SymbolBody *S) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000195
196private:
George Rimar98b060d2016-03-06 06:01:07 +0000197 void relocateTlsGdToLe(uint32_t Type, uint8_t *Loc, uint8_t *BufEnd,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000198 uint64_t P, uint64_t SA) const;
George Rimar98b060d2016-03-06 06:01:07 +0000199 void relocateTlsIeToLe(uint32_t Type, uint8_t *Loc, uint8_t *BufEnd,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000200 uint64_t P, uint64_t SA) const;
201
202 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000203};
204
Tom Stellard80efb162016-01-07 03:59:08 +0000205class AMDGPUTargetInfo final : public TargetInfo {
206public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000207 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000208 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
209 uint64_t SA, uint64_t ZA = 0,
210 uint8_t *PairedLoc = nullptr) const override;
211};
212
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000213template <class ELFT> class MipsTargetInfo final : public TargetInfo {
214public:
215 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000216 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000217 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
218 void writePltZero(uint8_t *Buf) const override;
219 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
220 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000221 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000222 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000223 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000224 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000225 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000226 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000227 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000228 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000229 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000230 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000231};
232} // anonymous namespace
233
Rui Ueyama91004392015-10-13 16:08:15 +0000234TargetInfo *createTarget() {
235 switch (Config->EMachine) {
236 case EM_386:
237 return new X86TargetInfo();
238 case EM_AARCH64:
239 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000240 case EM_AMDGPU:
241 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000242 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000243 switch (Config->EKind) {
244 case ELF32LEKind:
245 return new MipsTargetInfo<ELF32LE>();
246 case ELF32BEKind:
247 return new MipsTargetInfo<ELF32BE>();
248 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000249 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000250 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000251 case EM_PPC:
252 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000253 case EM_PPC64:
254 return new PPC64TargetInfo();
255 case EM_X86_64:
256 return new X86_64TargetInfo();
257 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000258 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000259}
260
Rafael Espindola01205f72015-09-22 18:19:46 +0000261TargetInfo::~TargetInfo() {}
262
George Rimar98b060d2016-03-06 06:01:07 +0000263bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000264 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000265 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000266
Rafael Espindolad405f472016-03-04 21:37:09 +0000267 // We know we are producing an executable.
268
269 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
270 // depending on the symbol being locally defined or not.
271 if (isTlsGlobalDynamicRel(Type))
272 return true;
273
274 // Local-Dynamic relocs can be relaxed to Local-Exec.
275 if (isTlsLocalDynamicRel(Type))
276 return true;
277
278 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
279 // defined.
280 if (isTlsInitialExecRel(Type))
Rafael Espindola1f04c442016-03-08 20:24:36 +0000281 return !canBePreempted(S, Type);
Rafael Espindolad405f472016-03-04 21:37:09 +0000282
George Rimar77d1cb12015-11-24 09:00:06 +0000283 return false;
284}
285
Igor Kudrinf6f45472015-11-10 08:39:27 +0000286uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
287
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000288bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
289
290template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
291 if (Config->Shared)
292 return false;
293 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
294 if (!SS)
295 return false;
296 return SS->Sym.getType() == STT_OBJECT;
297}
298
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000299template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000300bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000301 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000302}
303
George Rimarbfb7bf72015-12-21 10:00:12 +0000304bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000305bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000306bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000307bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000308
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000309bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000310
Rafael Espindola993f0272016-02-26 14:27:47 +0000311bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000312
313bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
314
315template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000316TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
317 const SymbolBody &S) const {
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000318 if (isGnuIFunc<ELFT>(S))
319 return Plt_Explicit;
Rafael Espindola1f04c442016-03-08 20:24:36 +0000320 if (canBePreempted(&S, Type) && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000321 return Plt_Explicit;
322
323 // This handles a non PIC program call to function in a shared library.
324 // In an ideal world, we could just report an error saying the relocation
325 // can overflow at runtime.
326 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
327 // libc.so.
328 //
329 // The general idea on how to handle such cases is to create a PLT entry
330 // and use that as the function value.
331 //
332 // For the static linking part, we just return true and everything else
333 // will use the the PLT entry as the address.
334 //
335 // The remaining problem is making sure pointer equality still works. We
336 // need the help of the dynamic linker for that. We let it know that we have
337 // a direct reference to a so symbol by creating an undefined symbol with a
338 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
339 // the value of the symbol we created. This is true even for got entries, so
340 // pointer equality is maintained. To avoid an infinite loop, the only entry
341 // that points to the real function is a dedicated got entry used by the
342 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
343 // R_386_JMP_SLOT, etc).
344 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
Rafael Espindola005d8442016-03-03 18:44:38 +0000345 if (!Config->Shared && SS->Sym.getType() == STT_FUNC &&
346 !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000347 return Plt_Implicit;
348
Rafael Espindola852860e2016-02-12 15:47:37 +0000349 return Plt_No;
350}
Rui Ueyama012eb782016-01-29 04:05:09 +0000351
George Rimar98b060d2016-03-06 06:01:07 +0000352bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000353
George Rimar98b060d2016-03-06 06:01:07 +0000354bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000355 return false;
356}
357
George Rimar98b060d2016-03-06 06:01:07 +0000358bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000359
George Rimar98b060d2016-03-06 06:01:07 +0000360bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000361 return false;
362}
363
George Rimar7b8850f2016-03-06 06:09:50 +0000364size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
365 uint64_t P, uint64_t SA,
366 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000367 return 0;
368}
George Rimar77d1cb12015-11-24 09:00:06 +0000369
Rafael Espindola7f074422015-09-22 21:35:51 +0000370X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000371 CopyRel = R_386_COPY;
372 GotRel = R_386_GLOB_DAT;
373 PltRel = R_386_JUMP_SLOT;
374 IRelativeRel = R_386_IRELATIVE;
375 RelativeRel = R_386_RELATIVE;
376 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000377 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
378 TlsOffsetRel = R_386_TLS_DTPOFF32;
379 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000380 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000381 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000382}
383
Rui Ueyamac516ae12016-01-29 02:33:45 +0000384void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000385 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
386}
387
Rui Ueyamac516ae12016-01-29 02:33:45 +0000388void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000389 // Entries in .got.plt initially points back to the corresponding
390 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000391 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000392}
Rafael Espindola01205f72015-09-22 18:19:46 +0000393
George Rimar98b060d2016-03-06 06:01:07 +0000394uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000395 if (Type == R_386_TLS_LE)
396 return R_386_TLS_TPOFF;
397 if (Type == R_386_TLS_LE_32)
398 return R_386_TLS_TPOFF32;
399 return Type;
400}
401
George Rimar98b060d2016-03-06 06:01:07 +0000402uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000403 if (Type == R_386_TLS_IE)
404 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000405 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000406}
407
George Rimar98b060d2016-03-06 06:01:07 +0000408bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000409 return Type == R_386_TLS_GD;
410}
411
George Rimar98b060d2016-03-06 06:01:07 +0000412bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000413 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
414}
415
George Rimar98b060d2016-03-06 06:01:07 +0000416bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000417 return Type == R_386_TLS_LDM;
418}
419
George Rimar98b060d2016-03-06 06:01:07 +0000420bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000421 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
422}
423
Rui Ueyama900e2d22016-01-29 03:51:49 +0000424void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000425 // Executable files and shared object files have
426 // separate procedure linkage tables.
427 if (Config->Shared) {
428 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000429 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000430 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
431 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000432 };
433 memcpy(Buf, V, sizeof(V));
434 return;
435 }
George Rimar648a2c32015-10-20 08:54:27 +0000436
George Rimar77b77792015-11-25 22:15:01 +0000437 const uint8_t PltData[] = {
438 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000439 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
440 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000441 };
442 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000443 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000444 write32le(Buf + 2, Got + 4);
445 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000446}
447
Rui Ueyama9398f862016-01-29 04:15:02 +0000448void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
449 uint64_t PltEntryAddr, int32_t Index,
450 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000451 const uint8_t Inst[] = {
452 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
453 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
454 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
455 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000456 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000457
George Rimar77b77792015-11-25 22:15:01 +0000458 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
459 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000460 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
461 : Out<ELF32LE>::Got->getVA();
462 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000463 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000464 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000465}
466
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000467bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
468 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000469}
470
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000471bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000472 if (S.IsTls && Type == R_386_TLS_GD)
Rafael Espindola1f04c442016-03-08 20:24:36 +0000473 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, Type);
George Rimar6f17e092015-12-17 09:32:21 +0000474 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000475 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000476 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000477}
478
Rafael Espindola993f0272016-02-26 14:27:47 +0000479bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
480 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000481}
482
George Rimarbfb7bf72015-12-21 10:00:12 +0000483bool X86TargetInfo::isGotRelative(uint32_t Type) const {
484 // This relocation does not require got entry,
485 // but it is relative to got and needs it to be created.
486 // Here we request for that.
487 return Type == R_386_GOTOFF;
488}
489
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000490bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
491 return Type == R_386_GOT32;
492}
493
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000494void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000495 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000496 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000497 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000498 case R_386_32:
499 add32le(Loc, SA);
500 break;
George Rimarffb67352016-01-19 11:00:48 +0000501 case R_386_GOT32: {
502 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
503 Out<ELF32LE>::Got->getNumEntries() * 4;
504 checkInt<32>(V, Type);
505 add32le(Loc, V);
506 break;
507 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000508 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000509 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000510 break;
George Rimar13934772015-11-25 20:20:31 +0000511 case R_386_GOTPC:
512 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
513 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000514 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000515 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000516 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000517 break;
George Rimar9db204a2015-12-02 09:58:20 +0000518 case R_386_TLS_GD:
519 case R_386_TLS_LDM:
520 case R_386_TLS_TPOFF: {
521 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
522 Out<ELF32LE>::Got->getNumEntries() * 4;
523 checkInt<32>(V, Type);
524 write32le(Loc, V);
525 break;
526 }
George Rimar6f17e092015-12-17 09:32:21 +0000527 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000528 case R_386_TLS_LDO_32:
529 write32le(Loc, SA);
530 break;
George Rimard23970f2015-11-25 20:41:53 +0000531 case R_386_TLS_LE:
532 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
533 break;
534 case R_386_TLS_LE_32:
535 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
536 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000537 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000538 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000539 }
540}
541
George Rimar98b060d2016-03-06 06:01:07 +0000542bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000543 return Config->Shared && Type == R_386_TLS_IE;
544}
545
George Rimar7b8850f2016-03-06 06:09:50 +0000546size_t X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
547 uint64_t P, uint64_t SA,
548 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000549 switch (Type) {
550 case R_386_TLS_GD:
Rafael Espindola1f04c442016-03-08 20:24:36 +0000551 if (canBePreempted(S, Type))
George Rimar2558e122015-12-09 09:55:54 +0000552 relocateTlsGdToIe(Loc, BufEnd, P, SA);
553 else
554 relocateTlsGdToLe(Loc, BufEnd, P, SA);
555 // The next relocation should be against __tls_get_addr, so skip it
556 return 1;
557 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000558 case R_386_TLS_IE:
559 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000560 return 0;
561 case R_386_TLS_LDM:
562 relocateTlsLdToLe(Loc, BufEnd, P, SA);
563 // The next relocation should be against __tls_get_addr, so skip it
564 return 1;
565 case R_386_TLS_LDO_32:
566 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
567 return 0;
568 }
569 llvm_unreachable("Unknown TLS optimization");
570}
571
572// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
573// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
574// how GD can be optimized to IE:
575// leal x@tlsgd(, %ebx, 1),
576// call __tls_get_addr@plt
577// Is converted to:
578// movl %gs:0, %eax
579// addl x@gotntpoff(%ebx), %eax
580void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
581 uint64_t SA) const {
582 const uint8_t Inst[] = {
583 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
584 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
585 };
586 memcpy(Loc - 3, Inst, sizeof(Inst));
587 relocateOne(Loc + 5, BufEnd, R_386_32, P,
588 SA - Out<ELF32LE>::Got->getVA() -
589 Out<ELF32LE>::Got->getNumEntries() * 4);
590}
591
592// GD can be optimized to LE:
593// leal x@tlsgd(, %ebx, 1),
594// call __tls_get_addr@plt
595// Can be converted to:
596// movl %gs:0,%eax
597// addl $x@ntpoff,%eax
598// But gold emits subl $foo@tpoff,%eax instead of addl.
599// These instructions are completely equal in behavior.
600// This method generates subl to be consistent with gold.
601void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
602 uint64_t SA) const {
603 const uint8_t Inst[] = {
604 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
605 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
606 };
607 memcpy(Loc - 3, Inst, sizeof(Inst));
608 relocateOne(Loc + 5, BufEnd, R_386_32, P,
609 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
610}
611
612// LD can be optimized to LE:
613// leal foo(%reg),%eax
614// call ___tls_get_addr
615// Is converted to:
616// movl %gs:0,%eax
617// nop
618// leal 0(%esi,1),%esi
619void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
620 uint64_t SA) const {
621 const uint8_t Inst[] = {
622 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
623 0x90, // nop
624 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
625 };
626 memcpy(Loc - 2, Inst, sizeof(Inst));
627}
628
George Rimar6f17e092015-12-17 09:32:21 +0000629// In some conditions, relocations can be optimized to avoid using GOT.
630// This function does that for Initial Exec to Local Exec case.
631// Read "ELF Handling For Thread-Local Storage, 5.1
632// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000633// by Ulrich Drepper for details.
George Rimar98b060d2016-03-06 06:01:07 +0000634void X86TargetInfo::relocateTlsIeToLe(uint32_t Type, uint8_t *Loc,
George Rimar6f17e092015-12-17 09:32:21 +0000635 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000636 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000637 // Ulrich's document section 6.2 says that @gotntpoff can
638 // be used with MOVL or ADDL instructions.
639 // @indntpoff is similar to @gotntpoff, but for use in
640 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000641 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000642 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000643 uint8_t Reg = (Loc[-1] >> 3) & 7;
644 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000645 if (Type == R_386_TLS_IE) {
646 // For R_386_TLS_IE relocation we perform the next transformations:
647 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
648 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
649 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
650 // First one is special because when EAX is used the sequence is 5 bytes
651 // long, otherwise it is 6 bytes.
652 if (*Op == 0xa1) {
653 *Op = 0xb8;
654 } else {
655 *Inst = IsMov ? 0xc7 : 0x81;
656 *Op = 0xc0 | ((*Op >> 3) & 7);
657 }
658 } else {
659 // R_386_TLS_GOTIE relocation can be optimized to
660 // R_386_TLS_LE so that it does not use GOT.
661 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
662 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
663 // Note: gold converts to ADDL instead of LEAL.
664 *Inst = IsMov ? 0xc7 : 0x8d;
665 if (IsMov)
666 *Op = 0xc0 | ((*Op >> 3) & 7);
667 else
668 *Op = 0x80 | Reg | (Reg << 3);
669 }
George Rimar2558e122015-12-09 09:55:54 +0000670 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
671}
672
Rafael Espindola7f074422015-09-22 21:35:51 +0000673X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000674 CopyRel = R_X86_64_COPY;
675 GotRel = R_X86_64_GLOB_DAT;
676 PltRel = R_X86_64_JUMP_SLOT;
677 RelativeRel = R_X86_64_RELATIVE;
678 IRelativeRel = R_X86_64_IRELATIVE;
679 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000680 TlsModuleIndexRel = R_X86_64_DTPMOD64;
681 TlsOffsetRel = R_X86_64_DTPOFF64;
682 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000683 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000684 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000685}
686
Rui Ueyamac516ae12016-01-29 02:33:45 +0000687void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000688 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
689}
690
Rui Ueyamac516ae12016-01-29 02:33:45 +0000691void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000692 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000693 write32le(Buf, Plt + 6);
694}
695
Rui Ueyama900e2d22016-01-29 03:51:49 +0000696void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000697 const uint8_t PltData[] = {
698 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
699 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
700 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
701 };
702 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000703 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
704 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
705 write32le(Buf + 2, Got - Plt + 2); // GOT+8
706 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000707}
Rafael Espindola01205f72015-09-22 18:19:46 +0000708
Rui Ueyama9398f862016-01-29 04:15:02 +0000709void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
710 uint64_t PltEntryAddr, int32_t Index,
711 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000712 const uint8_t Inst[] = {
713 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
714 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
715 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
716 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000717 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000718
George Rimar648a2c32015-10-20 08:54:27 +0000719 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
720 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000721 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000722}
723
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000724bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
725 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
726 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000727}
728
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000729bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
730 return Type == R_X86_64_GOTPCREL;
731}
732
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000733bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000734 if (Type == R_X86_64_TLSGD)
Rafael Espindola1f04c442016-03-08 20:24:36 +0000735 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, Type);
George Rimar77d1cb12015-11-24 09:00:06 +0000736 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000737 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000738 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000739}
740
George Rimar98b060d2016-03-06 06:01:07 +0000741uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000742 // No other types of TLS relocations requiring GOT should
743 // reach here.
744 assert(Type == R_X86_64_GOTTPOFF);
745 return R_X86_64_PC32;
746}
747
George Rimar98b060d2016-03-06 06:01:07 +0000748bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000749 return Type == R_X86_64_GOTTPOFF;
750}
751
George Rimar98b060d2016-03-06 06:01:07 +0000752bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000753 return Type == R_X86_64_TLSGD;
754}
755
George Rimar98b060d2016-03-06 06:01:07 +0000756bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000757 return Type == R_X86_64_TLSLD;
758}
759
George Rimar98b060d2016-03-06 06:01:07 +0000760bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000761 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
762 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000763}
764
Rafael Espindola993f0272016-02-26 14:27:47 +0000765bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
766 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000767}
Rafael Espindolac4010882015-09-22 20:54:08 +0000768
Rafael Espindolaae244002015-10-05 19:30:12 +0000769bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
770 switch (Type) {
771 default:
772 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000773 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000774 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000775 case R_X86_64_PC8:
776 case R_X86_64_PC16:
777 case R_X86_64_PC32:
778 case R_X86_64_PC64:
779 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000780 return true;
781 }
782}
783
Rui Ueyamac516ae12016-01-29 02:33:45 +0000784bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000785 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000786}
787
George Rimar6713cf82015-11-25 21:46:05 +0000788// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
789// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
790// how LD can be optimized to LE:
791// leaq bar@tlsld(%rip), %rdi
792// callq __tls_get_addr@PLT
793// leaq bar@dtpoff(%rax), %rcx
794// Is converted to:
795// .word 0x6666
796// .byte 0x66
797// mov %fs:0,%rax
798// leaq bar@tpoff(%rax), %rcx
799void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
800 uint64_t P, uint64_t SA) const {
801 const uint8_t Inst[] = {
802 0x66, 0x66, //.word 0x6666
803 0x66, //.byte 0x66
804 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
805 };
806 memcpy(Loc - 3, Inst, sizeof(Inst));
807}
808
809// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
810// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
811// how GD can be optimized to LE:
812// .byte 0x66
813// leaq x@tlsgd(%rip), %rdi
814// .word 0x6666
815// rex64
816// call __tls_get_addr@plt
817// Is converted to:
818// mov %fs:0x0,%rax
819// lea x@tpoff,%rax
820void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
821 uint64_t P, uint64_t SA) const {
822 const uint8_t Inst[] = {
823 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
824 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
825 };
826 memcpy(Loc - 4, Inst, sizeof(Inst));
827 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
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
841void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
842 uint64_t P, uint64_t SA) const {
843 const uint8_t Inst[] = {
844 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
845 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
846 };
847 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000848 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000849}
850
George Rimar77d1cb12015-11-24 09:00:06 +0000851// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000852// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000853// This function does that. Read "ELF Handling For Thread-Local Storage,
854// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
855// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000856void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
857 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000858 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
859 // used in MOVQ or ADDQ instructions only.
860 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
861 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
862 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
863 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
864 uint8_t *Prefix = Loc - 3;
865 uint8_t *Inst = Loc - 2;
866 uint8_t *RegSlot = Loc - 1;
867 uint8_t Reg = Loc[-1] >> 3;
868 bool IsMov = *Inst == 0x8b;
869 bool RspAdd = !IsMov && Reg == 4;
870 // r12 and rsp registers requires special handling.
871 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
872 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
873 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
874 // The same true for rsp. So we convert to addq for them, saving 1 byte that
875 // we dont have.
876 if (RspAdd)
877 *Inst = 0x81;
878 else
879 *Inst = IsMov ? 0xc7 : 0x8d;
880 if (*Prefix == 0x4c)
881 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
882 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
883 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
884}
885
George Rimar6713cf82015-11-25 21:46:05 +0000886// This function applies a TLS relocation with an optimization as described
887// in the Ulrich's document. As a result of rewriting instructions at the
888// relocation target, relocations immediately follow the TLS relocation (which
889// would be applied to rewritten instructions) may have to be skipped.
890// This function returns a number of relocations that need to be skipped.
George Rimar7b8850f2016-03-06 06:09:50 +0000891size_t X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
892 uint64_t P, uint64_t SA,
893 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000894 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000895 case R_X86_64_DTPOFF32:
896 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
897 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000898 case R_X86_64_GOTTPOFF:
899 relocateTlsIeToLe(Loc, BufEnd, P, SA);
900 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000901 case R_X86_64_TLSGD: {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000902 if (canBePreempted(S, Type))
George Rimar25411f252015-12-04 11:20:13 +0000903 relocateTlsGdToIe(Loc, BufEnd, P, SA);
904 else
905 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000906 // The next relocation should be against __tls_get_addr, so skip it
907 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000908 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000909 case R_X86_64_TLSLD:
910 relocateTlsLdToLe(Loc, BufEnd, P, SA);
911 // The next relocation should be against __tls_get_addr, so skip it
912 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000913 }
914 llvm_unreachable("Unknown TLS optimization");
915}
916
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000917void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000918 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000919 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000920 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000921 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000922 checkUInt<32>(SA, Type);
923 write32le(Loc, SA);
924 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000925 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000926 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000927 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000928 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000929 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000930 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000931 write64le(Loc, SA);
932 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000933 case R_X86_64_DTPOFF32:
934 write32le(Loc, SA);
935 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000936 case R_X86_64_GOTPCREL:
937 case R_X86_64_PC32:
938 case R_X86_64_PLT32:
939 case R_X86_64_TLSGD:
940 case R_X86_64_TLSLD:
941 write32le(Loc, SA - P);
942 break;
George Rimar48651482015-12-11 08:59:37 +0000943 case R_X86_64_SIZE32:
944 write32le(Loc, ZA);
945 break;
946 case R_X86_64_SIZE64:
947 write64le(Loc, ZA);
948 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000949 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000950 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000951 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000952 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000953 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000954 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000955 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000956 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000957 }
958}
959
Hal Finkel3c8cc672015-10-12 20:56:18 +0000960// Relocation masks following the #lo(value), #hi(value), #ha(value),
961// #higher(value), #highera(value), #highest(value), and #highesta(value)
962// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
963// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000964static uint16_t applyPPCLo(uint64_t V) { return V; }
965static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
966static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
967static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
968static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000969static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000970static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
971
Davide Italiano8c3444362016-01-11 19:45:33 +0000972PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000973bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
974
975void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
976 uint64_t P, uint64_t SA, uint64_t ZA,
977 uint8_t *PairedLoc) const {
978 switch (Type) {
979 case R_PPC_ADDR16_HA:
980 write16be(Loc, applyPPCHa(SA));
981 break;
982 case R_PPC_ADDR16_LO:
983 write16be(Loc, applyPPCLo(SA));
984 break;
985 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000986 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000987 }
988}
989
Rafael Espindolac4010882015-09-22 20:54:08 +0000990PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000991 GotRel = R_PPC64_GLOB_DAT;
992 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000993 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000994
995 // We need 64K pages (at least under glibc/Linux, the loader won't
996 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000997 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000998
999 // The PPC64 ELF ABI v1 spec, says:
1000 //
1001 // It is normally desirable to put segments with different characteristics
1002 // in separate 256 Mbyte portions of the address space, to give the
1003 // operating system full paging flexibility in the 64-bit address space.
1004 //
1005 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1006 // use 0x10000000 as the starting address.
1007 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001008}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001009
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001010uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001011 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1012 // order. The TOC starts where the first of these sections starts.
1013
1014 // FIXME: This obviously does not do the right thing when there is no .got
1015 // section, but there is a .toc or .tocbss section.
1016 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1017 if (!TocVA)
1018 TocVA = Out<ELF64BE>::Plt->getVA();
1019
1020 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1021 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1022 // code (crt1.o) assumes that you can get from the TOC base to the
1023 // start of the .toc section with only a single (signed) 16-bit relocation.
1024 return TocVA + 0x8000;
1025}
1026
Rui Ueyama9398f862016-01-29 04:15:02 +00001027void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1028 uint64_t PltEntryAddr, int32_t Index,
1029 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1031
1032 // FIXME: What we should do, in theory, is get the offset of the function
1033 // descriptor in the .opd section, and use that as the offset from %r2 (the
1034 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1035 // be a pointer to the function descriptor in the .opd section. Using
1036 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1037
Hal Finkelfa92f682015-10-13 21:47:34 +00001038 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001039 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1040 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1041 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1042 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1043 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1044 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1045 write32be(Buf + 28, 0x4e800420); // bctr
1046}
1047
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001048bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001049 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001050 return true;
1051
1052 switch (Type) {
1053 default: return false;
1054 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001056 case R_PPC64_GOT16_HA:
1057 case R_PPC64_GOT16_HI:
1058 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059 case R_PPC64_GOT16_LO_DS:
1060 return true;
1061 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001062}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063
Rafael Espindola993f0272016-02-26 14:27:47 +00001064bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001066 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001067}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068
Hal Finkelbe0823d2015-10-12 20:58:52 +00001069bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1070 switch (Type) {
1071 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001072 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001073 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001074 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001075 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001076 }
1077}
1078
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001079void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001080 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001081 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082 uint64_t TB = getPPC64TocBase();
1083
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1085 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001086 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001087 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1088 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001089 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1090 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001091 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1092 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001093 default: break;
1094 }
1095
Hal Finkel3c8cc672015-10-12 20:56:18 +00001096 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001097 case R_PPC64_ADDR14: {
1098 checkAlignment<4>(SA, Type);
1099 // Preserve the AA/LK bits in the branch instruction
1100 uint8_t AALK = Loc[3];
1101 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1102 break;
1103 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001105 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001106 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001107 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001108 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001109 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001110 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001112 case R_PPC64_ADDR16_HA:
1113 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 break;
1115 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001118 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
1121 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001122 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 break;
1124 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001129 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001130 case R_PPC64_ADDR16_LO:
1131 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001133 case R_PPC64_ADDR16_LO_DS:
1134 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001135 break;
1136 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001137 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001140 case R_PPC64_ADDR64:
1141 write64be(Loc, SA);
1142 break;
1143 case R_PPC64_REL16_HA:
1144 write16be(Loc, applyPPCHa(SA - P));
1145 break;
1146 case R_PPC64_REL16_HI:
1147 write16be(Loc, applyPPCHi(SA - P));
1148 break;
1149 case R_PPC64_REL16_LO:
1150 write16be(Loc, applyPPCLo(SA - P));
1151 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001152 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001153 // If we have an undefined weak symbol, we might get here with a symbol
1154 // address of zero. That could overflow, but the code must be unreachable,
1155 // so don't bother doing anything at all.
1156 if (!SA)
1157 break;
1158
Hal Finkeldaedc122015-10-12 23:16:53 +00001159 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1160 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001161 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001162
1163 if (!InPlt && Out<ELF64BE>::Opd) {
1164 // If this is a local call, and we currently have the address of a
1165 // function-descriptor, get the underlying code address instead.
1166 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1167 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001168 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001169
1170 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001171 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001172 }
1173
Hal Finkel3c8cc672015-10-12 20:56:18 +00001174 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001175 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001176 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001177
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001178 uint32_t Nop = 0x60000000;
1179 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1180 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001181 break;
1182 }
1183 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001184 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001185 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001186 break;
1187 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001188 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001189 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001190 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001191 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001192 break;
1193 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001194 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001195 }
1196}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001197
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001198AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001199 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001200 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001201 IRelativeRel = R_AARCH64_IRELATIVE;
1202 GotRel = R_AARCH64_GLOB_DAT;
1203 PltRel = R_AARCH64_JUMP_SLOT;
1204 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001205 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1206 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001207 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001208 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001209 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001210}
George Rimar648a2c32015-10-20 08:54:27 +00001211
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001212bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001213 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001214 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
Rafael Espindola57ca2702016-02-24 20:52:58 +00001215 Type == R_AARCH64_LDST32_ABS_LO12_NC ||
Rafael Espindola47ed5422016-02-24 21:48:06 +00001216 Type == R_AARCH64_LDST64_ABS_LO12_NC ||
Rafael Espindolaa598a3a2016-02-24 22:07:12 +00001217 Type == R_AARCH64_ADD_ABS_LO12_NC || Type == R_AARCH64_CALL26;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001218}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001219
George Rimar98b060d2016-03-06 06:01:07 +00001220bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001221 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1222 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1223 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1224 Type == R_AARCH64_TLSDESC_CALL;
1225}
1226
George Rimar98b060d2016-03-06 06:01:07 +00001227bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001228 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1229 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1230}
1231
George Rimar98b060d2016-03-06 06:01:07 +00001232uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001233 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1234 return Type;
1235 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001236 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001237 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001238 // Keep it going with a dummy value so that we can find more reloc errors.
1239 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001240}
1241
Rui Ueyamac516ae12016-01-29 02:33:45 +00001242void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001243 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1244}
1245
Rui Ueyama900e2d22016-01-29 03:51:49 +00001246void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001247 const uint8_t PltData[] = {
1248 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1249 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1250 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1251 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1252 0x20, 0x02, 0x1f, 0xd6, // br x17
1253 0x1f, 0x20, 0x03, 0xd5, // nop
1254 0x1f, 0x20, 0x03, 0xd5, // nop
1255 0x1f, 0x20, 0x03, 0xd5 // nop
1256 };
1257 memcpy(Buf, PltData, sizeof(PltData));
1258
Rui Ueyama900e2d22016-01-29 03:51:49 +00001259 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1260 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1261 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1262 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1263 Got + 16);
1264 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1265 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001266}
1267
Rui Ueyama9398f862016-01-29 04:15:02 +00001268void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1269 uint64_t PltEntryAddr, int32_t Index,
1270 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001271 const uint8_t Inst[] = {
1272 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1273 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1274 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1275 0x20, 0x02, 0x1f, 0xd6 // br x17
1276 };
1277 memcpy(Buf, Inst, sizeof(Inst));
1278
1279 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1280 GotEntryAddr);
1281 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1282 GotEntryAddr);
1283 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1284 GotEntryAddr);
1285}
1286
George Rimar98b060d2016-03-06 06:01:07 +00001287uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001288 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001289 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1290 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001291}
1292
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001293bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001294 switch (Type) {
1295 default:
1296 return false;
1297 case R_AARCH64_ABS16:
1298 case R_AARCH64_ABS32:
1299 case R_AARCH64_ABS64:
1300 case R_AARCH64_ADD_ABS_LO12_NC:
1301 case R_AARCH64_ADR_PREL_LO21:
1302 case R_AARCH64_ADR_PREL_PG_HI21:
1303 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001304 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001305 case R_AARCH64_LDST32_ABS_LO12_NC:
1306 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001307 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001308 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001309 }
1310}
1311
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001312bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001313 switch (Type) {
1314 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1315 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1316 case R_AARCH64_ADR_GOT_PAGE:
1317 case R_AARCH64_LD64_GOT_LO12_NC:
1318 return true;
1319 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001320 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001321 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001322}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001323
Rafael Espindola993f0272016-02-26 14:27:47 +00001324bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001325 switch (Type) {
1326 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001327 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001328 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001329 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001330 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001331 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001332 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001333 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001334}
Davide Italiano1d750a62015-09-27 08:45:38 +00001335
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001336static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001337 uint32_t ImmLo = (Imm & 0x3) << 29;
1338 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1339 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001340 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001341}
1342
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001343static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1344 or32le(L, (Imm & 0xFFF) << 10);
1345}
1346
Davide Italiano318ca222015-10-02 22:13:51 +00001347// Page(Expr) is the page address of the expression Expr, defined
1348// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001349// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001350static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001351 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001352}
1353
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001354void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001355 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001356 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001357 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001358 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001359 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001360 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001361 break;
1362 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001363 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001364 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001365 break;
1366 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001367 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001368 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001369 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001370 // This relocation stores 12 bits and there's no instruction
1371 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001372 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1373 // bits in Loc are zero.
1374 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001375 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001376 case R_AARCH64_ADR_GOT_PAGE: {
1377 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1378 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001379 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001380 break;
1381 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001382 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001383 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001384 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001385 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001386 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001387 }
George Rimar3d737e42016-01-13 13:04:46 +00001388 case R_AARCH64_ADR_PREL_PG_HI21:
1389 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001390 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001391 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001392 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001393 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001394 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001395 case R_AARCH64_CALL26:
1396 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001397 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001398 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001399 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1400 break;
1401 }
George Rimar4102bfb2016-01-11 14:22:00 +00001402 case R_AARCH64_CONDBR19: {
1403 uint64_t X = SA - P;
1404 checkInt<21>(X, Type);
1405 or32le(Loc, (X & 0x1FFFFC) << 3);
1406 break;
1407 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001408 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001409 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001410 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001411 or32le(Loc, (SA & 0xFF8) << 7);
1412 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001413 case R_AARCH64_LDST128_ABS_LO12_NC:
1414 or32le(Loc, (SA & 0x0FF8) << 6);
1415 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001416 case R_AARCH64_LDST16_ABS_LO12_NC:
1417 or32le(Loc, (SA & 0x0FFC) << 9);
1418 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001419 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001420 or32le(Loc, (SA & 0xFFF) << 10);
1421 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001422 case R_AARCH64_LDST32_ABS_LO12_NC:
1423 or32le(Loc, (SA & 0xFFC) << 8);
1424 break;
1425 case R_AARCH64_LDST64_ABS_LO12_NC:
1426 or32le(Loc, (SA & 0xFF8) << 7);
1427 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001428 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001429 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001430 write16le(Loc, SA - P);
1431 break;
1432 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001433 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001434 write32le(Loc, SA - P);
1435 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001436 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001437 write64le(Loc, SA - P);
1438 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001439 case R_AARCH64_TSTBR14: {
1440 uint64_t X = SA - P;
1441 checkInt<16>(X, Type);
1442 or32le(Loc, (X & 0xFFFC) << 3);
1443 break;
1444 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001445 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1446 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1447 checkInt<24>(V, Type);
1448 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1449 break;
1450 }
1451 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1452 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1453 updateAArch64Add(Loc, V & 0xFFF);
1454 break;
1455 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001456 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001457 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001458 }
1459}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001460
George Rimar7b8850f2016-03-06 06:09:50 +00001461size_t AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1462 uint64_t P, uint64_t SA,
1463 const SymbolBody *S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001464 switch (Type) {
1465 case R_AARCH64_TLSDESC_ADR_PAGE21:
1466 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1467 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1468 case R_AARCH64_TLSDESC_CALL: {
Rafael Espindola1f04c442016-03-08 20:24:36 +00001469 if (canBePreempted(S, Type))
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001470 fatal("Unsupported TLS optimization");
1471 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1472 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1473 return 0;
1474 }
1475 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1476 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1477 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1478 return 0;
1479 }
1480 llvm_unreachable("Unknown TLS optimization");
1481}
1482
1483// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1484// an executable and target is final (can notbe preempted).
George Rimar98b060d2016-03-06 06:01:07 +00001485void AArch64TargetInfo::relocateTlsGdToLe(uint32_t Type, uint8_t *Loc,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001486 uint8_t *BufEnd, uint64_t P,
1487 uint64_t SA) const {
1488 // TLSDESC Global-Dynamic relocation are in the form:
1489 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1490 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1491 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1492 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1493 // And it can optimized to:
1494 // movz x0, #0x0, lsl #16
1495 // movk x0, #0x10
1496 // nop
1497 // nop
1498
1499 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1500 uint64_t X = SA + TPOff;
1501 checkUInt<32>(X, Type);
1502
1503 uint32_t NewInst;
1504 switch (Type) {
1505 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1506 case R_AARCH64_TLSDESC_CALL:
1507 // nop
1508 NewInst = 0xd503201f;
1509 break;
1510 case R_AARCH64_TLSDESC_ADR_PAGE21:
1511 // movz
1512 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1513 break;
1514 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1515 // movk
1516 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1517 break;
1518 default:
1519 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1520 }
1521 write32le(Loc, NewInst);
1522}
1523
1524// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1525// (can not be preempted).
George Rimar98b060d2016-03-06 06:01:07 +00001526void AArch64TargetInfo::relocateTlsIeToLe(uint32_t Type, uint8_t *Loc,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001527 uint8_t *BufEnd, uint64_t P,
1528 uint64_t SA) const {
1529 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1530 uint64_t X = SA + TPOff;
1531 checkUInt<32>(X, Type);
1532
George Rimar4d1d16d2016-03-06 06:16:05 +00001533 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001534 uint32_t NewInst;
1535 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1536 // Generate movz.
1537 unsigned RegNo = (Inst & 0x1f);
1538 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1539 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1540 // Generate movk
1541 unsigned RegNo = (Inst & 0x1f);
1542 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1543 } else {
1544 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1545 }
1546 write32le(Loc, NewInst);
1547}
1548
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001549// Implementing relocations for AMDGPU is low priority since most
1550// programs don't use relocations now. Thus, this function is not
1551// actually called (relocateOne is called for each relocation).
1552// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001553void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1554 uint64_t P, uint64_t SA, uint64_t ZA,
1555 uint8_t *PairedLoc) const {
1556 llvm_unreachable("not implemented");
1557}
1558
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001559template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001560 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001561 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001562 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001563 PltEntrySize = 16;
1564 PltZeroSize = 32;
1565 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001566 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001567 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001568 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001569}
1570
1571template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001572uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001573 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1574 return R_MIPS_REL32;
1575 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001576 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001577 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001578 // Keep it going with a dummy value so that we can find more reloc errors.
1579 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001580}
1581
1582template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001583void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001584 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001585 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1586
1587 // Set the MSB of the second GOT slot. This is not required by any
1588 // MIPS ABI documentation, though.
1589 //
1590 // There is a comment in glibc saying that "The MSB of got[1] of a
1591 // gnu object is set to identify gnu objects," and in GNU gold it
1592 // says "the second entry will be used by some runtime loaders".
1593 // But how this field is being used is unclear.
1594 //
1595 // We are not really willing to mimic other linkers behaviors
1596 // without understanding why they do that, but because all files
1597 // generated by GNU tools have this special GOT value, and because
1598 // we've been doing this for years, it is probably a safe bet to
1599 // keep doing this for now. We really need to revisit this to see
1600 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001601 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001602 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001603}
1604
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001605template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001606void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1607 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001608}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001609
Simon Atanasyan35031192015-12-15 06:06:34 +00001610static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001611
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001612template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001613static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001614 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001615 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001616 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001617 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1618 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001619 checkAlignment<(1 << SHIFT)>(S + A, Type);
1620 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001621 checkInt<BSIZE + SHIFT>(V, Type);
1622 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001623}
1624
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001625template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001626static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001627 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001628 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001629}
1630
Simon Atanasyan3b377852016-03-04 10:55:20 +00001631template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001632static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1633 uint32_t Instr = read32<E>(Loc);
1634 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1635}
1636
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001637template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1638 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1639}
1640
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001641template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001642static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001643 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001644}
1645
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001646template <class ELFT>
1647void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1648 const endianness E = ELFT::TargetEndianness;
1649 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1650 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1651 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1652 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1653 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1654 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1655 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1656 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1657 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001658 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001659 writeMipsLo16<E>(Buf + 4, Got);
1660 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001661}
1662
1663template <class ELFT>
1664void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1665 uint64_t PltEntryAddr, int32_t Index,
1666 unsigned RelOff) const {
1667 const endianness E = ELFT::TargetEndianness;
1668 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1669 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1670 write32<E>(Buf + 8, 0x03200008); // jr $25
1671 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001672 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001673 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1674 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001675}
1676
1677template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001678bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001679 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001680}
1681
1682template <class ELFT>
1683bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyand040a582016-02-25 16:19:15 +00001684 return needsPlt<ELFT>(Type, S) || refersToGotEntry(Type);
1685}
1686
1687template <class ELFT>
1688bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1689 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001690}
1691
1692template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001693bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1694 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001695}
1696
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001697template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001698void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001699 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001700 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001701 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001702 switch (Type) {
1703 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001704 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001705 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001706 case R_MIPS_26: {
1707 uint32_t Instr = read32<E>(Loc);
1708 // FIXME (simon): If the relocation target symbol is not a PLT entry
1709 // we should use another expression for calculation:
1710 // ((A << 2) | (P & 0xf0000000)) >> 2
1711 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1712 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1713 break;
1714 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001715 case R_MIPS_CALL16:
1716 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001717 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001718 if (Type == R_MIPS_GOT16)
1719 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001720 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001721 break;
1722 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001723 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001724 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001725 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001726 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001727 break;
1728 }
1729 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001730 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001731 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001732 case R_MIPS_HI16:
1733 if (PairedLoc)
1734 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1735 else {
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001736 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001737 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001738 }
1739 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001740 case R_MIPS_JALR:
1741 // Ignore this optimization relocation for now
1742 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001743 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001744 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001745 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001746 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001747 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001748 break;
1749 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001750 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001751 break;
1752 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001753 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001754 break;
1755 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001756 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001757 break;
1758 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001759 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001760 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001761 case R_MIPS_PCHI16:
1762 if (PairedLoc)
1763 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1764 else {
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001765 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001766 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001767 }
1768 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001769 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001770 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001771 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001772 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001773 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001774 }
1775}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001776
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001777template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001778bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001779 return Type == R_MIPS_JALR;
1780}
1781
1782template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001783bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1784 switch (Type) {
1785 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001786 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001787 case R_MIPS_26:
1788 case R_MIPS_32:
1789 case R_MIPS_64:
1790 case R_MIPS_HI16:
1791 case R_MIPS_LO16:
1792 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001793 }
1794}
1795
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001796// _gp is a MIPS-specific ABI-defined symbol which points to
1797// a location that is relative to GOT. This function returns
1798// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001799template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001800 unsigned GPOffset = 0x7ff0;
1801 if (uint64_t V = Out<ELFT>::Got->getVA())
1802 return V + GPOffset;
1803 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001804}
1805
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001806template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1807template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1808template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1809template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1810
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001811template uint32_t getMipsGpAddr<ELF32LE>();
1812template uint32_t getMipsGpAddr<ELF32BE>();
1813template uint64_t getMipsGpAddr<ELF64LE>();
1814template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001815
1816template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1817 const SymbolBody &) const;
1818template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1819 const SymbolBody &) const;
1820template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1821 const SymbolBody &) const;
1822template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1823 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001824
1825template TargetInfo::PltNeed
1826TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1827template TargetInfo::PltNeed
1828TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1829template TargetInfo::PltNeed
1830TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1831template TargetInfo::PltNeed
1832TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001833}
1834}