blob: db381a63c0fdabf45120f75e3588f4d28859d807 [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();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000117 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
118 void writeGotPltHeader(uint8_t *Buf) const override;
119 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000120 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000121 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
122 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000123 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000124 bool needsGot(uint32_t Type, SymbolBody &S) const override;
125 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000126 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000127 uint64_t SA, uint64_t ZA = 0,
128 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000130 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000131 bool isSizeRel(uint32_t Type) const override;
132 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
133 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000134
135private:
136 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
137 uint64_t SA) const;
138 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
139 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000140 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
141 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000142 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
143 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000144};
145
Davide Italiano8c3444362016-01-11 19:45:33 +0000146class PPCTargetInfo final : public TargetInfo {
147public:
148 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000149 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
150 uint64_t SA, uint64_t ZA = 0,
151 uint8_t *PairedLoc = nullptr) const override;
152 bool isRelRelative(uint32_t Type) const override;
153};
154
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155class PPC64TargetInfo final : public TargetInfo {
156public:
157 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000158 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
159 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000160 bool needsGot(uint32_t Type, SymbolBody &S) const override;
161 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000162 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000163 uint64_t SA, uint64_t ZA = 0,
164 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000165 bool isRelRelative(uint32_t Type) const override;
166};
167
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000168class AArch64TargetInfo final : public TargetInfo {
169public:
170 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000171 unsigned getDynRel(unsigned Type) const override;
172 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000173 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000174 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
175 int32_t Index, unsigned RelOff) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +0000176 unsigned getTlsGotRel(unsigned Type = -1) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000177 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000178 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000179 bool needsGot(uint32_t Type, SymbolBody &S) const override;
180 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000181 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000182 uint64_t SA, uint64_t ZA = 0,
183 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000184};
185
Tom Stellard80efb162016-01-07 03:59:08 +0000186class AMDGPUTargetInfo final : public TargetInfo {
187public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000188 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000189 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
190 uint64_t SA, uint64_t ZA = 0,
191 uint8_t *PairedLoc = nullptr) const override;
192};
193
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000194template <class ELFT> class MipsTargetInfo final : public TargetInfo {
195public:
196 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000197 unsigned getDynRel(unsigned Type) const override;
198 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyane1bfc2e2016-02-08 10:05:13 +0000199 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000200 bool needsGot(uint32_t Type, SymbolBody &S) const override;
201 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000202 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000203 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000204 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000205 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000206 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000207};
208} // anonymous namespace
209
Rui Ueyama91004392015-10-13 16:08:15 +0000210TargetInfo *createTarget() {
211 switch (Config->EMachine) {
212 case EM_386:
213 return new X86TargetInfo();
214 case EM_AARCH64:
215 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000216 case EM_AMDGPU:
217 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000218 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000219 switch (Config->EKind) {
220 case ELF32LEKind:
221 return new MipsTargetInfo<ELF32LE>();
222 case ELF32BEKind:
223 return new MipsTargetInfo<ELF32BE>();
224 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000225 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000226 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000227 case EM_PPC:
228 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000229 case EM_PPC64:
230 return new PPC64TargetInfo();
231 case EM_X86_64:
232 return new X86_64TargetInfo();
233 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000234 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000235}
236
Rafael Espindola01205f72015-09-22 18:19:46 +0000237TargetInfo::~TargetInfo() {}
238
Rui Ueyamabaf16512016-01-29 00:20:12 +0000239bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000240 return false;
241}
242
Igor Kudrinf6f45472015-11-10 08:39:27 +0000243uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
244
Rui Ueyama02dfd492015-12-17 01:18:40 +0000245bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000246 return false;
247}
248
Rui Ueyama012eb782016-01-29 04:05:09 +0000249bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
250 return Type == TlsLocalDynamicRel;
251}
252
253bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
254 return Type == TlsGlobalDynamicRel;
255}
256
257bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
258 return false;
259}
260
George Rimarbfb7bf72015-12-21 10:00:12 +0000261bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000262bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000263bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000264bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000265
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000266bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000267
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000268bool TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000269
Rui Ueyamac516ae12016-01-29 02:33:45 +0000270unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
271 uint64_t P, uint64_t SA,
272 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000273 return 0;
274}
George Rimar77d1cb12015-11-24 09:00:06 +0000275
Rafael Espindola7f074422015-09-22 21:35:51 +0000276X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000277 CopyRel = R_386_COPY;
278 GotRel = R_386_GLOB_DAT;
279 PltRel = R_386_JUMP_SLOT;
280 IRelativeRel = R_386_IRELATIVE;
281 RelativeRel = R_386_RELATIVE;
282 TlsGotRel = R_386_TLS_TPOFF;
283 TlsGlobalDynamicRel = R_386_TLS_GD;
284 TlsLocalDynamicRel = R_386_TLS_LDM;
285 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
286 TlsOffsetRel = R_386_TLS_DTPOFF32;
287 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000288 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000289 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000290}
291
Rui Ueyamac516ae12016-01-29 02:33:45 +0000292void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000293 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
294}
295
Rui Ueyamac516ae12016-01-29 02:33:45 +0000296void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000297 // Entries in .got.plt initially points back to the corresponding
298 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000299 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000300}
Rafael Espindola01205f72015-09-22 18:19:46 +0000301
Rui Ueyamac516ae12016-01-29 02:33:45 +0000302unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000303 if (Type == R_386_TLS_LE)
304 return R_386_TLS_TPOFF;
305 if (Type == R_386_TLS_LE_32)
306 return R_386_TLS_TPOFF32;
307 return Type;
308}
309
Rui Ueyama724d6252016-01-29 01:49:32 +0000310unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000311 if (Type == R_386_TLS_IE)
312 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000313 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000314}
315
Rui Ueyamac516ae12016-01-29 02:33:45 +0000316bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000317 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
318 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000319 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000320 if (Type == R_386_TLS_IE)
321 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000322 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000323}
324
Rui Ueyama900e2d22016-01-29 03:51:49 +0000325void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000326 // Executable files and shared object files have
327 // separate procedure linkage tables.
328 if (Config->Shared) {
329 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000330 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000331 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
332 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000333 };
334 memcpy(Buf, V, sizeof(V));
335 return;
336 }
George Rimar648a2c32015-10-20 08:54:27 +0000337
George Rimar77b77792015-11-25 22:15:01 +0000338 const uint8_t PltData[] = {
339 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000340 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
341 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000342 };
343 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000344 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000345 write32le(Buf + 2, Got + 4);
346 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000347}
348
Rui Ueyama9398f862016-01-29 04:15:02 +0000349void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
350 uint64_t PltEntryAddr, int32_t Index,
351 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000352 const uint8_t Inst[] = {
353 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
354 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
355 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
356 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000357 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000358
George Rimar77b77792015-11-25 22:15:01 +0000359 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
360 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000361 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
362 : Out<ELF32LE>::Got->getVA();
363 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000364 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000365 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000366}
367
Rui Ueyama02dfd492015-12-17 01:18:40 +0000368bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000369 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
370 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
371 return SS->Sym.getType() == STT_OBJECT;
372 return false;
373}
374
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000375bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000376 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000377 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000378 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000379 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000380 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000381}
382
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000383bool X86TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000384 return isGnuIFunc<ELF32LE>(S) ||
385 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000386 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000387}
388
George Rimarbfb7bf72015-12-21 10:00:12 +0000389bool X86TargetInfo::isGotRelative(uint32_t Type) const {
390 // This relocation does not require got entry,
391 // but it is relative to got and needs it to be created.
392 // Here we request for that.
393 return Type == R_386_GOTOFF;
394}
395
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000396void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000397 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000398 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000399 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000400 case R_386_32:
401 add32le(Loc, SA);
402 break;
George Rimarffb67352016-01-19 11:00:48 +0000403 case R_386_GOT32: {
404 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
405 Out<ELF32LE>::Got->getNumEntries() * 4;
406 checkInt<32>(V, Type);
407 add32le(Loc, V);
408 break;
409 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000410 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000411 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000412 break;
George Rimar13934772015-11-25 20:20:31 +0000413 case R_386_GOTPC:
414 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
415 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000416 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000417 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000418 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000419 break;
George Rimar9db204a2015-12-02 09:58:20 +0000420 case R_386_TLS_GD:
421 case R_386_TLS_LDM:
422 case R_386_TLS_TPOFF: {
423 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
424 Out<ELF32LE>::Got->getNumEntries() * 4;
425 checkInt<32>(V, Type);
426 write32le(Loc, V);
427 break;
428 }
George Rimar6f17e092015-12-17 09:32:21 +0000429 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000430 case R_386_TLS_LDO_32:
431 write32le(Loc, SA);
432 break;
George Rimard23970f2015-11-25 20:41:53 +0000433 case R_386_TLS_LE:
434 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
435 break;
436 case R_386_TLS_LE_32:
437 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
438 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000439 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000440 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000441 }
442}
443
Rui Ueyamabaf16512016-01-29 00:20:12 +0000444bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000445 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000446 return false;
447 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
448 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000449 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000450 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
451}
452
Rui Ueyamac516ae12016-01-29 02:33:45 +0000453bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000454 return Config->Shared && Type == R_386_TLS_IE;
455}
456
Rui Ueyamac516ae12016-01-29 02:33:45 +0000457unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
458 uint64_t P, uint64_t SA,
459 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000460 switch (Type) {
461 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000462 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000463 relocateTlsGdToIe(Loc, BufEnd, P, SA);
464 else
465 relocateTlsGdToLe(Loc, BufEnd, P, SA);
466 // The next relocation should be against __tls_get_addr, so skip it
467 return 1;
468 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000469 case R_386_TLS_IE:
470 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000471 return 0;
472 case R_386_TLS_LDM:
473 relocateTlsLdToLe(Loc, BufEnd, P, SA);
474 // The next relocation should be against __tls_get_addr, so skip it
475 return 1;
476 case R_386_TLS_LDO_32:
477 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
478 return 0;
479 }
480 llvm_unreachable("Unknown TLS optimization");
481}
482
483// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
484// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
485// how GD can be optimized to IE:
486// leal x@tlsgd(, %ebx, 1),
487// call __tls_get_addr@plt
488// Is converted to:
489// movl %gs:0, %eax
490// addl x@gotntpoff(%ebx), %eax
491void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
492 uint64_t SA) const {
493 const uint8_t Inst[] = {
494 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
495 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
496 };
497 memcpy(Loc - 3, Inst, sizeof(Inst));
498 relocateOne(Loc + 5, BufEnd, R_386_32, P,
499 SA - Out<ELF32LE>::Got->getVA() -
500 Out<ELF32LE>::Got->getNumEntries() * 4);
501}
502
503// GD can be optimized to LE:
504// leal x@tlsgd(, %ebx, 1),
505// call __tls_get_addr@plt
506// Can be converted to:
507// movl %gs:0,%eax
508// addl $x@ntpoff,%eax
509// But gold emits subl $foo@tpoff,%eax instead of addl.
510// These instructions are completely equal in behavior.
511// This method generates subl to be consistent with gold.
512void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
513 uint64_t SA) const {
514 const uint8_t Inst[] = {
515 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
516 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
517 };
518 memcpy(Loc - 3, Inst, sizeof(Inst));
519 relocateOne(Loc + 5, BufEnd, R_386_32, P,
520 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
521}
522
523// LD can be optimized to LE:
524// leal foo(%reg),%eax
525// call ___tls_get_addr
526// Is converted to:
527// movl %gs:0,%eax
528// nop
529// leal 0(%esi,1),%esi
530void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
531 uint64_t SA) const {
532 const uint8_t Inst[] = {
533 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
534 0x90, // nop
535 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
536 };
537 memcpy(Loc - 2, Inst, sizeof(Inst));
538}
539
George Rimar6f17e092015-12-17 09:32:21 +0000540// In some conditions, relocations can be optimized to avoid using GOT.
541// This function does that for Initial Exec to Local Exec case.
542// Read "ELF Handling For Thread-Local Storage, 5.1
543// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000544// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000545void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
546 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000547 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000548 // Ulrich's document section 6.2 says that @gotntpoff can
549 // be used with MOVL or ADDL instructions.
550 // @indntpoff is similar to @gotntpoff, but for use in
551 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000552 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000553 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000554 uint8_t Reg = (Loc[-1] >> 3) & 7;
555 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000556 if (Type == R_386_TLS_IE) {
557 // For R_386_TLS_IE relocation we perform the next transformations:
558 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
559 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
560 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
561 // First one is special because when EAX is used the sequence is 5 bytes
562 // long, otherwise it is 6 bytes.
563 if (*Op == 0xa1) {
564 *Op = 0xb8;
565 } else {
566 *Inst = IsMov ? 0xc7 : 0x81;
567 *Op = 0xc0 | ((*Op >> 3) & 7);
568 }
569 } else {
570 // R_386_TLS_GOTIE relocation can be optimized to
571 // R_386_TLS_LE so that it does not use GOT.
572 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
573 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
574 // Note: gold converts to ADDL instead of LEAL.
575 *Inst = IsMov ? 0xc7 : 0x8d;
576 if (IsMov)
577 *Op = 0xc0 | ((*Op >> 3) & 7);
578 else
579 *Op = 0x80 | Reg | (Reg << 3);
580 }
George Rimar2558e122015-12-09 09:55:54 +0000581 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
582}
583
Rafael Espindola7f074422015-09-22 21:35:51 +0000584X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000585 CopyRel = R_X86_64_COPY;
586 GotRel = R_X86_64_GLOB_DAT;
587 PltRel = R_X86_64_JUMP_SLOT;
588 RelativeRel = R_X86_64_RELATIVE;
589 IRelativeRel = R_X86_64_IRELATIVE;
590 TlsGotRel = R_X86_64_TPOFF64;
591 TlsLocalDynamicRel = R_X86_64_TLSLD;
592 TlsGlobalDynamicRel = R_X86_64_TLSGD;
593 TlsModuleIndexRel = R_X86_64_DTPMOD64;
594 TlsOffsetRel = R_X86_64_DTPOFF64;
595 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000596 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000597 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000598}
599
Rui Ueyamac516ae12016-01-29 02:33:45 +0000600void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000601 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
602}
603
Rui Ueyamac516ae12016-01-29 02:33:45 +0000604void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000605 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000606 write32le(Buf, Plt + 6);
607}
608
Rui Ueyama900e2d22016-01-29 03:51:49 +0000609void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000610 const uint8_t PltData[] = {
611 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
612 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
613 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
614 };
615 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000616 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
617 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
618 write32le(Buf + 2, Got - Plt + 2); // GOT+8
619 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000620}
Rafael Espindola01205f72015-09-22 18:19:46 +0000621
Rui Ueyama9398f862016-01-29 04:15:02 +0000622void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
623 uint64_t PltEntryAddr, int32_t Index,
624 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000625 const uint8_t Inst[] = {
626 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
627 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
628 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
629 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000630 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000631
George Rimar648a2c32015-10-20 08:54:27 +0000632 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
633 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000634 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000635}
636
Rui Ueyama02dfd492015-12-17 01:18:40 +0000637bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000638 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
639 Type == R_X86_64_64)
640 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
641 return SS->Sym.getType() == STT_OBJECT;
642 return false;
643}
644
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000645bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000646 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000647 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000648 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000649 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000650 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000651}
652
Rui Ueyamac516ae12016-01-29 02:33:45 +0000653bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000654 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000655}
656
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000657bool X86_64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000658 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000659 return false;
George Rimara07ff662015-12-21 10:12:06 +0000660 if (isGnuIFunc<ELF64LE>(S))
661 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000662
Rafael Espindola01205f72015-09-22 18:19:46 +0000663 switch (Type) {
664 default:
665 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000666 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000667 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000668 case R_X86_64_PC32:
669 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000670 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000671 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000672 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000673 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000674 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
675 // libc.so.
676 //
677 // The general idea on how to handle such cases is to create a PLT entry
678 // and use that as the function value.
679 //
680 // For the static linking part, we just return true and everything else
681 // will use the the PLT entry as the address.
682 //
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000683 // The remaining problem is making sure pointer equality still works. We
684 // need the help of the dynamic linker for that. We let it know that we have
685 // a direct reference to a so symbol by creating an undefined symbol with a
686 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
687 // the value of the symbol we created. This is true even for got entries, so
688 // pointer equality is maintained. To avoid an infinite loop, the only entry
689 // that points to the real function is a dedicated got entry used by the
690 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
Rafael Espindola3c412e12015-09-30 12:30:58 +0000691 // R_386_JMP_SLOT, etc).
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000692 if (!S.isShared())
693 return false;
694 S.NeedsCopyOrPltAddr = true;
695 return true;
Rafael Espindola01205f72015-09-22 18:19:46 +0000696 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000697 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000698 }
699}
Rafael Espindolac4010882015-09-22 20:54:08 +0000700
Rafael Espindolaae244002015-10-05 19:30:12 +0000701bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
702 switch (Type) {
703 default:
704 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000705 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000706 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000707 case R_X86_64_PC8:
708 case R_X86_64_PC16:
709 case R_X86_64_PC32:
710 case R_X86_64_PC64:
711 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000712 return true;
713 }
714}
715
Rui Ueyamac516ae12016-01-29 02:33:45 +0000716bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000717 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000718}
719
Rui Ueyamabaf16512016-01-29 00:20:12 +0000720bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000721 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000722 return false;
George Rimar25411f252015-12-04 11:20:13 +0000723 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
724 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000725 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
726}
727
728// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
729// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
730// how LD can be optimized to LE:
731// leaq bar@tlsld(%rip), %rdi
732// callq __tls_get_addr@PLT
733// leaq bar@dtpoff(%rax), %rcx
734// Is converted to:
735// .word 0x6666
736// .byte 0x66
737// mov %fs:0,%rax
738// leaq bar@tpoff(%rax), %rcx
739void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
740 uint64_t P, uint64_t SA) const {
741 const uint8_t Inst[] = {
742 0x66, 0x66, //.word 0x6666
743 0x66, //.byte 0x66
744 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
745 };
746 memcpy(Loc - 3, Inst, sizeof(Inst));
747}
748
749// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
750// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
751// how GD can be optimized to LE:
752// .byte 0x66
753// leaq x@tlsgd(%rip), %rdi
754// .word 0x6666
755// rex64
756// call __tls_get_addr@plt
757// Is converted to:
758// mov %fs:0x0,%rax
759// lea x@tpoff,%rax
760void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
761 uint64_t P, uint64_t SA) const {
762 const uint8_t Inst[] = {
763 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
764 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
765 };
766 memcpy(Loc - 4, Inst, sizeof(Inst));
767 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000768}
769
George Rimar25411f252015-12-04 11:20:13 +0000770// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
771// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
772// how GD can be optimized to IE:
773// .byte 0x66
774// leaq x@tlsgd(%rip), %rdi
775// .word 0x6666
776// rex64
777// call __tls_get_addr@plt
778// Is converted to:
779// mov %fs:0x0,%rax
780// addq x@tpoff,%rax
781void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
782 uint64_t P, uint64_t SA) const {
783 const uint8_t Inst[] = {
784 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
785 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
786 };
787 memcpy(Loc - 4, Inst, sizeof(Inst));
788 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
789}
790
George Rimar77d1cb12015-11-24 09:00:06 +0000791// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000792// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000793// This function does that. Read "ELF Handling For Thread-Local Storage,
794// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
795// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000796void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
797 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000798 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
799 // used in MOVQ or ADDQ instructions only.
800 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
801 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
802 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
803 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
804 uint8_t *Prefix = Loc - 3;
805 uint8_t *Inst = Loc - 2;
806 uint8_t *RegSlot = Loc - 1;
807 uint8_t Reg = Loc[-1] >> 3;
808 bool IsMov = *Inst == 0x8b;
809 bool RspAdd = !IsMov && Reg == 4;
810 // r12 and rsp registers requires special handling.
811 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
812 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
813 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
814 // The same true for rsp. So we convert to addq for them, saving 1 byte that
815 // we dont have.
816 if (RspAdd)
817 *Inst = 0x81;
818 else
819 *Inst = IsMov ? 0xc7 : 0x8d;
820 if (*Prefix == 0x4c)
821 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
822 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
823 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
824}
825
George Rimar6713cf82015-11-25 21:46:05 +0000826// This function applies a TLS relocation with an optimization as described
827// in the Ulrich's document. As a result of rewriting instructions at the
828// relocation target, relocations immediately follow the TLS relocation (which
829// would be applied to rewritten instructions) may have to be skipped.
830// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000831unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
832 uint32_t Type, uint64_t P, uint64_t SA,
833 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000834 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000835 case R_X86_64_DTPOFF32:
836 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
837 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000838 case R_X86_64_GOTTPOFF:
839 relocateTlsIeToLe(Loc, BufEnd, P, SA);
840 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000841 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000842 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000843 relocateTlsGdToIe(Loc, BufEnd, P, SA);
844 else
845 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000846 // The next relocation should be against __tls_get_addr, so skip it
847 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000848 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000849 case R_X86_64_TLSLD:
850 relocateTlsLdToLe(Loc, BufEnd, P, SA);
851 // The next relocation should be against __tls_get_addr, so skip it
852 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000853 }
854 llvm_unreachable("Unknown TLS optimization");
855}
856
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000857void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000858 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000859 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000860 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000861 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000862 checkUInt<32>(SA, Type);
863 write32le(Loc, SA);
864 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000865 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000866 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000867 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000868 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000869 case R_X86_64_64:
870 write64le(Loc, SA);
871 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000872 case R_X86_64_DTPOFF32:
873 write32le(Loc, SA);
874 break;
Rafael Espindola96449902016-02-10 16:26:31 +0000875 case R_X86_64_DTPOFF64:
876 write64le(Loc, SA);
877 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000878 case R_X86_64_GOTPCREL:
879 case R_X86_64_PC32:
880 case R_X86_64_PLT32:
881 case R_X86_64_TLSGD:
882 case R_X86_64_TLSLD:
883 write32le(Loc, SA - P);
884 break;
George Rimar48651482015-12-11 08:59:37 +0000885 case R_X86_64_SIZE32:
886 write32le(Loc, ZA);
887 break;
888 case R_X86_64_SIZE64:
889 write64le(Loc, ZA);
890 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000891 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000892 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000893 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000894 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000895 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000896 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000897 case R_X86_64_TPOFF64:
898 write32le(Loc, SA - P);
899 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000900 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000901 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000902 }
903}
904
Hal Finkel3c8cc672015-10-12 20:56:18 +0000905// Relocation masks following the #lo(value), #hi(value), #ha(value),
906// #higher(value), #highera(value), #highest(value), and #highesta(value)
907// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
908// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000909static uint16_t applyPPCLo(uint64_t V) { return V; }
910static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
911static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
912static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
913static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000914static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000915static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
916
Davide Italiano8c3444362016-01-11 19:45:33 +0000917PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000918bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
919
920void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
921 uint64_t P, uint64_t SA, uint64_t ZA,
922 uint8_t *PairedLoc) const {
923 switch (Type) {
924 case R_PPC_ADDR16_HA:
925 write16be(Loc, applyPPCHa(SA));
926 break;
927 case R_PPC_ADDR16_LO:
928 write16be(Loc, applyPPCLo(SA));
929 break;
930 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000931 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000932 }
933}
934
Rafael Espindolac4010882015-09-22 20:54:08 +0000935PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000936 GotRel = R_PPC64_GLOB_DAT;
937 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000938 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000939
940 // We need 64K pages (at least under glibc/Linux, the loader won't
941 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000942 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000943
944 // The PPC64 ELF ABI v1 spec, says:
945 //
946 // It is normally desirable to put segments with different characteristics
947 // in separate 256 Mbyte portions of the address space, to give the
948 // operating system full paging flexibility in the 64-bit address space.
949 //
950 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
951 // use 0x10000000 as the starting address.
952 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000953}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000954
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000955uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000956 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
957 // order. The TOC starts where the first of these sections starts.
958
959 // FIXME: This obviously does not do the right thing when there is no .got
960 // section, but there is a .toc or .tocbss section.
961 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
962 if (!TocVA)
963 TocVA = Out<ELF64BE>::Plt->getVA();
964
965 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
966 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
967 // code (crt1.o) assumes that you can get from the TOC base to the
968 // start of the .toc section with only a single (signed) 16-bit relocation.
969 return TocVA + 0x8000;
970}
971
Rui Ueyama9398f862016-01-29 04:15:02 +0000972void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
973 uint64_t PltEntryAddr, int32_t Index,
974 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000975 uint64_t Off = GotEntryAddr - getPPC64TocBase();
976
977 // FIXME: What we should do, in theory, is get the offset of the function
978 // descriptor in the .opd section, and use that as the offset from %r2 (the
979 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
980 // be a pointer to the function descriptor in the .opd section. Using
981 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
982
Hal Finkelfa92f682015-10-13 21:47:34 +0000983 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000984 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
985 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
986 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
987 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
988 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
989 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
990 write32be(Buf + 28, 0x4e800420); // bctr
991}
992
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000993bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000994 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000995 return true;
996
997 switch (Type) {
998 default: return false;
999 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001000 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001001 case R_PPC64_GOT16_HA:
1002 case R_PPC64_GOT16_HI:
1003 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001004 case R_PPC64_GOT16_LO_DS:
1005 return true;
1006 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001007}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001008
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001009bool PPC64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001011 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001012}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001013
Hal Finkelbe0823d2015-10-12 20:58:52 +00001014bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1015 switch (Type) {
1016 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001017 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001018 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001019 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001020 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001021 }
1022}
1023
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001024void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001025 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001026 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 uint64_t TB = getPPC64TocBase();
1028
Hal Finkel3c8cc672015-10-12 20:56:18 +00001029 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1030 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001031 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001032 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1033 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001034 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1035 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001036 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1037 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 default: break;
1039 }
1040
Hal Finkel3c8cc672015-10-12 20:56:18 +00001041 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001042 case R_PPC64_ADDR14: {
1043 checkAlignment<4>(SA, Type);
1044 // Preserve the AA/LK bits in the branch instruction
1045 uint8_t AALK = Loc[3];
1046 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1047 break;
1048 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001049 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001050 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001051 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001052 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001053 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001054 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001055 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001056 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001057 case R_PPC64_ADDR16_HA:
1058 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059 break;
1060 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001061 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001064 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 break;
1066 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001067 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 break;
1069 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001070 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071 break;
1072 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001073 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001074 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001075 case R_PPC64_ADDR16_LO:
1076 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001077 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001078 case R_PPC64_ADDR16_LO_DS:
1079 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 break;
1081 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001082 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001083 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001085 case R_PPC64_ADDR64:
1086 write64be(Loc, SA);
1087 break;
1088 case R_PPC64_REL16_HA:
1089 write16be(Loc, applyPPCHa(SA - P));
1090 break;
1091 case R_PPC64_REL16_HI:
1092 write16be(Loc, applyPPCHi(SA - P));
1093 break;
1094 case R_PPC64_REL16_LO:
1095 write16be(Loc, applyPPCLo(SA - P));
1096 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001097 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001098 // If we have an undefined weak symbol, we might get here with a symbol
1099 // address of zero. That could overflow, but the code must be unreachable,
1100 // so don't bother doing anything at all.
1101 if (!SA)
1102 break;
1103
Hal Finkeldaedc122015-10-12 23:16:53 +00001104 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1105 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001106 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001107
1108 if (!InPlt && Out<ELF64BE>::Opd) {
1109 // If this is a local call, and we currently have the address of a
1110 // function-descriptor, get the underlying code address instead.
1111 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1112 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001113 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001114
1115 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001116 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001117 }
1118
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001120 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001121 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001122
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001123 uint32_t Nop = 0x60000000;
1124 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1125 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 }
1128 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001129 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001130 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 break;
1132 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001133 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001134 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001135 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001136 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001137 break;
1138 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001139 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001140 }
1141}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001142
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001143AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001144 CopyRel = R_AARCH64_COPY;
1145 IRelativeRel = R_AARCH64_IRELATIVE;
1146 GotRel = R_AARCH64_GLOB_DAT;
1147 PltRel = R_AARCH64_JUMP_SLOT;
1148 TlsGotRel = R_AARCH64_TLS_TPREL64;
1149 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001150 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001151 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001152}
George Rimar648a2c32015-10-20 08:54:27 +00001153
Rui Ueyamac516ae12016-01-29 02:33:45 +00001154unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001155 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1156 return Type;
1157 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001158 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001159 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001160 // Keep it going with a dummy value so that we can find more reloc errors.
1161 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001162}
1163
Rui Ueyamac516ae12016-01-29 02:33:45 +00001164void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001165 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1166}
1167
Rui Ueyama900e2d22016-01-29 03:51:49 +00001168void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001169 const uint8_t PltData[] = {
1170 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1171 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1172 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1173 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1174 0x20, 0x02, 0x1f, 0xd6, // br x17
1175 0x1f, 0x20, 0x03, 0xd5, // nop
1176 0x1f, 0x20, 0x03, 0xd5, // nop
1177 0x1f, 0x20, 0x03, 0xd5 // nop
1178 };
1179 memcpy(Buf, PltData, sizeof(PltData));
1180
Rui Ueyama900e2d22016-01-29 03:51:49 +00001181 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1182 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1183 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1184 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1185 Got + 16);
1186 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1187 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001188}
1189
Rui Ueyama9398f862016-01-29 04:15:02 +00001190void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1191 uint64_t PltEntryAddr, int32_t Index,
1192 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001193 const uint8_t Inst[] = {
1194 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1195 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1196 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1197 0x20, 0x02, 0x1f, 0xd6 // br x17
1198 };
1199 memcpy(Buf, Inst, sizeof(Inst));
1200
1201 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1202 GotEntryAddr);
1203 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1204 GotEntryAddr);
1205 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1206 GotEntryAddr);
1207}
1208
Rui Ueyama724d6252016-01-29 01:49:32 +00001209unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar3d737e42016-01-13 13:04:46 +00001210 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1211 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1212 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +00001213 return TlsGotRel;
George Rimar3d737e42016-01-13 13:04:46 +00001214}
1215
Rui Ueyamac516ae12016-01-29 02:33:45 +00001216bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001217 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1218 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1219}
1220
Rui Ueyama02dfd492015-12-17 01:18:40 +00001221bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001222 if (Config->Shared)
1223 return false;
1224 switch (Type) {
1225 default:
1226 return false;
1227 case R_AARCH64_ABS16:
1228 case R_AARCH64_ABS32:
1229 case R_AARCH64_ABS64:
1230 case R_AARCH64_ADD_ABS_LO12_NC:
1231 case R_AARCH64_ADR_PREL_LO21:
1232 case R_AARCH64_ADR_PREL_PG_HI21:
1233 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001234 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001235 case R_AARCH64_LDST32_ABS_LO12_NC:
1236 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001237 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001238 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1239 return SS->Sym.getType() == STT_OBJECT;
1240 return false;
1241 }
1242}
1243
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001244bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001245 switch (Type) {
1246 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1247 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1248 case R_AARCH64_ADR_GOT_PAGE:
1249 case R_AARCH64_LD64_GOT_LO12_NC:
1250 return true;
1251 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001252 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001253 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001254}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001255
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001256bool AArch64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001257 if (isGnuIFunc<ELF64LE>(S))
1258 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001259 switch (Type) {
1260 default:
1261 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001262 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001263 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001264 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001265 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001266 return canBePreempted(&S, true);
1267 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001268}
Davide Italiano1d750a62015-09-27 08:45:38 +00001269
Davide Italianoef4be6b2015-10-06 19:01:32 +00001270static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001271 uint32_t ImmLo = (Imm & 0x3) << 29;
1272 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1273 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001274 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001275}
1276
Davide Italiano318ca222015-10-02 22:13:51 +00001277// Page(Expr) is the page address of the expression Expr, defined
1278// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001279// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001280static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001281 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001282}
1283
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001284void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001285 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001286 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001287 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001288 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001289 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001290 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001291 break;
1292 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001293 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001294 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001295 break;
1296 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001297 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001298 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001299 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001300 // This relocation stores 12 bits and there's no instruction
1301 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001302 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1303 // bits in Loc are zero.
1304 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001305 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001306 case R_AARCH64_ADR_GOT_PAGE: {
1307 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1308 checkInt<33>(X, Type);
1309 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1310 break;
1311 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001312 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001313 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001314 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001315 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001316 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001317 }
George Rimar3d737e42016-01-13 13:04:46 +00001318 case R_AARCH64_ADR_PREL_PG_HI21:
1319 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001320 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001321 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001322 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001323 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001324 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001325 case R_AARCH64_CALL26:
1326 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001327 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001328 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001329 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1330 break;
1331 }
George Rimar4102bfb2016-01-11 14:22:00 +00001332 case R_AARCH64_CONDBR19: {
1333 uint64_t X = SA - P;
1334 checkInt<21>(X, Type);
1335 or32le(Loc, (X & 0x1FFFFC) << 3);
1336 break;
1337 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001338 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001339 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001340 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001341 or32le(Loc, (SA & 0xFF8) << 7);
1342 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001343 case R_AARCH64_LDST128_ABS_LO12_NC:
1344 or32le(Loc, (SA & 0x0FF8) << 6);
1345 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001346 case R_AARCH64_LDST16_ABS_LO12_NC:
1347 or32le(Loc, (SA & 0x0FFC) << 9);
1348 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001349 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001350 or32le(Loc, (SA & 0xFFF) << 10);
1351 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001352 case R_AARCH64_LDST32_ABS_LO12_NC:
1353 or32le(Loc, (SA & 0xFFC) << 8);
1354 break;
1355 case R_AARCH64_LDST64_ABS_LO12_NC:
1356 or32le(Loc, (SA & 0xFF8) << 7);
1357 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001358 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001359 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001360 write16le(Loc, SA - P);
1361 break;
1362 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001363 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001364 write32le(Loc, SA - P);
1365 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001366 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001367 write64le(Loc, SA - P);
1368 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001369 case R_AARCH64_TSTBR14: {
1370 uint64_t X = SA - P;
1371 checkInt<16>(X, Type);
1372 or32le(Loc, (X & 0xFFFC) << 3);
1373 break;
1374 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001375 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001376 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001377 }
1378}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001379
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001380// Implementing relocations for AMDGPU is low priority since most
1381// programs don't use relocations now. Thus, this function is not
1382// actually called (relocateOne is called for each relocation).
1383// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001384void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1385 uint64_t P, uint64_t SA, uint64_t ZA,
1386 uint8_t *PairedLoc) const {
1387 llvm_unreachable("not implemented");
1388}
1389
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001390template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001391 GotHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001392 PageSize = 65536;
1393 CopyRel = R_MIPS_COPY;
Rui Ueyama724d6252016-01-29 01:49:32 +00001394 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001395}
1396
1397template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001398unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001399 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1400 return R_MIPS_REL32;
1401 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001402 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001403 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001404 // Keep it going with a dummy value so that we can find more reloc errors.
1405 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001406}
1407
1408template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001409void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001410 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001411 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1412
1413 // Set the MSB of the second GOT slot. This is not required by any
1414 // MIPS ABI documentation, though.
1415 //
1416 // There is a comment in glibc saying that "The MSB of got[1] of a
1417 // gnu object is set to identify gnu objects," and in GNU gold it
1418 // says "the second entry will be used by some runtime loaders".
1419 // But how this field is being used is unclear.
1420 //
1421 // We are not really willing to mimic other linkers behaviors
1422 // without understanding why they do that, but because all files
1423 // generated by GNU tools have this special GOT value, and because
1424 // we've been doing this for years, it is probably a safe bet to
1425 // keep doing this for now. We really need to revisit this to see
1426 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001427 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001428 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001429}
1430
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001431template <class ELFT>
Simon Atanasyane1bfc2e2016-02-08 10:05:13 +00001432bool MipsTargetInfo<ELFT>::needsCopyRel(uint32_t Type,
1433 const SymbolBody &S) const {
1434 if (Config->Shared)
1435 return false;
1436 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1437 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
1438 return SS->Sym.getType() == STT_OBJECT;
1439 return false;
1440}
1441
1442template <class ELFT>
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001443bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001444 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001445}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001446
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001447template <class ELFT>
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001448bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001449 return false;
1450}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001451
Simon Atanasyan35031192015-12-15 06:06:34 +00001452static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001453
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001454template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001455static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001456 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001457 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001458 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001459 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1460 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001461 checkAlignment<(1 << SHIFT)>(S + A, Type);
1462 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001463 checkInt<BSIZE + SHIFT>(V, Type);
1464 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001465}
1466
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001467template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001468void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001469 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001470 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001471 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001472 switch (Type) {
1473 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001474 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001475 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001476 case R_MIPS_CALL16:
1477 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001478 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001479 if (Type == R_MIPS_GOT16)
1480 checkInt<16>(V, Type);
1481 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1482 break;
1483 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001484 case R_MIPS_GPREL16: {
1485 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001486 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001487 checkInt<16>(V, Type);
1488 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1489 break;
1490 }
1491 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001492 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001493 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001494 case R_MIPS_HI16: {
1495 uint32_t Instr = read32<E>(Loc);
1496 if (PairedLoc) {
1497 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001498 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001499 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001500 } else {
1501 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001502 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001503 }
1504 break;
1505 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001506 case R_MIPS_JALR:
1507 // Ignore this optimization relocation for now
1508 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001509 case R_MIPS_LO16: {
1510 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001511 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001512 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001513 break;
1514 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001515 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001516 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001517 break;
1518 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001519 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001520 break;
1521 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001522 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001523 break;
1524 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001525 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001526 break;
1527 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001528 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001529 break;
1530 case R_MIPS_PCHI16: {
1531 uint32_t Instr = read32<E>(Loc);
1532 if (PairedLoc) {
1533 uint64_t AHL = ((Instr & 0xffff) << 16) +
1534 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001535 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001536 } else {
1537 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001538 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001539 }
1540 break;
1541 }
1542 case R_MIPS_PCLO16: {
1543 uint32_t Instr = read32<E>(Loc);
1544 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001545 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001546 break;
1547 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001548 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001549 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001550 }
1551}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001552
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001553template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001554bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001555 return Type == R_MIPS_JALR;
1556}
1557
1558template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001559bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1560 switch (Type) {
1561 default:
1562 return false;
1563 case R_MIPS_PC16:
1564 case R_MIPS_PC19_S2:
1565 case R_MIPS_PC21_S2:
1566 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001567 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001568 case R_MIPS_PCHI16:
1569 case R_MIPS_PCLO16:
1570 return true;
1571 }
1572}
1573
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001574// _gp is a MIPS-specific ABI-defined symbol which points to
1575// a location that is relative to GOT. This function returns
1576// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001577template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001578 unsigned GPOffset = 0x7ff0;
1579 if (uint64_t V = Out<ELFT>::Got->getVA())
1580 return V + GPOffset;
1581 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001582}
1583
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001584template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1585template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1586template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1587template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1588
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001589template uint32_t getMipsGpAddr<ELF32LE>();
1590template uint32_t getMipsGpAddr<ELF32BE>();
1591template uint64_t getMipsGpAddr<ELF64LE>();
1592template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001593}
1594}