blob: 64a3b4790411e2cb15a25d710db9032257cf9dde [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;
90 bool isTlsDynRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000091 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000092 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000093 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
94 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000095 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000096 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000097 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000098 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000099 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000100 uint64_t SA, uint64_t ZA = 0,
101 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000102 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
103 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000104 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000105 bool refersToGotEntry(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000106
107private:
108 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
109 uint64_t SA) const;
110 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
111 uint64_t SA) const;
112 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
113 uint64_t SA) const;
George Rimar98b060d2016-03-06 06:01:07 +0000114 void relocateTlsIeToLe(uint32_t Type, uint8_t *Loc, uint8_t *BufEnd,
George Rimar6f17e092015-12-17 09:32:21 +0000115 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000116};
117
118class X86_64TargetInfo final : public TargetInfo {
119public:
120 X86_64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000121 uint32_t getTlsGotRel(uint32_t Type) const override;
122 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
123 bool isTlsLocalDynamicRel(uint32_t Type) const override;
124 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
125 bool isTlsInitialExecRel(uint32_t Type) const override;
126 bool isTlsDynRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000127 void writeGotPltHeader(uint8_t *Buf) const override;
128 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000129 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000130 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
131 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000132 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000133 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000134 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000135 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000136 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000137 uint64_t SA, uint64_t ZA = 0,
138 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000139 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000140 bool isSizeRel(uint32_t Type) const override;
141 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
142 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000143
144private:
145 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
147 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000149 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
150 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000151 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
152 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000153};
154
Davide Italiano8c3444362016-01-11 19:45:33 +0000155class PPCTargetInfo final : public TargetInfo {
156public:
157 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000158 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
159 uint64_t SA, uint64_t ZA = 0,
160 uint8_t *PairedLoc = nullptr) const override;
161 bool isRelRelative(uint32_t Type) const override;
162};
163
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000164class PPC64TargetInfo final : public TargetInfo {
165public:
166 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000167 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
168 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000169 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000170 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000171 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000172 uint64_t SA, uint64_t ZA = 0,
173 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000174 bool isRelRelative(uint32_t Type) const override;
175};
176
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000177class AArch64TargetInfo final : public TargetInfo {
178public:
179 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000180 uint32_t getDynRel(uint32_t Type) const override;
181 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
182 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000183 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000184 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000185 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
186 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000187 uint32_t getTlsGotRel(uint32_t Type) const override;
188 bool isTlsDynRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000189 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000190 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000191 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000192 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000193 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000194 uint64_t SA, uint64_t ZA = 0,
195 uint8_t *PairedLoc = nullptr) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000196 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
197 uint64_t SA, const SymbolBody *S) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000198
199private:
George Rimar98b060d2016-03-06 06:01:07 +0000200 void relocateTlsGdToLe(uint32_t Type, uint8_t *Loc, uint8_t *BufEnd,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000201 uint64_t P, uint64_t SA) const;
George Rimar98b060d2016-03-06 06:01:07 +0000202 void relocateTlsIeToLe(uint32_t Type, uint8_t *Loc, uint8_t *BufEnd,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000203 uint64_t P, uint64_t SA) const;
204
205 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000206};
207
Tom Stellard80efb162016-01-07 03:59:08 +0000208class AMDGPUTargetInfo final : public TargetInfo {
209public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000210 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000211 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
212 uint64_t SA, uint64_t ZA = 0,
213 uint8_t *PairedLoc = nullptr) const override;
214};
215
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000216template <class ELFT> class MipsTargetInfo final : public TargetInfo {
217public:
218 MipsTargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000219 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000220 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
221 void writePltZero(uint8_t *Buf) const override;
222 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
223 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000224 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000225 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000226 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000227 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000228 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000229 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000230 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000231 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000232 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000233 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000234};
235} // anonymous namespace
236
Rui Ueyama91004392015-10-13 16:08:15 +0000237TargetInfo *createTarget() {
238 switch (Config->EMachine) {
239 case EM_386:
240 return new X86TargetInfo();
241 case EM_AARCH64:
242 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000243 case EM_AMDGPU:
244 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000245 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000246 switch (Config->EKind) {
247 case ELF32LEKind:
248 return new MipsTargetInfo<ELF32LE>();
249 case ELF32BEKind:
250 return new MipsTargetInfo<ELF32BE>();
251 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000252 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000253 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000254 case EM_PPC:
255 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000256 case EM_PPC64:
257 return new PPC64TargetInfo();
258 case EM_X86_64:
259 return new X86_64TargetInfo();
260 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000261 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000262}
263
Rafael Espindola01205f72015-09-22 18:19:46 +0000264TargetInfo::~TargetInfo() {}
265
George Rimar98b060d2016-03-06 06:01:07 +0000266bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000267 if (Config->Shared || (S && !S->isTls()))
268 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000269
Rafael Espindolad405f472016-03-04 21:37:09 +0000270 // We know we are producing an executable.
271
272 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
273 // depending on the symbol being locally defined or not.
274 if (isTlsGlobalDynamicRel(Type))
275 return true;
276
277 // Local-Dynamic relocs can be relaxed to Local-Exec.
278 if (isTlsLocalDynamicRel(Type))
279 return true;
280
281 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
282 // defined.
283 if (isTlsInitialExecRel(Type))
284 return !canBePreempted(S);
285
George Rimar77d1cb12015-11-24 09:00:06 +0000286 return false;
287}
288
Igor Kudrinf6f45472015-11-10 08:39:27 +0000289uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
290
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000291bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
292
293template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
294 if (Config->Shared)
295 return false;
296 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
297 if (!SS)
298 return false;
299 return SS->Sym.getType() == STT_OBJECT;
300}
301
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000302template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000303bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000304 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000305}
306
George Rimar98b060d2016-03-06 06:01:07 +0000307bool TargetInfo::isTlsDynRel(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama012eb782016-01-29 04:05:09 +0000308 return false;
309}
310
George Rimarbfb7bf72015-12-21 10:00:12 +0000311bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000312bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000313bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000314bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000315
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000316bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000317
Rafael Espindola993f0272016-02-26 14:27:47 +0000318bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000319
320bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
321
322template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +0000323TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
324 const SymbolBody &S) const {
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000325 if (isGnuIFunc<ELFT>(S))
326 return Plt_Explicit;
Rafael Espindola993f0272016-02-26 14:27:47 +0000327 if (canBePreempted(&S) && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000328 return Plt_Explicit;
329
330 // This handles a non PIC program call to function in a shared library.
331 // In an ideal world, we could just report an error saying the relocation
332 // can overflow at runtime.
333 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
334 // libc.so.
335 //
336 // The general idea on how to handle such cases is to create a PLT entry
337 // and use that as the function value.
338 //
339 // For the static linking part, we just return true and everything else
340 // will use the the PLT entry as the address.
341 //
342 // The remaining problem is making sure pointer equality still works. We
343 // need the help of the dynamic linker for that. We let it know that we have
344 // a direct reference to a so symbol by creating an undefined symbol with a
345 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
346 // the value of the symbol we created. This is true even for got entries, so
347 // pointer equality is maintained. To avoid an infinite loop, the only entry
348 // that points to the real function is a dedicated got entry used by the
349 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
350 // R_386_JMP_SLOT, etc).
351 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
Rafael Espindola005d8442016-03-03 18:44:38 +0000352 if (!Config->Shared && SS->Sym.getType() == STT_FUNC &&
353 !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000354 return Plt_Implicit;
355
Rafael Espindola852860e2016-02-12 15:47:37 +0000356 return Plt_No;
357}
Rui Ueyama012eb782016-01-29 04:05:09 +0000358
George Rimar98b060d2016-03-06 06:01:07 +0000359bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000360
George Rimar98b060d2016-03-06 06:01:07 +0000361bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000362 return false;
363}
364
George Rimar98b060d2016-03-06 06:01:07 +0000365bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000366
George Rimar98b060d2016-03-06 06:01:07 +0000367bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000368 return false;
369}
370
Rui Ueyamac516ae12016-01-29 02:33:45 +0000371unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
372 uint64_t P, uint64_t SA,
373 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000374 return 0;
375}
George Rimar77d1cb12015-11-24 09:00:06 +0000376
Rafael Espindola7f074422015-09-22 21:35:51 +0000377X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000378 CopyRel = R_386_COPY;
379 GotRel = R_386_GLOB_DAT;
380 PltRel = R_386_JUMP_SLOT;
381 IRelativeRel = R_386_IRELATIVE;
382 RelativeRel = R_386_RELATIVE;
383 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000384 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
385 TlsOffsetRel = R_386_TLS_DTPOFF32;
386 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000387 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000388 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000389}
390
Rui Ueyamac516ae12016-01-29 02:33:45 +0000391void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000392 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
393}
394
Rui Ueyamac516ae12016-01-29 02:33:45 +0000395void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000396 // Entries in .got.plt initially points back to the corresponding
397 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000398 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000399}
Rafael Espindola01205f72015-09-22 18:19:46 +0000400
George Rimar98b060d2016-03-06 06:01:07 +0000401uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000402 if (Type == R_386_TLS_LE)
403 return R_386_TLS_TPOFF;
404 if (Type == R_386_TLS_LE_32)
405 return R_386_TLS_TPOFF32;
406 return Type;
407}
408
George Rimar98b060d2016-03-06 06:01:07 +0000409uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000410 if (Type == R_386_TLS_IE)
411 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000412 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000413}
414
George Rimar98b060d2016-03-06 06:01:07 +0000415bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000416 return Type == R_386_TLS_GD;
417}
418
George Rimar98b060d2016-03-06 06:01:07 +0000419bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000420 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
421}
422
George Rimar98b060d2016-03-06 06:01:07 +0000423bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000424 return Type == R_386_TLS_LDM;
425}
426
George Rimar98b060d2016-03-06 06:01:07 +0000427bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000428 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
429}
430
George Rimar98b060d2016-03-06 06:01:07 +0000431bool X86TargetInfo::isTlsDynRel(uint32_t Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000432 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
433 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000434 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000435 if (Type == R_386_TLS_IE)
Rafael Espindola993f0272016-02-26 14:27:47 +0000436 return canBePreempted(&S);
George Rimar2558e122015-12-09 09:55:54 +0000437 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000438}
439
Rui Ueyama900e2d22016-01-29 03:51:49 +0000440void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000441 // Executable files and shared object files have
442 // separate procedure linkage tables.
443 if (Config->Shared) {
444 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000445 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000446 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
447 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000448 };
449 memcpy(Buf, V, sizeof(V));
450 return;
451 }
George Rimar648a2c32015-10-20 08:54:27 +0000452
George Rimar77b77792015-11-25 22:15:01 +0000453 const uint8_t PltData[] = {
454 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000455 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
456 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000457 };
458 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000459 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000460 write32le(Buf + 2, Got + 4);
461 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000462}
463
Rui Ueyama9398f862016-01-29 04:15:02 +0000464void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
465 uint64_t PltEntryAddr, int32_t Index,
466 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000467 const uint8_t Inst[] = {
468 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
469 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
470 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
471 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000472 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000473
George Rimar77b77792015-11-25 22:15:01 +0000474 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
475 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000476 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
477 : Out<ELF32LE>::Got->getVA();
478 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000479 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000480 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000481}
482
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000483bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
484 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000485}
486
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000487bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Davide Italiano255730c2016-03-04 01:55:28 +0000488 if (S.isTls() && Type == R_386_TLS_GD)
Rafael Espindola993f0272016-02-26 14:27:47 +0000489 return Target->canRelaxTls(Type, &S) && canBePreempted(&S);
George Rimar6f17e092015-12-17 09:32:21 +0000490 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000491 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000492 return Type == R_386_GOT32 || needsPlt<ELF32LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000493}
494
Rafael Espindola993f0272016-02-26 14:27:47 +0000495bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
496 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000497}
498
George Rimarbfb7bf72015-12-21 10:00:12 +0000499bool X86TargetInfo::isGotRelative(uint32_t Type) const {
500 // This relocation does not require got entry,
501 // but it is relative to got and needs it to be created.
502 // Here we request for that.
503 return Type == R_386_GOTOFF;
504}
505
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000506bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
507 return Type == R_386_GOT32;
508}
509
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000510void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000511 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000512 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000513 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000514 case R_386_32:
515 add32le(Loc, SA);
516 break;
George Rimarffb67352016-01-19 11:00:48 +0000517 case R_386_GOT32: {
518 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
519 Out<ELF32LE>::Got->getNumEntries() * 4;
520 checkInt<32>(V, Type);
521 add32le(Loc, V);
522 break;
523 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000524 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000525 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000526 break;
George Rimar13934772015-11-25 20:20:31 +0000527 case R_386_GOTPC:
528 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
529 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000530 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000531 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000532 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000533 break;
George Rimar9db204a2015-12-02 09:58:20 +0000534 case R_386_TLS_GD:
535 case R_386_TLS_LDM:
536 case R_386_TLS_TPOFF: {
537 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
538 Out<ELF32LE>::Got->getNumEntries() * 4;
539 checkInt<32>(V, Type);
540 write32le(Loc, V);
541 break;
542 }
George Rimar6f17e092015-12-17 09:32:21 +0000543 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000544 case R_386_TLS_LDO_32:
545 write32le(Loc, SA);
546 break;
George Rimard23970f2015-11-25 20:41:53 +0000547 case R_386_TLS_LE:
548 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
549 break;
550 case R_386_TLS_LE_32:
551 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
552 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000553 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000554 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000555 }
556}
557
George Rimar98b060d2016-03-06 06:01:07 +0000558bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000559 return Config->Shared && Type == R_386_TLS_IE;
560}
561
Rui Ueyamac516ae12016-01-29 02:33:45 +0000562unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
563 uint64_t P, uint64_t SA,
564 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000565 switch (Type) {
566 case R_386_TLS_GD:
Rafael Espindola993f0272016-02-26 14:27:47 +0000567 if (canBePreempted(S))
George Rimar2558e122015-12-09 09:55:54 +0000568 relocateTlsGdToIe(Loc, BufEnd, P, SA);
569 else
570 relocateTlsGdToLe(Loc, BufEnd, P, SA);
571 // The next relocation should be against __tls_get_addr, so skip it
572 return 1;
573 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000574 case R_386_TLS_IE:
575 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000576 return 0;
577 case R_386_TLS_LDM:
578 relocateTlsLdToLe(Loc, BufEnd, P, SA);
579 // The next relocation should be against __tls_get_addr, so skip it
580 return 1;
581 case R_386_TLS_LDO_32:
582 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
583 return 0;
584 }
585 llvm_unreachable("Unknown TLS optimization");
586}
587
588// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
589// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
590// how GD can be optimized to IE:
591// leal x@tlsgd(, %ebx, 1),
592// call __tls_get_addr@plt
593// Is converted to:
594// movl %gs:0, %eax
595// addl x@gotntpoff(%ebx), %eax
596void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
597 uint64_t SA) const {
598 const uint8_t Inst[] = {
599 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
600 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
601 };
602 memcpy(Loc - 3, Inst, sizeof(Inst));
603 relocateOne(Loc + 5, BufEnd, R_386_32, P,
604 SA - Out<ELF32LE>::Got->getVA() -
605 Out<ELF32LE>::Got->getNumEntries() * 4);
606}
607
608// GD can be optimized to LE:
609// leal x@tlsgd(, %ebx, 1),
610// call __tls_get_addr@plt
611// Can be converted to:
612// movl %gs:0,%eax
613// addl $x@ntpoff,%eax
614// But gold emits subl $foo@tpoff,%eax instead of addl.
615// These instructions are completely equal in behavior.
616// This method generates subl to be consistent with gold.
617void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
618 uint64_t SA) const {
619 const uint8_t Inst[] = {
620 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
621 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
622 };
623 memcpy(Loc - 3, Inst, sizeof(Inst));
624 relocateOne(Loc + 5, BufEnd, R_386_32, P,
625 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
626}
627
628// LD can be optimized to LE:
629// leal foo(%reg),%eax
630// call ___tls_get_addr
631// Is converted to:
632// movl %gs:0,%eax
633// nop
634// leal 0(%esi,1),%esi
635void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
636 uint64_t SA) const {
637 const uint8_t Inst[] = {
638 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
639 0x90, // nop
640 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
641 };
642 memcpy(Loc - 2, Inst, sizeof(Inst));
643}
644
George Rimar6f17e092015-12-17 09:32:21 +0000645// In some conditions, relocations can be optimized to avoid using GOT.
646// This function does that for Initial Exec to Local Exec case.
647// Read "ELF Handling For Thread-Local Storage, 5.1
648// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000649// by Ulrich Drepper for details.
George Rimar98b060d2016-03-06 06:01:07 +0000650void X86TargetInfo::relocateTlsIeToLe(uint32_t Type, uint8_t *Loc,
George Rimar6f17e092015-12-17 09:32:21 +0000651 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000652 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000653 // Ulrich's document section 6.2 says that @gotntpoff can
654 // be used with MOVL or ADDL instructions.
655 // @indntpoff is similar to @gotntpoff, but for use in
656 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000657 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000658 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000659 uint8_t Reg = (Loc[-1] >> 3) & 7;
660 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000661 if (Type == R_386_TLS_IE) {
662 // For R_386_TLS_IE relocation we perform the next transformations:
663 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
664 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
665 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
666 // First one is special because when EAX is used the sequence is 5 bytes
667 // long, otherwise it is 6 bytes.
668 if (*Op == 0xa1) {
669 *Op = 0xb8;
670 } else {
671 *Inst = IsMov ? 0xc7 : 0x81;
672 *Op = 0xc0 | ((*Op >> 3) & 7);
673 }
674 } else {
675 // R_386_TLS_GOTIE relocation can be optimized to
676 // R_386_TLS_LE so that it does not use GOT.
677 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
678 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
679 // Note: gold converts to ADDL instead of LEAL.
680 *Inst = IsMov ? 0xc7 : 0x8d;
681 if (IsMov)
682 *Op = 0xc0 | ((*Op >> 3) & 7);
683 else
684 *Op = 0x80 | Reg | (Reg << 3);
685 }
George Rimar2558e122015-12-09 09:55:54 +0000686 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
687}
688
Rafael Espindola7f074422015-09-22 21:35:51 +0000689X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000690 CopyRel = R_X86_64_COPY;
691 GotRel = R_X86_64_GLOB_DAT;
692 PltRel = R_X86_64_JUMP_SLOT;
693 RelativeRel = R_X86_64_RELATIVE;
694 IRelativeRel = R_X86_64_IRELATIVE;
695 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000696 TlsModuleIndexRel = R_X86_64_DTPMOD64;
697 TlsOffsetRel = R_X86_64_DTPOFF64;
698 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000699 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000700 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000701}
702
Rui Ueyamac516ae12016-01-29 02:33:45 +0000703void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000704 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
705}
706
Rui Ueyamac516ae12016-01-29 02:33:45 +0000707void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000708 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000709 write32le(Buf, Plt + 6);
710}
711
Rui Ueyama900e2d22016-01-29 03:51:49 +0000712void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000713 const uint8_t PltData[] = {
714 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
715 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
716 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
717 };
718 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000719 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
720 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
721 write32le(Buf + 2, Got - Plt + 2); // GOT+8
722 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000723}
Rafael Espindola01205f72015-09-22 18:19:46 +0000724
Rui Ueyama9398f862016-01-29 04:15:02 +0000725void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
726 uint64_t PltEntryAddr, int32_t Index,
727 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000728 const uint8_t Inst[] = {
729 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
730 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
731 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
732 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000733 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000734
George Rimar648a2c32015-10-20 08:54:27 +0000735 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
736 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000737 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000738}
739
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000740bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
741 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
742 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000743}
744
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000745bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
746 return Type == R_X86_64_GOTPCREL;
747}
748
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000749bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000750 if (Type == R_X86_64_TLSGD)
Rafael Espindola993f0272016-02-26 14:27:47 +0000751 return Target->canRelaxTls(Type, &S) && canBePreempted(&S);
George Rimar77d1cb12015-11-24 09:00:06 +0000752 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000753 return !canRelaxTls(Type, &S);
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000754 return refersToGotEntry(Type) || needsPlt<ELF64LE>(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000755}
756
George Rimar98b060d2016-03-06 06:01:07 +0000757uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000758 // No other types of TLS relocations requiring GOT should
759 // reach here.
760 assert(Type == R_X86_64_GOTTPOFF);
761 return R_X86_64_PC32;
762}
763
George Rimar98b060d2016-03-06 06:01:07 +0000764bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000765 return Type == R_X86_64_GOTTPOFF;
766}
767
George Rimar98b060d2016-03-06 06:01:07 +0000768bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000769 return Type == R_X86_64_TLSGD;
770}
771
George Rimar98b060d2016-03-06 06:01:07 +0000772bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000773 return Type == R_X86_64_TLSLD;
774}
775
George Rimar98b060d2016-03-06 06:01:07 +0000776bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000777 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_TLSLD;
778}
779
George Rimar98b060d2016-03-06 06:01:07 +0000780bool X86_64TargetInfo::isTlsDynRel(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000781 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000782}
783
Rafael Espindola993f0272016-02-26 14:27:47 +0000784bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
785 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000786}
Rafael Espindolac4010882015-09-22 20:54:08 +0000787
Rafael Espindolaae244002015-10-05 19:30:12 +0000788bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
789 switch (Type) {
790 default:
791 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000792 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000793 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000794 case R_X86_64_PC8:
795 case R_X86_64_PC16:
796 case R_X86_64_PC32:
797 case R_X86_64_PC64:
798 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000799 return true;
800 }
801}
802
Rui Ueyamac516ae12016-01-29 02:33:45 +0000803bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000804 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000805}
806
George Rimar6713cf82015-11-25 21:46:05 +0000807// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
808// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
809// how LD can be optimized to LE:
810// leaq bar@tlsld(%rip), %rdi
811// callq __tls_get_addr@PLT
812// leaq bar@dtpoff(%rax), %rcx
813// Is converted to:
814// .word 0x6666
815// .byte 0x66
816// mov %fs:0,%rax
817// leaq bar@tpoff(%rax), %rcx
818void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
819 uint64_t P, uint64_t SA) const {
820 const uint8_t Inst[] = {
821 0x66, 0x66, //.word 0x6666
822 0x66, //.byte 0x66
823 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
824 };
825 memcpy(Loc - 3, Inst, sizeof(Inst));
826}
827
828// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
829// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
830// how GD can be optimized to LE:
831// .byte 0x66
832// leaq x@tlsgd(%rip), %rdi
833// .word 0x6666
834// rex64
835// call __tls_get_addr@plt
836// Is converted to:
837// mov %fs:0x0,%rax
838// lea x@tpoff,%rax
839void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
840 uint64_t P, uint64_t SA) const {
841 const uint8_t Inst[] = {
842 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
843 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
844 };
845 memcpy(Loc - 4, Inst, sizeof(Inst));
846 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000847}
848
George Rimar25411f252015-12-04 11:20:13 +0000849// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
850// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
851// how GD can be optimized to IE:
852// .byte 0x66
853// leaq x@tlsgd(%rip), %rdi
854// .word 0x6666
855// rex64
856// call __tls_get_addr@plt
857// Is converted to:
858// mov %fs:0x0,%rax
859// addq x@tpoff,%rax
860void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
861 uint64_t P, uint64_t SA) const {
862 const uint8_t Inst[] = {
863 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
864 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
865 };
866 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000867 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000868}
869
George Rimar77d1cb12015-11-24 09:00:06 +0000870// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000871// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000872// This function does that. Read "ELF Handling For Thread-Local Storage,
873// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
874// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000875void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
876 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000877 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
878 // used in MOVQ or ADDQ instructions only.
879 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
880 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
881 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
882 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
883 uint8_t *Prefix = Loc - 3;
884 uint8_t *Inst = Loc - 2;
885 uint8_t *RegSlot = Loc - 1;
886 uint8_t Reg = Loc[-1] >> 3;
887 bool IsMov = *Inst == 0x8b;
888 bool RspAdd = !IsMov && Reg == 4;
889 // r12 and rsp registers requires special handling.
890 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
891 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
892 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
893 // The same true for rsp. So we convert to addq for them, saving 1 byte that
894 // we dont have.
895 if (RspAdd)
896 *Inst = 0x81;
897 else
898 *Inst = IsMov ? 0xc7 : 0x8d;
899 if (*Prefix == 0x4c)
900 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
901 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
902 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
903}
904
George Rimar6713cf82015-11-25 21:46:05 +0000905// This function applies a TLS relocation with an optimization as described
906// in the Ulrich's document. As a result of rewriting instructions at the
907// relocation target, relocations immediately follow the TLS relocation (which
908// would be applied to rewritten instructions) may have to be skipped.
909// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000910unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
911 uint32_t Type, uint64_t P, uint64_t SA,
912 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000913 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000914 case R_X86_64_DTPOFF32:
915 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
916 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000917 case R_X86_64_GOTTPOFF:
918 relocateTlsIeToLe(Loc, BufEnd, P, SA);
919 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000920 case R_X86_64_TLSGD: {
Rafael Espindola993f0272016-02-26 14:27:47 +0000921 if (canBePreempted(S))
George Rimar25411f252015-12-04 11:20:13 +0000922 relocateTlsGdToIe(Loc, BufEnd, P, SA);
923 else
924 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000925 // The next relocation should be against __tls_get_addr, so skip it
926 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000927 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000928 case R_X86_64_TLSLD:
929 relocateTlsLdToLe(Loc, BufEnd, P, SA);
930 // The next relocation should be against __tls_get_addr, so skip it
931 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000932 }
933 llvm_unreachable("Unknown TLS optimization");
934}
935
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000936void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000937 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000938 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000939 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000940 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000941 checkUInt<32>(SA, Type);
942 write32le(Loc, SA);
943 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000944 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000945 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000946 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000947 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000948 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000949 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000950 write64le(Loc, SA);
951 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000952 case R_X86_64_DTPOFF32:
953 write32le(Loc, SA);
954 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000955 case R_X86_64_GOTPCREL:
956 case R_X86_64_PC32:
957 case R_X86_64_PLT32:
958 case R_X86_64_TLSGD:
959 case R_X86_64_TLSLD:
960 write32le(Loc, SA - P);
961 break;
George Rimar48651482015-12-11 08:59:37 +0000962 case R_X86_64_SIZE32:
963 write32le(Loc, ZA);
964 break;
965 case R_X86_64_SIZE64:
966 write64le(Loc, ZA);
967 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000968 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000969 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000970 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000971 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000972 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000973 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000974 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000975 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000976 }
977}
978
Hal Finkel3c8cc672015-10-12 20:56:18 +0000979// Relocation masks following the #lo(value), #hi(value), #ha(value),
980// #higher(value), #highera(value), #highest(value), and #highesta(value)
981// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
982// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000983static uint16_t applyPPCLo(uint64_t V) { return V; }
984static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
985static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
986static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
987static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000988static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000989static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
990
Davide Italiano8c3444362016-01-11 19:45:33 +0000991PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000992bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
993
994void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
995 uint64_t P, uint64_t SA, uint64_t ZA,
996 uint8_t *PairedLoc) const {
997 switch (Type) {
998 case R_PPC_ADDR16_HA:
999 write16be(Loc, applyPPCHa(SA));
1000 break;
1001 case R_PPC_ADDR16_LO:
1002 write16be(Loc, applyPPCLo(SA));
1003 break;
1004 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001005 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +00001006 }
1007}
1008
Rafael Espindolac4010882015-09-22 20:54:08 +00001009PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001010 GotRel = R_PPC64_GLOB_DAT;
1011 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001012 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001013
1014 // We need 64K pages (at least under glibc/Linux, the loader won't
1015 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001016 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001017
1018 // The PPC64 ELF ABI v1 spec, says:
1019 //
1020 // It is normally desirable to put segments with different characteristics
1021 // in separate 256 Mbyte portions of the address space, to give the
1022 // operating system full paging flexibility in the 64-bit address space.
1023 //
1024 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1025 // use 0x10000000 as the starting address.
1026 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001027}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001028
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001029uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1031 // order. The TOC starts where the first of these sections starts.
1032
1033 // FIXME: This obviously does not do the right thing when there is no .got
1034 // section, but there is a .toc or .tocbss section.
1035 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1036 if (!TocVA)
1037 TocVA = Out<ELF64BE>::Plt->getVA();
1038
1039 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1040 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1041 // code (crt1.o) assumes that you can get from the TOC base to the
1042 // start of the .toc section with only a single (signed) 16-bit relocation.
1043 return TocVA + 0x8000;
1044}
1045
Rui Ueyama9398f862016-01-29 04:15:02 +00001046void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1047 uint64_t PltEntryAddr, int32_t Index,
1048 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001049 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1050
1051 // FIXME: What we should do, in theory, is get the offset of the function
1052 // descriptor in the .opd section, and use that as the offset from %r2 (the
1053 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1054 // be a pointer to the function descriptor in the .opd section. Using
1055 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1056
Hal Finkelfa92f682015-10-13 21:47:34 +00001057 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1059 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1060 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1061 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1062 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1063 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1064 write32be(Buf + 28, 0x4e800420); // bctr
1065}
1066
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001067bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001068 if (needsPlt<ELF64BE>(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001069 return true;
1070
1071 switch (Type) {
1072 default: return false;
1073 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001074 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001075 case R_PPC64_GOT16_HA:
1076 case R_PPC64_GOT16_HI:
1077 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 case R_PPC64_GOT16_LO_DS:
1079 return true;
1080 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001081}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082
Rafael Espindola993f0272016-02-26 14:27:47 +00001083bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001085 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001086}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087
Hal Finkelbe0823d2015-10-12 20:58:52 +00001088bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1089 switch (Type) {
1090 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001091 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001092 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001093 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001094 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001095 }
1096}
1097
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001098void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001099 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001100 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001101 uint64_t TB = getPPC64TocBase();
1102
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1104 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001105 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001106 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1107 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001108 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1109 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001110 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1111 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001112 default: break;
1113 }
1114
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001116 case R_PPC64_ADDR14: {
1117 checkAlignment<4>(SA, Type);
1118 // Preserve the AA/LK bits in the branch instruction
1119 uint8_t AALK = Loc[3];
1120 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1121 break;
1122 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001124 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001126 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001127 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001128 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001131 case R_PPC64_ADDR16_HA:
1132 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001137 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
1140 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001141 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
1143 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001144 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
1146 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001147 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001148 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001149 case R_PPC64_ADDR16_LO:
1150 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001151 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001152 case R_PPC64_ADDR16_LO_DS:
1153 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 break;
1155 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001156 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001157 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001158 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001159 case R_PPC64_ADDR64:
1160 write64be(Loc, SA);
1161 break;
1162 case R_PPC64_REL16_HA:
1163 write16be(Loc, applyPPCHa(SA - P));
1164 break;
1165 case R_PPC64_REL16_HI:
1166 write16be(Loc, applyPPCHi(SA - P));
1167 break;
1168 case R_PPC64_REL16_LO:
1169 write16be(Loc, applyPPCLo(SA - P));
1170 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001171 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001172 // If we have an undefined weak symbol, we might get here with a symbol
1173 // address of zero. That could overflow, but the code must be unreachable,
1174 // so don't bother doing anything at all.
1175 if (!SA)
1176 break;
1177
Hal Finkeldaedc122015-10-12 23:16:53 +00001178 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1179 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001180 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001181
1182 if (!InPlt && Out<ELF64BE>::Opd) {
1183 // If this is a local call, and we currently have the address of a
1184 // function-descriptor, get the underlying code address instead.
1185 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1186 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001187 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001188
1189 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001190 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001191 }
1192
Hal Finkel3c8cc672015-10-12 20:56:18 +00001193 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001194 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001195 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001196
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001197 uint32_t Nop = 0x60000000;
1198 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1199 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001200 break;
1201 }
1202 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001203 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001204 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001205 break;
1206 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001207 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001208 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001209 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001210 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001211 break;
1212 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001213 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001214 }
1215}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001216
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001217AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001218 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001219 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001220 IRelativeRel = R_AARCH64_IRELATIVE;
1221 GotRel = R_AARCH64_GLOB_DAT;
1222 PltRel = R_AARCH64_JUMP_SLOT;
1223 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001224 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1225 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001226 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001227 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001228 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001229}
George Rimar648a2c32015-10-20 08:54:27 +00001230
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001231bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Rafael Espindola9a3bb542016-02-24 19:58:50 +00001232 return Type == R_AARCH64_PREL32 || Type == R_AARCH64_ADR_PREL_PG_HI21 ||
Rafael Espindola40afcb52016-02-24 20:18:06 +00001233 Type == R_AARCH64_LDST8_ABS_LO12_NC ||
Rafael Espindola57ca2702016-02-24 20:52:58 +00001234 Type == R_AARCH64_LDST32_ABS_LO12_NC ||
Rafael Espindola47ed5422016-02-24 21:48:06 +00001235 Type == R_AARCH64_LDST64_ABS_LO12_NC ||
Rafael Espindolaa598a3a2016-02-24 22:07:12 +00001236 Type == R_AARCH64_ADD_ABS_LO12_NC || Type == R_AARCH64_CALL26;
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001237}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001238
George Rimar98b060d2016-03-06 06:01:07 +00001239bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001240 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1241 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1242 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1243 Type == R_AARCH64_TLSDESC_CALL;
1244}
1245
George Rimar98b060d2016-03-06 06:01:07 +00001246bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001247 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1248 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1249}
1250
George Rimar98b060d2016-03-06 06:01:07 +00001251uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001252 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1253 return Type;
1254 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001255 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001256 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001257 // Keep it going with a dummy value so that we can find more reloc errors.
1258 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001259}
1260
Rui Ueyamac516ae12016-01-29 02:33:45 +00001261void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001262 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1263}
1264
Rui Ueyama900e2d22016-01-29 03:51:49 +00001265void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001266 const uint8_t PltData[] = {
1267 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1268 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1269 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1270 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1271 0x20, 0x02, 0x1f, 0xd6, // br x17
1272 0x1f, 0x20, 0x03, 0xd5, // nop
1273 0x1f, 0x20, 0x03, 0xd5, // nop
1274 0x1f, 0x20, 0x03, 0xd5 // nop
1275 };
1276 memcpy(Buf, PltData, sizeof(PltData));
1277
Rui Ueyama900e2d22016-01-29 03:51:49 +00001278 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1279 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1280 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1281 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1282 Got + 16);
1283 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1284 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001285}
1286
Rui Ueyama9398f862016-01-29 04:15:02 +00001287void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1288 uint64_t PltEntryAddr, int32_t Index,
1289 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001290 const uint8_t Inst[] = {
1291 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1292 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1293 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1294 0x20, 0x02, 0x1f, 0xd6 // br x17
1295 };
1296 memcpy(Buf, Inst, sizeof(Inst));
1297
1298 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1299 GotEntryAddr);
1300 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1301 GotEntryAddr);
1302 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1303 GotEntryAddr);
1304}
1305
George Rimar98b060d2016-03-06 06:01:07 +00001306uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001307 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1308 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001309 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001310}
1311
George Rimar98b060d2016-03-06 06:01:07 +00001312bool AArch64TargetInfo::isTlsDynRel(uint32_t Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001313 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1314 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1315 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1316 Type == R_AARCH64_TLSDESC_CALL ||
1317 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001318 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1319}
1320
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001321bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001322 switch (Type) {
1323 default:
1324 return false;
1325 case R_AARCH64_ABS16:
1326 case R_AARCH64_ABS32:
1327 case R_AARCH64_ABS64:
1328 case R_AARCH64_ADD_ABS_LO12_NC:
1329 case R_AARCH64_ADR_PREL_LO21:
1330 case R_AARCH64_ADR_PREL_PG_HI21:
1331 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001332 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001333 case R_AARCH64_LDST32_ABS_LO12_NC:
1334 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001335 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001336 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001337 }
1338}
1339
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001340bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001341 switch (Type) {
1342 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1343 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1344 case R_AARCH64_ADR_GOT_PAGE:
1345 case R_AARCH64_LD64_GOT_LO12_NC:
1346 return true;
1347 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001348 return needsPlt<ELF64LE>(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001349 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001350}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001351
Rafael Espindola993f0272016-02-26 14:27:47 +00001352bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001353 switch (Type) {
1354 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001355 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001356 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001357 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001358 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001359 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001360 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001361 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001362}
Davide Italiano1d750a62015-09-27 08:45:38 +00001363
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001364static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001365 uint32_t ImmLo = (Imm & 0x3) << 29;
1366 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1367 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001368 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001369}
1370
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001371static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1372 or32le(L, (Imm & 0xFFF) << 10);
1373}
1374
Davide Italiano318ca222015-10-02 22:13:51 +00001375// Page(Expr) is the page address of the expression Expr, defined
1376// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001377// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001378static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001379 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001380}
1381
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001382void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001383 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001384 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001385 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001386 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001387 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001388 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001389 break;
1390 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001391 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001392 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001393 break;
1394 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001395 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001396 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001397 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001398 // This relocation stores 12 bits and there's no instruction
1399 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001400 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1401 // bits in Loc are zero.
1402 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001403 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001404 case R_AARCH64_ADR_GOT_PAGE: {
1405 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1406 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001407 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001408 break;
1409 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001410 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001411 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001412 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001413 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001414 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001415 }
George Rimar3d737e42016-01-13 13:04:46 +00001416 case R_AARCH64_ADR_PREL_PG_HI21:
1417 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001418 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001419 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001420 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001421 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001422 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001423 case R_AARCH64_CALL26:
1424 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001425 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001426 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001427 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1428 break;
1429 }
George Rimar4102bfb2016-01-11 14:22:00 +00001430 case R_AARCH64_CONDBR19: {
1431 uint64_t X = SA - P;
1432 checkInt<21>(X, Type);
1433 or32le(Loc, (X & 0x1FFFFC) << 3);
1434 break;
1435 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001436 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001437 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001438 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001439 or32le(Loc, (SA & 0xFF8) << 7);
1440 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001441 case R_AARCH64_LDST128_ABS_LO12_NC:
1442 or32le(Loc, (SA & 0x0FF8) << 6);
1443 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001444 case R_AARCH64_LDST16_ABS_LO12_NC:
1445 or32le(Loc, (SA & 0x0FFC) << 9);
1446 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001447 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001448 or32le(Loc, (SA & 0xFFF) << 10);
1449 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001450 case R_AARCH64_LDST32_ABS_LO12_NC:
1451 or32le(Loc, (SA & 0xFFC) << 8);
1452 break;
1453 case R_AARCH64_LDST64_ABS_LO12_NC:
1454 or32le(Loc, (SA & 0xFF8) << 7);
1455 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001456 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001457 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001458 write16le(Loc, SA - P);
1459 break;
1460 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001461 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001462 write32le(Loc, SA - P);
1463 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001464 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001465 write64le(Loc, SA - P);
1466 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001467 case R_AARCH64_TSTBR14: {
1468 uint64_t X = SA - P;
1469 checkInt<16>(X, Type);
1470 or32le(Loc, (X & 0xFFFC) << 3);
1471 break;
1472 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001473 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1474 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1475 checkInt<24>(V, Type);
1476 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1477 break;
1478 }
1479 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1480 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1481 updateAArch64Add(Loc, V & 0xFFF);
1482 break;
1483 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001484 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001485 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001486 }
1487}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001488
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001489unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1490 uint32_t Type, uint64_t P, uint64_t SA,
1491 const SymbolBody *S) const {
1492 switch (Type) {
1493 case R_AARCH64_TLSDESC_ADR_PAGE21:
1494 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1495 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1496 case R_AARCH64_TLSDESC_CALL: {
Rafael Espindola993f0272016-02-26 14:27:47 +00001497 if (canBePreempted(S))
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001498 fatal("Unsupported TLS optimization");
1499 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1500 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1501 return 0;
1502 }
1503 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1504 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1505 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1506 return 0;
1507 }
1508 llvm_unreachable("Unknown TLS optimization");
1509}
1510
1511// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1512// an executable and target is final (can notbe preempted).
George Rimar98b060d2016-03-06 06:01:07 +00001513void AArch64TargetInfo::relocateTlsGdToLe(uint32_t Type, uint8_t *Loc,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001514 uint8_t *BufEnd, uint64_t P,
1515 uint64_t SA) const {
1516 // TLSDESC Global-Dynamic relocation are in the form:
1517 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1518 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1519 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1520 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1521 // And it can optimized to:
1522 // movz x0, #0x0, lsl #16
1523 // movk x0, #0x10
1524 // nop
1525 // nop
1526
1527 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1528 uint64_t X = SA + TPOff;
1529 checkUInt<32>(X, Type);
1530
1531 uint32_t NewInst;
1532 switch (Type) {
1533 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1534 case R_AARCH64_TLSDESC_CALL:
1535 // nop
1536 NewInst = 0xd503201f;
1537 break;
1538 case R_AARCH64_TLSDESC_ADR_PAGE21:
1539 // movz
1540 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1541 break;
1542 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1543 // movk
1544 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1545 break;
1546 default:
1547 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1548 }
1549 write32le(Loc, NewInst);
1550}
1551
1552// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1553// (can not be preempted).
George Rimar98b060d2016-03-06 06:01:07 +00001554void AArch64TargetInfo::relocateTlsIeToLe(uint32_t Type, uint8_t *Loc,
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001555 uint8_t *BufEnd, uint64_t P,
1556 uint64_t SA) const {
1557 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1558 uint64_t X = SA + TPOff;
1559 checkUInt<32>(X, Type);
1560
1561 uint32_t Inst = read32le (Loc);
1562 uint32_t NewInst;
1563 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1564 // Generate movz.
1565 unsigned RegNo = (Inst & 0x1f);
1566 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1567 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1568 // Generate movk
1569 unsigned RegNo = (Inst & 0x1f);
1570 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1571 } else {
1572 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1573 }
1574 write32le(Loc, NewInst);
1575}
1576
1577
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001578// Implementing relocations for AMDGPU is low priority since most
1579// programs don't use relocations now. Thus, this function is not
1580// actually called (relocateOne is called for each relocation).
1581// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001582void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1583 uint64_t P, uint64_t SA, uint64_t ZA,
1584 uint8_t *PairedLoc) const {
1585 llvm_unreachable("not implemented");
1586}
1587
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001588template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001589 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001590 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001591 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001592 PltEntrySize = 16;
1593 PltZeroSize = 32;
1594 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001595 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001596 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001597 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001598}
1599
1600template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001601uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001602 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1603 return R_MIPS_REL32;
1604 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001605 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001606 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001607 // Keep it going with a dummy value so that we can find more reloc errors.
1608 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001609}
1610
1611template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001612void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001613 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001614 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1615
1616 // Set the MSB of the second GOT slot. This is not required by any
1617 // MIPS ABI documentation, though.
1618 //
1619 // There is a comment in glibc saying that "The MSB of got[1] of a
1620 // gnu object is set to identify gnu objects," and in GNU gold it
1621 // says "the second entry will be used by some runtime loaders".
1622 // But how this field is being used is unclear.
1623 //
1624 // We are not really willing to mimic other linkers behaviors
1625 // without understanding why they do that, but because all files
1626 // generated by GNU tools have this special GOT value, and because
1627 // we've been doing this for years, it is probably a safe bet to
1628 // keep doing this for now. We really need to revisit this to see
1629 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001630 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001631 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001632}
1633
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001634template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001635void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1636 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001637}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001638
Simon Atanasyan35031192015-12-15 06:06:34 +00001639static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001640
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001641template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001642static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001643 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001644 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001645 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001646 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1647 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001648 checkAlignment<(1 << SHIFT)>(S + A, Type);
1649 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001650 checkInt<BSIZE + SHIFT>(V, Type);
1651 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001652}
1653
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001654template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001655static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001656 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001657 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001658}
1659
Simon Atanasyan3b377852016-03-04 10:55:20 +00001660template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001661static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1662 uint32_t Instr = read32<E>(Loc);
1663 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1664}
1665
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001666template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1667 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1668}
1669
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001670template <endianness E>
Simon Atanasyan3b377852016-03-04 10:55:20 +00001671static int64_t readMipsAHL(uint8_t *HiLoc, uint8_t *LoLoc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001672 return ((read32<E>(HiLoc) & 0xffff) << 16) + readSignedLo16<E>(LoLoc);
Simon Atanasyan3b377852016-03-04 10:55:20 +00001673}
1674
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001675template <class ELFT>
1676void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1677 const endianness E = ELFT::TargetEndianness;
1678 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1679 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1680 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1681 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1682 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1683 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1684 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1685 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1686 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001687 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001688 writeMipsLo16<E>(Buf + 4, Got);
1689 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001690}
1691
1692template <class ELFT>
1693void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1694 uint64_t PltEntryAddr, int32_t Index,
1695 unsigned RelOff) const {
1696 const endianness E = ELFT::TargetEndianness;
1697 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1698 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1699 write32<E>(Buf + 8, 0x03200008); // jr $25
1700 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001701 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001702 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1703 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001704}
1705
1706template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001707bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001708 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001709}
1710
1711template <class ELFT>
1712bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyand040a582016-02-25 16:19:15 +00001713 return needsPlt<ELFT>(Type, S) || refersToGotEntry(Type);
1714}
1715
1716template <class ELFT>
1717bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1718 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001719}
1720
1721template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001722bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1723 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001724}
1725
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001726template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001727void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001728 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001729 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001730 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001731 switch (Type) {
1732 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001733 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001734 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001735 case R_MIPS_26: {
1736 uint32_t Instr = read32<E>(Loc);
1737 // FIXME (simon): If the relocation target symbol is not a PLT entry
1738 // we should use another expression for calculation:
1739 // ((A << 2) | (P & 0xf0000000)) >> 2
1740 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1741 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1742 break;
1743 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001744 case R_MIPS_CALL16:
1745 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001746 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001747 if (Type == R_MIPS_GOT16)
1748 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001749 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001750 break;
1751 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001752 case R_MIPS_GPREL16: {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001753 int64_t V = S + readSignedLo16<E>(Loc) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001754 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001755 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001756 break;
1757 }
1758 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001759 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001760 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001761 case R_MIPS_HI16:
1762 if (PairedLoc)
1763 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc));
1764 else {
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001765 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyana888e672016-03-04 10:55:12 +00001766 writeMipsHi16<E>(Loc, S);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001767 }
1768 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001769 case R_MIPS_JALR:
1770 // Ignore this optimization relocation for now
1771 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001772 case R_MIPS_LO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001773 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001774 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001775 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001776 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001777 break;
1778 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001779 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001780 break;
1781 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001782 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001783 break;
1784 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001785 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001786 break;
1787 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001788 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001789 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001790 case R_MIPS_PCHI16:
1791 if (PairedLoc)
1792 writeMipsHi16<E>(Loc, S + readMipsAHL<E>(Loc, PairedLoc) - P);
1793 else {
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001794 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyandeed55d2016-03-04 10:55:16 +00001795 writeMipsHi16<E>(Loc, S - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001796 }
1797 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001798 case R_MIPS_PCLO16:
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001799 writeMipsLo16<E>(Loc, S + readSignedLo16<E>(Loc) - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001800 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001801 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001802 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001803 }
1804}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001805
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001806template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001807bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001808 return Type == R_MIPS_JALR;
1809}
1810
1811template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001812bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1813 switch (Type) {
1814 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001815 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001816 case R_MIPS_26:
1817 case R_MIPS_32:
1818 case R_MIPS_64:
1819 case R_MIPS_HI16:
1820 case R_MIPS_LO16:
1821 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001822 }
1823}
1824
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001825// _gp is a MIPS-specific ABI-defined symbol which points to
1826// a location that is relative to GOT. This function returns
1827// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001828template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001829 unsigned GPOffset = 0x7ff0;
1830 if (uint64_t V = Out<ELFT>::Got->getVA())
1831 return V + GPOffset;
1832 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001833}
1834
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001835template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1836template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1837template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1838template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1839
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001840template uint32_t getMipsGpAddr<ELF32LE>();
1841template uint32_t getMipsGpAddr<ELF32BE>();
1842template uint64_t getMipsGpAddr<ELF64LE>();
1843template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001844
1845template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1846 const SymbolBody &) const;
1847template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1848 const SymbolBody &) const;
1849template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1850 const SymbolBody &) const;
1851template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1852 const SymbolBody &) const;
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001853
1854template TargetInfo::PltNeed
1855TargetInfo::needsPlt<ELF32LE>(uint32_t, const SymbolBody &) const;
1856template TargetInfo::PltNeed
1857TargetInfo::needsPlt<ELF32BE>(uint32_t, const SymbolBody &) const;
1858template TargetInfo::PltNeed
1859TargetInfo::needsPlt<ELF64LE>(uint32_t, const SymbolBody &) const;
1860template TargetInfo::PltNeed
1861TargetInfo::needsPlt<ELF64BE>(uint32_t, const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001862}
1863}