blob: bd302accdcda9b9f774cb6c5584fe918d0c6278f [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();
Igor Kudrine7ad0932015-11-17 17:47:53 +0000125 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar6f17e092015-12-17 09:32:21 +0000126 bool isTlsDynReloc(unsigned Type, const SymbolBody &S) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +0000127 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000128 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
129 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
130 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000131 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
132 uint64_t PltEntryAddr, int32_t Index,
133 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000134 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000135 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
136 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000137 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000138 uint64_t SA, uint64_t ZA = 0,
139 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000140 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000141 bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override;
George Rimar48651482015-12-11 08:59:37 +0000142 bool isSizeDynReloc(uint32_t Type, const SymbolBody &S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000143 unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar25411f252015-12-04 11:20:13 +0000144 uint64_t P, uint64_t SA,
145 const SymbolBody &S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000146
147private:
148 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
149 uint64_t SA) const;
150 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
151 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000152 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
153 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000154 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
155 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000156};
157
158class PPC64TargetInfo final : public TargetInfo {
159public:
160 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +0000161 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
162 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
163 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000164 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
165 uint64_t PltEntryAddr, int32_t Index,
166 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000167 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
168 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000169 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000170 uint64_t SA, uint64_t ZA = 0,
171 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000172 bool isRelRelative(uint32_t Type) const override;
173};
174
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000175class AArch64TargetInfo final : public TargetInfo {
176public:
177 AArch64TargetInfo();
Igor Kudrincfe47f52015-12-05 06:20:24 +0000178 unsigned getDynReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000179 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000180 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
181 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
182 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000183 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
184 uint64_t PltEntryAddr, int32_t Index,
185 unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000186 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000187 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
188 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000189 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000190 uint64_t SA, uint64_t ZA = 0,
191 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000192};
193
Tom Stellard80efb162016-01-07 03:59:08 +0000194class AMDGPUTargetInfo final : public TargetInfo {
195public:
196 AMDGPUTargetInfo();
197 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
198 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
199 uint64_t PltEntryAddr) const override;
200 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
201 uint64_t PltEntryAddr, int32_t Index,
202 unsigned RelOff) const override;
203 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
204 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
205 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
206 uint64_t SA, uint64_t ZA = 0,
207 uint8_t *PairedLoc = nullptr) const override;
208};
209
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000210template <class ELFT> class MipsTargetInfo final : public TargetInfo {
211public:
212 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000213 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000214 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;
George Rimar77b77792015-11-25 22:15:01 +0000217 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
218 uint64_t PltEntryAddr, int32_t Index,
219 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000220 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
221 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000222 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000223 uint64_t SA, uint64_t ZA = 0,
224 uint8_t *PairedLoc = nullptr) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000225 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000226};
227} // anonymous namespace
228
Rui Ueyama91004392015-10-13 16:08:15 +0000229TargetInfo *createTarget() {
230 switch (Config->EMachine) {
231 case EM_386:
232 return new X86TargetInfo();
233 case EM_AARCH64:
234 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000235 case EM_AMDGPU:
236 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000237 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000238 switch (Config->EKind) {
239 case ELF32LEKind:
240 return new MipsTargetInfo<ELF32LE>();
241 case ELF32BEKind:
242 return new MipsTargetInfo<ELF32BE>();
243 default:
244 error("Unsupported MIPS target");
245 }
Rui Ueyama91004392015-10-13 16:08:15 +0000246 case EM_PPC64:
247 return new PPC64TargetInfo();
248 case EM_X86_64:
249 return new X86_64TargetInfo();
250 }
251 error("Unknown target machine");
252}
253
Rafael Espindola01205f72015-09-22 18:19:46 +0000254TargetInfo::~TargetInfo() {}
255
George Rimar6713cf82015-11-25 21:46:05 +0000256bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000257 return false;
258}
259
Igor Kudrinf6f45472015-11-10 08:39:27 +0000260uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
261
Rui Ueyama02dfd492015-12-17 01:18:40 +0000262bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000263 return false;
264}
265
George Rimarbfb7bf72015-12-21 10:00:12 +0000266bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
267
Igor Kudrine7ad0932015-11-17 17:47:53 +0000268unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000269
Rafael Espindolaae244002015-10-05 19:30:12 +0000270bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
271
George Rimar48651482015-12-11 08:59:37 +0000272bool TargetInfo::isSizeDynReloc(uint32_t Type, const SymbolBody &S) const {
273 return false;
274}
275
George Rimar6713cf82015-11-25 21:46:05 +0000276unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000277 uint32_t Type, uint64_t P, uint64_t SA,
278 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000279 return 0;
280}
George Rimar77d1cb12015-11-24 09:00:06 +0000281
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000282void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
283
Igor Kudrin351b41d2015-11-16 17:44:08 +0000284void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
285
Rafael Espindola7f074422015-09-22 21:35:51 +0000286X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000287 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000288 PCRelReloc = R_386_PC32;
289 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000290 PltReloc = R_386_JUMP_SLOT;
George Rimara07ff662015-12-21 10:12:06 +0000291 IRelativeReloc = R_386_IRELATIVE;
George Rimar6f17e092015-12-17 09:32:21 +0000292 RelativeReloc = R_386_RELATIVE;
George Rimar9db204a2015-12-02 09:58:20 +0000293 TlsGotReloc = R_386_TLS_TPOFF;
294 TlsGlobalDynamicReloc = R_386_TLS_GD;
295 TlsLocalDynamicReloc = R_386_TLS_LDM;
296 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
297 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000298 LazyRelocations = true;
299 PltEntrySize = 16;
300 PltZeroEntrySize = 16;
301}
302
303void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
304 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
305}
306
307void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
308 // Skip 6 bytes of "pushl (GOT+4)"
309 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000310}
Rafael Espindola01205f72015-09-22 18:19:46 +0000311
George Rimard23970f2015-11-25 20:41:53 +0000312unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
313 if (Type == R_386_TLS_LE)
314 return R_386_TLS_TPOFF;
315 if (Type == R_386_TLS_LE_32)
316 return R_386_TLS_TPOFF32;
317 return Type;
318}
319
George Rimar6f17e092015-12-17 09:32:21 +0000320unsigned X86TargetInfo::getTlsGotReloc(unsigned Type) const {
321 if (Type == R_386_TLS_IE)
322 return Type;
323 return TlsGotReloc;
324}
325
326bool X86TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000327 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
328 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000329 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000330 if (Type == R_386_TLS_IE)
331 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000332 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000333}
334
George Rimar648a2c32015-10-20 08:54:27 +0000335void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000336 uint64_t PltEntryAddr) const {
337 // Executable files and shared object files have
338 // separate procedure linkage tables.
339 if (Config->Shared) {
340 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000341 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
George Rimar77b77792015-11-25 22:15:01 +0000342 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
343 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
344 };
345 memcpy(Buf, V, sizeof(V));
346 return;
347 }
George Rimar648a2c32015-10-20 08:54:27 +0000348
George Rimar77b77792015-11-25 22:15:01 +0000349 const uint8_t PltData[] = {
350 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
351 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
352 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
353 };
354 memcpy(Buf, PltData, sizeof(PltData));
355 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
356 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
357}
358
359void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
360 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
361 int32_t Index, unsigned RelOff) const {
362 const uint8_t Inst[] = {
363 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
364 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
365 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
366 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000367 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000368 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
369 Buf[1] = Config->Shared ? 0xa3 : 0x25;
370 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
371 write32le(Buf + 7, RelOff);
372 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000373}
374
Rui Ueyama02dfd492015-12-17 01:18:40 +0000375bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000376 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
377 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
378 return SS->Sym.getType() == STT_OBJECT;
379 return false;
380}
381
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000382bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000383 if (S.isTls() && Type == R_386_TLS_GD)
George Rimar2558e122015-12-09 09:55:54 +0000384 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000385 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
George Rimar2558e122015-12-09 09:55:54 +0000386 return !isTlsOptimized(Type, &S);
387 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000388}
389
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000390bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000391 return isGnuIFunc<ELF32LE>(S) ||
392 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000393 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000394}
395
George Rimarbfb7bf72015-12-21 10:00:12 +0000396bool X86TargetInfo::isGotRelative(uint32_t Type) const {
397 // This relocation does not require got entry,
398 // but it is relative to got and needs it to be created.
399 // Here we request for that.
400 return Type == R_386_GOTOFF;
401}
402
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000403void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000404 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000405 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000406 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000407 case R_386_32:
408 add32le(Loc, SA);
409 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000410 case R_386_GOT32:
George Rimarbfb7bf72015-12-21 10:00:12 +0000411 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000412 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000413 break;
George Rimar13934772015-11-25 20:20:31 +0000414 case R_386_GOTPC:
415 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
416 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000417 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000418 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000419 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000420 break;
George Rimar9db204a2015-12-02 09:58:20 +0000421 case R_386_TLS_GD:
422 case R_386_TLS_LDM:
423 case R_386_TLS_TPOFF: {
424 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
425 Out<ELF32LE>::Got->getNumEntries() * 4;
426 checkInt<32>(V, Type);
427 write32le(Loc, V);
428 break;
429 }
George Rimar6f17e092015-12-17 09:32:21 +0000430 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000431 case R_386_TLS_LDO_32:
432 write32le(Loc, SA);
433 break;
George Rimard23970f2015-11-25 20:41:53 +0000434 case R_386_TLS_LE:
435 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
436 break;
437 case R_386_TLS_LE_32:
438 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
439 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000440 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000441 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000442 }
443}
444
George Rimar2558e122015-12-09 09:55:54 +0000445bool X86TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000446 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000447 return false;
448 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
449 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000450 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000451 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
452}
453
George Rimar6f17e092015-12-17 09:32:21 +0000454bool X86TargetInfo::relocNeedsDynRelative(unsigned Type) const {
455 return Config->Shared && Type == R_386_TLS_IE;
456}
457
George Rimar2558e122015-12-09 09:55:54 +0000458unsigned X86TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
459 uint32_t Type, uint64_t P,
460 uint64_t SA,
461 const SymbolBody &S) const {
462 switch (Type) {
463 case R_386_TLS_GD:
464 if (canBePreempted(&S, true))
465 relocateTlsGdToIe(Loc, BufEnd, P, SA);
466 else
467 relocateTlsGdToLe(Loc, BufEnd, P, SA);
468 // The next relocation should be against __tls_get_addr, so skip it
469 return 1;
470 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000471 case R_386_TLS_IE:
472 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000473 return 0;
474 case R_386_TLS_LDM:
475 relocateTlsLdToLe(Loc, BufEnd, P, SA);
476 // The next relocation should be against __tls_get_addr, so skip it
477 return 1;
478 case R_386_TLS_LDO_32:
479 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
480 return 0;
481 }
482 llvm_unreachable("Unknown TLS optimization");
483}
484
485// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
486// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
487// how GD can be optimized to IE:
488// leal x@tlsgd(, %ebx, 1),
489// call __tls_get_addr@plt
490// Is converted to:
491// movl %gs:0, %eax
492// addl x@gotntpoff(%ebx), %eax
493void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
494 uint64_t SA) const {
495 const uint8_t Inst[] = {
496 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
497 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
498 };
499 memcpy(Loc - 3, Inst, sizeof(Inst));
500 relocateOne(Loc + 5, BufEnd, R_386_32, P,
501 SA - Out<ELF32LE>::Got->getVA() -
502 Out<ELF32LE>::Got->getNumEntries() * 4);
503}
504
505// GD can be optimized to LE:
506// leal x@tlsgd(, %ebx, 1),
507// call __tls_get_addr@plt
508// Can be converted to:
509// movl %gs:0,%eax
510// addl $x@ntpoff,%eax
511// But gold emits subl $foo@tpoff,%eax instead of addl.
512// These instructions are completely equal in behavior.
513// This method generates subl to be consistent with gold.
514void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
515 uint64_t SA) const {
516 const uint8_t Inst[] = {
517 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
518 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
519 };
520 memcpy(Loc - 3, Inst, sizeof(Inst));
521 relocateOne(Loc + 5, BufEnd, R_386_32, P,
522 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
523}
524
525// LD can be optimized to LE:
526// leal foo(%reg),%eax
527// call ___tls_get_addr
528// Is converted to:
529// movl %gs:0,%eax
530// nop
531// leal 0(%esi,1),%esi
532void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
533 uint64_t SA) const {
534 const uint8_t Inst[] = {
535 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
536 0x90, // nop
537 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
538 };
539 memcpy(Loc - 2, Inst, sizeof(Inst));
540}
541
George Rimar6f17e092015-12-17 09:32:21 +0000542// In some conditions, relocations can be optimized to avoid using GOT.
543// This function does that for Initial Exec to Local Exec case.
544// Read "ELF Handling For Thread-Local Storage, 5.1
545// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000546// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000547void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
548 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000549 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000550 // Ulrich's document section 6.2 says that @gotntpoff can
551 // be used with MOVL or ADDL instructions.
552 // @indntpoff is similar to @gotntpoff, but for use in
553 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000554 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000555 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000556 uint8_t Reg = (Loc[-1] >> 3) & 7;
557 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000558 if (Type == R_386_TLS_IE) {
559 // For R_386_TLS_IE relocation we perform the next transformations:
560 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
561 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
562 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
563 // First one is special because when EAX is used the sequence is 5 bytes
564 // long, otherwise it is 6 bytes.
565 if (*Op == 0xa1) {
566 *Op = 0xb8;
567 } else {
568 *Inst = IsMov ? 0xc7 : 0x81;
569 *Op = 0xc0 | ((*Op >> 3) & 7);
570 }
571 } else {
572 // R_386_TLS_GOTIE relocation can be optimized to
573 // R_386_TLS_LE so that it does not use GOT.
574 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
575 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
576 // Note: gold converts to ADDL instead of LEAL.
577 *Inst = IsMov ? 0xc7 : 0x8d;
578 if (IsMov)
579 *Op = 0xc0 | ((*Op >> 3) & 7);
580 else
581 *Op = 0x80 | Reg | (Reg << 3);
582 }
George Rimar2558e122015-12-09 09:55:54 +0000583 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
584}
585
Rafael Espindola7f074422015-09-22 21:35:51 +0000586X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000587 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000588 PCRelReloc = R_X86_64_PC32;
589 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000590 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000591 RelativeReloc = R_X86_64_RELATIVE;
George Rimara07ff662015-12-21 10:12:06 +0000592 IRelativeReloc = R_X86_64_IRELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000593 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000594 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000595 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000596 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000597 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000598 LazyRelocations = true;
599 PltEntrySize = 16;
600 PltZeroEntrySize = 16;
601}
602
Igor Kudrin351b41d2015-11-16 17:44:08 +0000603void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
604 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
605}
606
George Rimar648a2c32015-10-20 08:54:27 +0000607void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
608 // Skip 6 bytes of "jmpq *got(%rip)"
609 write32le(Buf, Plt + 6);
610}
611
612void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
613 uint64_t PltEntryAddr) const {
614 const uint8_t PltData[] = {
615 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
616 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
617 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
618 };
619 memcpy(Buf, PltData, sizeof(PltData));
620 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
621 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000622}
Rafael Espindola01205f72015-09-22 18:19:46 +0000623
George Rimar77b77792015-11-25 22:15:01 +0000624void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
625 uint64_t GotEntryAddr,
626 uint64_t PltEntryAddr, int32_t Index,
627 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000628 const uint8_t Inst[] = {
629 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
630 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
631 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
632 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000633 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000634
George Rimar648a2c32015-10-20 08:54:27 +0000635 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
636 write32le(Buf + 7, Index);
637 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000638}
639
Rui Ueyama02dfd492015-12-17 01:18:40 +0000640bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000641 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
642 Type == R_X86_64_64)
643 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
644 return SS->Sym.getType() == STT_OBJECT;
645 return false;
646}
647
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000648bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000649 if (Type == R_X86_64_TLSGD)
650 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000651 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000652 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000653 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000654}
655
George Rimar6f17e092015-12-17 09:32:21 +0000656bool X86_64TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000657 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000658}
659
Igor Kudrine7ad0932015-11-17 17:47:53 +0000660unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000661 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000662 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000663 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000664}
665
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000666bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000667 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000668 return false;
George Rimara07ff662015-12-21 10:12:06 +0000669 if (isGnuIFunc<ELF64LE>(S))
670 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000671
Rafael Espindola01205f72015-09-22 18:19:46 +0000672 switch (Type) {
673 default:
674 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000675 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000676 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000677 case R_X86_64_PC32:
678 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000679 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000680 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000681 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000682 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000683 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
684 // libc.so.
685 //
686 // The general idea on how to handle such cases is to create a PLT entry
687 // and use that as the function value.
688 //
689 // For the static linking part, we just return true and everything else
690 // will use the the PLT entry as the address.
691 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000692 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000693 // still works. We need the help of the dynamic linker for that. We
694 // let it know that we have a direct reference to a so symbol by creating
695 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000696 // dynamic linker resolves the symbol to the value of the symbol we created.
697 // This is true even for got entries, so pointer equality is maintained.
698 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000699 // real function is a dedicated got entry used by the plt. That is
700 // identified by special relocation types (R_X86_64_JUMP_SLOT,
701 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000702 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000703 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000704 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000705 }
706}
Rafael Espindolac4010882015-09-22 20:54:08 +0000707
Rafael Espindolaae244002015-10-05 19:30:12 +0000708bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
709 switch (Type) {
710 default:
711 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000712 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000713 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000714 case R_X86_64_PC8:
715 case R_X86_64_PC16:
716 case R_X86_64_PC32:
717 case R_X86_64_PC64:
718 case R_X86_64_PLT32:
George Rimar48651482015-12-11 08:59:37 +0000719 case R_X86_64_SIZE32:
720 case R_X86_64_SIZE64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000721 return true;
722 }
723}
724
George Rimar48651482015-12-11 08:59:37 +0000725bool X86_64TargetInfo::isSizeDynReloc(uint32_t Type,
726 const SymbolBody &S) const {
727 return (Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64) &&
728 canBePreempted(&S, false);
729}
730
George Rimar77d1cb12015-11-24 09:00:06 +0000731bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000732 const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000733 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000734 return false;
George Rimar25411f252015-12-04 11:20:13 +0000735 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
736 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000737 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
738}
739
740// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
741// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
742// how LD can be optimized to LE:
743// leaq bar@tlsld(%rip), %rdi
744// callq __tls_get_addr@PLT
745// leaq bar@dtpoff(%rax), %rcx
746// Is converted to:
747// .word 0x6666
748// .byte 0x66
749// mov %fs:0,%rax
750// leaq bar@tpoff(%rax), %rcx
751void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
752 uint64_t P, uint64_t SA) const {
753 const uint8_t Inst[] = {
754 0x66, 0x66, //.word 0x6666
755 0x66, //.byte 0x66
756 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
757 };
758 memcpy(Loc - 3, Inst, sizeof(Inst));
759}
760
761// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
762// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
763// how GD can be optimized to LE:
764// .byte 0x66
765// leaq x@tlsgd(%rip), %rdi
766// .word 0x6666
767// rex64
768// call __tls_get_addr@plt
769// Is converted to:
770// mov %fs:0x0,%rax
771// lea x@tpoff,%rax
772void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
773 uint64_t P, uint64_t SA) const {
774 const uint8_t Inst[] = {
775 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
776 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
777 };
778 memcpy(Loc - 4, Inst, sizeof(Inst));
779 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000780}
781
George Rimar25411f252015-12-04 11:20:13 +0000782// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
783// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
784// how GD can be optimized to IE:
785// .byte 0x66
786// leaq x@tlsgd(%rip), %rdi
787// .word 0x6666
788// rex64
789// call __tls_get_addr@plt
790// Is converted to:
791// mov %fs:0x0,%rax
792// addq x@tpoff,%rax
793void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
794 uint64_t P, uint64_t SA) const {
795 const uint8_t Inst[] = {
796 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
797 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
798 };
799 memcpy(Loc - 4, Inst, sizeof(Inst));
800 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
801}
802
George Rimar77d1cb12015-11-24 09:00:06 +0000803// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000804// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000805// This function does that. Read "ELF Handling For Thread-Local Storage,
806// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
807// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000808void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
809 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000810 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
811 // used in MOVQ or ADDQ instructions only.
812 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
813 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
814 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
815 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
816 uint8_t *Prefix = Loc - 3;
817 uint8_t *Inst = Loc - 2;
818 uint8_t *RegSlot = Loc - 1;
819 uint8_t Reg = Loc[-1] >> 3;
820 bool IsMov = *Inst == 0x8b;
821 bool RspAdd = !IsMov && Reg == 4;
822 // r12 and rsp registers requires special handling.
823 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
824 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
825 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
826 // The same true for rsp. So we convert to addq for them, saving 1 byte that
827 // we dont have.
828 if (RspAdd)
829 *Inst = 0x81;
830 else
831 *Inst = IsMov ? 0xc7 : 0x8d;
832 if (*Prefix == 0x4c)
833 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
834 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
835 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
836}
837
George Rimar6713cf82015-11-25 21:46:05 +0000838// This function applies a TLS relocation with an optimization as described
839// in the Ulrich's document. As a result of rewriting instructions at the
840// relocation target, relocations immediately follow the TLS relocation (which
841// would be applied to rewritten instructions) may have to be skipped.
842// This function returns a number of relocations that need to be skipped.
843unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
844 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000845 uint64_t SA,
846 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000847 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000848 case R_X86_64_DTPOFF32:
849 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
850 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000851 case R_X86_64_GOTTPOFF:
852 relocateTlsIeToLe(Loc, BufEnd, P, SA);
853 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000854 case R_X86_64_TLSGD: {
855 if (canBePreempted(&S, true))
856 relocateTlsGdToIe(Loc, BufEnd, P, SA);
857 else
858 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000859 // The next relocation should be against __tls_get_addr, so skip it
860 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000861 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000862 case R_X86_64_TLSLD:
863 relocateTlsLdToLe(Loc, BufEnd, P, SA);
864 // The next relocation should be against __tls_get_addr, so skip it
865 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000866 }
867 llvm_unreachable("Unknown TLS optimization");
868}
869
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000870void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000871 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000872 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000873 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000874 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000875 checkUInt<32>(SA, Type);
876 write32le(Loc, SA);
877 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000878 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000879 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000880 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000881 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000882 case R_X86_64_64:
883 write64le(Loc, SA);
884 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000885 case R_X86_64_DTPOFF32:
886 write32le(Loc, SA);
887 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000888 case R_X86_64_DTPOFF64:
889 write64le(Loc, SA);
890 break;
891 case R_X86_64_GOTPCREL:
892 case R_X86_64_PC32:
893 case R_X86_64_PLT32:
894 case R_X86_64_TLSGD:
895 case R_X86_64_TLSLD:
896 write32le(Loc, SA - P);
897 break;
George Rimar48651482015-12-11 08:59:37 +0000898 case R_X86_64_SIZE32:
899 write32le(Loc, ZA);
900 break;
901 case R_X86_64_SIZE64:
902 write64le(Loc, ZA);
903 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000904 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000905 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000906 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000907 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000908 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000909 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000910 case R_X86_64_TPOFF64:
911 write32le(Loc, SA - P);
912 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000913 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000914 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000915 }
916}
917
Hal Finkel3c8cc672015-10-12 20:56:18 +0000918// Relocation masks following the #lo(value), #hi(value), #ha(value),
919// #higher(value), #highera(value), #highest(value), and #highesta(value)
920// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
921// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000922static uint16_t applyPPCLo(uint64_t V) { return V; }
923static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
924static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
925static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
926static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000927static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000928static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
929
Rafael Espindolac4010882015-09-22 20:54:08 +0000930PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000931 PCRelReloc = R_PPC64_REL24;
932 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000933 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000934 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000935
936 // We need 64K pages (at least under glibc/Linux, the loader won't
937 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000938 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000939
940 // The PPC64 ELF ABI v1 spec, says:
941 //
942 // It is normally desirable to put segments with different characteristics
943 // in separate 256 Mbyte portions of the address space, to give the
944 // operating system full paging flexibility in the 64-bit address space.
945 //
946 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
947 // use 0x10000000 as the starting address.
948 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000949}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000950
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000951uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000952 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
953 // order. The TOC starts where the first of these sections starts.
954
955 // FIXME: This obviously does not do the right thing when there is no .got
956 // section, but there is a .toc or .tocbss section.
957 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
958 if (!TocVA)
959 TocVA = Out<ELF64BE>::Plt->getVA();
960
961 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
962 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
963 // code (crt1.o) assumes that you can get from the TOC base to the
964 // start of the .toc section with only a single (signed) 16-bit relocation.
965 return TocVA + 0x8000;
966}
967
George Rimar648a2c32015-10-20 08:54:27 +0000968void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
969void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
970 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000971void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
972 uint64_t GotEntryAddr,
973 uint64_t PltEntryAddr, int32_t Index,
974 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000975 uint64_t Off = GotEntryAddr - getPPC64TocBase();
976
977 // FIXME: What we should do, in theory, is get the offset of the function
978 // descriptor in the .opd section, and use that as the offset from %r2 (the
979 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
980 // be a pointer to the function descriptor in the .opd section. Using
981 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
982
Hal Finkelfa92f682015-10-13 21:47:34 +0000983 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000984 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
985 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
986 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
987 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
988 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
989 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
990 write32be(Buf + 28, 0x4e800420); // bctr
991}
992
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000993bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000994 if (relocNeedsPlt(Type, S))
995 return true;
996
997 switch (Type) {
998 default: return false;
999 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001000 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001001 case R_PPC64_GOT16_HA:
1002 case R_PPC64_GOT16_HI:
1003 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001004 case R_PPC64_GOT16_LO_DS:
1005 return true;
1006 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001007}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001008
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001009bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001011 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001012}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001013
Hal Finkelbe0823d2015-10-12 20:58:52 +00001014bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1015 switch (Type) {
1016 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001017 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001018 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001019 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001020 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001021 }
1022}
1023
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001024void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001025 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001026 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 uint64_t TB = getPPC64TocBase();
1028
Hal Finkel3c8cc672015-10-12 20:56:18 +00001029 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1030 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001031 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001032 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1033 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001034 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1035 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001036 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1037 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 default: break;
1039 }
1040
Hal Finkel3c8cc672015-10-12 20:56:18 +00001041 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001042 case R_PPC64_ADDR14: {
1043 checkAlignment<4>(SA, Type);
1044 // Preserve the AA/LK bits in the branch instruction
1045 uint8_t AALK = Loc[3];
1046 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1047 break;
1048 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001049 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001050 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001051 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001052 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001053 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001054 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001055 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001056 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001057 case R_PPC64_ADDR16_HA:
1058 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059 break;
1060 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001061 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001063 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001064 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 break;
1066 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001067 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 break;
1069 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001070 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071 break;
1072 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001073 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001074 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001075 case R_PPC64_ADDR16_LO:
1076 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001077 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001078 case R_PPC64_ADDR16_LO_DS:
1079 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 break;
1081 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001082 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001083 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001085 case R_PPC64_ADDR64:
1086 write64be(Loc, SA);
1087 break;
1088 case R_PPC64_REL16_HA:
1089 write16be(Loc, applyPPCHa(SA - P));
1090 break;
1091 case R_PPC64_REL16_HI:
1092 write16be(Loc, applyPPCHi(SA - P));
1093 break;
1094 case R_PPC64_REL16_LO:
1095 write16be(Loc, applyPPCLo(SA - P));
1096 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001097 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001098 // If we have an undefined weak symbol, we might get here with a symbol
1099 // address of zero. That could overflow, but the code must be unreachable,
1100 // so don't bother doing anything at all.
1101 if (!SA)
1102 break;
1103
Hal Finkeldaedc122015-10-12 23:16:53 +00001104 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1105 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001106 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001107
1108 if (!InPlt && Out<ELF64BE>::Opd) {
1109 // If this is a local call, and we currently have the address of a
1110 // function-descriptor, get the underlying code address instead.
1111 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1112 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001113 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001114
1115 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001116 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001117 }
1118
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001120 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001121 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001122
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001123 uint32_t Nop = 0x60000000;
1124 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1125 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 }
1128 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001129 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001130 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 break;
1132 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001133 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001134 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001135 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001136 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001137 break;
1138 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001139 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001140 }
1141}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001142
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001143AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +00001144 CopyReloc = R_AARCH64_COPY;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001145 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001146 PltReloc = R_AARCH64_JUMP_SLOT;
1147 LazyRelocations = true;
1148 PltEntrySize = 16;
1149 PltZeroEntrySize = 32;
1150}
George Rimar648a2c32015-10-20 08:54:27 +00001151
Igor Kudrincfe47f52015-12-05 06:20:24 +00001152unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
1153 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1154 return Type;
1155 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
1156 error("Relocation " + S + " cannot be used when making a shared object; "
1157 "recompile with -fPIC.");
1158}
1159
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001160unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
1161
1162void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1163 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1164}
1165
George Rimar648a2c32015-10-20 08:54:27 +00001166void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001167 uint64_t PltEntryAddr) const {
1168 const uint8_t PltData[] = {
1169 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1170 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1171 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1172 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1173 0x20, 0x02, 0x1f, 0xd6, // br x17
1174 0x1f, 0x20, 0x03, 0xd5, // nop
1175 0x1f, 0x20, 0x03, 0xd5, // nop
1176 0x1f, 0x20, 0x03, 0xd5 // nop
1177 };
1178 memcpy(Buf, PltData, sizeof(PltData));
1179
1180 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
1181 GotEntryAddr + 16);
1182 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
1183 GotEntryAddr + 16);
1184 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
1185 GotEntryAddr + 16);
1186}
1187
George Rimar77b77792015-11-25 22:15:01 +00001188void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1189 uint64_t GotEntryAddr,
1190 uint64_t PltEntryAddr, int32_t Index,
1191 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001192 const uint8_t Inst[] = {
1193 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1194 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1195 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1196 0x20, 0x02, 0x1f, 0xd6 // br x17
1197 };
1198 memcpy(Buf, Inst, sizeof(Inst));
1199
1200 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1201 GotEntryAddr);
1202 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1203 GotEntryAddr);
1204 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1205 GotEntryAddr);
1206}
1207
Rui Ueyama02dfd492015-12-17 01:18:40 +00001208bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001209 if (Config->Shared)
1210 return false;
1211 switch (Type) {
1212 default:
1213 return false;
1214 case R_AARCH64_ABS16:
1215 case R_AARCH64_ABS32:
1216 case R_AARCH64_ABS64:
1217 case R_AARCH64_ADD_ABS_LO12_NC:
1218 case R_AARCH64_ADR_PREL_LO21:
1219 case R_AARCH64_ADR_PREL_PG_HI21:
1220 case R_AARCH64_LDST8_ABS_LO12_NC:
1221 case R_AARCH64_LDST32_ABS_LO12_NC:
1222 case R_AARCH64_LDST64_ABS_LO12_NC:
1223 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1224 return SS->Sym.getType() == STT_OBJECT;
1225 return false;
1226 }
1227}
1228
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001229bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
1230 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001231 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
1232 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001233}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001234
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001235bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
1236 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001237 switch (Type) {
1238 default:
1239 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001240 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001241 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001242 return canBePreempted(&S, true);
1243 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001244}
Davide Italiano1d750a62015-09-27 08:45:38 +00001245
Davide Italianoef4be6b2015-10-06 19:01:32 +00001246static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001247 uint32_t ImmLo = (Imm & 0x3) << 29;
1248 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1249 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001250 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001251}
1252
Davide Italiano318ca222015-10-02 22:13:51 +00001253// Page(Expr) is the page address of the expression Expr, defined
1254// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001255// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001256static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001257 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001258}
1259
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001260void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001261 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001262 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001263 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001264 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001265 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001266 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001267 break;
1268 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001269 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001270 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001271 break;
1272 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001273 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001274 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001275 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001276 // This relocation stores 12 bits and there's no instruction
1277 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001278 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1279 // bits in Loc are zero.
1280 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001281 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001282 case R_AARCH64_ADR_GOT_PAGE: {
1283 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1284 checkInt<33>(X, Type);
1285 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1286 break;
1287 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001288 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001289 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001290 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001291 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001292 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001293 }
1294 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001295 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001296 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001297 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001298 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001299 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001300 case R_AARCH64_CALL26:
1301 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001302 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001303 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001304 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1305 break;
1306 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001307 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001308 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001309 or32le(Loc, (SA & 0xFF8) << 7);
1310 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001311 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001312 or32le(Loc, (SA & 0xFFF) << 10);
1313 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001314 case R_AARCH64_LDST32_ABS_LO12_NC:
1315 or32le(Loc, (SA & 0xFFC) << 8);
1316 break;
1317 case R_AARCH64_LDST64_ABS_LO12_NC:
1318 or32le(Loc, (SA & 0xFF8) << 7);
1319 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001320 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001321 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001322 write16le(Loc, SA - P);
1323 break;
1324 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001325 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001326 write32le(Loc, SA - P);
1327 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001328 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001329 write64le(Loc, SA - P);
1330 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001331 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001332 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001333 }
1334}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001335
Tom Stellard80efb162016-01-07 03:59:08 +00001336AMDGPUTargetInfo::AMDGPUTargetInfo() {}
1337
1338void AMDGPUTargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1339 llvm_unreachable("not implemented");
1340}
1341
1342void AMDGPUTargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1343 uint64_t PltEntryAddr) const {
1344 llvm_unreachable("not implemented");
1345}
1346
1347void AMDGPUTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1348 uint64_t GotEntryAddr,
1349 uint64_t PltEntryAddr, int32_t Index,
1350 unsigned RelOff) const {
1351 llvm_unreachable("not implemented");
1352}
1353
1354bool AMDGPUTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
1355 return false;
1356}
1357
1358bool AMDGPUTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
1359 return false;
1360}
1361
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001362// Implementing relocations for AMDGPU is low priority since most
1363// programs don't use relocations now. Thus, this function is not
1364// actually called (relocateOne is called for each relocation).
1365// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001366void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1367 uint64_t P, uint64_t SA, uint64_t ZA,
1368 uint8_t *PairedLoc) const {
1369 llvm_unreachable("not implemented");
1370}
1371
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001372template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001373 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001374 GotHeaderEntriesNum = 2;
1375}
1376
1377template <class ELFT>
1378void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001379 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001380 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1381 // Module pointer
1382 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001383}
1384
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001385template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001386void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1387template <class ELFT>
1388void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1389 uint64_t PltEntryAddr) const {}
1390template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001391void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1392 uint64_t GotEntryAddr,
1393 uint64_t PltEntryAddr, int32_t Index,
1394 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001395
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001396template <class ELFT>
1397bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1398 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001399 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001400}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001401
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001402template <class ELFT>
1403bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1404 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001405 return false;
1406}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001407
Simon Atanasyan35031192015-12-15 06:06:34 +00001408static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001409
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001410template <endianness E, uint8_t BSIZE>
1411static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1412 uint64_t SA) {
1413 uint32_t Mask = ~(0xffffffff << BSIZE);
1414 uint32_t Instr = read32<E>(Loc);
1415 int64_t A = SignExtend64<BSIZE + 2>((Instr & Mask) << 2);
1416 checkAlignment<4>(SA + A, Type);
1417 int64_t V = SA + A - P;
1418 checkInt<BSIZE + 2>(V, Type);
1419 write32<E>(Loc, (Instr & ~Mask) | ((V >> 2) & Mask));
1420}
1421
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001422template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001423void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001424 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001425 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001426 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001427 switch (Type) {
1428 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001429 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001430 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001431 case R_MIPS_CALL16:
1432 case R_MIPS_GOT16: {
1433 int64_t V = SA - getMipsGpAddr<ELFT>();
1434 if (Type == R_MIPS_GOT16)
1435 checkInt<16>(V, Type);
1436 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1437 break;
1438 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001439 case R_MIPS_GPREL16: {
1440 uint32_t Instr = read32<E>(Loc);
1441 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1442 checkInt<16>(V, Type);
1443 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1444 break;
1445 }
1446 case R_MIPS_GPREL32:
1447 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1448 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001449 case R_MIPS_HI16: {
1450 uint32_t Instr = read32<E>(Loc);
1451 if (PairedLoc) {
1452 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001453 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001454 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001455 } else {
1456 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001457 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001458 }
1459 break;
1460 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001461 case R_MIPS_JALR:
1462 // Ignore this optimization relocation for now
1463 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001464 case R_MIPS_LO16: {
1465 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001466 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001467 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1468 break;
1469 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001470 case R_MIPS_PC16:
1471 applyMipsPcReloc<E, 16>(Loc, Type, P, SA);
1472 break;
1473 case R_MIPS_PC19_S2:
1474 applyMipsPcReloc<E, 19>(Loc, Type, P, SA);
1475 break;
1476 case R_MIPS_PC21_S2:
1477 applyMipsPcReloc<E, 21>(Loc, Type, P, SA);
1478 break;
1479 case R_MIPS_PC26_S2:
1480 applyMipsPcReloc<E, 26>(Loc, Type, P, SA);
1481 break;
1482 case R_MIPS_PCHI16: {
1483 uint32_t Instr = read32<E>(Loc);
1484 if (PairedLoc) {
1485 uint64_t AHL = ((Instr & 0xffff) << 16) +
1486 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1487 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1488 } else {
1489 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1490 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1491 }
1492 break;
1493 }
1494 case R_MIPS_PCLO16: {
1495 uint32_t Instr = read32<E>(Loc);
1496 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1497 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1498 break;
1499 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001500 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001501 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001502 }
1503}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001504
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001505template <class ELFT>
1506bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1507 switch (Type) {
1508 default:
1509 return false;
1510 case R_MIPS_PC16:
1511 case R_MIPS_PC19_S2:
1512 case R_MIPS_PC21_S2:
1513 case R_MIPS_PC26_S2:
1514 case R_MIPS_PCHI16:
1515 case R_MIPS_PCLO16:
1516 return true;
1517 }
1518}
1519
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001520// _gp is a MIPS-specific ABI-defined symbol which points to
1521// a location that is relative to GOT. This function returns
1522// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001523template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001524 unsigned GPOffset = 0x7ff0;
1525 if (uint64_t V = Out<ELFT>::Got->getVA())
1526 return V + GPOffset;
1527 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001528}
1529
1530template uint32_t getMipsGpAddr<ELF32LE>();
1531template uint32_t getMipsGpAddr<ELF32BE>();
1532template uint64_t getMipsGpAddr<ELF64LE>();
1533template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001534}
1535}