blob: cf0f61c156c857e3c9f9a12c3dbd8e4bef76edb4 [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);
49 error("Relocation " + S + " out of range");
50}
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);
56 error("Relocation " + S + " out of range");
57}
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);
63 error("Relocation " + S + " out of range");
64}
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);
70 error("Improper alignment for relocation " + S);
71}
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,
108 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,
144 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
157class PPC64TargetInfo final : public TargetInfo {
158public:
159 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +0000160 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;
George Rimar77b77792015-11-25 22:15:01 +0000163 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
164 uint64_t PltEntryAddr, int32_t Index,
165 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000166 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
167 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000168 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000169 uint64_t SA, uint64_t ZA = 0,
170 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000171 bool isRelRelative(uint32_t Type) const override;
172};
173
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000174class AArch64TargetInfo final : public TargetInfo {
175public:
176 AArch64TargetInfo();
Igor Kudrincfe47f52015-12-05 06:20:24 +0000177 unsigned getDynReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000178 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
179 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
180 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000181 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
182 uint64_t PltEntryAddr, int32_t Index,
183 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000184 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000185 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
186 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000187 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000188 uint64_t SA, uint64_t ZA = 0,
189 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000190};
191
Tom Stellard80efb162016-01-07 03:59:08 +0000192class AMDGPUTargetInfo final : public TargetInfo {
193public:
194 AMDGPUTargetInfo();
195 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;
198 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
199 uint64_t PltEntryAddr, int32_t Index,
200 unsigned RelOff) const override;
201 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
202 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
203 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
204 uint64_t SA, uint64_t ZA = 0,
205 uint8_t *PairedLoc = nullptr) const override;
206};
207
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000208template <class ELFT> class MipsTargetInfo final : public TargetInfo {
209public:
210 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000211 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000212 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
213 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
214 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000215 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
216 uint64_t PltEntryAddr, int32_t Index,
217 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000218 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
219 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000220 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000221 uint64_t SA, uint64_t ZA = 0,
222 uint8_t *PairedLoc = nullptr) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000223 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000224};
225} // anonymous namespace
226
Rui Ueyama91004392015-10-13 16:08:15 +0000227TargetInfo *createTarget() {
228 switch (Config->EMachine) {
229 case EM_386:
230 return new X86TargetInfo();
231 case EM_AARCH64:
232 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000233 case EM_AMDGPU:
234 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000235 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000236 switch (Config->EKind) {
237 case ELF32LEKind:
238 return new MipsTargetInfo<ELF32LE>();
239 case ELF32BEKind:
240 return new MipsTargetInfo<ELF32BE>();
241 default:
242 error("Unsupported MIPS target");
243 }
Rui Ueyama91004392015-10-13 16:08:15 +0000244 case EM_PPC64:
245 return new PPC64TargetInfo();
246 case EM_X86_64:
247 return new X86_64TargetInfo();
248 }
249 error("Unknown target machine");
250}
251
Rafael Espindola01205f72015-09-22 18:19:46 +0000252TargetInfo::~TargetInfo() {}
253
George Rimar6713cf82015-11-25 21:46:05 +0000254bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000255 return false;
256}
257
Igor Kudrinf6f45472015-11-10 08:39:27 +0000258uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
259
Rui Ueyama02dfd492015-12-17 01:18:40 +0000260bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000261 return false;
262}
263
George Rimarbfb7bf72015-12-21 10:00:12 +0000264bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
265
Rafael Espindolaae244002015-10-05 19:30:12 +0000266bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
267
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000268bool TargetInfo::isSizeReloc(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000269
George Rimar6713cf82015-11-25 21:46:05 +0000270unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000271 uint32_t Type, uint64_t P, uint64_t SA,
272 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000273 return 0;
274}
George Rimar77d1cb12015-11-24 09:00:06 +0000275
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000276void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
277
Igor Kudrin351b41d2015-11-16 17:44:08 +0000278void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
279
Rafael Espindola7f074422015-09-22 21:35:51 +0000280X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000281 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000282 PCRelReloc = R_386_PC32;
283 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000284 PltReloc = R_386_JUMP_SLOT;
George Rimara07ff662015-12-21 10:12:06 +0000285 IRelativeReloc = R_386_IRELATIVE;
George Rimar6f17e092015-12-17 09:32:21 +0000286 RelativeReloc = R_386_RELATIVE;
George Rimar9db204a2015-12-02 09:58:20 +0000287 TlsGotReloc = R_386_TLS_TPOFF;
288 TlsGlobalDynamicReloc = R_386_TLS_GD;
289 TlsLocalDynamicReloc = R_386_TLS_LDM;
290 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
291 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000292 LazyRelocations = true;
293 PltEntrySize = 16;
294 PltZeroEntrySize = 16;
295}
296
297void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
298 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
299}
300
301void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
302 // Skip 6 bytes of "pushl (GOT+4)"
303 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000304}
Rafael Espindola01205f72015-09-22 18:19:46 +0000305
George Rimard23970f2015-11-25 20:41:53 +0000306unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
307 if (Type == R_386_TLS_LE)
308 return R_386_TLS_TPOFF;
309 if (Type == R_386_TLS_LE_32)
310 return R_386_TLS_TPOFF32;
311 return Type;
312}
313
George Rimar6f17e092015-12-17 09:32:21 +0000314unsigned X86TargetInfo::getTlsGotReloc(unsigned Type) const {
315 if (Type == R_386_TLS_IE)
316 return Type;
317 return TlsGotReloc;
318}
319
320bool X86TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000321 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
322 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000323 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000324 if (Type == R_386_TLS_IE)
325 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000326 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000327}
328
George Rimar648a2c32015-10-20 08:54:27 +0000329void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000330 uint64_t PltEntryAddr) const {
331 // Executable files and shared object files have
332 // separate procedure linkage tables.
333 if (Config->Shared) {
334 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000335 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
George Rimar77b77792015-11-25 22:15:01 +0000336 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
337 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
338 };
339 memcpy(Buf, V, sizeof(V));
340 return;
341 }
George Rimar648a2c32015-10-20 08:54:27 +0000342
George Rimar77b77792015-11-25 22:15:01 +0000343 const uint8_t PltData[] = {
344 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
345 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
346 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
347 };
348 memcpy(Buf, PltData, sizeof(PltData));
349 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
350 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
351}
352
353void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
354 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
355 int32_t Index, unsigned RelOff) const {
356 const uint8_t Inst[] = {
357 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
358 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
359 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
360 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000361 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000362 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
363 Buf[1] = Config->Shared ? 0xa3 : 0x25;
364 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
365 write32le(Buf + 7, RelOff);
366 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000367}
368
Rui Ueyama02dfd492015-12-17 01:18:40 +0000369bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000370 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
371 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
372 return SS->Sym.getType() == STT_OBJECT;
373 return false;
374}
375
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000376bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000377 if (S.isTls() && Type == R_386_TLS_GD)
George Rimar2558e122015-12-09 09:55:54 +0000378 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000379 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
George Rimar2558e122015-12-09 09:55:54 +0000380 return !isTlsOptimized(Type, &S);
381 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000382}
383
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000384bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000385 return isGnuIFunc<ELF32LE>(S) ||
386 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000387 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000388}
389
George Rimarbfb7bf72015-12-21 10:00:12 +0000390bool X86TargetInfo::isGotRelative(uint32_t Type) const {
391 // This relocation does not require got entry,
392 // but it is relative to got and needs it to be created.
393 // Here we request for that.
394 return Type == R_386_GOTOFF;
395}
396
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000397void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000398 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000399 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000400 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000401 case R_386_32:
402 add32le(Loc, SA);
403 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000404 case R_386_GOT32:
George Rimarbfb7bf72015-12-21 10:00:12 +0000405 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000406 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000407 break;
George Rimar13934772015-11-25 20:20:31 +0000408 case R_386_GOTPC:
409 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
410 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000411 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000412 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000413 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000414 break;
George Rimar9db204a2015-12-02 09:58:20 +0000415 case R_386_TLS_GD:
416 case R_386_TLS_LDM:
417 case R_386_TLS_TPOFF: {
418 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
419 Out<ELF32LE>::Got->getNumEntries() * 4;
420 checkInt<32>(V, Type);
421 write32le(Loc, V);
422 break;
423 }
George Rimar6f17e092015-12-17 09:32:21 +0000424 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000425 case R_386_TLS_LDO_32:
426 write32le(Loc, SA);
427 break;
George Rimard23970f2015-11-25 20:41:53 +0000428 case R_386_TLS_LE:
429 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
430 break;
431 case R_386_TLS_LE_32:
432 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
433 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000434 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000435 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000436 }
437}
438
George Rimar2558e122015-12-09 09:55:54 +0000439bool X86TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000440 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000441 return false;
442 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
443 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000444 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000445 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
446}
447
George Rimar6f17e092015-12-17 09:32:21 +0000448bool X86TargetInfo::relocNeedsDynRelative(unsigned Type) const {
449 return Config->Shared && Type == R_386_TLS_IE;
450}
451
George Rimar2558e122015-12-09 09:55:54 +0000452unsigned X86TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
453 uint32_t Type, uint64_t P,
454 uint64_t SA,
455 const SymbolBody &S) const {
456 switch (Type) {
457 case R_386_TLS_GD:
458 if (canBePreempted(&S, true))
459 relocateTlsGdToIe(Loc, BufEnd, P, SA);
460 else
461 relocateTlsGdToLe(Loc, BufEnd, P, SA);
462 // The next relocation should be against __tls_get_addr, so skip it
463 return 1;
464 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000465 case R_386_TLS_IE:
466 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000467 return 0;
468 case R_386_TLS_LDM:
469 relocateTlsLdToLe(Loc, BufEnd, P, SA);
470 // The next relocation should be against __tls_get_addr, so skip it
471 return 1;
472 case R_386_TLS_LDO_32:
473 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
474 return 0;
475 }
476 llvm_unreachable("Unknown TLS optimization");
477}
478
479// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
480// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
481// how GD can be optimized to IE:
482// leal x@tlsgd(, %ebx, 1),
483// call __tls_get_addr@plt
484// Is converted to:
485// movl %gs:0, %eax
486// addl x@gotntpoff(%ebx), %eax
487void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
488 uint64_t SA) const {
489 const uint8_t Inst[] = {
490 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
491 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
492 };
493 memcpy(Loc - 3, Inst, sizeof(Inst));
494 relocateOne(Loc + 5, BufEnd, R_386_32, P,
495 SA - Out<ELF32LE>::Got->getVA() -
496 Out<ELF32LE>::Got->getNumEntries() * 4);
497}
498
499// GD can be optimized to LE:
500// leal x@tlsgd(, %ebx, 1),
501// call __tls_get_addr@plt
502// Can be converted to:
503// movl %gs:0,%eax
504// addl $x@ntpoff,%eax
505// But gold emits subl $foo@tpoff,%eax instead of addl.
506// These instructions are completely equal in behavior.
507// This method generates subl to be consistent with gold.
508void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
509 uint64_t SA) const {
510 const uint8_t Inst[] = {
511 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
512 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
513 };
514 memcpy(Loc - 3, Inst, sizeof(Inst));
515 relocateOne(Loc + 5, BufEnd, R_386_32, P,
516 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
517}
518
519// LD can be optimized to LE:
520// leal foo(%reg),%eax
521// call ___tls_get_addr
522// Is converted to:
523// movl %gs:0,%eax
524// nop
525// leal 0(%esi,1),%esi
526void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
527 uint64_t SA) const {
528 const uint8_t Inst[] = {
529 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
530 0x90, // nop
531 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
532 };
533 memcpy(Loc - 2, Inst, sizeof(Inst));
534}
535
George Rimar6f17e092015-12-17 09:32:21 +0000536// In some conditions, relocations can be optimized to avoid using GOT.
537// This function does that for Initial Exec to Local Exec case.
538// Read "ELF Handling For Thread-Local Storage, 5.1
539// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000540// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000541void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
542 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000543 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000544 // Ulrich's document section 6.2 says that @gotntpoff can
545 // be used with MOVL or ADDL instructions.
546 // @indntpoff is similar to @gotntpoff, but for use in
547 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000548 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000549 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000550 uint8_t Reg = (Loc[-1] >> 3) & 7;
551 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000552 if (Type == R_386_TLS_IE) {
553 // For R_386_TLS_IE relocation we perform the next transformations:
554 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
555 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
556 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
557 // First one is special because when EAX is used the sequence is 5 bytes
558 // long, otherwise it is 6 bytes.
559 if (*Op == 0xa1) {
560 *Op = 0xb8;
561 } else {
562 *Inst = IsMov ? 0xc7 : 0x81;
563 *Op = 0xc0 | ((*Op >> 3) & 7);
564 }
565 } else {
566 // R_386_TLS_GOTIE relocation can be optimized to
567 // R_386_TLS_LE so that it does not use GOT.
568 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
569 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
570 // Note: gold converts to ADDL instead of LEAL.
571 *Inst = IsMov ? 0xc7 : 0x8d;
572 if (IsMov)
573 *Op = 0xc0 | ((*Op >> 3) & 7);
574 else
575 *Op = 0x80 | Reg | (Reg << 3);
576 }
George Rimar2558e122015-12-09 09:55:54 +0000577 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
578}
579
Rafael Espindola7f074422015-09-22 21:35:51 +0000580X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000581 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000582 PCRelReloc = R_X86_64_PC32;
583 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000584 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000585 RelativeReloc = R_X86_64_RELATIVE;
George Rimara07ff662015-12-21 10:12:06 +0000586 IRelativeReloc = R_X86_64_IRELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000587 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000588 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000589 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000590 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000591 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000592 LazyRelocations = true;
593 PltEntrySize = 16;
594 PltZeroEntrySize = 16;
595}
596
Igor Kudrin351b41d2015-11-16 17:44:08 +0000597void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
598 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
599}
600
George Rimar648a2c32015-10-20 08:54:27 +0000601void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
602 // Skip 6 bytes of "jmpq *got(%rip)"
603 write32le(Buf, Plt + 6);
604}
605
606void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
607 uint64_t PltEntryAddr) const {
608 const uint8_t PltData[] = {
609 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
610 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
611 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
612 };
613 memcpy(Buf, PltData, sizeof(PltData));
614 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
615 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000616}
Rafael Espindola01205f72015-09-22 18:19:46 +0000617
George Rimar77b77792015-11-25 22:15:01 +0000618void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
619 uint64_t GotEntryAddr,
620 uint64_t PltEntryAddr, int32_t Index,
621 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000622 const uint8_t Inst[] = {
623 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
624 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
625 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
626 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000627 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000628
George Rimar648a2c32015-10-20 08:54:27 +0000629 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
630 write32le(Buf + 7, Index);
631 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000632}
633
Rui Ueyama02dfd492015-12-17 01:18:40 +0000634bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000635 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
636 Type == R_X86_64_64)
637 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
638 return SS->Sym.getType() == STT_OBJECT;
639 return false;
640}
641
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000642bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000643 if (Type == R_X86_64_TLSGD)
644 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000645 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000646 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000647 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000648}
649
George Rimar6f17e092015-12-17 09:32:21 +0000650bool X86_64TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000651 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000652}
653
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000654bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000655 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000656 return false;
George Rimara07ff662015-12-21 10:12:06 +0000657 if (isGnuIFunc<ELF64LE>(S))
658 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000659
Rafael Espindola01205f72015-09-22 18:19:46 +0000660 switch (Type) {
661 default:
662 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000663 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000664 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000665 case R_X86_64_PC32:
666 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000667 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000668 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000669 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000670 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000671 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
672 // libc.so.
673 //
674 // The general idea on how to handle such cases is to create a PLT entry
675 // and use that as the function value.
676 //
677 // For the static linking part, we just return true and everything else
678 // will use the the PLT entry as the address.
679 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000680 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000681 // still works. We need the help of the dynamic linker for that. We
682 // let it know that we have a direct reference to a so symbol by creating
683 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000684 // dynamic linker resolves the symbol to the value of the symbol we created.
685 // This is true even for got entries, so pointer equality is maintained.
686 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000687 // real function is a dedicated got entry used by the plt. That is
688 // identified by special relocation types (R_X86_64_JUMP_SLOT,
689 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000690 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000691 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000692 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000693 }
694}
Rafael Espindolac4010882015-09-22 20:54:08 +0000695
Rafael Espindolaae244002015-10-05 19:30:12 +0000696bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
697 switch (Type) {
698 default:
699 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000700 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000701 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000702 case R_X86_64_PC8:
703 case R_X86_64_PC16:
704 case R_X86_64_PC32:
705 case R_X86_64_PC64:
706 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000707 return true;
708 }
709}
710
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000711bool X86_64TargetInfo::isSizeReloc(uint32_t Type) const {
712 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000713}
714
George Rimar77d1cb12015-11-24 09:00:06 +0000715bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000716 const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000717 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000718 return false;
George Rimar25411f252015-12-04 11:20:13 +0000719 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
720 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000721 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
722}
723
724// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
725// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
726// how LD can be optimized to LE:
727// leaq bar@tlsld(%rip), %rdi
728// callq __tls_get_addr@PLT
729// leaq bar@dtpoff(%rax), %rcx
730// Is converted to:
731// .word 0x6666
732// .byte 0x66
733// mov %fs:0,%rax
734// leaq bar@tpoff(%rax), %rcx
735void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
736 uint64_t P, uint64_t SA) const {
737 const uint8_t Inst[] = {
738 0x66, 0x66, //.word 0x6666
739 0x66, //.byte 0x66
740 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
741 };
742 memcpy(Loc - 3, Inst, sizeof(Inst));
743}
744
745// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
746// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
747// how GD can be optimized to LE:
748// .byte 0x66
749// leaq x@tlsgd(%rip), %rdi
750// .word 0x6666
751// rex64
752// call __tls_get_addr@plt
753// Is converted to:
754// mov %fs:0x0,%rax
755// lea x@tpoff,%rax
756void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
757 uint64_t P, uint64_t SA) const {
758 const uint8_t Inst[] = {
759 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
760 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
761 };
762 memcpy(Loc - 4, Inst, sizeof(Inst));
763 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000764}
765
George Rimar25411f252015-12-04 11:20:13 +0000766// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
767// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
768// how GD can be optimized to IE:
769// .byte 0x66
770// leaq x@tlsgd(%rip), %rdi
771// .word 0x6666
772// rex64
773// call __tls_get_addr@plt
774// Is converted to:
775// mov %fs:0x0,%rax
776// addq x@tpoff,%rax
777void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
778 uint64_t P, uint64_t SA) const {
779 const uint8_t Inst[] = {
780 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
781 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
782 };
783 memcpy(Loc - 4, Inst, sizeof(Inst));
784 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
785}
786
George Rimar77d1cb12015-11-24 09:00:06 +0000787// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000788// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000789// This function does that. Read "ELF Handling For Thread-Local Storage,
790// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
791// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000792void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
793 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000794 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
795 // used in MOVQ or ADDQ instructions only.
796 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
797 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
798 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
799 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
800 uint8_t *Prefix = Loc - 3;
801 uint8_t *Inst = Loc - 2;
802 uint8_t *RegSlot = Loc - 1;
803 uint8_t Reg = Loc[-1] >> 3;
804 bool IsMov = *Inst == 0x8b;
805 bool RspAdd = !IsMov && Reg == 4;
806 // r12 and rsp registers requires special handling.
807 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
808 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
809 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
810 // The same true for rsp. So we convert to addq for them, saving 1 byte that
811 // we dont have.
812 if (RspAdd)
813 *Inst = 0x81;
814 else
815 *Inst = IsMov ? 0xc7 : 0x8d;
816 if (*Prefix == 0x4c)
817 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
818 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
819 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
820}
821
George Rimar6713cf82015-11-25 21:46:05 +0000822// This function applies a TLS relocation with an optimization as described
823// in the Ulrich's document. As a result of rewriting instructions at the
824// relocation target, relocations immediately follow the TLS relocation (which
825// would be applied to rewritten instructions) may have to be skipped.
826// This function returns a number of relocations that need to be skipped.
827unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
828 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000829 uint64_t SA,
830 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000831 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000832 case R_X86_64_DTPOFF32:
833 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
834 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000835 case R_X86_64_GOTTPOFF:
836 relocateTlsIeToLe(Loc, BufEnd, P, SA);
837 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000838 case R_X86_64_TLSGD: {
839 if (canBePreempted(&S, true))
840 relocateTlsGdToIe(Loc, BufEnd, P, SA);
841 else
842 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000843 // The next relocation should be against __tls_get_addr, so skip it
844 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000845 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000846 case R_X86_64_TLSLD:
847 relocateTlsLdToLe(Loc, BufEnd, P, SA);
848 // The next relocation should be against __tls_get_addr, so skip it
849 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000850 }
851 llvm_unreachable("Unknown TLS optimization");
852}
853
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000854void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000855 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000856 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000857 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000858 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000859 checkUInt<32>(SA, Type);
860 write32le(Loc, SA);
861 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000862 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000863 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000864 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000865 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000866 case R_X86_64_64:
867 write64le(Loc, SA);
868 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000869 case R_X86_64_DTPOFF32:
870 write32le(Loc, SA);
871 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000872 case R_X86_64_DTPOFF64:
873 write64le(Loc, SA);
874 break;
875 case R_X86_64_GOTPCREL:
876 case R_X86_64_PC32:
877 case R_X86_64_PLT32:
878 case R_X86_64_TLSGD:
879 case R_X86_64_TLSLD:
880 write32le(Loc, SA - P);
881 break;
George Rimar48651482015-12-11 08:59:37 +0000882 case R_X86_64_SIZE32:
883 write32le(Loc, ZA);
884 break;
885 case R_X86_64_SIZE64:
886 write64le(Loc, ZA);
887 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000888 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000889 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000890 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000891 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000892 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000893 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000894 case R_X86_64_TPOFF64:
895 write32le(Loc, SA - P);
896 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000897 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000898 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000899 }
900}
901
Hal Finkel3c8cc672015-10-12 20:56:18 +0000902// Relocation masks following the #lo(value), #hi(value), #ha(value),
903// #higher(value), #highera(value), #highest(value), and #highesta(value)
904// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
905// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000906static uint16_t applyPPCLo(uint64_t V) { return V; }
907static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
908static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
909static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
910static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000911static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000912static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
913
Rafael Espindolac4010882015-09-22 20:54:08 +0000914PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000915 PCRelReloc = R_PPC64_REL24;
916 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000917 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000918 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000919
920 // We need 64K pages (at least under glibc/Linux, the loader won't
921 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000922 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000923
924 // The PPC64 ELF ABI v1 spec, says:
925 //
926 // It is normally desirable to put segments with different characteristics
927 // in separate 256 Mbyte portions of the address space, to give the
928 // operating system full paging flexibility in the 64-bit address space.
929 //
930 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
931 // use 0x10000000 as the starting address.
932 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000933}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000934
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000935uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000936 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
937 // order. The TOC starts where the first of these sections starts.
938
939 // FIXME: This obviously does not do the right thing when there is no .got
940 // section, but there is a .toc or .tocbss section.
941 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
942 if (!TocVA)
943 TocVA = Out<ELF64BE>::Plt->getVA();
944
945 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
946 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
947 // code (crt1.o) assumes that you can get from the TOC base to the
948 // start of the .toc section with only a single (signed) 16-bit relocation.
949 return TocVA + 0x8000;
950}
951
George Rimar648a2c32015-10-20 08:54:27 +0000952void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
953void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
954 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000955void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
956 uint64_t GotEntryAddr,
957 uint64_t PltEntryAddr, int32_t Index,
958 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000959 uint64_t Off = GotEntryAddr - getPPC64TocBase();
960
961 // FIXME: What we should do, in theory, is get the offset of the function
962 // descriptor in the .opd section, and use that as the offset from %r2 (the
963 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
964 // be a pointer to the function descriptor in the .opd section. Using
965 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
966
Hal Finkelfa92f682015-10-13 21:47:34 +0000967 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000968 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
969 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
970 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
971 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
972 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
973 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
974 write32be(Buf + 28, 0x4e800420); // bctr
975}
976
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000977bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000978 if (relocNeedsPlt(Type, S))
979 return true;
980
981 switch (Type) {
982 default: return false;
983 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000984 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000985 case R_PPC64_GOT16_HA:
986 case R_PPC64_GOT16_HI:
987 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000988 case R_PPC64_GOT16_LO_DS:
989 return true;
990 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000991}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000992
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000993bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000994 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000995 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000996}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000997
Hal Finkelbe0823d2015-10-12 20:58:52 +0000998bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
999 switch (Type) {
1000 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001001 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001002 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001003 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001004 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001005 }
1006}
1007
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001008void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001009 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001010 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001011 uint64_t TB = getPPC64TocBase();
1012
Hal Finkel3c8cc672015-10-12 20:56:18 +00001013 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1014 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001015 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001016 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1017 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001018 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1019 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001020 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1021 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001022 default: break;
1023 }
1024
Hal Finkel3c8cc672015-10-12 20:56:18 +00001025 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001026 case R_PPC64_ADDR14: {
1027 checkAlignment<4>(SA, Type);
1028 // Preserve the AA/LK bits in the branch instruction
1029 uint8_t AALK = Loc[3];
1030 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1031 break;
1032 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001033 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001034 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001035 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001036 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001037 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001038 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001039 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001040 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001041 case R_PPC64_ADDR16_HA:
1042 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 break;
1044 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001045 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001046 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001048 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001049 break;
1050 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001051 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 break;
1053 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001054 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 break;
1056 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001057 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001059 case R_PPC64_ADDR16_LO:
1060 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001062 case R_PPC64_ADDR16_LO_DS:
1063 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001064 break;
1065 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001066 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001067 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001069 case R_PPC64_ADDR64:
1070 write64be(Loc, SA);
1071 break;
1072 case R_PPC64_REL16_HA:
1073 write16be(Loc, applyPPCHa(SA - P));
1074 break;
1075 case R_PPC64_REL16_HI:
1076 write16be(Loc, applyPPCHi(SA - P));
1077 break;
1078 case R_PPC64_REL16_LO:
1079 write16be(Loc, applyPPCLo(SA - P));
1080 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001081 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001082 // If we have an undefined weak symbol, we might get here with a symbol
1083 // address of zero. That could overflow, but the code must be unreachable,
1084 // so don't bother doing anything at all.
1085 if (!SA)
1086 break;
1087
Hal Finkeldaedc122015-10-12 23:16:53 +00001088 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1089 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001090 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001091
1092 if (!InPlt && Out<ELF64BE>::Opd) {
1093 // If this is a local call, and we currently have the address of a
1094 // function-descriptor, get the underlying code address instead.
1095 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1096 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001097 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001098
1099 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001100 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001101 }
1102
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001104 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001105 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001106
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001107 uint32_t Nop = 0x60000000;
1108 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1109 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001110 break;
1111 }
1112 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001113 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001114 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 break;
1116 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001117 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001118 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001119 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001120 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001121 break;
1122 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001123 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001124 }
1125}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001126
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001127AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +00001128 CopyReloc = R_AARCH64_COPY;
George Rimara4804352016-01-11 14:15:17 +00001129 IRelativeReloc = R_AARCH64_IRELATIVE;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001130 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001131 PltReloc = R_AARCH64_JUMP_SLOT;
1132 LazyRelocations = true;
1133 PltEntrySize = 16;
1134 PltZeroEntrySize = 32;
1135}
George Rimar648a2c32015-10-20 08:54:27 +00001136
Igor Kudrincfe47f52015-12-05 06:20:24 +00001137unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
1138 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1139 return Type;
1140 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
1141 error("Relocation " + S + " cannot be used when making a shared object; "
1142 "recompile with -fPIC.");
1143}
1144
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001145void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1146 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1147}
1148
George Rimar648a2c32015-10-20 08:54:27 +00001149void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001150 uint64_t PltEntryAddr) const {
1151 const uint8_t PltData[] = {
1152 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1153 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1154 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1155 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1156 0x20, 0x02, 0x1f, 0xd6, // br x17
1157 0x1f, 0x20, 0x03, 0xd5, // nop
1158 0x1f, 0x20, 0x03, 0xd5, // nop
1159 0x1f, 0x20, 0x03, 0xd5 // nop
1160 };
1161 memcpy(Buf, PltData, sizeof(PltData));
1162
1163 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
1164 GotEntryAddr + 16);
1165 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
1166 GotEntryAddr + 16);
1167 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
1168 GotEntryAddr + 16);
1169}
1170
George Rimar77b77792015-11-25 22:15:01 +00001171void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1172 uint64_t GotEntryAddr,
1173 uint64_t PltEntryAddr, int32_t Index,
1174 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001175 const uint8_t Inst[] = {
1176 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1177 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1178 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1179 0x20, 0x02, 0x1f, 0xd6 // br x17
1180 };
1181 memcpy(Buf, Inst, sizeof(Inst));
1182
1183 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1184 GotEntryAddr);
1185 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1186 GotEntryAddr);
1187 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1188 GotEntryAddr);
1189}
1190
Rui Ueyama02dfd492015-12-17 01:18:40 +00001191bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001192 if (Config->Shared)
1193 return false;
1194 switch (Type) {
1195 default:
1196 return false;
1197 case R_AARCH64_ABS16:
1198 case R_AARCH64_ABS32:
1199 case R_AARCH64_ABS64:
1200 case R_AARCH64_ADD_ABS_LO12_NC:
1201 case R_AARCH64_ADR_PREL_LO21:
1202 case R_AARCH64_ADR_PREL_PG_HI21:
1203 case R_AARCH64_LDST8_ABS_LO12_NC:
1204 case R_AARCH64_LDST32_ABS_LO12_NC:
1205 case R_AARCH64_LDST64_ABS_LO12_NC:
1206 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1207 return SS->Sym.getType() == STT_OBJECT;
1208 return false;
1209 }
1210}
1211
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001212bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
1213 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001214 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
1215 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001216}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001217
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001218bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
1219 const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001220 if (isGnuIFunc<ELF64LE>(S))
1221 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001222 switch (Type) {
1223 default:
1224 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001225 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001226 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001227 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001228 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001229 return canBePreempted(&S, true);
1230 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001231}
Davide Italiano1d750a62015-09-27 08:45:38 +00001232
Davide Italianoef4be6b2015-10-06 19:01:32 +00001233static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001234 uint32_t ImmLo = (Imm & 0x3) << 29;
1235 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1236 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001237 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001238}
1239
Davide Italiano318ca222015-10-02 22:13:51 +00001240// Page(Expr) is the page address of the expression Expr, defined
1241// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001242// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001243static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001244 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001245}
1246
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001247void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001248 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001249 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001250 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001251 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001252 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001253 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001254 break;
1255 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001256 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001257 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001258 break;
1259 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001260 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001261 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001262 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001263 // This relocation stores 12 bits and there's no instruction
1264 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001265 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1266 // bits in Loc are zero.
1267 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001268 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001269 case R_AARCH64_ADR_GOT_PAGE: {
1270 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1271 checkInt<33>(X, Type);
1272 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1273 break;
1274 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001275 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001276 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001277 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001278 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001279 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001280 }
1281 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001282 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001283 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001284 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001285 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001286 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001287 case R_AARCH64_CALL26:
1288 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001289 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001290 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001291 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1292 break;
1293 }
George Rimar4102bfb2016-01-11 14:22:00 +00001294 case R_AARCH64_CONDBR19: {
1295 uint64_t X = SA - P;
1296 checkInt<21>(X, Type);
1297 or32le(Loc, (X & 0x1FFFFC) << 3);
1298 break;
1299 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001300 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001301 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001302 or32le(Loc, (SA & 0xFF8) << 7);
1303 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001304 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001305 or32le(Loc, (SA & 0xFFF) << 10);
1306 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001307 case R_AARCH64_LDST32_ABS_LO12_NC:
1308 or32le(Loc, (SA & 0xFFC) << 8);
1309 break;
1310 case R_AARCH64_LDST64_ABS_LO12_NC:
1311 or32le(Loc, (SA & 0xFF8) << 7);
1312 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001313 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001314 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001315 write16le(Loc, SA - P);
1316 break;
1317 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001318 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001319 write32le(Loc, SA - P);
1320 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001321 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001322 write64le(Loc, SA - P);
1323 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001324 case R_AARCH64_TSTBR14: {
1325 uint64_t X = SA - P;
1326 checkInt<16>(X, Type);
1327 or32le(Loc, (X & 0xFFFC) << 3);
1328 break;
1329 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001330 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001331 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001332 }
1333}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001334
Tom Stellard80efb162016-01-07 03:59:08 +00001335AMDGPUTargetInfo::AMDGPUTargetInfo() {}
1336
1337void AMDGPUTargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1338 llvm_unreachable("not implemented");
1339}
1340
1341void AMDGPUTargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1342 uint64_t PltEntryAddr) const {
1343 llvm_unreachable("not implemented");
1344}
1345
1346void AMDGPUTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1347 uint64_t GotEntryAddr,
1348 uint64_t PltEntryAddr, int32_t Index,
1349 unsigned RelOff) const {
1350 llvm_unreachable("not implemented");
1351}
1352
1353bool AMDGPUTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
1354 return false;
1355}
1356
1357bool AMDGPUTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
1358 return false;
1359}
1360
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001361// Implementing relocations for AMDGPU is low priority since most
1362// programs don't use relocations now. Thus, this function is not
1363// actually called (relocateOne is called for each relocation).
1364// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001365void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1366 uint64_t P, uint64_t SA, uint64_t ZA,
1367 uint8_t *PairedLoc) const {
1368 llvm_unreachable("not implemented");
1369}
1370
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001371template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001372 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001373 GotHeaderEntriesNum = 2;
1374}
1375
1376template <class ELFT>
1377void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001378 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001379 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1380 // Module pointer
1381 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001382}
1383
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001384template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001385void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1386template <class ELFT>
1387void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1388 uint64_t PltEntryAddr) const {}
1389template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001390void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1391 uint64_t GotEntryAddr,
1392 uint64_t PltEntryAddr, int32_t Index,
1393 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001394
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001395template <class ELFT>
1396bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1397 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001398 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001399}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001400
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001401template <class ELFT>
1402bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1403 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001404 return false;
1405}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001406
Simon Atanasyan35031192015-12-15 06:06:34 +00001407static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001408
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001409template <endianness E, uint8_t BSIZE>
1410static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1411 uint64_t SA) {
1412 uint32_t Mask = ~(0xffffffff << BSIZE);
1413 uint32_t Instr = read32<E>(Loc);
1414 int64_t A = SignExtend64<BSIZE + 2>((Instr & Mask) << 2);
1415 checkAlignment<4>(SA + A, Type);
1416 int64_t V = SA + A - P;
1417 checkInt<BSIZE + 2>(V, Type);
1418 write32<E>(Loc, (Instr & ~Mask) | ((V >> 2) & Mask));
1419}
1420
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001421template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001422void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001423 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001424 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001425 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001426 switch (Type) {
1427 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001428 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001429 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001430 case R_MIPS_CALL16:
1431 case R_MIPS_GOT16: {
1432 int64_t V = SA - getMipsGpAddr<ELFT>();
1433 if (Type == R_MIPS_GOT16)
1434 checkInt<16>(V, Type);
1435 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1436 break;
1437 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001438 case R_MIPS_GPREL16: {
1439 uint32_t Instr = read32<E>(Loc);
1440 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1441 checkInt<16>(V, Type);
1442 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1443 break;
1444 }
1445 case R_MIPS_GPREL32:
1446 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1447 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001448 case R_MIPS_HI16: {
1449 uint32_t Instr = read32<E>(Loc);
1450 if (PairedLoc) {
1451 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001452 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001453 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001454 } else {
1455 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001456 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001457 }
1458 break;
1459 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001460 case R_MIPS_JALR:
1461 // Ignore this optimization relocation for now
1462 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001463 case R_MIPS_LO16: {
1464 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001465 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001466 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1467 break;
1468 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001469 case R_MIPS_PC16:
1470 applyMipsPcReloc<E, 16>(Loc, Type, P, SA);
1471 break;
1472 case R_MIPS_PC19_S2:
1473 applyMipsPcReloc<E, 19>(Loc, Type, P, SA);
1474 break;
1475 case R_MIPS_PC21_S2:
1476 applyMipsPcReloc<E, 21>(Loc, Type, P, SA);
1477 break;
1478 case R_MIPS_PC26_S2:
1479 applyMipsPcReloc<E, 26>(Loc, Type, P, SA);
1480 break;
1481 case R_MIPS_PCHI16: {
1482 uint32_t Instr = read32<E>(Loc);
1483 if (PairedLoc) {
1484 uint64_t AHL = ((Instr & 0xffff) << 16) +
1485 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1486 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1487 } else {
1488 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1489 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1490 }
1491 break;
1492 }
1493 case R_MIPS_PCLO16: {
1494 uint32_t Instr = read32<E>(Loc);
1495 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1496 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1497 break;
1498 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001499 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001500 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001501 }
1502}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001503
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001504template <class ELFT>
1505bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1506 switch (Type) {
1507 default:
1508 return false;
1509 case R_MIPS_PC16:
1510 case R_MIPS_PC19_S2:
1511 case R_MIPS_PC21_S2:
1512 case R_MIPS_PC26_S2:
1513 case R_MIPS_PCHI16:
1514 case R_MIPS_PCLO16:
1515 return true;
1516 }
1517}
1518
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001519// _gp is a MIPS-specific ABI-defined symbol which points to
1520// a location that is relative to GOT. This function returns
1521// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001522template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001523 unsigned GPOffset = 0x7ff0;
1524 if (uint64_t V = Out<ELFT>::Got->getVA())
1525 return V + GPOffset;
1526 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001527}
1528
1529template uint32_t getMipsGpAddr<ELF32LE>();
1530template uint32_t getMipsGpAddr<ELF32BE>();
1531template uint64_t getMipsGpAddr<ELF64LE>();
1532template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001533}
1534}