blob: ff30b8a17245e99ebbeb0fe7aa18abac87b9c318 [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 {
34namespace elf2 {
35
36std::unique_ptr<TargetInfo> Target;
37
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;
84 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000085 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000086 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
87 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000088 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000089 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
90 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000091 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000092 bool needsDynRelative(unsigned Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000093 bool needsGot(uint32_t Type, SymbolBody &S) const override;
94 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000095 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000096 uint64_t SA, uint64_t ZA = 0,
97 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +000098 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000099 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
100 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000101 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000102
103private:
104 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
105 uint64_t SA) const;
106 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
107 uint64_t SA) const;
108 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
109 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000110 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
111 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000112};
113
114class X86_64TargetInfo final : public TargetInfo {
115public:
116 X86_64TargetInfo();
George Rimar2960c982016-02-11 11:14:46 +0000117 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000118 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
119 void writeGotPltHeader(uint8_t *Buf) const override;
120 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000121 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000122 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
123 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000124 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000125 bool needsGot(uint32_t Type, SymbolBody &S) const override;
126 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000127 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000128 uint64_t SA, uint64_t ZA = 0,
129 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000130 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000131 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000132 bool isSizeRel(uint32_t Type) const override;
133 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
134 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000135
136private:
137 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
138 uint64_t SA) const;
139 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
140 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000141 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
142 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000143 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000145};
146
Davide Italiano8c3444362016-01-11 19:45:33 +0000147class PPCTargetInfo final : public TargetInfo {
148public:
149 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000150 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
151 uint64_t SA, uint64_t ZA = 0,
152 uint8_t *PairedLoc = nullptr) const override;
153 bool isRelRelative(uint32_t Type) const override;
154};
155
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000156class PPC64TargetInfo final : public TargetInfo {
157public:
158 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000159 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
160 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000161 bool needsGot(uint32_t Type, SymbolBody &S) const override;
162 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000163 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000164 uint64_t SA, uint64_t ZA = 0,
165 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000166 bool isRelRelative(uint32_t Type) const override;
167};
168
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000169class AArch64TargetInfo final : public TargetInfo {
170public:
171 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000172 unsigned getDynRel(unsigned Type) const override;
173 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000174 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000175 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
176 int32_t Index, unsigned RelOff) const override;
George Rimar2960c982016-02-11 11:14:46 +0000177 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000178 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000179 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000180 bool needsGot(uint32_t Type, SymbolBody &S) const override;
181 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000182 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000183 uint64_t SA, uint64_t ZA = 0,
184 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000185};
186
Tom Stellard80efb162016-01-07 03:59:08 +0000187class AMDGPUTargetInfo final : public TargetInfo {
188public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000189 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000190 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
191 uint64_t SA, uint64_t ZA = 0,
192 uint8_t *PairedLoc = nullptr) const override;
193};
194
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000195template <class ELFT> class MipsTargetInfo final : public TargetInfo {
196public:
197 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000198 unsigned getDynRel(unsigned Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000199 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
200 void writePltZero(uint8_t *Buf) const override;
201 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
202 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000203 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyane1bfc2e2016-02-08 10:05:13 +0000204 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000205 bool needsGot(uint32_t Type, SymbolBody &S) const override;
206 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000207 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000208 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000209 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000210 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000211 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000212};
213} // anonymous namespace
214
Rui Ueyama91004392015-10-13 16:08:15 +0000215TargetInfo *createTarget() {
216 switch (Config->EMachine) {
217 case EM_386:
218 return new X86TargetInfo();
219 case EM_AARCH64:
220 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000221 case EM_AMDGPU:
222 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000223 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000224 switch (Config->EKind) {
225 case ELF32LEKind:
226 return new MipsTargetInfo<ELF32LE>();
227 case ELF32BEKind:
228 return new MipsTargetInfo<ELF32BE>();
229 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000230 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000231 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000232 case EM_PPC:
233 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000234 case EM_PPC64:
235 return new PPC64TargetInfo();
236 case EM_X86_64:
237 return new X86_64TargetInfo();
238 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000239 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000240}
241
Rafael Espindola01205f72015-09-22 18:19:46 +0000242TargetInfo::~TargetInfo() {}
243
Rui Ueyamabaf16512016-01-29 00:20:12 +0000244bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000245 return false;
246}
247
Igor Kudrinf6f45472015-11-10 08:39:27 +0000248uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
249
Rui Ueyama02dfd492015-12-17 01:18:40 +0000250bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000251 return false;
252}
253
Rui Ueyama012eb782016-01-29 04:05:09 +0000254bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
255 return Type == TlsLocalDynamicRel;
256}
257
258bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
259 return Type == TlsGlobalDynamicRel;
260}
261
262bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
263 return false;
264}
265
George Rimarbfb7bf72015-12-21 10:00:12 +0000266bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000267bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000268bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000269bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000270
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000271bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000272
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000273bool TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000274
Rui Ueyamac516ae12016-01-29 02:33:45 +0000275unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
276 uint64_t P, uint64_t SA,
277 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000278 return 0;
279}
George Rimar77d1cb12015-11-24 09:00:06 +0000280
Rafael Espindola7f074422015-09-22 21:35:51 +0000281X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000282 CopyRel = R_386_COPY;
283 GotRel = R_386_GLOB_DAT;
284 PltRel = R_386_JUMP_SLOT;
285 IRelativeRel = R_386_IRELATIVE;
286 RelativeRel = R_386_RELATIVE;
287 TlsGotRel = R_386_TLS_TPOFF;
288 TlsGlobalDynamicRel = R_386_TLS_GD;
289 TlsLocalDynamicRel = R_386_TLS_LDM;
290 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
291 TlsOffsetRel = R_386_TLS_DTPOFF32;
292 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000293 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000294 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000295}
296
Rui Ueyamac516ae12016-01-29 02:33:45 +0000297void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000298 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
299}
300
Rui Ueyamac516ae12016-01-29 02:33:45 +0000301void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000302 // Entries in .got.plt initially points back to the corresponding
303 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000304 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000305}
Rafael Espindola01205f72015-09-22 18:19:46 +0000306
Rui Ueyamac516ae12016-01-29 02:33:45 +0000307unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000308 if (Type == R_386_TLS_LE)
309 return R_386_TLS_TPOFF;
310 if (Type == R_386_TLS_LE_32)
311 return R_386_TLS_TPOFF32;
312 return Type;
313}
314
Rui Ueyama724d6252016-01-29 01:49:32 +0000315unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000316 if (Type == R_386_TLS_IE)
317 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000318 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000319}
320
Rui Ueyamac516ae12016-01-29 02:33:45 +0000321bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000322 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
323 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000324 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000325 if (Type == R_386_TLS_IE)
326 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000327 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000328}
329
Rui Ueyama900e2d22016-01-29 03:51:49 +0000330void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000331 // Executable files and shared object files have
332 // separate procedure linkage tables.
333 if (Config->Shared) {
334 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000335 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000336 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
337 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000338 };
339 memcpy(Buf, V, sizeof(V));
340 return;
341 }
George Rimar648a2c32015-10-20 08:54:27 +0000342
George Rimar77b77792015-11-25 22:15:01 +0000343 const uint8_t PltData[] = {
344 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000345 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
346 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000347 };
348 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000349 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000350 write32le(Buf + 2, Got + 4);
351 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000352}
353
Rui Ueyama9398f862016-01-29 04:15:02 +0000354void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
355 uint64_t PltEntryAddr, int32_t Index,
356 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000357 const uint8_t Inst[] = {
358 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
359 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
360 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
361 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000362 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000363
George Rimar77b77792015-11-25 22:15:01 +0000364 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
365 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000366 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
367 : Out<ELF32LE>::Got->getVA();
368 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000369 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000370 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000371}
372
Rui Ueyama02dfd492015-12-17 01:18:40 +0000373bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000374 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
375 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
376 return SS->Sym.getType() == STT_OBJECT;
377 return false;
378}
379
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000380bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000381 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000382 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000383 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000384 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000385 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000386}
387
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000388bool X86TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000389 return isGnuIFunc<ELF32LE>(S) ||
390 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000391 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000392}
393
George Rimarbfb7bf72015-12-21 10:00:12 +0000394bool X86TargetInfo::isGotRelative(uint32_t Type) const {
395 // This relocation does not require got entry,
396 // but it is relative to got and needs it to be created.
397 // Here we request for that.
398 return Type == R_386_GOTOFF;
399}
400
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000401void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000402 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000403 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000404 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000405 case R_386_32:
406 add32le(Loc, SA);
407 break;
George Rimarffb67352016-01-19 11:00:48 +0000408 case R_386_GOT32: {
409 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
410 Out<ELF32LE>::Got->getNumEntries() * 4;
411 checkInt<32>(V, Type);
412 add32le(Loc, V);
413 break;
414 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000415 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000416 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000417 break;
George Rimar13934772015-11-25 20:20:31 +0000418 case R_386_GOTPC:
419 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
420 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000421 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000422 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000423 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000424 break;
George Rimar9db204a2015-12-02 09:58:20 +0000425 case R_386_TLS_GD:
426 case R_386_TLS_LDM:
427 case R_386_TLS_TPOFF: {
428 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
429 Out<ELF32LE>::Got->getNumEntries() * 4;
430 checkInt<32>(V, Type);
431 write32le(Loc, V);
432 break;
433 }
George Rimar6f17e092015-12-17 09:32:21 +0000434 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000435 case R_386_TLS_LDO_32:
436 write32le(Loc, SA);
437 break;
George Rimard23970f2015-11-25 20:41:53 +0000438 case R_386_TLS_LE:
439 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
440 break;
441 case R_386_TLS_LE_32:
442 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
443 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000444 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000445 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000446 }
447}
448
Rui Ueyamabaf16512016-01-29 00:20:12 +0000449bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000450 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000451 return false;
452 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
453 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000454 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000455 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
456}
457
Rui Ueyamac516ae12016-01-29 02:33:45 +0000458bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000459 return Config->Shared && Type == R_386_TLS_IE;
460}
461
Rui Ueyamac516ae12016-01-29 02:33:45 +0000462unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
463 uint64_t P, uint64_t SA,
464 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000465 switch (Type) {
466 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000467 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000468 relocateTlsGdToIe(Loc, BufEnd, P, SA);
469 else
470 relocateTlsGdToLe(Loc, BufEnd, P, SA);
471 // The next relocation should be against __tls_get_addr, so skip it
472 return 1;
473 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000474 case R_386_TLS_IE:
475 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000476 return 0;
477 case R_386_TLS_LDM:
478 relocateTlsLdToLe(Loc, BufEnd, P, SA);
479 // The next relocation should be against __tls_get_addr, so skip it
480 return 1;
481 case R_386_TLS_LDO_32:
482 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
483 return 0;
484 }
485 llvm_unreachable("Unknown TLS optimization");
486}
487
488// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
489// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
490// how GD can be optimized to IE:
491// leal x@tlsgd(, %ebx, 1),
492// call __tls_get_addr@plt
493// Is converted to:
494// movl %gs:0, %eax
495// addl x@gotntpoff(%ebx), %eax
496void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
497 uint64_t SA) const {
498 const uint8_t Inst[] = {
499 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
500 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
501 };
502 memcpy(Loc - 3, Inst, sizeof(Inst));
503 relocateOne(Loc + 5, BufEnd, R_386_32, P,
504 SA - Out<ELF32LE>::Got->getVA() -
505 Out<ELF32LE>::Got->getNumEntries() * 4);
506}
507
508// GD can be optimized to LE:
509// leal x@tlsgd(, %ebx, 1),
510// call __tls_get_addr@plt
511// Can be converted to:
512// movl %gs:0,%eax
513// addl $x@ntpoff,%eax
514// But gold emits subl $foo@tpoff,%eax instead of addl.
515// These instructions are completely equal in behavior.
516// This method generates subl to be consistent with gold.
517void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
518 uint64_t SA) const {
519 const uint8_t Inst[] = {
520 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
521 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
522 };
523 memcpy(Loc - 3, Inst, sizeof(Inst));
524 relocateOne(Loc + 5, BufEnd, R_386_32, P,
525 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
526}
527
528// LD can be optimized to LE:
529// leal foo(%reg),%eax
530// call ___tls_get_addr
531// Is converted to:
532// movl %gs:0,%eax
533// nop
534// leal 0(%esi,1),%esi
535void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
536 uint64_t SA) const {
537 const uint8_t Inst[] = {
538 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
539 0x90, // nop
540 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
541 };
542 memcpy(Loc - 2, Inst, sizeof(Inst));
543}
544
George Rimar6f17e092015-12-17 09:32:21 +0000545// In some conditions, relocations can be optimized to avoid using GOT.
546// This function does that for Initial Exec to Local Exec case.
547// Read "ELF Handling For Thread-Local Storage, 5.1
548// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000549// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000550void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
551 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000552 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000553 // Ulrich's document section 6.2 says that @gotntpoff can
554 // be used with MOVL or ADDL instructions.
555 // @indntpoff is similar to @gotntpoff, but for use in
556 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000557 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000558 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000559 uint8_t Reg = (Loc[-1] >> 3) & 7;
560 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000561 if (Type == R_386_TLS_IE) {
562 // For R_386_TLS_IE relocation we perform the next transformations:
563 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
564 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
565 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
566 // First one is special because when EAX is used the sequence is 5 bytes
567 // long, otherwise it is 6 bytes.
568 if (*Op == 0xa1) {
569 *Op = 0xb8;
570 } else {
571 *Inst = IsMov ? 0xc7 : 0x81;
572 *Op = 0xc0 | ((*Op >> 3) & 7);
573 }
574 } else {
575 // R_386_TLS_GOTIE relocation can be optimized to
576 // R_386_TLS_LE so that it does not use GOT.
577 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
578 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
579 // Note: gold converts to ADDL instead of LEAL.
580 *Inst = IsMov ? 0xc7 : 0x8d;
581 if (IsMov)
582 *Op = 0xc0 | ((*Op >> 3) & 7);
583 else
584 *Op = 0x80 | Reg | (Reg << 3);
585 }
George Rimar2558e122015-12-09 09:55:54 +0000586 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
587}
588
Rafael Espindola7f074422015-09-22 21:35:51 +0000589X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000590 CopyRel = R_X86_64_COPY;
591 GotRel = R_X86_64_GLOB_DAT;
592 PltRel = R_X86_64_JUMP_SLOT;
593 RelativeRel = R_X86_64_RELATIVE;
594 IRelativeRel = R_X86_64_IRELATIVE;
595 TlsGotRel = R_X86_64_TPOFF64;
596 TlsLocalDynamicRel = R_X86_64_TLSLD;
597 TlsGlobalDynamicRel = R_X86_64_TLSGD;
598 TlsModuleIndexRel = R_X86_64_DTPMOD64;
599 TlsOffsetRel = R_X86_64_DTPOFF64;
600 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000601 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000602 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000603}
604
Rui Ueyamac516ae12016-01-29 02:33:45 +0000605void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000606 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
607}
608
Rui Ueyamac516ae12016-01-29 02:33:45 +0000609void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000610 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000611 write32le(Buf, Plt + 6);
612}
613
Rui Ueyama900e2d22016-01-29 03:51:49 +0000614void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000615 const uint8_t PltData[] = {
616 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
617 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
618 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
619 };
620 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000621 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
622 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
623 write32le(Buf + 2, Got - Plt + 2); // GOT+8
624 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000625}
Rafael Espindola01205f72015-09-22 18:19:46 +0000626
Rui Ueyama9398f862016-01-29 04:15:02 +0000627void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
628 uint64_t PltEntryAddr, int32_t Index,
629 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000630 const uint8_t Inst[] = {
631 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
632 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
633 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
634 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000635 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000636
George Rimar648a2c32015-10-20 08:54:27 +0000637 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
638 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000639 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000640}
641
Rui Ueyama02dfd492015-12-17 01:18:40 +0000642bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000643 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
644 Type == R_X86_64_64)
645 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
646 return SS->Sym.getType() == STT_OBJECT;
647 return false;
648}
649
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000650bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000651 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000652 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000653 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000654 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000655 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000656}
657
George Rimar2960c982016-02-11 11:14:46 +0000658unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const {
659 // No other types of TLS relocations requiring GOT should
660 // reach here.
661 assert(Type == R_X86_64_GOTTPOFF);
662 return R_X86_64_PC32;
663}
664
Rui Ueyamac516ae12016-01-29 02:33:45 +0000665bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000666 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000667}
668
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000669bool X86_64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000670 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000671 return false;
George Rimara07ff662015-12-21 10:12:06 +0000672 if (isGnuIFunc<ELF64LE>(S))
673 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000674
Rafael Espindola01205f72015-09-22 18:19:46 +0000675 switch (Type) {
676 default:
677 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000678 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000679 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000680 case R_X86_64_PC32:
681 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000682 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000683 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000684 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000685 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000686 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
687 // libc.so.
688 //
689 // The general idea on how to handle such cases is to create a PLT entry
690 // and use that as the function value.
691 //
692 // For the static linking part, we just return true and everything else
693 // will use the the PLT entry as the address.
694 //
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000695 // The remaining problem is making sure pointer equality still works. We
696 // need the help of the dynamic linker for that. We let it know that we have
697 // a direct reference to a so symbol by creating an undefined symbol with a
698 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
699 // the value of the symbol we created. This is true even for got entries, so
700 // pointer equality is maintained. To avoid an infinite loop, the only entry
701 // that points to the real function is a dedicated got entry used by the
702 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
Rafael Espindola3c412e12015-09-30 12:30:58 +0000703 // R_386_JMP_SLOT, etc).
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000704 if (!S.isShared())
705 return false;
706 S.NeedsCopyOrPltAddr = true;
707 return true;
Rafael Espindola01205f72015-09-22 18:19:46 +0000708 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000709 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000710 }
711}
Rafael Espindolac4010882015-09-22 20:54:08 +0000712
Rafael Espindolaae244002015-10-05 19:30:12 +0000713bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
714 switch (Type) {
715 default:
716 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000717 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000718 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000719 case R_X86_64_PC8:
720 case R_X86_64_PC16:
721 case R_X86_64_PC32:
722 case R_X86_64_PC64:
723 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000724 return true;
725 }
726}
727
Rui Ueyamac516ae12016-01-29 02:33:45 +0000728bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000729 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000730}
731
Rui Ueyamabaf16512016-01-29 00:20:12 +0000732bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000733 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000734 return false;
George Rimar25411f252015-12-04 11:20:13 +0000735 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
736 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000737 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
738}
739
740// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
741// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
742// how LD can be optimized to LE:
743// leaq bar@tlsld(%rip), %rdi
744// callq __tls_get_addr@PLT
745// leaq bar@dtpoff(%rax), %rcx
746// Is converted to:
747// .word 0x6666
748// .byte 0x66
749// mov %fs:0,%rax
750// leaq bar@tpoff(%rax), %rcx
751void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
752 uint64_t P, uint64_t SA) const {
753 const uint8_t Inst[] = {
754 0x66, 0x66, //.word 0x6666
755 0x66, //.byte 0x66
756 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
757 };
758 memcpy(Loc - 3, Inst, sizeof(Inst));
759}
760
761// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
762// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
763// how GD can be optimized to LE:
764// .byte 0x66
765// leaq x@tlsgd(%rip), %rdi
766// .word 0x6666
767// rex64
768// call __tls_get_addr@plt
769// Is converted to:
770// mov %fs:0x0,%rax
771// lea x@tpoff,%rax
772void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
773 uint64_t P, uint64_t SA) const {
774 const uint8_t Inst[] = {
775 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
776 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
777 };
778 memcpy(Loc - 4, Inst, sizeof(Inst));
779 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000780}
781
George Rimar25411f252015-12-04 11:20:13 +0000782// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
783// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
784// how GD can be optimized to IE:
785// .byte 0x66
786// leaq x@tlsgd(%rip), %rdi
787// .word 0x6666
788// rex64
789// call __tls_get_addr@plt
790// Is converted to:
791// mov %fs:0x0,%rax
792// addq x@tpoff,%rax
793void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
794 uint64_t P, uint64_t SA) const {
795 const uint8_t Inst[] = {
796 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
797 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
798 };
799 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000800 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000801}
802
George Rimar77d1cb12015-11-24 09:00:06 +0000803// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000804// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000805// This function does that. Read "ELF Handling For Thread-Local Storage,
806// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
807// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000808void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
809 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000810 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
811 // used in MOVQ or ADDQ instructions only.
812 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
813 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
814 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
815 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
816 uint8_t *Prefix = Loc - 3;
817 uint8_t *Inst = Loc - 2;
818 uint8_t *RegSlot = Loc - 1;
819 uint8_t Reg = Loc[-1] >> 3;
820 bool IsMov = *Inst == 0x8b;
821 bool RspAdd = !IsMov && Reg == 4;
822 // r12 and rsp registers requires special handling.
823 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
824 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
825 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
826 // The same true for rsp. So we convert to addq for them, saving 1 byte that
827 // we dont have.
828 if (RspAdd)
829 *Inst = 0x81;
830 else
831 *Inst = IsMov ? 0xc7 : 0x8d;
832 if (*Prefix == 0x4c)
833 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
834 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
835 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
836}
837
George Rimar6713cf82015-11-25 21:46:05 +0000838// This function applies a TLS relocation with an optimization as described
839// in the Ulrich's document. As a result of rewriting instructions at the
840// relocation target, relocations immediately follow the TLS relocation (which
841// would be applied to rewritten instructions) may have to be skipped.
842// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000843unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
844 uint32_t Type, uint64_t P, uint64_t SA,
845 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000846 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000847 case R_X86_64_DTPOFF32:
848 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
849 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000850 case R_X86_64_GOTTPOFF:
851 relocateTlsIeToLe(Loc, BufEnd, P, SA);
852 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000853 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000854 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000855 relocateTlsGdToIe(Loc, BufEnd, P, SA);
856 else
857 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000858 // The next relocation should be against __tls_get_addr, so skip it
859 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000860 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000861 case R_X86_64_TLSLD:
862 relocateTlsLdToLe(Loc, BufEnd, P, SA);
863 // The next relocation should be against __tls_get_addr, so skip it
864 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000865 }
866 llvm_unreachable("Unknown TLS optimization");
867}
868
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000869void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000870 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000871 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000872 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000873 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000874 checkUInt<32>(SA, Type);
875 write32le(Loc, SA);
876 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000877 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000878 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000879 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000880 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000881 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000882 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000883 write64le(Loc, SA);
884 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000885 case R_X86_64_DTPOFF32:
886 write32le(Loc, SA);
887 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000888 case R_X86_64_GOTPCREL:
889 case R_X86_64_PC32:
890 case R_X86_64_PLT32:
891 case R_X86_64_TLSGD:
892 case R_X86_64_TLSLD:
893 write32le(Loc, SA - P);
894 break;
George Rimar48651482015-12-11 08:59:37 +0000895 case R_X86_64_SIZE32:
896 write32le(Loc, ZA);
897 break;
898 case R_X86_64_SIZE64:
899 write64le(Loc, ZA);
900 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000901 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000902 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000903 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000904 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000905 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000906 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000907 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000908 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000909 }
910}
911
Hal Finkel3c8cc672015-10-12 20:56:18 +0000912// Relocation masks following the #lo(value), #hi(value), #ha(value),
913// #higher(value), #highera(value), #highest(value), and #highesta(value)
914// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
915// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000916static uint16_t applyPPCLo(uint64_t V) { return V; }
917static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
918static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
919static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
920static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000921static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000922static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
923
Davide Italiano8c3444362016-01-11 19:45:33 +0000924PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000925bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
926
927void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
928 uint64_t P, uint64_t SA, uint64_t ZA,
929 uint8_t *PairedLoc) const {
930 switch (Type) {
931 case R_PPC_ADDR16_HA:
932 write16be(Loc, applyPPCHa(SA));
933 break;
934 case R_PPC_ADDR16_LO:
935 write16be(Loc, applyPPCLo(SA));
936 break;
937 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000938 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000939 }
940}
941
Rafael Espindolac4010882015-09-22 20:54:08 +0000942PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000943 GotRel = R_PPC64_GLOB_DAT;
944 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000945 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000946
947 // We need 64K pages (at least under glibc/Linux, the loader won't
948 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000949 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000950
951 // The PPC64 ELF ABI v1 spec, says:
952 //
953 // It is normally desirable to put segments with different characteristics
954 // in separate 256 Mbyte portions of the address space, to give the
955 // operating system full paging flexibility in the 64-bit address space.
956 //
957 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
958 // use 0x10000000 as the starting address.
959 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000960}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000961
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000962uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000963 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
964 // order. The TOC starts where the first of these sections starts.
965
966 // FIXME: This obviously does not do the right thing when there is no .got
967 // section, but there is a .toc or .tocbss section.
968 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
969 if (!TocVA)
970 TocVA = Out<ELF64BE>::Plt->getVA();
971
972 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
973 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
974 // code (crt1.o) assumes that you can get from the TOC base to the
975 // start of the .toc section with only a single (signed) 16-bit relocation.
976 return TocVA + 0x8000;
977}
978
Rui Ueyama9398f862016-01-29 04:15:02 +0000979void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
980 uint64_t PltEntryAddr, int32_t Index,
981 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000982 uint64_t Off = GotEntryAddr - getPPC64TocBase();
983
984 // FIXME: What we should do, in theory, is get the offset of the function
985 // descriptor in the .opd section, and use that as the offset from %r2 (the
986 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
987 // be a pointer to the function descriptor in the .opd section. Using
988 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
989
Hal Finkelfa92f682015-10-13 21:47:34 +0000990 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000991 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
992 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
993 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
994 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
995 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
996 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
997 write32be(Buf + 28, 0x4e800420); // bctr
998}
999
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001000bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyamac516ae12016-01-29 02:33:45 +00001001 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001002 return true;
1003
1004 switch (Type) {
1005 default: return false;
1006 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001007 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001008 case R_PPC64_GOT16_HA:
1009 case R_PPC64_GOT16_HI:
1010 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001011 case R_PPC64_GOT16_LO_DS:
1012 return true;
1013 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001014}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001015
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001016bool PPC64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001017 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001018 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001019}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001020
Hal Finkelbe0823d2015-10-12 20:58:52 +00001021bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1022 switch (Type) {
1023 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001024 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001025 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001026 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001027 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001028 }
1029}
1030
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001031void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001032 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001033 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001034 uint64_t TB = getPPC64TocBase();
1035
Hal Finkel3c8cc672015-10-12 20:56:18 +00001036 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1037 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001038 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001039 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1040 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001041 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1042 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001043 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1044 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001045 default: break;
1046 }
1047
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001049 case R_PPC64_ADDR14: {
1050 checkAlignment<4>(SA, Type);
1051 // Preserve the AA/LK bits in the branch instruction
1052 uint8_t AALK = Loc[3];
1053 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1054 break;
1055 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001056 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001057 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001058 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001059 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001061 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001062 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001064 case R_PPC64_ADDR16_HA:
1065 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001066 break;
1067 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001068 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001069 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001070 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001071 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001072 break;
1073 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001074 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001075 break;
1076 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001077 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 break;
1079 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001080 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001081 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001082 case R_PPC64_ADDR16_LO:
1083 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001085 case R_PPC64_ADDR16_LO_DS:
1086 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 break;
1088 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001089 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001090 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001091 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001092 case R_PPC64_ADDR64:
1093 write64be(Loc, SA);
1094 break;
1095 case R_PPC64_REL16_HA:
1096 write16be(Loc, applyPPCHa(SA - P));
1097 break;
1098 case R_PPC64_REL16_HI:
1099 write16be(Loc, applyPPCHi(SA - P));
1100 break;
1101 case R_PPC64_REL16_LO:
1102 write16be(Loc, applyPPCLo(SA - P));
1103 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001105 // If we have an undefined weak symbol, we might get here with a symbol
1106 // address of zero. That could overflow, but the code must be unreachable,
1107 // so don't bother doing anything at all.
1108 if (!SA)
1109 break;
1110
Hal Finkeldaedc122015-10-12 23:16:53 +00001111 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1112 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001113 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001114
1115 if (!InPlt && Out<ELF64BE>::Opd) {
1116 // If this is a local call, and we currently have the address of a
1117 // function-descriptor, get the underlying code address instead.
1118 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1119 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001120 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001121
1122 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001123 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001124 }
1125
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001127 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001129
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001130 uint32_t Nop = 0x60000000;
1131 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1132 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 }
1135 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001136 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001137 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001138 break;
1139 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001140 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001141 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001142 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001143 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001144 break;
1145 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001146 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001147 }
1148}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001149
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001150AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001151 CopyRel = R_AARCH64_COPY;
1152 IRelativeRel = R_AARCH64_IRELATIVE;
1153 GotRel = R_AARCH64_GLOB_DAT;
1154 PltRel = R_AARCH64_JUMP_SLOT;
1155 TlsGotRel = R_AARCH64_TLS_TPREL64;
1156 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001157 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001158 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001159}
George Rimar648a2c32015-10-20 08:54:27 +00001160
Rui Ueyamac516ae12016-01-29 02:33:45 +00001161unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001162 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1163 return Type;
1164 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001165 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001166 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001167 // Keep it going with a dummy value so that we can find more reloc errors.
1168 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001169}
1170
Rui Ueyamac516ae12016-01-29 02:33:45 +00001171void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001172 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1173}
1174
Rui Ueyama900e2d22016-01-29 03:51:49 +00001175void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001176 const uint8_t PltData[] = {
1177 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1178 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1179 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1180 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1181 0x20, 0x02, 0x1f, 0xd6, // br x17
1182 0x1f, 0x20, 0x03, 0xd5, // nop
1183 0x1f, 0x20, 0x03, 0xd5, // nop
1184 0x1f, 0x20, 0x03, 0xd5 // nop
1185 };
1186 memcpy(Buf, PltData, sizeof(PltData));
1187
Rui Ueyama900e2d22016-01-29 03:51:49 +00001188 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1189 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1190 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1191 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1192 Got + 16);
1193 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1194 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001195}
1196
Rui Ueyama9398f862016-01-29 04:15:02 +00001197void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1198 uint64_t PltEntryAddr, int32_t Index,
1199 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001200 const uint8_t Inst[] = {
1201 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1202 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1203 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1204 0x20, 0x02, 0x1f, 0xd6 // br x17
1205 };
1206 memcpy(Buf, Inst, sizeof(Inst));
1207
1208 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1209 GotEntryAddr);
1210 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1211 GotEntryAddr);
1212 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1213 GotEntryAddr);
1214}
1215
Rui Ueyama724d6252016-01-29 01:49:32 +00001216unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001217 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1218 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001219 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001220}
1221
Rui Ueyamac516ae12016-01-29 02:33:45 +00001222bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001223 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1224 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1225}
1226
Rui Ueyama02dfd492015-12-17 01:18:40 +00001227bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001228 if (Config->Shared)
1229 return false;
1230 switch (Type) {
1231 default:
1232 return false;
1233 case R_AARCH64_ABS16:
1234 case R_AARCH64_ABS32:
1235 case R_AARCH64_ABS64:
1236 case R_AARCH64_ADD_ABS_LO12_NC:
1237 case R_AARCH64_ADR_PREL_LO21:
1238 case R_AARCH64_ADR_PREL_PG_HI21:
1239 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001240 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001241 case R_AARCH64_LDST32_ABS_LO12_NC:
1242 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001243 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001244 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1245 return SS->Sym.getType() == STT_OBJECT;
1246 return false;
1247 }
1248}
1249
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001250bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001251 switch (Type) {
1252 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1253 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1254 case R_AARCH64_ADR_GOT_PAGE:
1255 case R_AARCH64_LD64_GOT_LO12_NC:
1256 return true;
1257 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001258 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001259 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001260}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001261
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001262bool AArch64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001263 if (isGnuIFunc<ELF64LE>(S))
1264 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001265 switch (Type) {
1266 default:
1267 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001268 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001269 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001270 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001271 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001272 return canBePreempted(&S, true);
1273 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001274}
Davide Italiano1d750a62015-09-27 08:45:38 +00001275
Davide Italianoef4be6b2015-10-06 19:01:32 +00001276static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001277 uint32_t ImmLo = (Imm & 0x3) << 29;
1278 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1279 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001280 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001281}
1282
Davide Italiano318ca222015-10-02 22:13:51 +00001283// Page(Expr) is the page address of the expression Expr, defined
1284// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001285// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001286static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001287 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001288}
1289
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001290void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001291 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001292 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001293 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001294 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001295 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001296 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001297 break;
1298 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001299 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001300 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001301 break;
1302 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001303 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001304 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001305 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001306 // This relocation stores 12 bits and there's no instruction
1307 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001308 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1309 // bits in Loc are zero.
1310 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001311 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001312 case R_AARCH64_ADR_GOT_PAGE: {
1313 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1314 checkInt<33>(X, Type);
1315 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1316 break;
1317 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001318 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001319 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001320 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001321 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001322 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001323 }
George Rimar3d737e42016-01-13 13:04:46 +00001324 case R_AARCH64_ADR_PREL_PG_HI21:
1325 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001326 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001327 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001328 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001329 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001330 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001331 case R_AARCH64_CALL26:
1332 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001333 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001334 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001335 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1336 break;
1337 }
George Rimar4102bfb2016-01-11 14:22:00 +00001338 case R_AARCH64_CONDBR19: {
1339 uint64_t X = SA - P;
1340 checkInt<21>(X, Type);
1341 or32le(Loc, (X & 0x1FFFFC) << 3);
1342 break;
1343 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001344 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001345 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001346 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001347 or32le(Loc, (SA & 0xFF8) << 7);
1348 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001349 case R_AARCH64_LDST128_ABS_LO12_NC:
1350 or32le(Loc, (SA & 0x0FF8) << 6);
1351 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001352 case R_AARCH64_LDST16_ABS_LO12_NC:
1353 or32le(Loc, (SA & 0x0FFC) << 9);
1354 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001355 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001356 or32le(Loc, (SA & 0xFFF) << 10);
1357 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001358 case R_AARCH64_LDST32_ABS_LO12_NC:
1359 or32le(Loc, (SA & 0xFFC) << 8);
1360 break;
1361 case R_AARCH64_LDST64_ABS_LO12_NC:
1362 or32le(Loc, (SA & 0xFF8) << 7);
1363 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001364 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001365 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001366 write16le(Loc, SA - P);
1367 break;
1368 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001369 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001370 write32le(Loc, SA - P);
1371 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001372 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001373 write64le(Loc, SA - P);
1374 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001375 case R_AARCH64_TSTBR14: {
1376 uint64_t X = SA - P;
1377 checkInt<16>(X, Type);
1378 or32le(Loc, (X & 0xFFFC) << 3);
1379 break;
1380 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001381 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001382 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001383 }
1384}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001385
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001386// Implementing relocations for AMDGPU is low priority since most
1387// programs don't use relocations now. Thus, this function is not
1388// actually called (relocateOne is called for each relocation).
1389// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001390void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1391 uint64_t P, uint64_t SA, uint64_t ZA,
1392 uint8_t *PairedLoc) const {
1393 llvm_unreachable("not implemented");
1394}
1395
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001396template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001397 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001398 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001399 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001400 PltEntrySize = 16;
1401 PltZeroSize = 32;
1402 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001403 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001404 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001405 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001406}
1407
1408template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001409unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001410 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1411 return R_MIPS_REL32;
1412 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001413 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001414 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001415 // Keep it going with a dummy value so that we can find more reloc errors.
1416 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001417}
1418
1419template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001420void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001421 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001422 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1423
1424 // Set the MSB of the second GOT slot. This is not required by any
1425 // MIPS ABI documentation, though.
1426 //
1427 // There is a comment in glibc saying that "The MSB of got[1] of a
1428 // gnu object is set to identify gnu objects," and in GNU gold it
1429 // says "the second entry will be used by some runtime loaders".
1430 // But how this field is being used is unclear.
1431 //
1432 // We are not really willing to mimic other linkers behaviors
1433 // without understanding why they do that, but because all files
1434 // generated by GNU tools have this special GOT value, and because
1435 // we've been doing this for years, it is probably a safe bet to
1436 // keep doing this for now. We really need to revisit this to see
1437 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001438 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001439 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001440}
1441
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001442template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001443void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1444 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001445}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001446
Simon Atanasyan35031192015-12-15 06:06:34 +00001447static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001448
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001449template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001450static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001451 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001452 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001453 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001454 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1455 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001456 checkAlignment<(1 << SHIFT)>(S + A, Type);
1457 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001458 checkInt<BSIZE + SHIFT>(V, Type);
1459 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001460}
1461
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001462template <endianness E>
1463static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1464 uint32_t Instr = read32<E>(Loc);
1465 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1466}
1467
1468template <class ELFT>
1469void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1470 const endianness E = ELFT::TargetEndianness;
1471 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1472 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1473 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1474 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1475 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1476 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1477 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1478 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1479 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1480 uint64_t Plt = Out<ELFT>::Plt->getVA();
1481 applyMipsHi16Reloc<E>(Buf, Got, 0);
1482 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1483 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1484}
1485
1486template <class ELFT>
1487void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1488 uint64_t PltEntryAddr, int32_t Index,
1489 unsigned RelOff) const {
1490 const endianness E = ELFT::TargetEndianness;
1491 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1492 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1493 write32<E>(Buf + 8, 0x03200008); // jr $25
1494 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1495 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1496 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1497 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1498}
1499
1500template <class ELFT>
1501bool MipsTargetInfo<ELFT>::needsCopyRel(uint32_t Type,
1502 const SymbolBody &S) const {
1503 if (Config->Shared)
1504 return false;
1505 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1506 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
1507 return SS->Sym.getType() == STT_OBJECT;
1508 return false;
1509}
1510
1511template <class ELFT>
1512bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
1513 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
1514}
1515
1516template <class ELFT>
1517bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, SymbolBody &S) const {
1518 if (needsCopyRel(Type, S))
1519 return false;
1520 if (Type == R_MIPS_26 && canBePreempted(&S, false))
1521 return true;
1522 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1523 return S.isShared();
1524 return false;
1525}
1526
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001527template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001528void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001529 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001530 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001531 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001532 switch (Type) {
1533 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001534 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001535 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001536 case R_MIPS_26: {
1537 uint32_t Instr = read32<E>(Loc);
1538 // FIXME (simon): If the relocation target symbol is not a PLT entry
1539 // we should use another expression for calculation:
1540 // ((A << 2) | (P & 0xf0000000)) >> 2
1541 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1542 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1543 break;
1544 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001545 case R_MIPS_CALL16:
1546 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001547 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001548 if (Type == R_MIPS_GOT16)
1549 checkInt<16>(V, Type);
1550 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1551 break;
1552 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001553 case R_MIPS_GPREL16: {
1554 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001555 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001556 checkInt<16>(V, Type);
1557 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1558 break;
1559 }
1560 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001561 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001562 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001563 case R_MIPS_HI16: {
1564 uint32_t Instr = read32<E>(Loc);
1565 if (PairedLoc) {
1566 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001567 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001568 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001569 } else {
1570 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001571 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001572 }
1573 break;
1574 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001575 case R_MIPS_JALR:
1576 // Ignore this optimization relocation for now
1577 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001578 case R_MIPS_LO16: {
1579 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001580 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001581 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001582 break;
1583 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001584 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001585 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001586 break;
1587 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001588 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001589 break;
1590 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001591 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001592 break;
1593 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001594 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001595 break;
1596 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001597 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001598 break;
1599 case R_MIPS_PCHI16: {
1600 uint32_t Instr = read32<E>(Loc);
1601 if (PairedLoc) {
1602 uint64_t AHL = ((Instr & 0xffff) << 16) +
1603 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001604 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001605 } else {
1606 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001607 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001608 }
1609 break;
1610 }
1611 case R_MIPS_PCLO16: {
1612 uint32_t Instr = read32<E>(Loc);
1613 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001614 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001615 break;
1616 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001617 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001618 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001619 }
1620}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001621
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001622template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001623bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001624 return Type == R_MIPS_JALR;
1625}
1626
1627template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001628bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1629 switch (Type) {
1630 default:
1631 return false;
1632 case R_MIPS_PC16:
1633 case R_MIPS_PC19_S2:
1634 case R_MIPS_PC21_S2:
1635 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001636 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001637 case R_MIPS_PCHI16:
1638 case R_MIPS_PCLO16:
1639 return true;
1640 }
1641}
1642
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001643// _gp is a MIPS-specific ABI-defined symbol which points to
1644// a location that is relative to GOT. This function returns
1645// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001646template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001647 unsigned GPOffset = 0x7ff0;
1648 if (uint64_t V = Out<ELFT>::Got->getVA())
1649 return V + GPOffset;
1650 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001651}
1652
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001653template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1654template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1655template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1656template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1657
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001658template uint32_t getMipsGpAddr<ELF32LE>();
1659template uint32_t getMipsGpAddr<ELF32BE>();
1660template uint64_t getMipsGpAddr<ELF64LE>();
1661template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001662}
1663}