blob: f1e7d80d075bf90ef8371cca9e5e0bb5717e4ee6 [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 Ueyama64cfffd2016-01-28 18:40:06 +000049 fatal("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 Ueyama64cfffd2016-01-28 18:40:06 +000056 fatal("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 Ueyama64cfffd2016-01-28 18:40:06 +000063 fatal("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 Ueyama64cfffd2016-01-28 18:40:06 +000070 fatal("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
79template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
80template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
81template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
82template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
83
Rui Ueyamaefc23de2015-10-14 21:30:32 +000084namespace {
85class X86TargetInfo final : public TargetInfo {
86public:
87 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000088 void writeGotPltHeader(uint8_t *Buf) const override;
89 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000090 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000091 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
92 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000093 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000094 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
95 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000096 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000097 bool needsDynRelative(unsigned Type) const override;
98 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
99 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000100 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000101 uint64_t SA, uint64_t ZA = 0,
102 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000103 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000104 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
105 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000106 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000107
108private:
109 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
110 uint64_t SA) const;
111 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
112 uint64_t SA) const;
113 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
114 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000115 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
116 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000117};
118
119class X86_64TargetInfo final : public TargetInfo {
120public:
121 X86_64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000122 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
123 void writeGotPltHeader(uint8_t *Buf) const override;
124 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000125 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000126 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
127 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000128 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000129 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
130 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000131 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000132 uint64_t SA, uint64_t ZA = 0,
133 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000135 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000136 bool isSizeRel(uint32_t Type) const override;
137 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
138 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000139
140private:
141 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
142 uint64_t SA) const;
143 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000145 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000147 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000149};
150
Davide Italiano8c3444362016-01-11 19:45:33 +0000151class PPCTargetInfo final : public TargetInfo {
152public:
153 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
155 uint64_t SA, uint64_t ZA = 0,
156 uint8_t *PairedLoc = nullptr) const override;
157 bool isRelRelative(uint32_t Type) const override;
158};
159
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000160class PPC64TargetInfo final : public TargetInfo {
161public:
162 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
164 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000165 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
166 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000167 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000168 uint64_t SA, uint64_t ZA = 0,
169 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170 bool isRelRelative(uint32_t Type) const override;
171};
172
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173class AArch64TargetInfo final : public TargetInfo {
174public:
175 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000176 unsigned getDynRel(unsigned Type) const override;
177 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000178 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000179 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
180 int32_t Index, unsigned RelOff) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +0000181 unsigned getTlsGotRel(unsigned Type = -1) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000182 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000183 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000184 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
185 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000186 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000187 uint64_t SA, uint64_t ZA = 0,
188 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000189};
190
Tom Stellard80efb162016-01-07 03:59:08 +0000191class AMDGPUTargetInfo final : public TargetInfo {
192public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000193 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000194 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
195 uint64_t SA, uint64_t ZA = 0,
196 uint8_t *PairedLoc = nullptr) const override;
197};
198
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000199template <class ELFT> class MipsTargetInfo final : public TargetInfo {
200public:
201 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000202 unsigned getDynRel(unsigned Type) const override;
203 void writeGotHeader(uint8_t *Buf) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000204 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
205 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000206 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000207 uint64_t SA, uint64_t ZA = 0,
208 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000209 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000210 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000211};
212} // anonymous namespace
213
Rui Ueyama91004392015-10-13 16:08:15 +0000214TargetInfo *createTarget() {
215 switch (Config->EMachine) {
216 case EM_386:
217 return new X86TargetInfo();
218 case EM_AARCH64:
219 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000220 case EM_AMDGPU:
221 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000222 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000223 switch (Config->EKind) {
224 case ELF32LEKind:
225 return new MipsTargetInfo<ELF32LE>();
226 case ELF32BEKind:
227 return new MipsTargetInfo<ELF32BE>();
228 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000229 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000230 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000231 case EM_PPC:
232 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000233 case EM_PPC64:
234 return new PPC64TargetInfo();
235 case EM_X86_64:
236 return new X86_64TargetInfo();
237 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000238 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000239}
240
Rafael Espindola01205f72015-09-22 18:19:46 +0000241TargetInfo::~TargetInfo() {}
242
Rui Ueyamabaf16512016-01-29 00:20:12 +0000243bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000244 return false;
245}
246
Igor Kudrinf6f45472015-11-10 08:39:27 +0000247uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
248
Rui Ueyama02dfd492015-12-17 01:18:40 +0000249bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000250 return false;
251}
252
Rui Ueyama012eb782016-01-29 04:05:09 +0000253bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
254 return Type == TlsLocalDynamicRel;
255}
256
257bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
258 return Type == TlsGlobalDynamicRel;
259}
260
261bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
262 return false;
263}
264
George Rimarbfb7bf72015-12-21 10:00:12 +0000265bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000266bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000267bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000268bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000269
Rui Ueyama012eb782016-01-29 04:05:09 +0000270bool TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
271 return false;
272}
273
274bool TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
275 return false;
276}
277
Rui Ueyamac516ae12016-01-29 02:33:45 +0000278unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
279 uint64_t P, uint64_t SA,
280 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000281 return 0;
282}
George Rimar77d1cb12015-11-24 09:00:06 +0000283
Rafael Espindola7f074422015-09-22 21:35:51 +0000284X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000285 CopyRel = R_386_COPY;
286 GotRel = R_386_GLOB_DAT;
287 PltRel = R_386_JUMP_SLOT;
288 IRelativeRel = R_386_IRELATIVE;
289 RelativeRel = R_386_RELATIVE;
290 TlsGotRel = R_386_TLS_TPOFF;
291 TlsGlobalDynamicRel = R_386_TLS_GD;
292 TlsLocalDynamicRel = R_386_TLS_LDM;
293 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
294 TlsOffsetRel = R_386_TLS_DTPOFF32;
295 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000296 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000297 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000298}
299
Rui Ueyamac516ae12016-01-29 02:33:45 +0000300void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000301 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
302}
303
Rui Ueyamac516ae12016-01-29 02:33:45 +0000304void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
George Rimar77b77792015-11-25 22:15:01 +0000305 // Skip 6 bytes of "pushl (GOT+4)"
306 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000307}
Rafael Espindola01205f72015-09-22 18:19:46 +0000308
Rui Ueyamac516ae12016-01-29 02:33:45 +0000309unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000310 if (Type == R_386_TLS_LE)
311 return R_386_TLS_TPOFF;
312 if (Type == R_386_TLS_LE_32)
313 return R_386_TLS_TPOFF32;
314 return Type;
315}
316
Rui Ueyama724d6252016-01-29 01:49:32 +0000317unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000318 if (Type == R_386_TLS_IE)
319 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000320 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000321}
322
Rui Ueyamac516ae12016-01-29 02:33:45 +0000323bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000324 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
325 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000326 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000327 if (Type == R_386_TLS_IE)
328 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000329 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000330}
331
Rui Ueyama900e2d22016-01-29 03:51:49 +0000332void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000333 // Executable files and shared object files have
334 // separate procedure linkage tables.
335 if (Config->Shared) {
336 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000337 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
George Rimar77b77792015-11-25 22:15:01 +0000338 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
339 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
340 };
341 memcpy(Buf, V, sizeof(V));
342 return;
343 }
George Rimar648a2c32015-10-20 08:54:27 +0000344
George Rimar77b77792015-11-25 22:15:01 +0000345 const uint8_t PltData[] = {
346 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
347 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
348 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
349 };
350 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000351 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
352 write32le(Buf + 2, Got + 4); // GOT+4
353 write32le(Buf + 8, Got + 8); // GOT+8
George Rimar77b77792015-11-25 22:15:01 +0000354}
355
Rui Ueyama9398f862016-01-29 04:15:02 +0000356void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
357 uint64_t PltEntryAddr, int32_t Index,
358 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000359 const uint8_t Inst[] = {
360 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
361 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
362 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
363 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000364 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000365
George Rimar77b77792015-11-25 22:15:01 +0000366 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
367 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000368 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
369 : Out<ELF32LE>::Got->getVA();
370 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000371 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000372 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000373}
374
Rui Ueyama02dfd492015-12-17 01:18:40 +0000375bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000376 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
377 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
378 return SS->Sym.getType() == STT_OBJECT;
379 return false;
380}
381
Rui Ueyamac516ae12016-01-29 02:33:45 +0000382bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000383 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000384 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000385 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000386 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000387 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000388}
389
Rui Ueyamac516ae12016-01-29 02:33:45 +0000390bool X86TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000391 return isGnuIFunc<ELF32LE>(S) ||
392 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000393 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000394}
395
George Rimarbfb7bf72015-12-21 10:00:12 +0000396bool X86TargetInfo::isGotRelative(uint32_t Type) const {
397 // This relocation does not require got entry,
398 // but it is relative to got and needs it to be created.
399 // Here we request for that.
400 return Type == R_386_GOTOFF;
401}
402
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000403void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000404 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000405 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000406 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000407 case R_386_32:
408 add32le(Loc, SA);
409 break;
George Rimarffb67352016-01-19 11:00:48 +0000410 case R_386_GOT32: {
411 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
412 Out<ELF32LE>::Got->getNumEntries() * 4;
413 checkInt<32>(V, Type);
414 add32le(Loc, V);
415 break;
416 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000417 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000418 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000419 break;
George Rimar13934772015-11-25 20:20:31 +0000420 case R_386_GOTPC:
421 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
422 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000423 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000424 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000425 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000426 break;
George Rimar9db204a2015-12-02 09:58:20 +0000427 case R_386_TLS_GD:
428 case R_386_TLS_LDM:
429 case R_386_TLS_TPOFF: {
430 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
431 Out<ELF32LE>::Got->getNumEntries() * 4;
432 checkInt<32>(V, Type);
433 write32le(Loc, V);
434 break;
435 }
George Rimar6f17e092015-12-17 09:32:21 +0000436 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000437 case R_386_TLS_LDO_32:
438 write32le(Loc, SA);
439 break;
George Rimard23970f2015-11-25 20:41:53 +0000440 case R_386_TLS_LE:
441 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
442 break;
443 case R_386_TLS_LE_32:
444 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
445 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000446 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000447 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000448 }
449}
450
Rui Ueyamabaf16512016-01-29 00:20:12 +0000451bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000452 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000453 return false;
454 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
455 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000456 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000457 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
458}
459
Rui Ueyamac516ae12016-01-29 02:33:45 +0000460bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000461 return Config->Shared && Type == R_386_TLS_IE;
462}
463
Rui Ueyamac516ae12016-01-29 02:33:45 +0000464unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
465 uint64_t P, uint64_t SA,
466 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000467 switch (Type) {
468 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000469 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000470 relocateTlsGdToIe(Loc, BufEnd, P, SA);
471 else
472 relocateTlsGdToLe(Loc, BufEnd, P, SA);
473 // The next relocation should be against __tls_get_addr, so skip it
474 return 1;
475 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000476 case R_386_TLS_IE:
477 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000478 return 0;
479 case R_386_TLS_LDM:
480 relocateTlsLdToLe(Loc, BufEnd, P, SA);
481 // The next relocation should be against __tls_get_addr, so skip it
482 return 1;
483 case R_386_TLS_LDO_32:
484 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
485 return 0;
486 }
487 llvm_unreachable("Unknown TLS optimization");
488}
489
490// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
491// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
492// how GD can be optimized to IE:
493// leal x@tlsgd(, %ebx, 1),
494// call __tls_get_addr@plt
495// Is converted to:
496// movl %gs:0, %eax
497// addl x@gotntpoff(%ebx), %eax
498void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
499 uint64_t SA) const {
500 const uint8_t Inst[] = {
501 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
502 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
503 };
504 memcpy(Loc - 3, Inst, sizeof(Inst));
505 relocateOne(Loc + 5, BufEnd, R_386_32, P,
506 SA - Out<ELF32LE>::Got->getVA() -
507 Out<ELF32LE>::Got->getNumEntries() * 4);
508}
509
510// GD can be optimized to LE:
511// leal x@tlsgd(, %ebx, 1),
512// call __tls_get_addr@plt
513// Can be converted to:
514// movl %gs:0,%eax
515// addl $x@ntpoff,%eax
516// But gold emits subl $foo@tpoff,%eax instead of addl.
517// These instructions are completely equal in behavior.
518// This method generates subl to be consistent with gold.
519void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
520 uint64_t SA) const {
521 const uint8_t Inst[] = {
522 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
523 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
524 };
525 memcpy(Loc - 3, Inst, sizeof(Inst));
526 relocateOne(Loc + 5, BufEnd, R_386_32, P,
527 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
528}
529
530// LD can be optimized to LE:
531// leal foo(%reg),%eax
532// call ___tls_get_addr
533// Is converted to:
534// movl %gs:0,%eax
535// nop
536// leal 0(%esi,1),%esi
537void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
538 uint64_t SA) const {
539 const uint8_t Inst[] = {
540 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
541 0x90, // nop
542 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
543 };
544 memcpy(Loc - 2, Inst, sizeof(Inst));
545}
546
George Rimar6f17e092015-12-17 09:32:21 +0000547// In some conditions, relocations can be optimized to avoid using GOT.
548// This function does that for Initial Exec to Local Exec case.
549// Read "ELF Handling For Thread-Local Storage, 5.1
550// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000551// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000552void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
553 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000554 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000555 // Ulrich's document section 6.2 says that @gotntpoff can
556 // be used with MOVL or ADDL instructions.
557 // @indntpoff is similar to @gotntpoff, but for use in
558 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000559 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000560 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000561 uint8_t Reg = (Loc[-1] >> 3) & 7;
562 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000563 if (Type == R_386_TLS_IE) {
564 // For R_386_TLS_IE relocation we perform the next transformations:
565 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
566 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
567 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
568 // First one is special because when EAX is used the sequence is 5 bytes
569 // long, otherwise it is 6 bytes.
570 if (*Op == 0xa1) {
571 *Op = 0xb8;
572 } else {
573 *Inst = IsMov ? 0xc7 : 0x81;
574 *Op = 0xc0 | ((*Op >> 3) & 7);
575 }
576 } else {
577 // R_386_TLS_GOTIE relocation can be optimized to
578 // R_386_TLS_LE so that it does not use GOT.
579 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
580 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
581 // Note: gold converts to ADDL instead of LEAL.
582 *Inst = IsMov ? 0xc7 : 0x8d;
583 if (IsMov)
584 *Op = 0xc0 | ((*Op >> 3) & 7);
585 else
586 *Op = 0x80 | Reg | (Reg << 3);
587 }
George Rimar2558e122015-12-09 09:55:54 +0000588 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
589}
590
Rafael Espindola7f074422015-09-22 21:35:51 +0000591X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000592 CopyRel = R_X86_64_COPY;
593 GotRel = R_X86_64_GLOB_DAT;
594 PltRel = R_X86_64_JUMP_SLOT;
595 RelativeRel = R_X86_64_RELATIVE;
596 IRelativeRel = R_X86_64_IRELATIVE;
597 TlsGotRel = R_X86_64_TPOFF64;
598 TlsLocalDynamicRel = R_X86_64_TLSLD;
599 TlsGlobalDynamicRel = R_X86_64_TLSGD;
600 TlsModuleIndexRel = R_X86_64_DTPMOD64;
601 TlsOffsetRel = R_X86_64_DTPOFF64;
602 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000603 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000604 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000605}
606
Rui Ueyamac516ae12016-01-29 02:33:45 +0000607void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000608 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
609}
610
Rui Ueyamac516ae12016-01-29 02:33:45 +0000611void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
George Rimar648a2c32015-10-20 08:54:27 +0000612 // Skip 6 bytes of "jmpq *got(%rip)"
613 write32le(Buf, Plt + 6);
614}
615
Rui Ueyama900e2d22016-01-29 03:51:49 +0000616void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000617 const uint8_t PltData[] = {
618 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
619 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
620 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
621 };
622 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000623 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
624 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
625 write32le(Buf + 2, Got - Plt + 2); // GOT+8
626 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000627}
Rafael Espindola01205f72015-09-22 18:19:46 +0000628
Rui Ueyama9398f862016-01-29 04:15:02 +0000629void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
630 uint64_t PltEntryAddr, int32_t Index,
631 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000632 const uint8_t Inst[] = {
633 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
634 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
635 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
636 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000637 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000638
George Rimar648a2c32015-10-20 08:54:27 +0000639 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
640 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000641 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000642}
643
Rui Ueyama02dfd492015-12-17 01:18:40 +0000644bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000645 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
646 Type == R_X86_64_64)
647 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
648 return SS->Sym.getType() == STT_OBJECT;
649 return false;
650}
651
Rui Ueyamac516ae12016-01-29 02:33:45 +0000652bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000653 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000654 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000655 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000656 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000657 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000658}
659
Rui Ueyamac516ae12016-01-29 02:33:45 +0000660bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000661 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000662}
663
Rui Ueyamac516ae12016-01-29 02:33:45 +0000664bool X86_64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000665 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000666 return false;
George Rimara07ff662015-12-21 10:12:06 +0000667 if (isGnuIFunc<ELF64LE>(S))
668 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000669
Rafael Espindola01205f72015-09-22 18:19:46 +0000670 switch (Type) {
671 default:
672 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000673 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000674 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000675 case R_X86_64_PC32:
676 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000677 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000678 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000679 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000680 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000681 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
682 // libc.so.
683 //
684 // The general idea on how to handle such cases is to create a PLT entry
685 // and use that as the function value.
686 //
687 // For the static linking part, we just return true and everything else
688 // will use the the PLT entry as the address.
689 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000690 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000691 // still works. We need the help of the dynamic linker for that. We
692 // let it know that we have a direct reference to a so symbol by creating
693 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000694 // dynamic linker resolves the symbol to the value of the symbol we created.
695 // This is true even for got entries, so pointer equality is maintained.
696 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000697 // real function is a dedicated got entry used by the plt. That is
698 // identified by special relocation types (R_X86_64_JUMP_SLOT,
699 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000700 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000701 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000702 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000703 }
704}
Rafael Espindolac4010882015-09-22 20:54:08 +0000705
Rafael Espindolaae244002015-10-05 19:30:12 +0000706bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
707 switch (Type) {
708 default:
709 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000710 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000711 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000712 case R_X86_64_PC8:
713 case R_X86_64_PC16:
714 case R_X86_64_PC32:
715 case R_X86_64_PC64:
716 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000717 return true;
718 }
719}
720
Rui Ueyamac516ae12016-01-29 02:33:45 +0000721bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000722 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000723}
724
Rui Ueyamabaf16512016-01-29 00:20:12 +0000725bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000726 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000727 return false;
George Rimar25411f252015-12-04 11:20:13 +0000728 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
729 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000730 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
731}
732
733// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
734// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
735// how LD can be optimized to LE:
736// leaq bar@tlsld(%rip), %rdi
737// callq __tls_get_addr@PLT
738// leaq bar@dtpoff(%rax), %rcx
739// Is converted to:
740// .word 0x6666
741// .byte 0x66
742// mov %fs:0,%rax
743// leaq bar@tpoff(%rax), %rcx
744void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
745 uint64_t P, uint64_t SA) const {
746 const uint8_t Inst[] = {
747 0x66, 0x66, //.word 0x6666
748 0x66, //.byte 0x66
749 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
750 };
751 memcpy(Loc - 3, Inst, sizeof(Inst));
752}
753
754// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
755// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
756// how GD can be optimized to LE:
757// .byte 0x66
758// leaq x@tlsgd(%rip), %rdi
759// .word 0x6666
760// rex64
761// call __tls_get_addr@plt
762// Is converted to:
763// mov %fs:0x0,%rax
764// lea x@tpoff,%rax
765void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
766 uint64_t P, uint64_t SA) const {
767 const uint8_t Inst[] = {
768 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
769 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
770 };
771 memcpy(Loc - 4, Inst, sizeof(Inst));
772 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000773}
774
George Rimar25411f252015-12-04 11:20:13 +0000775// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
776// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
777// how GD can be optimized to IE:
778// .byte 0x66
779// leaq x@tlsgd(%rip), %rdi
780// .word 0x6666
781// rex64
782// call __tls_get_addr@plt
783// Is converted to:
784// mov %fs:0x0,%rax
785// addq x@tpoff,%rax
786void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
787 uint64_t P, uint64_t SA) const {
788 const uint8_t Inst[] = {
789 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
790 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
791 };
792 memcpy(Loc - 4, Inst, sizeof(Inst));
793 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
794}
795
George Rimar77d1cb12015-11-24 09:00:06 +0000796// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000797// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000798// This function does that. Read "ELF Handling For Thread-Local Storage,
799// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
800// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000801void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
802 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000803 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
804 // used in MOVQ or ADDQ instructions only.
805 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
806 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
807 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
808 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
809 uint8_t *Prefix = Loc - 3;
810 uint8_t *Inst = Loc - 2;
811 uint8_t *RegSlot = Loc - 1;
812 uint8_t Reg = Loc[-1] >> 3;
813 bool IsMov = *Inst == 0x8b;
814 bool RspAdd = !IsMov && Reg == 4;
815 // r12 and rsp registers requires special handling.
816 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
817 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
818 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
819 // The same true for rsp. So we convert to addq for them, saving 1 byte that
820 // we dont have.
821 if (RspAdd)
822 *Inst = 0x81;
823 else
824 *Inst = IsMov ? 0xc7 : 0x8d;
825 if (*Prefix == 0x4c)
826 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
827 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
828 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
829}
830
George Rimar6713cf82015-11-25 21:46:05 +0000831// This function applies a TLS relocation with an optimization as described
832// in the Ulrich's document. As a result of rewriting instructions at the
833// relocation target, relocations immediately follow the TLS relocation (which
834// would be applied to rewritten instructions) may have to be skipped.
835// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000836unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
837 uint32_t Type, uint64_t P, uint64_t SA,
838 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000839 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000840 case R_X86_64_DTPOFF32:
841 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
842 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000843 case R_X86_64_GOTTPOFF:
844 relocateTlsIeToLe(Loc, BufEnd, P, SA);
845 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000846 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000847 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000848 relocateTlsGdToIe(Loc, BufEnd, P, SA);
849 else
850 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000851 // The next relocation should be against __tls_get_addr, so skip it
852 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000853 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000854 case R_X86_64_TLSLD:
855 relocateTlsLdToLe(Loc, BufEnd, P, SA);
856 // The next relocation should be against __tls_get_addr, so skip it
857 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000858 }
859 llvm_unreachable("Unknown TLS optimization");
860}
861
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000862void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000863 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000864 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000865 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000866 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000867 checkUInt<32>(SA, Type);
868 write32le(Loc, SA);
869 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000870 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000871 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000872 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000873 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000874 case R_X86_64_64:
875 write64le(Loc, SA);
876 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000877 case R_X86_64_DTPOFF32:
878 write32le(Loc, SA);
879 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000880 case R_X86_64_DTPOFF64:
881 write64le(Loc, SA);
882 break;
883 case R_X86_64_GOTPCREL:
884 case R_X86_64_PC32:
885 case R_X86_64_PLT32:
886 case R_X86_64_TLSGD:
887 case R_X86_64_TLSLD:
888 write32le(Loc, SA - P);
889 break;
George Rimar48651482015-12-11 08:59:37 +0000890 case R_X86_64_SIZE32:
891 write32le(Loc, ZA);
892 break;
893 case R_X86_64_SIZE64:
894 write64le(Loc, ZA);
895 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000896 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000897 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000898 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000899 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000900 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000901 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000902 case R_X86_64_TPOFF64:
903 write32le(Loc, SA - P);
904 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000905 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000906 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000907 }
908}
909
Hal Finkel3c8cc672015-10-12 20:56:18 +0000910// Relocation masks following the #lo(value), #hi(value), #ha(value),
911// #higher(value), #highera(value), #highest(value), and #highesta(value)
912// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
913// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000914static uint16_t applyPPCLo(uint64_t V) { return V; }
915static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
916static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
917static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
918static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000919static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000920static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
921
Davide Italiano8c3444362016-01-11 19:45:33 +0000922PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000923bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
924
925void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
926 uint64_t P, uint64_t SA, uint64_t ZA,
927 uint8_t *PairedLoc) const {
928 switch (Type) {
929 case R_PPC_ADDR16_HA:
930 write16be(Loc, applyPPCHa(SA));
931 break;
932 case R_PPC_ADDR16_LO:
933 write16be(Loc, applyPPCLo(SA));
934 break;
935 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000936 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000937 }
938}
939
Rafael Espindolac4010882015-09-22 20:54:08 +0000940PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000941 GotRel = R_PPC64_GLOB_DAT;
942 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000943 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000944
945 // We need 64K pages (at least under glibc/Linux, the loader won't
946 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000947 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000948
949 // The PPC64 ELF ABI v1 spec, says:
950 //
951 // It is normally desirable to put segments with different characteristics
952 // in separate 256 Mbyte portions of the address space, to give the
953 // operating system full paging flexibility in the 64-bit address space.
954 //
955 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
956 // use 0x10000000 as the starting address.
957 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000958}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000959
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000960uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000961 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
962 // order. The TOC starts where the first of these sections starts.
963
964 // FIXME: This obviously does not do the right thing when there is no .got
965 // section, but there is a .toc or .tocbss section.
966 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
967 if (!TocVA)
968 TocVA = Out<ELF64BE>::Plt->getVA();
969
970 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
971 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
972 // code (crt1.o) assumes that you can get from the TOC base to the
973 // start of the .toc section with only a single (signed) 16-bit relocation.
974 return TocVA + 0x8000;
975}
976
Rui Ueyama9398f862016-01-29 04:15:02 +0000977void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
978 uint64_t PltEntryAddr, int32_t Index,
979 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000980 uint64_t Off = GotEntryAddr - getPPC64TocBase();
981
982 // FIXME: What we should do, in theory, is get the offset of the function
983 // descriptor in the .opd section, and use that as the offset from %r2 (the
984 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
985 // be a pointer to the function descriptor in the .opd section. Using
986 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
987
Hal Finkelfa92f682015-10-13 21:47:34 +0000988 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000989 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
990 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
991 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
992 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
993 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
994 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
995 write32be(Buf + 28, 0x4e800420); // bctr
996}
997
Rui Ueyamac516ae12016-01-29 02:33:45 +0000998bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
999 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001000 return true;
1001
1002 switch (Type) {
1003 default: return false;
1004 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001005 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001006 case R_PPC64_GOT16_HA:
1007 case R_PPC64_GOT16_HI:
1008 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001009 case R_PPC64_GOT16_LO_DS:
1010 return true;
1011 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001012}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001013
Rui Ueyamac516ae12016-01-29 02:33:45 +00001014bool PPC64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001015 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001016 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001017}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001018
Hal Finkelbe0823d2015-10-12 20:58:52 +00001019bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1020 switch (Type) {
1021 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001022 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001023 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001024 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001025 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001026 }
1027}
1028
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001029void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001030 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001031 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001032 uint64_t TB = getPPC64TocBase();
1033
Hal Finkel3c8cc672015-10-12 20:56:18 +00001034 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1035 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001036 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001037 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1038 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001039 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1040 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001041 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1042 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 default: break;
1044 }
1045
Hal Finkel3c8cc672015-10-12 20:56:18 +00001046 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001047 case R_PPC64_ADDR14: {
1048 checkAlignment<4>(SA, Type);
1049 // Preserve the AA/LK bits in the branch instruction
1050 uint8_t AALK = Loc[3];
1051 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1052 break;
1053 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001054 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001055 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001056 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001057 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001059 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001060 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001062 case R_PPC64_ADDR16_HA:
1063 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001064 break;
1065 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001066 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001067 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001069 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001070 break;
1071 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001072 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 break;
1074 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001075 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076 break;
1077 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001078 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001079 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001080 case R_PPC64_ADDR16_LO:
1081 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001083 case R_PPC64_ADDR16_LO_DS:
1084 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001085 break;
1086 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001087 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001088 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001089 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001090 case R_PPC64_ADDR64:
1091 write64be(Loc, SA);
1092 break;
1093 case R_PPC64_REL16_HA:
1094 write16be(Loc, applyPPCHa(SA - P));
1095 break;
1096 case R_PPC64_REL16_HI:
1097 write16be(Loc, applyPPCHi(SA - P));
1098 break;
1099 case R_PPC64_REL16_LO:
1100 write16be(Loc, applyPPCLo(SA - P));
1101 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001102 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001103 // If we have an undefined weak symbol, we might get here with a symbol
1104 // address of zero. That could overflow, but the code must be unreachable,
1105 // so don't bother doing anything at all.
1106 if (!SA)
1107 break;
1108
Hal Finkeldaedc122015-10-12 23:16:53 +00001109 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1110 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001111 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001112
1113 if (!InPlt && Out<ELF64BE>::Opd) {
1114 // If this is a local call, and we currently have the address of a
1115 // function-descriptor, get the underlying code address instead.
1116 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1117 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001118 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001119
1120 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001121 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001122 }
1123
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001125 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001126 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001127
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001128 uint32_t Nop = 0x60000000;
1129 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1130 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 break;
1132 }
1133 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001134 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
1137 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001140 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001141 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001142 break;
1143 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001144 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001145 }
1146}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001147
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001148AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001149 CopyRel = R_AARCH64_COPY;
1150 IRelativeRel = R_AARCH64_IRELATIVE;
1151 GotRel = R_AARCH64_GLOB_DAT;
1152 PltRel = R_AARCH64_JUMP_SLOT;
1153 TlsGotRel = R_AARCH64_TLS_TPREL64;
1154 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001155 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001156 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001157}
George Rimar648a2c32015-10-20 08:54:27 +00001158
Rui Ueyamac516ae12016-01-29 02:33:45 +00001159unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001160 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1161 return Type;
1162 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001163 fatal("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001164 "recompile with -fPIC.");
1165}
1166
Rui Ueyamac516ae12016-01-29 02:33:45 +00001167void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001168 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1169}
1170
Rui Ueyama900e2d22016-01-29 03:51:49 +00001171void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001172 const uint8_t PltData[] = {
1173 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1174 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1175 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1176 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1177 0x20, 0x02, 0x1f, 0xd6, // br x17
1178 0x1f, 0x20, 0x03, 0xd5, // nop
1179 0x1f, 0x20, 0x03, 0xd5, // nop
1180 0x1f, 0x20, 0x03, 0xd5 // nop
1181 };
1182 memcpy(Buf, PltData, sizeof(PltData));
1183
Rui Ueyama900e2d22016-01-29 03:51:49 +00001184 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1185 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1186 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1187 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1188 Got + 16);
1189 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1190 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001191}
1192
Rui Ueyama9398f862016-01-29 04:15:02 +00001193void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1194 uint64_t PltEntryAddr, int32_t Index,
1195 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001196 const uint8_t Inst[] = {
1197 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1198 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1199 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1200 0x20, 0x02, 0x1f, 0xd6 // br x17
1201 };
1202 memcpy(Buf, Inst, sizeof(Inst));
1203
1204 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1205 GotEntryAddr);
1206 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1207 GotEntryAddr);
1208 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1209 GotEntryAddr);
1210}
1211
Rui Ueyama724d6252016-01-29 01:49:32 +00001212unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar3d737e42016-01-13 13:04:46 +00001213 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1214 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1215 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +00001216 return TlsGotRel;
George Rimar3d737e42016-01-13 13:04:46 +00001217}
1218
Rui Ueyamac516ae12016-01-29 02:33:45 +00001219bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001220 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1221 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1222}
1223
Rui Ueyama02dfd492015-12-17 01:18:40 +00001224bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001225 if (Config->Shared)
1226 return false;
1227 switch (Type) {
1228 default:
1229 return false;
1230 case R_AARCH64_ABS16:
1231 case R_AARCH64_ABS32:
1232 case R_AARCH64_ABS64:
1233 case R_AARCH64_ADD_ABS_LO12_NC:
1234 case R_AARCH64_ADR_PREL_LO21:
1235 case R_AARCH64_ADR_PREL_PG_HI21:
1236 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001237 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001238 case R_AARCH64_LDST32_ABS_LO12_NC:
1239 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001240 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001241 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1242 return SS->Sym.getType() == STT_OBJECT;
1243 return false;
1244 }
1245}
1246
Rui Ueyamac516ae12016-01-29 02:33:45 +00001247bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001248 switch (Type) {
1249 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1250 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1251 case R_AARCH64_ADR_GOT_PAGE:
1252 case R_AARCH64_LD64_GOT_LO12_NC:
1253 return true;
1254 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001255 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001256 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001257}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001258
Rui Ueyamac516ae12016-01-29 02:33:45 +00001259bool AArch64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001260 if (isGnuIFunc<ELF64LE>(S))
1261 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001262 switch (Type) {
1263 default:
1264 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001265 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001266 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001267 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001268 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001269 return canBePreempted(&S, true);
1270 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001271}
Davide Italiano1d750a62015-09-27 08:45:38 +00001272
Davide Italianoef4be6b2015-10-06 19:01:32 +00001273static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001274 uint32_t ImmLo = (Imm & 0x3) << 29;
1275 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1276 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001277 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001278}
1279
Davide Italiano318ca222015-10-02 22:13:51 +00001280// Page(Expr) is the page address of the expression Expr, defined
1281// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001282// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001283static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001284 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001285}
1286
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001287void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001288 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001289 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001290 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001291 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001292 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001293 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001294 break;
1295 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001296 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001297 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001298 break;
1299 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001300 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001301 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001302 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001303 // This relocation stores 12 bits and there's no instruction
1304 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001305 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1306 // bits in Loc are zero.
1307 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001308 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001309 case R_AARCH64_ADR_GOT_PAGE: {
1310 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1311 checkInt<33>(X, Type);
1312 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1313 break;
1314 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001315 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001316 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001317 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001318 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001319 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001320 }
George Rimar3d737e42016-01-13 13:04:46 +00001321 case R_AARCH64_ADR_PREL_PG_HI21:
1322 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001323 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001324 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001325 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001326 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001327 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001328 case R_AARCH64_CALL26:
1329 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001330 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001331 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001332 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1333 break;
1334 }
George Rimar4102bfb2016-01-11 14:22:00 +00001335 case R_AARCH64_CONDBR19: {
1336 uint64_t X = SA - P;
1337 checkInt<21>(X, Type);
1338 or32le(Loc, (X & 0x1FFFFC) << 3);
1339 break;
1340 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001341 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001342 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001343 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001344 or32le(Loc, (SA & 0xFF8) << 7);
1345 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001346 case R_AARCH64_LDST128_ABS_LO12_NC:
1347 or32le(Loc, (SA & 0x0FF8) << 6);
1348 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001349 case R_AARCH64_LDST16_ABS_LO12_NC:
1350 or32le(Loc, (SA & 0x0FFC) << 9);
1351 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001352 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001353 or32le(Loc, (SA & 0xFFF) << 10);
1354 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001355 case R_AARCH64_LDST32_ABS_LO12_NC:
1356 or32le(Loc, (SA & 0xFFC) << 8);
1357 break;
1358 case R_AARCH64_LDST64_ABS_LO12_NC:
1359 or32le(Loc, (SA & 0xFF8) << 7);
1360 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001361 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001362 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001363 write16le(Loc, SA - P);
1364 break;
1365 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001366 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001367 write32le(Loc, SA - P);
1368 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001369 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001370 write64le(Loc, SA - P);
1371 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001372 case R_AARCH64_TSTBR14: {
1373 uint64_t X = SA - P;
1374 checkInt<16>(X, Type);
1375 or32le(Loc, (X & 0xFFFC) << 3);
1376 break;
1377 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001378 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001379 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001380 }
1381}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001382
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001383// Implementing relocations for AMDGPU is low priority since most
1384// programs don't use relocations now. Thus, this function is not
1385// actually called (relocateOne is called for each relocation).
1386// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001387void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1388 uint64_t P, uint64_t SA, uint64_t ZA,
1389 uint8_t *PairedLoc) const {
1390 llvm_unreachable("not implemented");
1391}
1392
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001393template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001394 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001395 GotHeaderEntriesNum = 2;
Rui Ueyama724d6252016-01-29 01:49:32 +00001396 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001397}
1398
1399template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001400unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001401 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1402 return R_MIPS_REL32;
1403 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001404 fatal("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001405 "recompile with -fPIC.");
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;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001411 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1412 // Module pointer
1413 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001414}
1415
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001416template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001417bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001418 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001419}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001420
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001421template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001422bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001423 return false;
1424}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001425
Simon Atanasyan35031192015-12-15 06:06:34 +00001426static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001427
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001428template <endianness E, uint8_t BSIZE>
1429static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1430 uint64_t SA) {
1431 uint32_t Mask = ~(0xffffffff << BSIZE);
1432 uint32_t Instr = read32<E>(Loc);
1433 int64_t A = SignExtend64<BSIZE + 2>((Instr & Mask) << 2);
1434 checkAlignment<4>(SA + A, Type);
1435 int64_t V = SA + A - P;
1436 checkInt<BSIZE + 2>(V, Type);
1437 write32<E>(Loc, (Instr & ~Mask) | ((V >> 2) & Mask));
1438}
1439
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001440template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001441void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001442 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001443 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001444 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001445 switch (Type) {
1446 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001447 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001448 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001449 case R_MIPS_CALL16:
1450 case R_MIPS_GOT16: {
1451 int64_t V = SA - getMipsGpAddr<ELFT>();
1452 if (Type == R_MIPS_GOT16)
1453 checkInt<16>(V, Type);
1454 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1455 break;
1456 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001457 case R_MIPS_GPREL16: {
1458 uint32_t Instr = read32<E>(Loc);
1459 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1460 checkInt<16>(V, Type);
1461 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1462 break;
1463 }
1464 case R_MIPS_GPREL32:
1465 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1466 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001467 case R_MIPS_HI16: {
1468 uint32_t Instr = read32<E>(Loc);
1469 if (PairedLoc) {
1470 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001471 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001472 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001473 } else {
1474 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001475 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001476 }
1477 break;
1478 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001479 case R_MIPS_JALR:
1480 // Ignore this optimization relocation for now
1481 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001482 case R_MIPS_LO16: {
1483 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001484 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001485 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1486 break;
1487 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001488 case R_MIPS_PC16:
1489 applyMipsPcReloc<E, 16>(Loc, Type, P, SA);
1490 break;
1491 case R_MIPS_PC19_S2:
1492 applyMipsPcReloc<E, 19>(Loc, Type, P, SA);
1493 break;
1494 case R_MIPS_PC21_S2:
1495 applyMipsPcReloc<E, 21>(Loc, Type, P, SA);
1496 break;
1497 case R_MIPS_PC26_S2:
1498 applyMipsPcReloc<E, 26>(Loc, Type, P, SA);
1499 break;
1500 case R_MIPS_PCHI16: {
1501 uint32_t Instr = read32<E>(Loc);
1502 if (PairedLoc) {
1503 uint64_t AHL = ((Instr & 0xffff) << 16) +
1504 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1505 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1506 } else {
1507 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1508 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1509 }
1510 break;
1511 }
1512 case R_MIPS_PCLO16: {
1513 uint32_t Instr = read32<E>(Loc);
1514 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1515 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1516 break;
1517 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001518 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001519 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001520 }
1521}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001522
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001523template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001524bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001525 return Type == R_MIPS_JALR;
1526}
1527
1528template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001529bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1530 switch (Type) {
1531 default:
1532 return false;
1533 case R_MIPS_PC16:
1534 case R_MIPS_PC19_S2:
1535 case R_MIPS_PC21_S2:
1536 case R_MIPS_PC26_S2:
1537 case R_MIPS_PCHI16:
1538 case R_MIPS_PCLO16:
1539 return true;
1540 }
1541}
1542
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001543// _gp is a MIPS-specific ABI-defined symbol which points to
1544// a location that is relative to GOT. This function returns
1545// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001546template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001547 unsigned GPOffset = 0x7ff0;
1548 if (uint64_t V = Out<ELFT>::Got->getVA())
1549 return V + GPOffset;
1550 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001551}
1552
Simon Atanasyan56ab5f02016-01-21 05:33:23 +00001553bool needsMipsLocalGot(uint32_t Type, SymbolBody *Body) {
1554 // The R_MIPS_GOT16 relocation requires creation of entry in the local part
1555 // of GOT if its target is a local symbol or non-local symbol with 'local'
1556 // visibility.
1557 if (Type != R_MIPS_GOT16)
1558 return false;
1559 if (!Body)
1560 return true;
1561 uint8_t V = Body->getVisibility();
1562 if (V != STV_DEFAULT && V != STV_PROTECTED)
1563 return true;
1564 return !Config->Shared;
1565}
1566
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001567template uint32_t getMipsGpAddr<ELF32LE>();
1568template uint32_t getMipsGpAddr<ELF32BE>();
1569template uint64_t getMipsGpAddr<ELF64LE>();
1570template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001571}
1572}