blob: 808f2850e246660886c4a807f0eee16b9b12ef91 [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();
George Rimar77b77792015-11-25 22:15:01 +000088 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimard23970f2015-11-25 20:41:53 +000089 unsigned getDynReloc(unsigned Type) const override;
George Rimar6f17e092015-12-17 09:32:21 +000090 unsigned getTlsGotReloc(unsigned Type) const override;
91 bool isTlsDynReloc(unsigned Type, const SymbolBody &S) const override;
George Rimar648a2c32015-10-20 08:54:27 +000092 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
93 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
94 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +000095 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
96 uint64_t PltEntryAddr, int32_t Index,
97 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000098 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
George Rimar6f17e092015-12-17 09:32:21 +000099 bool relocNeedsDynRelative(unsigned Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000100 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000101 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000102 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000103 uint64_t SA, uint64_t ZA = 0,
104 uint8_t *PairedLoc = nullptr) const override;
George Rimar2558e122015-12-09 09:55:54 +0000105 bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override;
106 unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
107 uint64_t P, uint64_t SA,
George Rimar237b2182016-01-22 18:02:28 +0000108 const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000109 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000110
111private:
112 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
113 uint64_t SA) const;
114 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
115 uint64_t SA) const;
116 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
117 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000118 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
119 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000120};
121
122class X86_64TargetInfo final : public TargetInfo {
123public:
124 X86_64TargetInfo();
George Rimar6f17e092015-12-17 09:32:21 +0000125 bool isTlsDynReloc(unsigned Type, const SymbolBody &S) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +0000126 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000127 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
128 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
129 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000130 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
131 uint64_t PltEntryAddr, int32_t Index,
132 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000133 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
135 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000136 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000137 uint64_t SA, uint64_t ZA = 0,
138 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000139 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000140 bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override;
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000141 bool isSizeReloc(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000142 unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar25411f252015-12-04 11:20:13 +0000143 uint64_t P, uint64_t SA,
George Rimar237b2182016-01-22 18:02:28 +0000144 const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000145
146private:
147 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
149 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
150 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000151 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
152 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000153 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
154 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155};
156
Davide Italiano8c3444362016-01-11 19:45:33 +0000157class PPCTargetInfo final : public TargetInfo {
158public:
159 PPCTargetInfo();
160 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
161 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
162 uint64_t PltEntryAddr) const override;
163 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
164 uint64_t PltEntryAddr, int32_t Index,
165 unsigned RelOff) const override;
166 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
167 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
168 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
169 uint64_t SA, uint64_t ZA = 0,
170 uint8_t *PairedLoc = nullptr) const override;
171 bool isRelRelative(uint32_t Type) const override;
172};
173
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000174class PPC64TargetInfo final : public TargetInfo {
175public:
176 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +0000177 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
178 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
179 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000180 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
181 uint64_t PltEntryAddr, int32_t Index,
182 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000183 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
184 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000185 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000186 uint64_t SA, uint64_t ZA = 0,
187 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000188 bool isRelRelative(uint32_t Type) const override;
189};
190
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000191class AArch64TargetInfo final : public TargetInfo {
192public:
193 AArch64TargetInfo();
Igor Kudrincfe47f52015-12-05 06:20:24 +0000194 unsigned getDynReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000195 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
196 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
197 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000198 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
199 uint64_t PltEntryAddr, int32_t Index,
200 unsigned RelOff) const override;
George Rimar3d737e42016-01-13 13:04:46 +0000201 unsigned getTlsGotReloc(unsigned Type = -1) const override;
202 bool isTlsDynReloc(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000203 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000204 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
205 bool relocNeedsPlt(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 Ueyamaefc23de2015-10-14 21:30:32 +0000209};
210
Tom Stellard80efb162016-01-07 03:59:08 +0000211class AMDGPUTargetInfo final : public TargetInfo {
212public:
213 AMDGPUTargetInfo();
214 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
215 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
216 uint64_t PltEntryAddr) const override;
217 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
218 uint64_t PltEntryAddr, int32_t Index,
219 unsigned RelOff) const override;
220 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
221 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
222 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
223 uint64_t SA, uint64_t ZA = 0,
224 uint8_t *PairedLoc = nullptr) const override;
225};
226
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000227template <class ELFT> class MipsTargetInfo final : public TargetInfo {
228public:
229 MipsTargetInfo();
Simon Atanasyanca558ea2016-01-14 21:34:50 +0000230 unsigned getDynReloc(unsigned Type) const override;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000231 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000232 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
233 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
234 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000235 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
236 uint64_t PltEntryAddr, int32_t Index,
237 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000238 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
239 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000240 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000241 uint64_t SA, uint64_t ZA = 0,
242 uint8_t *PairedLoc = nullptr) const override;
Simon Atanasyan682aeea2016-01-14 20:42:09 +0000243 bool isHintReloc(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000244 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000245};
246} // anonymous namespace
247
Rui Ueyama91004392015-10-13 16:08:15 +0000248TargetInfo *createTarget() {
249 switch (Config->EMachine) {
250 case EM_386:
251 return new X86TargetInfo();
252 case EM_AARCH64:
253 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000254 case EM_AMDGPU:
255 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000256 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000257 switch (Config->EKind) {
258 case ELF32LEKind:
259 return new MipsTargetInfo<ELF32LE>();
260 case ELF32BEKind:
261 return new MipsTargetInfo<ELF32BE>();
262 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000263 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000264 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000265 case EM_PPC:
266 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000267 case EM_PPC64:
268 return new PPC64TargetInfo();
269 case EM_X86_64:
270 return new X86_64TargetInfo();
271 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000272 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000273}
274
Rafael Espindola01205f72015-09-22 18:19:46 +0000275TargetInfo::~TargetInfo() {}
276
George Rimar6713cf82015-11-25 21:46:05 +0000277bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000278 return false;
279}
280
Igor Kudrinf6f45472015-11-10 08:39:27 +0000281uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
282
Rui Ueyama02dfd492015-12-17 01:18:40 +0000283bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000284 return false;
285}
286
George Rimarbfb7bf72015-12-21 10:00:12 +0000287bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
288
Simon Atanasyan682aeea2016-01-14 20:42:09 +0000289bool TargetInfo::isHintReloc(uint32_t Type) const { return false; }
290
Rafael Espindolaae244002015-10-05 19:30:12 +0000291bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
292
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000293bool TargetInfo::isSizeReloc(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000294
George Rimar6713cf82015-11-25 21:46:05 +0000295unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000296 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar237b2182016-01-22 18:02:28 +0000297 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000298 return 0;
299}
George Rimar77d1cb12015-11-24 09:00:06 +0000300
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000301void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
302
Igor Kudrin351b41d2015-11-16 17:44:08 +0000303void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
304
Rafael Espindola7f074422015-09-22 21:35:51 +0000305X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000306 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000307 PCRelReloc = R_386_PC32;
308 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000309 PltReloc = R_386_JUMP_SLOT;
George Rimara07ff662015-12-21 10:12:06 +0000310 IRelativeReloc = R_386_IRELATIVE;
George Rimar6f17e092015-12-17 09:32:21 +0000311 RelativeReloc = R_386_RELATIVE;
George Rimar9db204a2015-12-02 09:58:20 +0000312 TlsGotReloc = R_386_TLS_TPOFF;
313 TlsGlobalDynamicReloc = R_386_TLS_GD;
314 TlsLocalDynamicReloc = R_386_TLS_LDM;
315 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
316 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000317 LazyRelocations = true;
318 PltEntrySize = 16;
319 PltZeroEntrySize = 16;
320}
321
322void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
323 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
324}
325
326void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
327 // Skip 6 bytes of "pushl (GOT+4)"
328 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000329}
Rafael Espindola01205f72015-09-22 18:19:46 +0000330
George Rimard23970f2015-11-25 20:41:53 +0000331unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
332 if (Type == R_386_TLS_LE)
333 return R_386_TLS_TPOFF;
334 if (Type == R_386_TLS_LE_32)
335 return R_386_TLS_TPOFF32;
336 return Type;
337}
338
George Rimar6f17e092015-12-17 09:32:21 +0000339unsigned X86TargetInfo::getTlsGotReloc(unsigned Type) const {
340 if (Type == R_386_TLS_IE)
341 return Type;
342 return TlsGotReloc;
343}
344
345bool X86TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000346 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
347 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000348 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000349 if (Type == R_386_TLS_IE)
350 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000351 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000352}
353
George Rimar648a2c32015-10-20 08:54:27 +0000354void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000355 uint64_t PltEntryAddr) const {
356 // Executable files and shared object files have
357 // separate procedure linkage tables.
358 if (Config->Shared) {
359 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000360 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
George Rimar77b77792015-11-25 22:15:01 +0000361 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
362 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
363 };
364 memcpy(Buf, V, sizeof(V));
365 return;
366 }
George Rimar648a2c32015-10-20 08:54:27 +0000367
George Rimar77b77792015-11-25 22:15:01 +0000368 const uint8_t PltData[] = {
369 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
370 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
371 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
372 };
373 memcpy(Buf, PltData, sizeof(PltData));
374 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
375 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
376}
377
378void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
379 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
380 int32_t Index, unsigned RelOff) const {
381 const uint8_t Inst[] = {
382 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
383 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
384 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
385 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000386 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000387 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
388 Buf[1] = Config->Shared ? 0xa3 : 0x25;
389 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
390 write32le(Buf + 7, RelOff);
391 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000392}
393
Rui Ueyama02dfd492015-12-17 01:18:40 +0000394bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000395 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
396 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
397 return SS->Sym.getType() == STT_OBJECT;
398 return false;
399}
400
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000401bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000402 if (S.isTls() && Type == R_386_TLS_GD)
George Rimar2558e122015-12-09 09:55:54 +0000403 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000404 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
George Rimar2558e122015-12-09 09:55:54 +0000405 return !isTlsOptimized(Type, &S);
406 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000407}
408
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000409bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000410 return isGnuIFunc<ELF32LE>(S) ||
411 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000412 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000413}
414
George Rimarbfb7bf72015-12-21 10:00:12 +0000415bool X86TargetInfo::isGotRelative(uint32_t Type) const {
416 // This relocation does not require got entry,
417 // but it is relative to got and needs it to be created.
418 // Here we request for that.
419 return Type == R_386_GOTOFF;
420}
421
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000422void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000423 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000424 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000425 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000426 case R_386_32:
427 add32le(Loc, SA);
428 break;
George Rimarffb67352016-01-19 11:00:48 +0000429 case R_386_GOT32: {
430 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
431 Out<ELF32LE>::Got->getNumEntries() * 4;
432 checkInt<32>(V, Type);
433 add32le(Loc, V);
434 break;
435 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000436 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000437 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000438 break;
George Rimar13934772015-11-25 20:20:31 +0000439 case R_386_GOTPC:
440 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
441 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000442 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000443 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000444 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000445 break;
George Rimar9db204a2015-12-02 09:58:20 +0000446 case R_386_TLS_GD:
447 case R_386_TLS_LDM:
448 case R_386_TLS_TPOFF: {
449 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
450 Out<ELF32LE>::Got->getNumEntries() * 4;
451 checkInt<32>(V, Type);
452 write32le(Loc, V);
453 break;
454 }
George Rimar6f17e092015-12-17 09:32:21 +0000455 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000456 case R_386_TLS_LDO_32:
457 write32le(Loc, SA);
458 break;
George Rimard23970f2015-11-25 20:41:53 +0000459 case R_386_TLS_LE:
460 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
461 break;
462 case R_386_TLS_LE_32:
463 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
464 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000465 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000466 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000467 }
468}
469
George Rimar2558e122015-12-09 09:55:54 +0000470bool X86TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000471 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000472 return false;
473 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
474 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000475 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000476 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
477}
478
George Rimar6f17e092015-12-17 09:32:21 +0000479bool X86TargetInfo::relocNeedsDynRelative(unsigned Type) const {
480 return Config->Shared && Type == R_386_TLS_IE;
481}
482
George Rimar2558e122015-12-09 09:55:54 +0000483unsigned X86TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
484 uint32_t Type, uint64_t P,
485 uint64_t SA,
George Rimar237b2182016-01-22 18:02:28 +0000486 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000487 switch (Type) {
488 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000489 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000490 relocateTlsGdToIe(Loc, BufEnd, P, SA);
491 else
492 relocateTlsGdToLe(Loc, BufEnd, P, SA);
493 // The next relocation should be against __tls_get_addr, so skip it
494 return 1;
495 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000496 case R_386_TLS_IE:
497 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000498 return 0;
499 case R_386_TLS_LDM:
500 relocateTlsLdToLe(Loc, BufEnd, P, SA);
501 // The next relocation should be against __tls_get_addr, so skip it
502 return 1;
503 case R_386_TLS_LDO_32:
504 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
505 return 0;
506 }
507 llvm_unreachable("Unknown TLS optimization");
508}
509
510// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
511// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
512// how GD can be optimized to IE:
513// leal x@tlsgd(, %ebx, 1),
514// call __tls_get_addr@plt
515// Is converted to:
516// movl %gs:0, %eax
517// addl x@gotntpoff(%ebx), %eax
518void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
519 uint64_t SA) const {
520 const uint8_t Inst[] = {
521 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
522 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
523 };
524 memcpy(Loc - 3, Inst, sizeof(Inst));
525 relocateOne(Loc + 5, BufEnd, R_386_32, P,
526 SA - Out<ELF32LE>::Got->getVA() -
527 Out<ELF32LE>::Got->getNumEntries() * 4);
528}
529
530// GD can be optimized to LE:
531// leal x@tlsgd(, %ebx, 1),
532// call __tls_get_addr@plt
533// Can be converted to:
534// movl %gs:0,%eax
535// addl $x@ntpoff,%eax
536// But gold emits subl $foo@tpoff,%eax instead of addl.
537// These instructions are completely equal in behavior.
538// This method generates subl to be consistent with gold.
539void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
540 uint64_t SA) const {
541 const uint8_t Inst[] = {
542 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
543 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
544 };
545 memcpy(Loc - 3, Inst, sizeof(Inst));
546 relocateOne(Loc + 5, BufEnd, R_386_32, P,
547 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
548}
549
550// LD can be optimized to LE:
551// leal foo(%reg),%eax
552// call ___tls_get_addr
553// Is converted to:
554// movl %gs:0,%eax
555// nop
556// leal 0(%esi,1),%esi
557void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
558 uint64_t SA) const {
559 const uint8_t Inst[] = {
560 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
561 0x90, // nop
562 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
563 };
564 memcpy(Loc - 2, Inst, sizeof(Inst));
565}
566
George Rimar6f17e092015-12-17 09:32:21 +0000567// In some conditions, relocations can be optimized to avoid using GOT.
568// This function does that for Initial Exec to Local Exec case.
569// Read "ELF Handling For Thread-Local Storage, 5.1
570// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000571// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000572void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
573 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000574 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000575 // Ulrich's document section 6.2 says that @gotntpoff can
576 // be used with MOVL or ADDL instructions.
577 // @indntpoff is similar to @gotntpoff, but for use in
578 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000579 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000580 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000581 uint8_t Reg = (Loc[-1] >> 3) & 7;
582 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000583 if (Type == R_386_TLS_IE) {
584 // For R_386_TLS_IE relocation we perform the next transformations:
585 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
586 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
587 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
588 // First one is special because when EAX is used the sequence is 5 bytes
589 // long, otherwise it is 6 bytes.
590 if (*Op == 0xa1) {
591 *Op = 0xb8;
592 } else {
593 *Inst = IsMov ? 0xc7 : 0x81;
594 *Op = 0xc0 | ((*Op >> 3) & 7);
595 }
596 } else {
597 // R_386_TLS_GOTIE relocation can be optimized to
598 // R_386_TLS_LE so that it does not use GOT.
599 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
600 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
601 // Note: gold converts to ADDL instead of LEAL.
602 *Inst = IsMov ? 0xc7 : 0x8d;
603 if (IsMov)
604 *Op = 0xc0 | ((*Op >> 3) & 7);
605 else
606 *Op = 0x80 | Reg | (Reg << 3);
607 }
George Rimar2558e122015-12-09 09:55:54 +0000608 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
609}
610
Rafael Espindola7f074422015-09-22 21:35:51 +0000611X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000612 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000613 PCRelReloc = R_X86_64_PC32;
614 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000615 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000616 RelativeReloc = R_X86_64_RELATIVE;
George Rimara07ff662015-12-21 10:12:06 +0000617 IRelativeReloc = R_X86_64_IRELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000618 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000619 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000620 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000621 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000622 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000623 LazyRelocations = true;
624 PltEntrySize = 16;
625 PltZeroEntrySize = 16;
626}
627
Igor Kudrin351b41d2015-11-16 17:44:08 +0000628void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
629 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
630}
631
George Rimar648a2c32015-10-20 08:54:27 +0000632void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
633 // Skip 6 bytes of "jmpq *got(%rip)"
634 write32le(Buf, Plt + 6);
635}
636
637void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
638 uint64_t PltEntryAddr) const {
639 const uint8_t PltData[] = {
640 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
641 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
642 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
643 };
644 memcpy(Buf, PltData, sizeof(PltData));
645 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
646 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000647}
Rafael Espindola01205f72015-09-22 18:19:46 +0000648
George Rimar77b77792015-11-25 22:15:01 +0000649void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
650 uint64_t GotEntryAddr,
651 uint64_t PltEntryAddr, int32_t Index,
652 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000653 const uint8_t Inst[] = {
654 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
655 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
656 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
657 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000658 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000659
George Rimar648a2c32015-10-20 08:54:27 +0000660 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
661 write32le(Buf + 7, Index);
662 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000663}
664
Rui Ueyama02dfd492015-12-17 01:18:40 +0000665bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000666 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
667 Type == R_X86_64_64)
668 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
669 return SS->Sym.getType() == STT_OBJECT;
670 return false;
671}
672
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000673bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000674 if (Type == R_X86_64_TLSGD)
675 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000676 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000677 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000678 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000679}
680
George Rimar6f17e092015-12-17 09:32:21 +0000681bool X86_64TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000682 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000683}
684
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000685bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000686 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000687 return false;
George Rimara07ff662015-12-21 10:12:06 +0000688 if (isGnuIFunc<ELF64LE>(S))
689 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000690
Rafael Espindola01205f72015-09-22 18:19:46 +0000691 switch (Type) {
692 default:
693 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000694 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000695 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000696 case R_X86_64_PC32:
697 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000698 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000699 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000700 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000701 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000702 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
703 // libc.so.
704 //
705 // The general idea on how to handle such cases is to create a PLT entry
706 // and use that as the function value.
707 //
708 // For the static linking part, we just return true and everything else
709 // will use the the PLT entry as the address.
710 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000711 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000712 // still works. We need the help of the dynamic linker for that. We
713 // let it know that we have a direct reference to a so symbol by creating
714 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000715 // dynamic linker resolves the symbol to the value of the symbol we created.
716 // This is true even for got entries, so pointer equality is maintained.
717 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000718 // real function is a dedicated got entry used by the plt. That is
719 // identified by special relocation types (R_X86_64_JUMP_SLOT,
720 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000721 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000722 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000723 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000724 }
725}
Rafael Espindolac4010882015-09-22 20:54:08 +0000726
Rafael Espindolaae244002015-10-05 19:30:12 +0000727bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
728 switch (Type) {
729 default:
730 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000731 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000732 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000733 case R_X86_64_PC8:
734 case R_X86_64_PC16:
735 case R_X86_64_PC32:
736 case R_X86_64_PC64:
737 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000738 return true;
739 }
740}
741
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000742bool X86_64TargetInfo::isSizeReloc(uint32_t Type) const {
743 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000744}
745
George Rimar77d1cb12015-11-24 09:00:06 +0000746bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000747 const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000748 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000749 return false;
George Rimar25411f252015-12-04 11:20:13 +0000750 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
751 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000752 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
753}
754
755// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
756// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
757// how LD can be optimized to LE:
758// leaq bar@tlsld(%rip), %rdi
759// callq __tls_get_addr@PLT
760// leaq bar@dtpoff(%rax), %rcx
761// Is converted to:
762// .word 0x6666
763// .byte 0x66
764// mov %fs:0,%rax
765// leaq bar@tpoff(%rax), %rcx
766void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
767 uint64_t P, uint64_t SA) const {
768 const uint8_t Inst[] = {
769 0x66, 0x66, //.word 0x6666
770 0x66, //.byte 0x66
771 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
772 };
773 memcpy(Loc - 3, Inst, sizeof(Inst));
774}
775
776// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
777// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
778// how GD can be optimized to LE:
779// .byte 0x66
780// leaq x@tlsgd(%rip), %rdi
781// .word 0x6666
782// rex64
783// call __tls_get_addr@plt
784// Is converted to:
785// mov %fs:0x0,%rax
786// lea x@tpoff,%rax
787void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
788 uint64_t P, uint64_t SA) const {
789 const uint8_t Inst[] = {
790 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
791 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
792 };
793 memcpy(Loc - 4, Inst, sizeof(Inst));
794 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000795}
796
George Rimar25411f252015-12-04 11:20:13 +0000797// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
798// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
799// how GD can be optimized to IE:
800// .byte 0x66
801// leaq x@tlsgd(%rip), %rdi
802// .word 0x6666
803// rex64
804// call __tls_get_addr@plt
805// Is converted to:
806// mov %fs:0x0,%rax
807// addq x@tpoff,%rax
808void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
809 uint64_t P, uint64_t SA) const {
810 const uint8_t Inst[] = {
811 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
812 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
813 };
814 memcpy(Loc - 4, Inst, sizeof(Inst));
815 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
816}
817
George Rimar77d1cb12015-11-24 09:00:06 +0000818// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000819// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000820// This function does that. Read "ELF Handling For Thread-Local Storage,
821// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
822// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000823void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
824 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000825 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
826 // used in MOVQ or ADDQ instructions only.
827 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
828 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
829 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
830 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
831 uint8_t *Prefix = Loc - 3;
832 uint8_t *Inst = Loc - 2;
833 uint8_t *RegSlot = Loc - 1;
834 uint8_t Reg = Loc[-1] >> 3;
835 bool IsMov = *Inst == 0x8b;
836 bool RspAdd = !IsMov && Reg == 4;
837 // r12 and rsp registers requires special handling.
838 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
839 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
840 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
841 // The same true for rsp. So we convert to addq for them, saving 1 byte that
842 // we dont have.
843 if (RspAdd)
844 *Inst = 0x81;
845 else
846 *Inst = IsMov ? 0xc7 : 0x8d;
847 if (*Prefix == 0x4c)
848 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
849 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
850 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
851}
852
George Rimar6713cf82015-11-25 21:46:05 +0000853// This function applies a TLS relocation with an optimization as described
854// in the Ulrich's document. As a result of rewriting instructions at the
855// relocation target, relocations immediately follow the TLS relocation (which
856// would be applied to rewritten instructions) may have to be skipped.
857// This function returns a number of relocations that need to be skipped.
858unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
859 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000860 uint64_t SA,
George Rimar237b2182016-01-22 18:02:28 +0000861 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000862 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000863 case R_X86_64_DTPOFF32:
864 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
865 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000866 case R_X86_64_GOTTPOFF:
867 relocateTlsIeToLe(Loc, BufEnd, P, SA);
868 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000869 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000870 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000871 relocateTlsGdToIe(Loc, BufEnd, P, SA);
872 else
873 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000874 // The next relocation should be against __tls_get_addr, so skip it
875 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000876 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000877 case R_X86_64_TLSLD:
878 relocateTlsLdToLe(Loc, BufEnd, P, SA);
879 // The next relocation should be against __tls_get_addr, so skip it
880 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000881 }
882 llvm_unreachable("Unknown TLS optimization");
883}
884
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000885void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000886 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000887 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000888 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000889 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000890 checkUInt<32>(SA, Type);
891 write32le(Loc, SA);
892 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000893 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000894 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000895 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000896 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000897 case R_X86_64_64:
898 write64le(Loc, SA);
899 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000900 case R_X86_64_DTPOFF32:
901 write32le(Loc, SA);
902 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000903 case R_X86_64_DTPOFF64:
904 write64le(Loc, SA);
905 break;
906 case R_X86_64_GOTPCREL:
907 case R_X86_64_PC32:
908 case R_X86_64_PLT32:
909 case R_X86_64_TLSGD:
910 case R_X86_64_TLSLD:
911 write32le(Loc, SA - P);
912 break;
George Rimar48651482015-12-11 08:59:37 +0000913 case R_X86_64_SIZE32:
914 write32le(Loc, ZA);
915 break;
916 case R_X86_64_SIZE64:
917 write64le(Loc, ZA);
918 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000919 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000920 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000921 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000922 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000923 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000924 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000925 case R_X86_64_TPOFF64:
926 write32le(Loc, SA - P);
927 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000928 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000929 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000930 }
931}
932
Hal Finkel3c8cc672015-10-12 20:56:18 +0000933// Relocation masks following the #lo(value), #hi(value), #ha(value),
934// #higher(value), #highera(value), #highest(value), and #highesta(value)
935// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
936// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000937static uint16_t applyPPCLo(uint64_t V) { return V; }
938static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
939static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
940static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
941static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000942static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000943static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
944
Davide Italiano8c3444362016-01-11 19:45:33 +0000945PPCTargetInfo::PPCTargetInfo() {}
946void PPCTargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
947void PPCTargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
948 uint64_t PltEntryAddr) const {}
949void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
950 uint64_t GotEntryAddr,
951 uint64_t PltEntryAddr, int32_t Index,
952 unsigned RelOff) const {}
953bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
954 return false;
955}
956bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
957 return false;
958}
959bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
960
961void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
962 uint64_t P, uint64_t SA, uint64_t ZA,
963 uint8_t *PairedLoc) const {
964 switch (Type) {
965 case R_PPC_ADDR16_HA:
966 write16be(Loc, applyPPCHa(SA));
967 break;
968 case R_PPC_ADDR16_LO:
969 write16be(Loc, applyPPCLo(SA));
970 break;
971 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000972 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000973 }
974}
975
Rafael Espindolac4010882015-09-22 20:54:08 +0000976PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000977 PCRelReloc = R_PPC64_REL24;
978 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000979 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000980 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000981
982 // We need 64K pages (at least under glibc/Linux, the loader won't
983 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000984 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000985
986 // The PPC64 ELF ABI v1 spec, says:
987 //
988 // It is normally desirable to put segments with different characteristics
989 // in separate 256 Mbyte portions of the address space, to give the
990 // operating system full paging flexibility in the 64-bit address space.
991 //
992 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
993 // use 0x10000000 as the starting address.
994 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000995}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000996
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000997uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000998 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
999 // order. The TOC starts where the first of these sections starts.
1000
1001 // FIXME: This obviously does not do the right thing when there is no .got
1002 // section, but there is a .toc or .tocbss section.
1003 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1004 if (!TocVA)
1005 TocVA = Out<ELF64BE>::Plt->getVA();
1006
1007 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1008 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1009 // code (crt1.o) assumes that you can get from the TOC base to the
1010 // start of the .toc section with only a single (signed) 16-bit relocation.
1011 return TocVA + 0x8000;
1012}
1013
George Rimar648a2c32015-10-20 08:54:27 +00001014void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1015void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1016 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +00001017void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1018 uint64_t GotEntryAddr,
1019 uint64_t PltEntryAddr, int32_t Index,
1020 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001021 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1022
1023 // FIXME: What we should do, in theory, is get the offset of the function
1024 // descriptor in the .opd section, and use that as the offset from %r2 (the
1025 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1026 // be a pointer to the function descriptor in the .opd section. Using
1027 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1028
Hal Finkelfa92f682015-10-13 21:47:34 +00001029 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1031 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1032 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1033 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1034 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1035 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1036 write32be(Buf + 28, 0x4e800420); // bctr
1037}
1038
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001039bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001040 if (relocNeedsPlt(Type, S))
1041 return true;
1042
1043 switch (Type) {
1044 default: return false;
1045 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001046 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001047 case R_PPC64_GOT16_HA:
1048 case R_PPC64_GOT16_HI:
1049 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001050 case R_PPC64_GOT16_LO_DS:
1051 return true;
1052 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001053}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001054
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001055bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001056 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001057 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001058}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059
Hal Finkelbe0823d2015-10-12 20:58:52 +00001060bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1061 switch (Type) {
1062 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001063 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001064 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001065 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001066 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001067 }
1068}
1069
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001070void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001071 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001072 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 uint64_t TB = getPPC64TocBase();
1074
Hal Finkel3c8cc672015-10-12 20:56:18 +00001075 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1076 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001077 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001078 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1079 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001080 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1081 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001082 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1083 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 default: break;
1085 }
1086
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001088 case R_PPC64_ADDR14: {
1089 checkAlignment<4>(SA, Type);
1090 // Preserve the AA/LK bits in the branch instruction
1091 uint8_t AALK = Loc[3];
1092 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1093 break;
1094 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001095 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001096 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001097 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001098 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001099 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001100 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001101 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001102 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001103 case R_PPC64_ADDR16_HA:
1104 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001105 break;
1106 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001107 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001108 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001110 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 break;
1112 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001113 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 break;
1115 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 break;
1118 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001121 case R_PPC64_ADDR16_LO:
1122 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001124 case R_PPC64_ADDR16_LO_DS:
1125 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001128 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001131 case R_PPC64_ADDR64:
1132 write64be(Loc, SA);
1133 break;
1134 case R_PPC64_REL16_HA:
1135 write16be(Loc, applyPPCHa(SA - P));
1136 break;
1137 case R_PPC64_REL16_HI:
1138 write16be(Loc, applyPPCHi(SA - P));
1139 break;
1140 case R_PPC64_REL16_LO:
1141 write16be(Loc, applyPPCLo(SA - P));
1142 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001143 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001144 // If we have an undefined weak symbol, we might get here with a symbol
1145 // address of zero. That could overflow, but the code must be unreachable,
1146 // so don't bother doing anything at all.
1147 if (!SA)
1148 break;
1149
Hal Finkeldaedc122015-10-12 23:16:53 +00001150 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1151 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001152 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001153
1154 if (!InPlt && Out<ELF64BE>::Opd) {
1155 // If this is a local call, and we currently have the address of a
1156 // function-descriptor, get the underlying code address instead.
1157 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1158 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001159 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001160
1161 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001162 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001163 }
1164
Hal Finkel3c8cc672015-10-12 20:56:18 +00001165 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001166 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001167 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001168
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001169 uint32_t Nop = 0x60000000;
1170 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1171 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001172 break;
1173 }
1174 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001175 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001176 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001177 break;
1178 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001179 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001180 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001181 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001182 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001183 break;
1184 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001185 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001186 }
1187}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001188
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001189AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +00001190 CopyReloc = R_AARCH64_COPY;
George Rimara4804352016-01-11 14:15:17 +00001191 IRelativeReloc = R_AARCH64_IRELATIVE;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001192 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001193 PltReloc = R_AARCH64_JUMP_SLOT;
George Rimar3d737e42016-01-13 13:04:46 +00001194 TlsGotReloc = R_AARCH64_TLS_TPREL64;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001195 LazyRelocations = true;
1196 PltEntrySize = 16;
1197 PltZeroEntrySize = 32;
1198}
George Rimar648a2c32015-10-20 08:54:27 +00001199
Igor Kudrincfe47f52015-12-05 06:20:24 +00001200unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
1201 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1202 return Type;
1203 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001204 fatal("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001205 "recompile with -fPIC.");
1206}
1207
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001208void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1209 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1210}
1211
George Rimar648a2c32015-10-20 08:54:27 +00001212void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001213 uint64_t PltEntryAddr) const {
1214 const uint8_t PltData[] = {
1215 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1216 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1217 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1218 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1219 0x20, 0x02, 0x1f, 0xd6, // br x17
1220 0x1f, 0x20, 0x03, 0xd5, // nop
1221 0x1f, 0x20, 0x03, 0xd5, // nop
1222 0x1f, 0x20, 0x03, 0xd5 // nop
1223 };
1224 memcpy(Buf, PltData, sizeof(PltData));
1225
1226 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
1227 GotEntryAddr + 16);
1228 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
1229 GotEntryAddr + 16);
1230 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
1231 GotEntryAddr + 16);
1232}
1233
George Rimar77b77792015-11-25 22:15:01 +00001234void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1235 uint64_t GotEntryAddr,
1236 uint64_t PltEntryAddr, int32_t Index,
1237 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001238 const uint8_t Inst[] = {
1239 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1240 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1241 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1242 0x20, 0x02, 0x1f, 0xd6 // br x17
1243 };
1244 memcpy(Buf, Inst, sizeof(Inst));
1245
1246 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1247 GotEntryAddr);
1248 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1249 GotEntryAddr);
1250 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1251 GotEntryAddr);
1252}
1253
George Rimar3d737e42016-01-13 13:04:46 +00001254unsigned AArch64TargetInfo::getTlsGotReloc(unsigned Type) const {
1255 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1256 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1257 return Type;
1258 return TlsGotReloc;
1259}
1260
1261bool AArch64TargetInfo::isTlsDynReloc(unsigned Type,
1262 const SymbolBody &S) const {
1263 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1264 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1265}
1266
Rui Ueyama02dfd492015-12-17 01:18:40 +00001267bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001268 if (Config->Shared)
1269 return false;
1270 switch (Type) {
1271 default:
1272 return false;
1273 case R_AARCH64_ABS16:
1274 case R_AARCH64_ABS32:
1275 case R_AARCH64_ABS64:
1276 case R_AARCH64_ADD_ABS_LO12_NC:
1277 case R_AARCH64_ADR_PREL_LO21:
1278 case R_AARCH64_ADR_PREL_PG_HI21:
1279 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001280 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001281 case R_AARCH64_LDST32_ABS_LO12_NC:
1282 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001283 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001284 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1285 return SS->Sym.getType() == STT_OBJECT;
1286 return false;
1287 }
1288}
1289
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001290bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
1291 const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001292 switch (Type) {
1293 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1294 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1295 case R_AARCH64_ADR_GOT_PAGE:
1296 case R_AARCH64_LD64_GOT_LO12_NC:
1297 return true;
1298 default:
1299 return relocNeedsPlt(Type, S);
1300 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001301}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001302
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001303bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
1304 const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001305 if (isGnuIFunc<ELF64LE>(S))
1306 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001307 switch (Type) {
1308 default:
1309 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001310 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001311 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001312 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001313 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001314 return canBePreempted(&S, true);
1315 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001316}
Davide Italiano1d750a62015-09-27 08:45:38 +00001317
Davide Italianoef4be6b2015-10-06 19:01:32 +00001318static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001319 uint32_t ImmLo = (Imm & 0x3) << 29;
1320 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1321 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001322 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001323}
1324
Davide Italiano318ca222015-10-02 22:13:51 +00001325// Page(Expr) is the page address of the expression Expr, defined
1326// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001327// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001328static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001329 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001330}
1331
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001332void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001333 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001334 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001335 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001336 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001337 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001338 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001339 break;
1340 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001341 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001342 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001343 break;
1344 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001345 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001346 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001347 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001348 // This relocation stores 12 bits and there's no instruction
1349 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001350 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1351 // bits in Loc are zero.
1352 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001353 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001354 case R_AARCH64_ADR_GOT_PAGE: {
1355 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1356 checkInt<33>(X, Type);
1357 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1358 break;
1359 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001360 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001361 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001362 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001363 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001364 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001365 }
George Rimar3d737e42016-01-13 13:04:46 +00001366 case R_AARCH64_ADR_PREL_PG_HI21:
1367 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001368 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001369 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001370 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001371 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001372 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001373 case R_AARCH64_CALL26:
1374 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001375 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001376 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001377 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1378 break;
1379 }
George Rimar4102bfb2016-01-11 14:22:00 +00001380 case R_AARCH64_CONDBR19: {
1381 uint64_t X = SA - P;
1382 checkInt<21>(X, Type);
1383 or32le(Loc, (X & 0x1FFFFC) << 3);
1384 break;
1385 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001386 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001387 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001388 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001389 or32le(Loc, (SA & 0xFF8) << 7);
1390 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001391 case R_AARCH64_LDST128_ABS_LO12_NC:
1392 or32le(Loc, (SA & 0x0FF8) << 6);
1393 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001394 case R_AARCH64_LDST16_ABS_LO12_NC:
1395 or32le(Loc, (SA & 0x0FFC) << 9);
1396 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001397 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001398 or32le(Loc, (SA & 0xFFF) << 10);
1399 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001400 case R_AARCH64_LDST32_ABS_LO12_NC:
1401 or32le(Loc, (SA & 0xFFC) << 8);
1402 break;
1403 case R_AARCH64_LDST64_ABS_LO12_NC:
1404 or32le(Loc, (SA & 0xFF8) << 7);
1405 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001406 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001407 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001408 write16le(Loc, SA - P);
1409 break;
1410 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001411 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001412 write32le(Loc, SA - P);
1413 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001414 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001415 write64le(Loc, SA - P);
1416 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001417 case R_AARCH64_TSTBR14: {
1418 uint64_t X = SA - P;
1419 checkInt<16>(X, Type);
1420 or32le(Loc, (X & 0xFFFC) << 3);
1421 break;
1422 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001423 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001424 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001425 }
1426}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001427
Tom Stellard80efb162016-01-07 03:59:08 +00001428AMDGPUTargetInfo::AMDGPUTargetInfo() {}
1429
1430void AMDGPUTargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1431 llvm_unreachable("not implemented");
1432}
1433
1434void AMDGPUTargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1435 uint64_t PltEntryAddr) const {
1436 llvm_unreachable("not implemented");
1437}
1438
1439void AMDGPUTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1440 uint64_t GotEntryAddr,
1441 uint64_t PltEntryAddr, int32_t Index,
1442 unsigned RelOff) const {
1443 llvm_unreachable("not implemented");
1444}
1445
1446bool AMDGPUTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
1447 return false;
1448}
1449
1450bool AMDGPUTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
1451 return false;
1452}
1453
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001454// Implementing relocations for AMDGPU is low priority since most
1455// programs don't use relocations now. Thus, this function is not
1456// actually called (relocateOne is called for each relocation).
1457// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001458void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1459 uint64_t P, uint64_t SA, uint64_t ZA,
1460 uint8_t *PairedLoc) const {
1461 llvm_unreachable("not implemented");
1462}
1463
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001464template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001465 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001466 GotHeaderEntriesNum = 2;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001467 RelativeReloc = R_MIPS_REL32;
1468}
1469
1470template <class ELFT>
1471unsigned MipsTargetInfo<ELFT>::getDynReloc(unsigned Type) const {
1472 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1473 return R_MIPS_REL32;
1474 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001475 fatal("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001476 "recompile with -fPIC.");
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001477}
1478
1479template <class ELFT>
1480void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001481 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001482 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1483 // Module pointer
1484 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001485}
1486
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001487template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001488void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1489template <class ELFT>
1490void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1491 uint64_t PltEntryAddr) const {}
1492template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001493void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1494 uint64_t GotEntryAddr,
1495 uint64_t PltEntryAddr, int32_t Index,
1496 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001497
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001498template <class ELFT>
1499bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1500 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001501 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001502}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001503
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001504template <class ELFT>
1505bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1506 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001507 return false;
1508}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001509
Simon Atanasyan35031192015-12-15 06:06:34 +00001510static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001511
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001512template <endianness E, uint8_t BSIZE>
1513static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1514 uint64_t SA) {
1515 uint32_t Mask = ~(0xffffffff << BSIZE);
1516 uint32_t Instr = read32<E>(Loc);
1517 int64_t A = SignExtend64<BSIZE + 2>((Instr & Mask) << 2);
1518 checkAlignment<4>(SA + A, Type);
1519 int64_t V = SA + A - P;
1520 checkInt<BSIZE + 2>(V, Type);
1521 write32<E>(Loc, (Instr & ~Mask) | ((V >> 2) & Mask));
1522}
1523
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001524template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001525void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001526 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001527 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001528 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001529 switch (Type) {
1530 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001531 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001532 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001533 case R_MIPS_CALL16:
1534 case R_MIPS_GOT16: {
1535 int64_t V = SA - getMipsGpAddr<ELFT>();
1536 if (Type == R_MIPS_GOT16)
1537 checkInt<16>(V, Type);
1538 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1539 break;
1540 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001541 case R_MIPS_GPREL16: {
1542 uint32_t Instr = read32<E>(Loc);
1543 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1544 checkInt<16>(V, Type);
1545 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1546 break;
1547 }
1548 case R_MIPS_GPREL32:
1549 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1550 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001551 case R_MIPS_HI16: {
1552 uint32_t Instr = read32<E>(Loc);
1553 if (PairedLoc) {
1554 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001555 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001556 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001557 } else {
1558 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001559 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001560 }
1561 break;
1562 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001563 case R_MIPS_JALR:
1564 // Ignore this optimization relocation for now
1565 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001566 case R_MIPS_LO16: {
1567 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001568 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001569 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1570 break;
1571 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001572 case R_MIPS_PC16:
1573 applyMipsPcReloc<E, 16>(Loc, Type, P, SA);
1574 break;
1575 case R_MIPS_PC19_S2:
1576 applyMipsPcReloc<E, 19>(Loc, Type, P, SA);
1577 break;
1578 case R_MIPS_PC21_S2:
1579 applyMipsPcReloc<E, 21>(Loc, Type, P, SA);
1580 break;
1581 case R_MIPS_PC26_S2:
1582 applyMipsPcReloc<E, 26>(Loc, Type, P, SA);
1583 break;
1584 case R_MIPS_PCHI16: {
1585 uint32_t Instr = read32<E>(Loc);
1586 if (PairedLoc) {
1587 uint64_t AHL = ((Instr & 0xffff) << 16) +
1588 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1589 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1590 } else {
1591 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1592 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1593 }
1594 break;
1595 }
1596 case R_MIPS_PCLO16: {
1597 uint32_t Instr = read32<E>(Loc);
1598 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1599 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1600 break;
1601 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001602 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001603 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001604 }
1605}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001606
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001607template <class ELFT>
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001608bool MipsTargetInfo<ELFT>::isHintReloc(uint32_t Type) const {
1609 return Type == R_MIPS_JALR;
1610}
1611
1612template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001613bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1614 switch (Type) {
1615 default:
1616 return false;
1617 case R_MIPS_PC16:
1618 case R_MIPS_PC19_S2:
1619 case R_MIPS_PC21_S2:
1620 case R_MIPS_PC26_S2:
1621 case R_MIPS_PCHI16:
1622 case R_MIPS_PCLO16:
1623 return true;
1624 }
1625}
1626
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001627// _gp is a MIPS-specific ABI-defined symbol which points to
1628// a location that is relative to GOT. This function returns
1629// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001630template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001631 unsigned GPOffset = 0x7ff0;
1632 if (uint64_t V = Out<ELFT>::Got->getVA())
1633 return V + GPOffset;
1634 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001635}
1636
Simon Atanasyan56ab5f02016-01-21 05:33:23 +00001637bool needsMipsLocalGot(uint32_t Type, SymbolBody *Body) {
1638 // The R_MIPS_GOT16 relocation requires creation of entry in the local part
1639 // of GOT if its target is a local symbol or non-local symbol with 'local'
1640 // visibility.
1641 if (Type != R_MIPS_GOT16)
1642 return false;
1643 if (!Body)
1644 return true;
1645 uint8_t V = Body->getVisibility();
1646 if (V != STV_DEFAULT && V != STV_PROTECTED)
1647 return true;
1648 return !Config->Shared;
1649}
1650
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001651template uint32_t getMipsGpAddr<ELF32LE>();
1652template uint32_t getMipsGpAddr<ELF32BE>();
1653template uint64_t getMipsGpAddr<ELF64LE>();
1654template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001655}
1656}