blob: ef814a30ba0f2c5dece423e56fd1d7773b50a6d9 [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) {
74 if (auto *SS = dyn_cast<Defined<ELFT>>(&S))
75 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
194template <class ELFT> class MipsTargetInfo final : public TargetInfo {
195public:
196 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000197 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000198 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
199 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
200 uint64_t PltEntryAddr) const override;
George Rimar77b77792015-11-25 22:15:01 +0000201 void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr,
202 uint64_t PltEntryAddr, int32_t Index,
203 unsigned RelOff) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000204 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
205 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000206 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000207 uint64_t SA, uint64_t ZA = 0,
208 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000209};
210} // anonymous namespace
211
Rui Ueyama91004392015-10-13 16:08:15 +0000212TargetInfo *createTarget() {
213 switch (Config->EMachine) {
214 case EM_386:
215 return new X86TargetInfo();
216 case EM_AARCH64:
217 return new AArch64TargetInfo();
218 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000219 switch (Config->EKind) {
220 case ELF32LEKind:
221 return new MipsTargetInfo<ELF32LE>();
222 case ELF32BEKind:
223 return new MipsTargetInfo<ELF32BE>();
224 default:
225 error("Unsupported MIPS target");
226 }
Rui Ueyama91004392015-10-13 16:08:15 +0000227 case EM_PPC64:
228 return new PPC64TargetInfo();
229 case EM_X86_64:
230 return new X86_64TargetInfo();
231 }
232 error("Unknown target machine");
233}
234
Rafael Espindola01205f72015-09-22 18:19:46 +0000235TargetInfo::~TargetInfo() {}
236
George Rimar6713cf82015-11-25 21:46:05 +0000237bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000238 return false;
239}
240
Igor Kudrinf6f45472015-11-10 08:39:27 +0000241uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
242
Rui Ueyama02dfd492015-12-17 01:18:40 +0000243bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000244 return false;
245}
246
George Rimarbfb7bf72015-12-21 10:00:12 +0000247bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
248
Igor Kudrine7ad0932015-11-17 17:47:53 +0000249unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000250
Rafael Espindolaae244002015-10-05 19:30:12 +0000251bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
252
George Rimar48651482015-12-11 08:59:37 +0000253bool TargetInfo::isSizeDynReloc(uint32_t Type, const SymbolBody &S) const {
254 return false;
255}
256
George Rimar6713cf82015-11-25 21:46:05 +0000257unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
George Rimar25411f252015-12-04 11:20:13 +0000258 uint32_t Type, uint64_t P, uint64_t SA,
259 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000260 return 0;
261}
George Rimar77d1cb12015-11-24 09:00:06 +0000262
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000263void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
264
Igor Kudrin351b41d2015-11-16 17:44:08 +0000265void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
266
Rafael Espindola7f074422015-09-22 21:35:51 +0000267X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000268 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000269 PCRelReloc = R_386_PC32;
270 GotReloc = R_386_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000271 PltReloc = R_386_JUMP_SLOT;
George Rimara07ff662015-12-21 10:12:06 +0000272 IRelativeReloc = R_386_IRELATIVE;
George Rimar6f17e092015-12-17 09:32:21 +0000273 RelativeReloc = R_386_RELATIVE;
George Rimar9db204a2015-12-02 09:58:20 +0000274 TlsGotReloc = R_386_TLS_TPOFF;
275 TlsGlobalDynamicReloc = R_386_TLS_GD;
276 TlsLocalDynamicReloc = R_386_TLS_LDM;
277 TlsModuleIndexReloc = R_386_TLS_DTPMOD32;
278 TlsOffsetReloc = R_386_TLS_DTPOFF32;
George Rimar77b77792015-11-25 22:15:01 +0000279 LazyRelocations = true;
280 PltEntrySize = 16;
281 PltZeroEntrySize = 16;
282}
283
284void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
285 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
286}
287
288void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
289 // Skip 6 bytes of "pushl (GOT+4)"
290 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000291}
Rafael Espindola01205f72015-09-22 18:19:46 +0000292
George Rimard23970f2015-11-25 20:41:53 +0000293unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
294 if (Type == R_386_TLS_LE)
295 return R_386_TLS_TPOFF;
296 if (Type == R_386_TLS_LE_32)
297 return R_386_TLS_TPOFF32;
298 return Type;
299}
300
George Rimar6f17e092015-12-17 09:32:21 +0000301unsigned X86TargetInfo::getTlsGotReloc(unsigned Type) const {
302 if (Type == R_386_TLS_IE)
303 return Type;
304 return TlsGotReloc;
305}
306
307bool X86TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000308 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
309 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000310 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000311 if (Type == R_386_TLS_IE)
312 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000313 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000314}
315
George Rimar648a2c32015-10-20 08:54:27 +0000316void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar77b77792015-11-25 22:15:01 +0000317 uint64_t PltEntryAddr) const {
318 // Executable files and shared object files have
319 // separate procedure linkage tables.
320 if (Config->Shared) {
321 const uint8_t V[] = {
322 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx
323 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
324 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
325 };
326 memcpy(Buf, V, sizeof(V));
327 return;
328 }
George Rimar648a2c32015-10-20 08:54:27 +0000329
George Rimar77b77792015-11-25 22:15:01 +0000330 const uint8_t PltData[] = {
331 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
332 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
333 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop
334 };
335 memcpy(Buf, PltData, sizeof(PltData));
336 write32le(Buf + 2, GotEntryAddr + 4); // GOT+4
337 write32le(Buf + 8, GotEntryAddr + 8); // GOT+8
338}
339
340void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
341 uint64_t GotEntryAddr, uint64_t PltEntryAddr,
342 int32_t Index, unsigned RelOff) const {
343 const uint8_t Inst[] = {
344 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
345 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
346 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
347 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000348 memcpy(Buf, Inst, sizeof(Inst));
George Rimar77b77792015-11-25 22:15:01 +0000349 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
350 Buf[1] = Config->Shared ? 0xa3 : 0x25;
351 write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr);
352 write32le(Buf + 7, RelOff);
353 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000354}
355
Rui Ueyama02dfd492015-12-17 01:18:40 +0000356bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000357 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
358 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
359 return SS->Sym.getType() == STT_OBJECT;
360 return false;
361}
362
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000363bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000364 if (S.isTls() && Type == R_386_TLS_GD)
George Rimar2558e122015-12-09 09:55:54 +0000365 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000366 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
George Rimar2558e122015-12-09 09:55:54 +0000367 return !isTlsOptimized(Type, &S);
368 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000369}
370
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000371bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000372 return isGnuIFunc<ELF32LE>(S) ||
373 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000374 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000375}
376
George Rimarbfb7bf72015-12-21 10:00:12 +0000377bool X86TargetInfo::isGotRelative(uint32_t Type) const {
378 // This relocation does not require got entry,
379 // but it is relative to got and needs it to be created.
380 // Here we request for that.
381 return Type == R_386_GOTOFF;
382}
383
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000384void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000385 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000386 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000387 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000388 case R_386_32:
389 add32le(Loc, SA);
390 break;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000391 case R_386_GOT32:
George Rimarbfb7bf72015-12-21 10:00:12 +0000392 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000393 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000394 break;
George Rimar13934772015-11-25 20:20:31 +0000395 case R_386_GOTPC:
396 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
397 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000398 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000399 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000400 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000401 break;
George Rimar9db204a2015-12-02 09:58:20 +0000402 case R_386_TLS_GD:
403 case R_386_TLS_LDM:
404 case R_386_TLS_TPOFF: {
405 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
406 Out<ELF32LE>::Got->getNumEntries() * 4;
407 checkInt<32>(V, Type);
408 write32le(Loc, V);
409 break;
410 }
George Rimar6f17e092015-12-17 09:32:21 +0000411 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000412 case R_386_TLS_LDO_32:
413 write32le(Loc, SA);
414 break;
George Rimard23970f2015-11-25 20:41:53 +0000415 case R_386_TLS_LE:
416 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
417 break;
418 case R_386_TLS_LE_32:
419 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
420 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000421 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000422 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000423 }
424}
425
George Rimar2558e122015-12-09 09:55:54 +0000426bool X86TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000427 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000428 return false;
429 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
430 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000431 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000432 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
433}
434
George Rimar6f17e092015-12-17 09:32:21 +0000435bool X86TargetInfo::relocNeedsDynRelative(unsigned Type) const {
436 return Config->Shared && Type == R_386_TLS_IE;
437}
438
George Rimar2558e122015-12-09 09:55:54 +0000439unsigned X86TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
440 uint32_t Type, uint64_t P,
441 uint64_t SA,
442 const SymbolBody &S) const {
443 switch (Type) {
444 case R_386_TLS_GD:
445 if (canBePreempted(&S, true))
446 relocateTlsGdToIe(Loc, BufEnd, P, SA);
447 else
448 relocateTlsGdToLe(Loc, BufEnd, P, SA);
449 // The next relocation should be against __tls_get_addr, so skip it
450 return 1;
451 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000452 case R_386_TLS_IE:
453 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000454 return 0;
455 case R_386_TLS_LDM:
456 relocateTlsLdToLe(Loc, BufEnd, P, SA);
457 // The next relocation should be against __tls_get_addr, so skip it
458 return 1;
459 case R_386_TLS_LDO_32:
460 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
461 return 0;
462 }
463 llvm_unreachable("Unknown TLS optimization");
464}
465
466// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
467// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
468// how GD can be optimized to IE:
469// leal x@tlsgd(, %ebx, 1),
470// call __tls_get_addr@plt
471// Is converted to:
472// movl %gs:0, %eax
473// addl x@gotntpoff(%ebx), %eax
474void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
475 uint64_t SA) const {
476 const uint8_t Inst[] = {
477 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
478 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
479 };
480 memcpy(Loc - 3, Inst, sizeof(Inst));
481 relocateOne(Loc + 5, BufEnd, R_386_32, P,
482 SA - Out<ELF32LE>::Got->getVA() -
483 Out<ELF32LE>::Got->getNumEntries() * 4);
484}
485
486// GD can be optimized to LE:
487// leal x@tlsgd(, %ebx, 1),
488// call __tls_get_addr@plt
489// Can be converted to:
490// movl %gs:0,%eax
491// addl $x@ntpoff,%eax
492// But gold emits subl $foo@tpoff,%eax instead of addl.
493// These instructions are completely equal in behavior.
494// This method generates subl to be consistent with gold.
495void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
496 uint64_t SA) const {
497 const uint8_t Inst[] = {
498 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
499 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
500 };
501 memcpy(Loc - 3, Inst, sizeof(Inst));
502 relocateOne(Loc + 5, BufEnd, R_386_32, P,
503 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
504}
505
506// LD can be optimized to LE:
507// leal foo(%reg),%eax
508// call ___tls_get_addr
509// Is converted to:
510// movl %gs:0,%eax
511// nop
512// leal 0(%esi,1),%esi
513void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
514 uint64_t SA) const {
515 const uint8_t Inst[] = {
516 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
517 0x90, // nop
518 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
519 };
520 memcpy(Loc - 2, Inst, sizeof(Inst));
521}
522
George Rimar6f17e092015-12-17 09:32:21 +0000523// In some conditions, relocations can be optimized to avoid using GOT.
524// This function does that for Initial Exec to Local Exec case.
525// Read "ELF Handling For Thread-Local Storage, 5.1
526// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000527// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000528void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
529 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000530 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000531 // Ulrich's document section 6.2 says that @gotntpoff can
532 // be used with MOVL or ADDL instructions.
533 // @indntpoff is similar to @gotntpoff, but for use in
534 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000535 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000536 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000537 uint8_t Reg = (Loc[-1] >> 3) & 7;
538 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000539 if (Type == R_386_TLS_IE) {
540 // For R_386_TLS_IE relocation we perform the next transformations:
541 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
542 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
543 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
544 // First one is special because when EAX is used the sequence is 5 bytes
545 // long, otherwise it is 6 bytes.
546 if (*Op == 0xa1) {
547 *Op = 0xb8;
548 } else {
549 *Inst = IsMov ? 0xc7 : 0x81;
550 *Op = 0xc0 | ((*Op >> 3) & 7);
551 }
552 } else {
553 // R_386_TLS_GOTIE relocation can be optimized to
554 // R_386_TLS_LE so that it does not use GOT.
555 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
556 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
557 // Note: gold converts to ADDL instead of LEAL.
558 *Inst = IsMov ? 0xc7 : 0x8d;
559 if (IsMov)
560 *Op = 0xc0 | ((*Op >> 3) & 7);
561 else
562 *Op = 0x80 | Reg | (Reg << 3);
563 }
George Rimar2558e122015-12-09 09:55:54 +0000564 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
565}
566
Rafael Espindola7f074422015-09-22 21:35:51 +0000567X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000568 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000569 PCRelReloc = R_X86_64_PC32;
570 GotReloc = R_X86_64_GLOB_DAT;
George Rimar648a2c32015-10-20 08:54:27 +0000571 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000572 RelativeReloc = R_X86_64_RELATIVE;
George Rimara07ff662015-12-21 10:12:06 +0000573 IRelativeReloc = R_X86_64_IRELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000574 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000575 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000576 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000577 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000578 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000579 LazyRelocations = true;
580 PltEntrySize = 16;
581 PltZeroEntrySize = 16;
582}
583
Igor Kudrin351b41d2015-11-16 17:44:08 +0000584void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
585 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
586}
587
George Rimar648a2c32015-10-20 08:54:27 +0000588void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
589 // Skip 6 bytes of "jmpq *got(%rip)"
590 write32le(Buf, Plt + 6);
591}
592
593void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
594 uint64_t PltEntryAddr) const {
595 const uint8_t PltData[] = {
596 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
597 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
598 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
599 };
600 memcpy(Buf, PltData, sizeof(PltData));
601 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
602 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000603}
Rafael Espindola01205f72015-09-22 18:19:46 +0000604
George Rimar77b77792015-11-25 22:15:01 +0000605void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
606 uint64_t GotEntryAddr,
607 uint64_t PltEntryAddr, int32_t Index,
608 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000609 const uint8_t Inst[] = {
610 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
611 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
612 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
613 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000614 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000615
George Rimar648a2c32015-10-20 08:54:27 +0000616 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
617 write32le(Buf + 7, Index);
618 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000619}
620
Rui Ueyama02dfd492015-12-17 01:18:40 +0000621bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000622 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
623 Type == R_X86_64_64)
624 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
625 return SS->Sym.getType() == STT_OBJECT;
626 return false;
627}
628
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000629bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000630 if (Type == R_X86_64_TLSGD)
631 return Target->isTlsOptimized(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000632 if (Type == R_X86_64_GOTTPOFF)
George Rimar6713cf82015-11-25 21:46:05 +0000633 return !isTlsOptimized(Type, &S);
George Rimar25411f252015-12-04 11:20:13 +0000634 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000635}
636
George Rimar6f17e092015-12-17 09:32:21 +0000637bool X86_64TargetInfo::isTlsDynReloc(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000638 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000639}
640
Igor Kudrine7ad0932015-11-17 17:47:53 +0000641unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000642 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000643 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000644 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000645}
646
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000647bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000648 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000649 return false;
George Rimara07ff662015-12-21 10:12:06 +0000650 if (isGnuIFunc<ELF64LE>(S))
651 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000652
Rafael Espindola01205f72015-09-22 18:19:46 +0000653 switch (Type) {
654 default:
655 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000656 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000657 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000658 case R_X86_64_PC32:
659 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000660 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000661 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000662 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000663 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000664 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
665 // libc.so.
666 //
667 // The general idea on how to handle such cases is to create a PLT entry
668 // and use that as the function value.
669 //
670 // For the static linking part, we just return true and everything else
671 // will use the the PLT entry as the address.
672 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000673 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000674 // still works. We need the help of the dynamic linker for that. We
675 // let it know that we have a direct reference to a so symbol by creating
676 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000677 // dynamic linker resolves the symbol to the value of the symbol we created.
678 // This is true even for got entries, so pointer equality is maintained.
679 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000680 // real function is a dedicated got entry used by the plt. That is
681 // identified by special relocation types (R_X86_64_JUMP_SLOT,
682 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000683 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000684 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000685 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000686 }
687}
Rafael Espindolac4010882015-09-22 20:54:08 +0000688
Rafael Espindolaae244002015-10-05 19:30:12 +0000689bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
690 switch (Type) {
691 default:
692 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000693 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000694 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000695 case R_X86_64_PC8:
696 case R_X86_64_PC16:
697 case R_X86_64_PC32:
698 case R_X86_64_PC64:
699 case R_X86_64_PLT32:
George Rimar48651482015-12-11 08:59:37 +0000700 case R_X86_64_SIZE32:
701 case R_X86_64_SIZE64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000702 return true;
703 }
704}
705
George Rimar48651482015-12-11 08:59:37 +0000706bool X86_64TargetInfo::isSizeDynReloc(uint32_t Type,
707 const SymbolBody &S) const {
708 return (Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64) &&
709 canBePreempted(&S, false);
710}
711
George Rimar77d1cb12015-11-24 09:00:06 +0000712bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
George Rimar6713cf82015-11-25 21:46:05 +0000713 const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000714 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000715 return false;
George Rimar25411f252015-12-04 11:20:13 +0000716 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
717 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000718 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
719}
720
721// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
722// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
723// how LD can be optimized to LE:
724// leaq bar@tlsld(%rip), %rdi
725// callq __tls_get_addr@PLT
726// leaq bar@dtpoff(%rax), %rcx
727// Is converted to:
728// .word 0x6666
729// .byte 0x66
730// mov %fs:0,%rax
731// leaq bar@tpoff(%rax), %rcx
732void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
733 uint64_t P, uint64_t SA) const {
734 const uint8_t Inst[] = {
735 0x66, 0x66, //.word 0x6666
736 0x66, //.byte 0x66
737 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
738 };
739 memcpy(Loc - 3, Inst, sizeof(Inst));
740}
741
742// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
743// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
744// how GD can be optimized to LE:
745// .byte 0x66
746// leaq x@tlsgd(%rip), %rdi
747// .word 0x6666
748// rex64
749// call __tls_get_addr@plt
750// Is converted to:
751// mov %fs:0x0,%rax
752// lea x@tpoff,%rax
753void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
754 uint64_t P, uint64_t SA) const {
755 const uint8_t Inst[] = {
756 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
757 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
758 };
759 memcpy(Loc - 4, Inst, sizeof(Inst));
760 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000761}
762
George Rimar25411f252015-12-04 11:20:13 +0000763// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
764// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
765// how GD can be optimized to IE:
766// .byte 0x66
767// leaq x@tlsgd(%rip), %rdi
768// .word 0x6666
769// rex64
770// call __tls_get_addr@plt
771// Is converted to:
772// mov %fs:0x0,%rax
773// addq x@tpoff,%rax
774void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
775 uint64_t P, uint64_t SA) const {
776 const uint8_t Inst[] = {
777 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
778 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
779 };
780 memcpy(Loc - 4, Inst, sizeof(Inst));
781 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
782}
783
George Rimar77d1cb12015-11-24 09:00:06 +0000784// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000785// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000786// This function does that. Read "ELF Handling For Thread-Local Storage,
787// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
788// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000789void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
790 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000791 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
792 // used in MOVQ or ADDQ instructions only.
793 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
794 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
795 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
796 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
797 uint8_t *Prefix = Loc - 3;
798 uint8_t *Inst = Loc - 2;
799 uint8_t *RegSlot = Loc - 1;
800 uint8_t Reg = Loc[-1] >> 3;
801 bool IsMov = *Inst == 0x8b;
802 bool RspAdd = !IsMov && Reg == 4;
803 // r12 and rsp registers requires special handling.
804 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
805 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
806 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
807 // The same true for rsp. So we convert to addq for them, saving 1 byte that
808 // we dont have.
809 if (RspAdd)
810 *Inst = 0x81;
811 else
812 *Inst = IsMov ? 0xc7 : 0x8d;
813 if (*Prefix == 0x4c)
814 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
815 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
816 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
817}
818
George Rimar6713cf82015-11-25 21:46:05 +0000819// This function applies a TLS relocation with an optimization as described
820// in the Ulrich's document. As a result of rewriting instructions at the
821// relocation target, relocations immediately follow the TLS relocation (which
822// would be applied to rewritten instructions) may have to be skipped.
823// This function returns a number of relocations that need to be skipped.
824unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
825 uint32_t Type, uint64_t P,
George Rimar25411f252015-12-04 11:20:13 +0000826 uint64_t SA,
827 const SymbolBody &S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000828 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000829 case R_X86_64_DTPOFF32:
830 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
831 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000832 case R_X86_64_GOTTPOFF:
833 relocateTlsIeToLe(Loc, BufEnd, P, SA);
834 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000835 case R_X86_64_TLSGD: {
836 if (canBePreempted(&S, true))
837 relocateTlsGdToIe(Loc, BufEnd, P, SA);
838 else
839 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000840 // The next relocation should be against __tls_get_addr, so skip it
841 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000842 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000843 case R_X86_64_TLSLD:
844 relocateTlsLdToLe(Loc, BufEnd, P, SA);
845 // The next relocation should be against __tls_get_addr, so skip it
846 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000847 }
848 llvm_unreachable("Unknown TLS optimization");
849}
850
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000851void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000852 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000853 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000854 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000855 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000856 checkUInt<32>(SA, Type);
857 write32le(Loc, SA);
858 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000859 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000860 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000861 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000862 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000863 case R_X86_64_64:
864 write64le(Loc, SA);
865 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000866 case R_X86_64_DTPOFF32:
867 write32le(Loc, SA);
868 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000869 case R_X86_64_DTPOFF64:
870 write64le(Loc, SA);
871 break;
872 case R_X86_64_GOTPCREL:
873 case R_X86_64_PC32:
874 case R_X86_64_PLT32:
875 case R_X86_64_TLSGD:
876 case R_X86_64_TLSLD:
877 write32le(Loc, SA - P);
878 break;
George Rimar48651482015-12-11 08:59:37 +0000879 case R_X86_64_SIZE32:
880 write32le(Loc, ZA);
881 break;
882 case R_X86_64_SIZE64:
883 write64le(Loc, ZA);
884 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000885 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000886 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000887 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000888 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000889 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000890 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000891 case R_X86_64_TPOFF64:
892 write32le(Loc, SA - P);
893 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000894 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000895 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000896 }
897}
898
Hal Finkel3c8cc672015-10-12 20:56:18 +0000899// Relocation masks following the #lo(value), #hi(value), #ha(value),
900// #higher(value), #highera(value), #highest(value), and #highesta(value)
901// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
902// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000903static uint16_t applyPPCLo(uint64_t V) { return V; }
904static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
905static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
906static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
907static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000908static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000909static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
910
Rafael Espindolac4010882015-09-22 20:54:08 +0000911PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000912 PCRelReloc = R_PPC64_REL24;
913 GotReloc = R_PPC64_GLOB_DAT;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000914 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000915 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000916
917 // We need 64K pages (at least under glibc/Linux, the loader won't
918 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000919 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000920
921 // The PPC64 ELF ABI v1 spec, says:
922 //
923 // It is normally desirable to put segments with different characteristics
924 // in separate 256 Mbyte portions of the address space, to give the
925 // operating system full paging flexibility in the 64-bit address space.
926 //
927 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
928 // use 0x10000000 as the starting address.
929 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000930}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000931
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000932uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000933 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
934 // order. The TOC starts where the first of these sections starts.
935
936 // FIXME: This obviously does not do the right thing when there is no .got
937 // section, but there is a .toc or .tocbss section.
938 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
939 if (!TocVA)
940 TocVA = Out<ELF64BE>::Plt->getVA();
941
942 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
943 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
944 // code (crt1.o) assumes that you can get from the TOC base to the
945 // start of the .toc section with only a single (signed) 16-bit relocation.
946 return TocVA + 0x8000;
947}
948
George Rimar648a2c32015-10-20 08:54:27 +0000949void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
950void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
951 uint64_t PltEntryAddr) const {}
George Rimar77b77792015-11-25 22:15:01 +0000952void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
953 uint64_t GotEntryAddr,
954 uint64_t PltEntryAddr, int32_t Index,
955 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000956 uint64_t Off = GotEntryAddr - getPPC64TocBase();
957
958 // FIXME: What we should do, in theory, is get the offset of the function
959 // descriptor in the .opd section, and use that as the offset from %r2 (the
960 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
961 // be a pointer to the function descriptor in the .opd section. Using
962 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
963
Hal Finkelfa92f682015-10-13 21:47:34 +0000964 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000965 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
966 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
967 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
968 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
969 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
970 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
971 write32be(Buf + 28, 0x4e800420); // bctr
972}
973
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000974bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000975 if (relocNeedsPlt(Type, S))
976 return true;
977
978 switch (Type) {
979 default: return false;
980 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000981 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000982 case R_PPC64_GOT16_HA:
983 case R_PPC64_GOT16_HI:
984 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +0000985 case R_PPC64_GOT16_LO_DS:
986 return true;
987 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000988}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000989
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000990bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000991 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000992 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000993}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000994
Hal Finkelbe0823d2015-10-12 20:58:52 +0000995bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
996 switch (Type) {
997 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000998 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000999 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001000 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001001 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001002 }
1003}
1004
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001005void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001006 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001007 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001008 uint64_t TB = getPPC64TocBase();
1009
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1011 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001012 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001013 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1014 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001015 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1016 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001017 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1018 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019 default: break;
1020 }
1021
Hal Finkel3c8cc672015-10-12 20:56:18 +00001022 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001023 case R_PPC64_ADDR14: {
1024 checkAlignment<4>(SA, Type);
1025 // Preserve the AA/LK bits in the branch instruction
1026 uint8_t AALK = Loc[3];
1027 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1028 break;
1029 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001031 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001032 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001033 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001034 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001035 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001036 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001037 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001038 case R_PPC64_ADDR16_HA:
1039 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001040 break;
1041 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001042 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001044 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001045 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001046 break;
1047 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001048 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001049 break;
1050 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001051 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001052 break;
1053 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001054 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001056 case R_PPC64_ADDR16_LO:
1057 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001059 case R_PPC64_ADDR16_LO_DS:
1060 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 break;
1062 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001063 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001064 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001066 case R_PPC64_ADDR64:
1067 write64be(Loc, SA);
1068 break;
1069 case R_PPC64_REL16_HA:
1070 write16be(Loc, applyPPCHa(SA - P));
1071 break;
1072 case R_PPC64_REL16_HI:
1073 write16be(Loc, applyPPCHi(SA - P));
1074 break;
1075 case R_PPC64_REL16_LO:
1076 write16be(Loc, applyPPCLo(SA - P));
1077 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001079 // If we have an undefined weak symbol, we might get here with a symbol
1080 // address of zero. That could overflow, but the code must be unreachable,
1081 // so don't bother doing anything at all.
1082 if (!SA)
1083 break;
1084
Hal Finkeldaedc122015-10-12 23:16:53 +00001085 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1086 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001087 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001088
1089 if (!InPlt && Out<ELF64BE>::Opd) {
1090 // If this is a local call, and we currently have the address of a
1091 // function-descriptor, get the underlying code address instead.
1092 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1093 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001094 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001095
1096 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001097 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001098 }
1099
Hal Finkel3c8cc672015-10-12 20:56:18 +00001100 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001101 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001102 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001103
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001104 uint32_t Nop = 0x60000000;
1105 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1106 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001107 break;
1108 }
1109 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001110 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001111 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001112 break;
1113 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001114 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001116 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001117 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001118 break;
1119 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001120 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001121 }
1122}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001123
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001124AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin9606d192015-12-03 08:05:35 +00001125 CopyReloc = R_AARCH64_COPY;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001126 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001127 PltReloc = R_AARCH64_JUMP_SLOT;
1128 LazyRelocations = true;
1129 PltEntrySize = 16;
1130 PltZeroEntrySize = 32;
1131}
George Rimar648a2c32015-10-20 08:54:27 +00001132
Igor Kudrincfe47f52015-12-05 06:20:24 +00001133unsigned AArch64TargetInfo::getDynReloc(unsigned Type) const {
1134 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1135 return Type;
1136 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
1137 error("Relocation " + S + " cannot be used when making a shared object; "
1138 "recompile with -fPIC.");
1139}
1140
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001141unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
1142
1143void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
1144 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1145}
1146
George Rimar648a2c32015-10-20 08:54:27 +00001147void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001148 uint64_t PltEntryAddr) const {
1149 const uint8_t PltData[] = {
1150 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1151 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1152 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1153 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1154 0x20, 0x02, 0x1f, 0xd6, // br x17
1155 0x1f, 0x20, 0x03, 0xd5, // nop
1156 0x1f, 0x20, 0x03, 0xd5, // nop
1157 0x1f, 0x20, 0x03, 0xd5 // nop
1158 };
1159 memcpy(Buf, PltData, sizeof(PltData));
1160
1161 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
1162 GotEntryAddr + 16);
1163 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
1164 GotEntryAddr + 16);
1165 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
1166 GotEntryAddr + 16);
1167}
1168
George Rimar77b77792015-11-25 22:15:01 +00001169void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1170 uint64_t GotEntryAddr,
1171 uint64_t PltEntryAddr, int32_t Index,
1172 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001173 const uint8_t Inst[] = {
1174 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1175 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1176 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1177 0x20, 0x02, 0x1f, 0xd6 // br x17
1178 };
1179 memcpy(Buf, Inst, sizeof(Inst));
1180
1181 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1182 GotEntryAddr);
1183 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1184 GotEntryAddr);
1185 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1186 GotEntryAddr);
1187}
1188
Rui Ueyama02dfd492015-12-17 01:18:40 +00001189bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001190 if (Config->Shared)
1191 return false;
1192 switch (Type) {
1193 default:
1194 return false;
1195 case R_AARCH64_ABS16:
1196 case R_AARCH64_ABS32:
1197 case R_AARCH64_ABS64:
1198 case R_AARCH64_ADD_ABS_LO12_NC:
1199 case R_AARCH64_ADR_PREL_LO21:
1200 case R_AARCH64_ADR_PREL_PG_HI21:
1201 case R_AARCH64_LDST8_ABS_LO12_NC:
1202 case R_AARCH64_LDST32_ABS_LO12_NC:
1203 case R_AARCH64_LDST64_ABS_LO12_NC:
1204 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1205 return SS->Sym.getType() == STT_OBJECT;
1206 return false;
1207 }
1208}
1209
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001210bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
1211 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001212 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
1213 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001214}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001215
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001216bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
1217 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001218 switch (Type) {
1219 default:
1220 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001221 case R_AARCH64_CALL26:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001222 case R_AARCH64_JUMP26:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001223 return canBePreempted(&S, true);
1224 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001225}
Davide Italiano1d750a62015-09-27 08:45:38 +00001226
Davide Italianoef4be6b2015-10-06 19:01:32 +00001227static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001228 uint32_t ImmLo = (Imm & 0x3) << 29;
1229 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1230 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001231 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001232}
1233
Davide Italiano318ca222015-10-02 22:13:51 +00001234// Page(Expr) is the page address of the expression Expr, defined
1235// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001236// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001237static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001238 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001239}
1240
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001241void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001242 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001243 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001244 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001245 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001246 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001247 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001248 break;
1249 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001250 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001251 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001252 break;
1253 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001254 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001255 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001256 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001257 // This relocation stores 12 bits and there's no instruction
1258 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001259 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1260 // bits in Loc are zero.
1261 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001262 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001263 case R_AARCH64_ADR_GOT_PAGE: {
1264 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1265 checkInt<33>(X, Type);
1266 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1267 break;
1268 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001269 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001270 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001271 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001272 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001273 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001274 }
1275 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001276 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001277 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001278 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001279 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001280 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001281 case R_AARCH64_CALL26:
1282 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001283 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001284 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001285 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1286 break;
1287 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001288 case R_AARCH64_LD64_GOT_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001289 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001290 or32le(Loc, (SA & 0xFF8) << 7);
1291 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001292 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001293 or32le(Loc, (SA & 0xFFF) << 10);
1294 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001295 case R_AARCH64_LDST32_ABS_LO12_NC:
1296 or32le(Loc, (SA & 0xFFC) << 8);
1297 break;
1298 case R_AARCH64_LDST64_ABS_LO12_NC:
1299 or32le(Loc, (SA & 0xFF8) << 7);
1300 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001301 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001302 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001303 write16le(Loc, SA - P);
1304 break;
1305 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001306 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001307 write32le(Loc, SA - P);
1308 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001309 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001310 write64le(Loc, SA - P);
1311 break;
Davide Italiano1d750a62015-09-27 08:45:38 +00001312 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001313 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001314 }
1315}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001316
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001317template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001318 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001319 GotHeaderEntriesNum = 2;
1320}
1321
1322template <class ELFT>
1323void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001324 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001325 auto *P = reinterpret_cast<Elf_Off *>(Buf);
1326 // Module pointer
1327 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +00001328}
1329
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001330template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +00001331void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
1332template <class ELFT>
1333void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
1334 uint64_t PltEntryAddr) const {}
1335template <class ELFT>
George Rimar77b77792015-11-25 22:15:01 +00001336void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr,
1337 uint64_t GotEntryAddr,
1338 uint64_t PltEntryAddr, int32_t Index,
1339 unsigned RelOff) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001340
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001341template <class ELFT>
1342bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
1343 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001344 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001345}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001346
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001347template <class ELFT>
1348bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
1349 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001350 return false;
1351}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001352
Simon Atanasyan35031192015-12-15 06:06:34 +00001353static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001354
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001355template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001356void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001357 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001358 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001359 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001360 switch (Type) {
1361 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001362 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001363 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001364 case R_MIPS_CALL16:
1365 case R_MIPS_GOT16: {
1366 int64_t V = SA - getMipsGpAddr<ELFT>();
1367 if (Type == R_MIPS_GOT16)
1368 checkInt<16>(V, Type);
1369 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1370 break;
1371 }
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001372 case R_MIPS_HI16: {
1373 uint32_t Instr = read32<E>(Loc);
1374 if (PairedLoc) {
1375 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001376 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001377 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001378 } else {
1379 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001380 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001381 }
1382 break;
1383 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001384 case R_MIPS_JALR:
1385 // Ignore this optimization relocation for now
1386 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001387 case R_MIPS_LO16: {
1388 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001389 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001390 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1391 break;
1392 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001393 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +00001394 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001395 }
1396}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001397
Rui Ueyama24e39522015-12-03 20:57:45 +00001398template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001399 const unsigned GPOffset = 0x7ff0;
1400 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
1401}
1402
1403template uint32_t getMipsGpAddr<ELF32LE>();
1404template uint32_t getMipsGpAddr<ELF32BE>();
1405template uint64_t getMipsGpAddr<ELF64LE>();
1406template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001407}
1408}