blob: b77f2116f132bb1da69a823d946b5f490c190eb5 [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
34namespace elf2 {
35
36std::unique_ptr<TargetInfo> Target;
37
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000049 fatal("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050}
51
52template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
53 if (isUInt<N>(V))
54 return;
55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000056 fatal("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000057}
58
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
60 if (isInt<N>(V) || isUInt<N>(V))
61 return;
62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000063 fatal("Relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064}
65
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
67 if ((V & (N - 1)) == 0)
68 return;
69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +000070 fatal("Improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
George Rimara07ff662015-12-21 10:12:06 +000073template <class ELFT> bool isGnuIFunc(const SymbolBody &S) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000074 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S))
George Rimara07ff662015-12-21 10:12:06 +000075 return SS->Sym.getType() == STT_GNU_IFUNC;
76 return false;
77}
78
79template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
80template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
81template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
82template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
83
Rui Ueyamaefc23de2015-10-14 21:30:32 +000084namespace {
85class X86TargetInfo final : public TargetInfo {
86public:
87 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000088 void writeGotPltHeader(uint8_t *Buf) const override;
89 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000090 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000091 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
92 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000093 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000094 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
95 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000096 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000097 bool needsDynRelative(unsigned Type) const override;
98 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
99 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000100 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000101 uint64_t SA, uint64_t ZA = 0,
102 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000103 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000104 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
105 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000106 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000107
108private:
109 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
110 uint64_t SA) const;
111 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
112 uint64_t SA) const;
113 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
114 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000115 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
116 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000117};
118
119class X86_64TargetInfo final : public TargetInfo {
120public:
121 X86_64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000122 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
123 void writeGotPltHeader(uint8_t *Buf) const override;
124 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000125 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000126 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
127 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000128 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000129 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
130 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000131 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000132 uint64_t SA, uint64_t ZA = 0,
133 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000135 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000136 bool isSizeRel(uint32_t Type) const override;
137 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
138 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000139
140private:
141 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
142 uint64_t SA) const;
143 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000145 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000147 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000149};
150
Davide Italiano8c3444362016-01-11 19:45:33 +0000151class PPCTargetInfo final : public TargetInfo {
152public:
153 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
155 uint64_t SA, uint64_t ZA = 0,
156 uint8_t *PairedLoc = nullptr) const override;
157 bool isRelRelative(uint32_t Type) const override;
158};
159
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000160class PPC64TargetInfo final : public TargetInfo {
161public:
162 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
164 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000165 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
166 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000167 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000168 uint64_t SA, uint64_t ZA = 0,
169 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170 bool isRelRelative(uint32_t Type) const override;
171};
172
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173class AArch64TargetInfo final : public TargetInfo {
174public:
175 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000176 unsigned getDynRel(unsigned Type) const override;
177 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000178 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000179 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
180 int32_t Index, unsigned RelOff) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +0000181 unsigned getTlsGotRel(unsigned Type = -1) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000182 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000183 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000184 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
185 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000186 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000187 uint64_t SA, uint64_t ZA = 0,
188 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000189};
190
Tom Stellard80efb162016-01-07 03:59:08 +0000191class AMDGPUTargetInfo final : public TargetInfo {
192public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000193 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000194 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
195 uint64_t SA, uint64_t ZA = 0,
196 uint8_t *PairedLoc = nullptr) const override;
197};
198
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000199template <class ELFT> class MipsTargetInfo final : public TargetInfo {
200public:
201 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000202 unsigned getDynRel(unsigned Type) const override;
203 void writeGotHeader(uint8_t *Buf) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000204 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
205 bool needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000206 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000207 uint64_t SA, uint64_t ZA = 0,
208 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000209 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000210 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000211};
212} // anonymous namespace
213
Rui Ueyama91004392015-10-13 16:08:15 +0000214TargetInfo *createTarget() {
215 switch (Config->EMachine) {
216 case EM_386:
217 return new X86TargetInfo();
218 case EM_AARCH64:
219 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000220 case EM_AMDGPU:
221 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000222 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000223 switch (Config->EKind) {
224 case ELF32LEKind:
225 return new MipsTargetInfo<ELF32LE>();
226 case ELF32BEKind:
227 return new MipsTargetInfo<ELF32BE>();
228 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000229 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000230 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000231 case EM_PPC:
232 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000233 case EM_PPC64:
234 return new PPC64TargetInfo();
235 case EM_X86_64:
236 return new X86_64TargetInfo();
237 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000238 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000239}
240
Rafael Espindola01205f72015-09-22 18:19:46 +0000241TargetInfo::~TargetInfo() {}
242
Rui Ueyamabaf16512016-01-29 00:20:12 +0000243bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000244 return false;
245}
246
Igor Kudrinf6f45472015-11-10 08:39:27 +0000247uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
248
Rui Ueyama02dfd492015-12-17 01:18:40 +0000249bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000250 return false;
251}
252
Rui Ueyama012eb782016-01-29 04:05:09 +0000253bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
254 return Type == TlsLocalDynamicRel;
255}
256
257bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
258 return Type == TlsGlobalDynamicRel;
259}
260
261bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
262 return false;
263}
264
George Rimarbfb7bf72015-12-21 10:00:12 +0000265bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000266bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000267bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000268bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000269
Rui Ueyama012eb782016-01-29 04:05:09 +0000270bool TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
271 return false;
272}
273
274bool TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
275 return false;
276}
277
Rui Ueyamac516ae12016-01-29 02:33:45 +0000278unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
279 uint64_t P, uint64_t SA,
280 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000281 return 0;
282}
George Rimar77d1cb12015-11-24 09:00:06 +0000283
Rafael Espindola7f074422015-09-22 21:35:51 +0000284X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000285 CopyRel = R_386_COPY;
286 GotRel = R_386_GLOB_DAT;
287 PltRel = R_386_JUMP_SLOT;
288 IRelativeRel = R_386_IRELATIVE;
289 RelativeRel = R_386_RELATIVE;
290 TlsGotRel = R_386_TLS_TPOFF;
291 TlsGlobalDynamicRel = R_386_TLS_GD;
292 TlsLocalDynamicRel = R_386_TLS_LDM;
293 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
294 TlsOffsetRel = R_386_TLS_DTPOFF32;
295 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000296 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000297 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000298}
299
Rui Ueyamac516ae12016-01-29 02:33:45 +0000300void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000301 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
302}
303
Rui Ueyamac516ae12016-01-29 02:33:45 +0000304void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000305 // Entries in .got.plt initially points back to the corresponding
306 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000307 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000308}
Rafael Espindola01205f72015-09-22 18:19:46 +0000309
Rui Ueyamac516ae12016-01-29 02:33:45 +0000310unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000311 if (Type == R_386_TLS_LE)
312 return R_386_TLS_TPOFF;
313 if (Type == R_386_TLS_LE_32)
314 return R_386_TLS_TPOFF32;
315 return Type;
316}
317
Rui Ueyama724d6252016-01-29 01:49:32 +0000318unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000319 if (Type == R_386_TLS_IE)
320 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000321 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000322}
323
Rui Ueyamac516ae12016-01-29 02:33:45 +0000324bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000325 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
326 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000327 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000328 if (Type == R_386_TLS_IE)
329 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000330 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000331}
332
Rui Ueyama900e2d22016-01-29 03:51:49 +0000333void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000334 // Executable files and shared object files have
335 // separate procedure linkage tables.
336 if (Config->Shared) {
337 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000338 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000339 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
340 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000341 };
342 memcpy(Buf, V, sizeof(V));
343 return;
344 }
George Rimar648a2c32015-10-20 08:54:27 +0000345
George Rimar77b77792015-11-25 22:15:01 +0000346 const uint8_t PltData[] = {
347 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000348 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
349 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000350 };
351 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000352 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000353 write32le(Buf + 2, Got + 4);
354 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000355}
356
Rui Ueyama9398f862016-01-29 04:15:02 +0000357void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
358 uint64_t PltEntryAddr, int32_t Index,
359 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000360 const uint8_t Inst[] = {
361 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
362 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
363 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
364 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000365 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000366
George Rimar77b77792015-11-25 22:15:01 +0000367 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
368 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000369 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
370 : Out<ELF32LE>::Got->getVA();
371 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000372 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000373 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000374}
375
Rui Ueyama02dfd492015-12-17 01:18:40 +0000376bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000377 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
378 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
379 return SS->Sym.getType() == STT_OBJECT;
380 return false;
381}
382
Rui Ueyamac516ae12016-01-29 02:33:45 +0000383bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000384 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000385 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000386 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000387 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000388 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000389}
390
Rui Ueyamac516ae12016-01-29 02:33:45 +0000391bool X86TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000392 return isGnuIFunc<ELF32LE>(S) ||
393 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000394 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000395}
396
George Rimarbfb7bf72015-12-21 10:00:12 +0000397bool X86TargetInfo::isGotRelative(uint32_t Type) const {
398 // This relocation does not require got entry,
399 // but it is relative to got and needs it to be created.
400 // Here we request for that.
401 return Type == R_386_GOTOFF;
402}
403
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000404void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000405 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000406 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000407 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000408 case R_386_32:
409 add32le(Loc, SA);
410 break;
George Rimarffb67352016-01-19 11:00:48 +0000411 case R_386_GOT32: {
412 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
413 Out<ELF32LE>::Got->getNumEntries() * 4;
414 checkInt<32>(V, Type);
415 add32le(Loc, V);
416 break;
417 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000418 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000419 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000420 break;
George Rimar13934772015-11-25 20:20:31 +0000421 case R_386_GOTPC:
422 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
423 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000424 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000425 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000426 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000427 break;
George Rimar9db204a2015-12-02 09:58:20 +0000428 case R_386_TLS_GD:
429 case R_386_TLS_LDM:
430 case R_386_TLS_TPOFF: {
431 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
432 Out<ELF32LE>::Got->getNumEntries() * 4;
433 checkInt<32>(V, Type);
434 write32le(Loc, V);
435 break;
436 }
George Rimar6f17e092015-12-17 09:32:21 +0000437 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000438 case R_386_TLS_LDO_32:
439 write32le(Loc, SA);
440 break;
George Rimard23970f2015-11-25 20:41:53 +0000441 case R_386_TLS_LE:
442 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
443 break;
444 case R_386_TLS_LE_32:
445 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
446 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000447 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000448 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000449 }
450}
451
Rui Ueyamabaf16512016-01-29 00:20:12 +0000452bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000453 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000454 return false;
455 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
456 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000457 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000458 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
459}
460
Rui Ueyamac516ae12016-01-29 02:33:45 +0000461bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000462 return Config->Shared && Type == R_386_TLS_IE;
463}
464
Rui Ueyamac516ae12016-01-29 02:33:45 +0000465unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
466 uint64_t P, uint64_t SA,
467 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000468 switch (Type) {
469 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000470 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000471 relocateTlsGdToIe(Loc, BufEnd, P, SA);
472 else
473 relocateTlsGdToLe(Loc, BufEnd, P, SA);
474 // The next relocation should be against __tls_get_addr, so skip it
475 return 1;
476 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000477 case R_386_TLS_IE:
478 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000479 return 0;
480 case R_386_TLS_LDM:
481 relocateTlsLdToLe(Loc, BufEnd, P, SA);
482 // The next relocation should be against __tls_get_addr, so skip it
483 return 1;
484 case R_386_TLS_LDO_32:
485 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
486 return 0;
487 }
488 llvm_unreachable("Unknown TLS optimization");
489}
490
491// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
492// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
493// how GD can be optimized to IE:
494// leal x@tlsgd(, %ebx, 1),
495// call __tls_get_addr@plt
496// Is converted to:
497// movl %gs:0, %eax
498// addl x@gotntpoff(%ebx), %eax
499void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
500 uint64_t SA) const {
501 const uint8_t Inst[] = {
502 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
503 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
504 };
505 memcpy(Loc - 3, Inst, sizeof(Inst));
506 relocateOne(Loc + 5, BufEnd, R_386_32, P,
507 SA - Out<ELF32LE>::Got->getVA() -
508 Out<ELF32LE>::Got->getNumEntries() * 4);
509}
510
511// GD can be optimized to LE:
512// leal x@tlsgd(, %ebx, 1),
513// call __tls_get_addr@plt
514// Can be converted to:
515// movl %gs:0,%eax
516// addl $x@ntpoff,%eax
517// But gold emits subl $foo@tpoff,%eax instead of addl.
518// These instructions are completely equal in behavior.
519// This method generates subl to be consistent with gold.
520void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
521 uint64_t SA) const {
522 const uint8_t Inst[] = {
523 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
524 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
525 };
526 memcpy(Loc - 3, Inst, sizeof(Inst));
527 relocateOne(Loc + 5, BufEnd, R_386_32, P,
528 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
529}
530
531// LD can be optimized to LE:
532// leal foo(%reg),%eax
533// call ___tls_get_addr
534// Is converted to:
535// movl %gs:0,%eax
536// nop
537// leal 0(%esi,1),%esi
538void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
539 uint64_t SA) const {
540 const uint8_t Inst[] = {
541 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
542 0x90, // nop
543 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
544 };
545 memcpy(Loc - 2, Inst, sizeof(Inst));
546}
547
George Rimar6f17e092015-12-17 09:32:21 +0000548// In some conditions, relocations can be optimized to avoid using GOT.
549// This function does that for Initial Exec to Local Exec case.
550// Read "ELF Handling For Thread-Local Storage, 5.1
551// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000552// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000553void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
554 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000555 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000556 // Ulrich's document section 6.2 says that @gotntpoff can
557 // be used with MOVL or ADDL instructions.
558 // @indntpoff is similar to @gotntpoff, but for use in
559 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000560 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000561 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000562 uint8_t Reg = (Loc[-1] >> 3) & 7;
563 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000564 if (Type == R_386_TLS_IE) {
565 // For R_386_TLS_IE relocation we perform the next transformations:
566 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
567 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
568 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
569 // First one is special because when EAX is used the sequence is 5 bytes
570 // long, otherwise it is 6 bytes.
571 if (*Op == 0xa1) {
572 *Op = 0xb8;
573 } else {
574 *Inst = IsMov ? 0xc7 : 0x81;
575 *Op = 0xc0 | ((*Op >> 3) & 7);
576 }
577 } else {
578 // R_386_TLS_GOTIE relocation can be optimized to
579 // R_386_TLS_LE so that it does not use GOT.
580 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
581 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
582 // Note: gold converts to ADDL instead of LEAL.
583 *Inst = IsMov ? 0xc7 : 0x8d;
584 if (IsMov)
585 *Op = 0xc0 | ((*Op >> 3) & 7);
586 else
587 *Op = 0x80 | Reg | (Reg << 3);
588 }
George Rimar2558e122015-12-09 09:55:54 +0000589 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
590}
591
Rafael Espindola7f074422015-09-22 21:35:51 +0000592X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000593 CopyRel = R_X86_64_COPY;
594 GotRel = R_X86_64_GLOB_DAT;
595 PltRel = R_X86_64_JUMP_SLOT;
596 RelativeRel = R_X86_64_RELATIVE;
597 IRelativeRel = R_X86_64_IRELATIVE;
598 TlsGotRel = R_X86_64_TPOFF64;
599 TlsLocalDynamicRel = R_X86_64_TLSLD;
600 TlsGlobalDynamicRel = R_X86_64_TLSGD;
601 TlsModuleIndexRel = R_X86_64_DTPMOD64;
602 TlsOffsetRel = R_X86_64_DTPOFF64;
603 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000604 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000605 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000606}
607
Rui Ueyamac516ae12016-01-29 02:33:45 +0000608void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000609 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
610}
611
Rui Ueyamac516ae12016-01-29 02:33:45 +0000612void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000613 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000614 write32le(Buf, Plt + 6);
615}
616
Rui Ueyama900e2d22016-01-29 03:51:49 +0000617void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000618 const uint8_t PltData[] = {
619 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
620 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
621 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
622 };
623 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000624 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
625 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
626 write32le(Buf + 2, Got - Plt + 2); // GOT+8
627 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000628}
Rafael Espindola01205f72015-09-22 18:19:46 +0000629
Rui Ueyama9398f862016-01-29 04:15:02 +0000630void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
631 uint64_t PltEntryAddr, int32_t Index,
632 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000633 const uint8_t Inst[] = {
634 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
635 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
636 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
637 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000638 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000639
George Rimar648a2c32015-10-20 08:54:27 +0000640 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
641 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000642 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000643}
644
Rui Ueyama02dfd492015-12-17 01:18:40 +0000645bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000646 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
647 Type == R_X86_64_64)
648 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
649 return SS->Sym.getType() == STT_OBJECT;
650 return false;
651}
652
Rui Ueyamac516ae12016-01-29 02:33:45 +0000653bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000654 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000655 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000656 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000657 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000658 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000659}
660
Rui Ueyamac516ae12016-01-29 02:33:45 +0000661bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000662 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000663}
664
Rui Ueyamac516ae12016-01-29 02:33:45 +0000665bool X86_64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000666 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000667 return false;
George Rimara07ff662015-12-21 10:12:06 +0000668 if (isGnuIFunc<ELF64LE>(S))
669 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000670
Rafael Espindola01205f72015-09-22 18:19:46 +0000671 switch (Type) {
672 default:
673 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000674 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000675 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000676 case R_X86_64_PC32:
677 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000678 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000679 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000680 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000681 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000682 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
683 // libc.so.
684 //
685 // The general idea on how to handle such cases is to create a PLT entry
686 // and use that as the function value.
687 //
688 // For the static linking part, we just return true and everything else
689 // will use the the PLT entry as the address.
690 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000691 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000692 // still works. We need the help of the dynamic linker for that. We
693 // let it know that we have a direct reference to a so symbol by creating
694 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000695 // dynamic linker resolves the symbol to the value of the symbol we created.
696 // This is true even for got entries, so pointer equality is maintained.
697 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000698 // real function is a dedicated got entry used by the plt. That is
699 // identified by special relocation types (R_X86_64_JUMP_SLOT,
700 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000701 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000702 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000703 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000704 }
705}
Rafael Espindolac4010882015-09-22 20:54:08 +0000706
Rafael Espindolaae244002015-10-05 19:30:12 +0000707bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
708 switch (Type) {
709 default:
710 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000711 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000712 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000713 case R_X86_64_PC8:
714 case R_X86_64_PC16:
715 case R_X86_64_PC32:
716 case R_X86_64_PC64:
717 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000718 return true;
719 }
720}
721
Rui Ueyamac516ae12016-01-29 02:33:45 +0000722bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000723 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000724}
725
Rui Ueyamabaf16512016-01-29 00:20:12 +0000726bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000727 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000728 return false;
George Rimar25411f252015-12-04 11:20:13 +0000729 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
730 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000731 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
732}
733
734// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
735// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
736// how LD can be optimized to LE:
737// leaq bar@tlsld(%rip), %rdi
738// callq __tls_get_addr@PLT
739// leaq bar@dtpoff(%rax), %rcx
740// Is converted to:
741// .word 0x6666
742// .byte 0x66
743// mov %fs:0,%rax
744// leaq bar@tpoff(%rax), %rcx
745void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
746 uint64_t P, uint64_t SA) const {
747 const uint8_t Inst[] = {
748 0x66, 0x66, //.word 0x6666
749 0x66, //.byte 0x66
750 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
751 };
752 memcpy(Loc - 3, Inst, sizeof(Inst));
753}
754
755// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
756// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
757// how GD can be optimized to LE:
758// .byte 0x66
759// leaq x@tlsgd(%rip), %rdi
760// .word 0x6666
761// rex64
762// call __tls_get_addr@plt
763// Is converted to:
764// mov %fs:0x0,%rax
765// lea x@tpoff,%rax
766void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
767 uint64_t P, uint64_t SA) const {
768 const uint8_t Inst[] = {
769 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
770 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
771 };
772 memcpy(Loc - 4, Inst, sizeof(Inst));
773 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000774}
775
George Rimar25411f252015-12-04 11:20:13 +0000776// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
777// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
778// how GD can be optimized to IE:
779// .byte 0x66
780// leaq x@tlsgd(%rip), %rdi
781// .word 0x6666
782// rex64
783// call __tls_get_addr@plt
784// Is converted to:
785// mov %fs:0x0,%rax
786// addq x@tpoff,%rax
787void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
788 uint64_t P, uint64_t SA) const {
789 const uint8_t Inst[] = {
790 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
791 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
792 };
793 memcpy(Loc - 4, Inst, sizeof(Inst));
794 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
795}
796
George Rimar77d1cb12015-11-24 09:00:06 +0000797// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000798// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000799// This function does that. Read "ELF Handling For Thread-Local Storage,
800// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
801// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000802void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
803 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000804 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
805 // used in MOVQ or ADDQ instructions only.
806 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
807 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
808 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
809 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
810 uint8_t *Prefix = Loc - 3;
811 uint8_t *Inst = Loc - 2;
812 uint8_t *RegSlot = Loc - 1;
813 uint8_t Reg = Loc[-1] >> 3;
814 bool IsMov = *Inst == 0x8b;
815 bool RspAdd = !IsMov && Reg == 4;
816 // r12 and rsp registers requires special handling.
817 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
818 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
819 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
820 // The same true for rsp. So we convert to addq for them, saving 1 byte that
821 // we dont have.
822 if (RspAdd)
823 *Inst = 0x81;
824 else
825 *Inst = IsMov ? 0xc7 : 0x8d;
826 if (*Prefix == 0x4c)
827 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
828 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
829 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
830}
831
George Rimar6713cf82015-11-25 21:46:05 +0000832// This function applies a TLS relocation with an optimization as described
833// in the Ulrich's document. As a result of rewriting instructions at the
834// relocation target, relocations immediately follow the TLS relocation (which
835// would be applied to rewritten instructions) may have to be skipped.
836// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000837unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
838 uint32_t Type, uint64_t P, uint64_t SA,
839 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000840 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000841 case R_X86_64_DTPOFF32:
842 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
843 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000844 case R_X86_64_GOTTPOFF:
845 relocateTlsIeToLe(Loc, BufEnd, P, SA);
846 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000847 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000848 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000849 relocateTlsGdToIe(Loc, BufEnd, P, SA);
850 else
851 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000852 // The next relocation should be against __tls_get_addr, so skip it
853 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000854 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000855 case R_X86_64_TLSLD:
856 relocateTlsLdToLe(Loc, BufEnd, P, SA);
857 // The next relocation should be against __tls_get_addr, so skip it
858 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000859 }
860 llvm_unreachable("Unknown TLS optimization");
861}
862
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000863void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000864 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000865 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000866 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000867 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000868 checkUInt<32>(SA, Type);
869 write32le(Loc, SA);
870 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000871 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000872 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000873 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000874 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000875 case R_X86_64_64:
876 write64le(Loc, SA);
877 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000878 case R_X86_64_DTPOFF32:
879 write32le(Loc, SA);
880 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000881 case R_X86_64_DTPOFF64:
882 write64le(Loc, SA);
883 break;
884 case R_X86_64_GOTPCREL:
885 case R_X86_64_PC32:
886 case R_X86_64_PLT32:
887 case R_X86_64_TLSGD:
888 case R_X86_64_TLSLD:
889 write32le(Loc, SA - P);
890 break;
George Rimar48651482015-12-11 08:59:37 +0000891 case R_X86_64_SIZE32:
892 write32le(Loc, ZA);
893 break;
894 case R_X86_64_SIZE64:
895 write64le(Loc, ZA);
896 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000897 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000898 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000899 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000900 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000901 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000902 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000903 case R_X86_64_TPOFF64:
904 write32le(Loc, SA - P);
905 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000906 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000907 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000908 }
909}
910
Hal Finkel3c8cc672015-10-12 20:56:18 +0000911// Relocation masks following the #lo(value), #hi(value), #ha(value),
912// #higher(value), #highera(value), #highest(value), and #highesta(value)
913// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
914// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000915static uint16_t applyPPCLo(uint64_t V) { return V; }
916static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
917static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
918static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
919static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000920static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000921static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
922
Davide Italiano8c3444362016-01-11 19:45:33 +0000923PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000924bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
925
926void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
927 uint64_t P, uint64_t SA, uint64_t ZA,
928 uint8_t *PairedLoc) const {
929 switch (Type) {
930 case R_PPC_ADDR16_HA:
931 write16be(Loc, applyPPCHa(SA));
932 break;
933 case R_PPC_ADDR16_LO:
934 write16be(Loc, applyPPCLo(SA));
935 break;
936 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000937 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000938 }
939}
940
Rafael Espindolac4010882015-09-22 20:54:08 +0000941PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000942 GotRel = R_PPC64_GLOB_DAT;
943 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000944 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000945
946 // We need 64K pages (at least under glibc/Linux, the loader won't
947 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000948 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000949
950 // The PPC64 ELF ABI v1 spec, says:
951 //
952 // It is normally desirable to put segments with different characteristics
953 // in separate 256 Mbyte portions of the address space, to give the
954 // operating system full paging flexibility in the 64-bit address space.
955 //
956 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
957 // use 0x10000000 as the starting address.
958 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000959}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000960
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000961uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000962 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
963 // order. The TOC starts where the first of these sections starts.
964
965 // FIXME: This obviously does not do the right thing when there is no .got
966 // section, but there is a .toc or .tocbss section.
967 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
968 if (!TocVA)
969 TocVA = Out<ELF64BE>::Plt->getVA();
970
971 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
972 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
973 // code (crt1.o) assumes that you can get from the TOC base to the
974 // start of the .toc section with only a single (signed) 16-bit relocation.
975 return TocVA + 0x8000;
976}
977
Rui Ueyama9398f862016-01-29 04:15:02 +0000978void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
979 uint64_t PltEntryAddr, int32_t Index,
980 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000981 uint64_t Off = GotEntryAddr - getPPC64TocBase();
982
983 // FIXME: What we should do, in theory, is get the offset of the function
984 // descriptor in the .opd section, and use that as the offset from %r2 (the
985 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
986 // be a pointer to the function descriptor in the .opd section. Using
987 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
988
Hal Finkelfa92f682015-10-13 21:47:34 +0000989 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000990 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
991 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
992 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
993 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
994 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
995 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
996 write32be(Buf + 28, 0x4e800420); // bctr
997}
998
Rui Ueyamac516ae12016-01-29 02:33:45 +0000999bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
1000 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001001 return true;
1002
1003 switch (Type) {
1004 default: return false;
1005 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001006 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001007 case R_PPC64_GOT16_HA:
1008 case R_PPC64_GOT16_HI:
1009 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 case R_PPC64_GOT16_LO_DS:
1011 return true;
1012 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001013}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001014
Rui Ueyamac516ae12016-01-29 02:33:45 +00001015bool PPC64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001016 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001017 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001018}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019
Hal Finkelbe0823d2015-10-12 20:58:52 +00001020bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1021 switch (Type) {
1022 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001023 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001024 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001025 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001026 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001027 }
1028}
1029
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001030void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001031 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001032 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001033 uint64_t TB = getPPC64TocBase();
1034
Hal Finkel3c8cc672015-10-12 20:56:18 +00001035 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1036 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001037 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001038 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1039 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001040 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1041 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001042 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1043 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001044 default: break;
1045 }
1046
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001048 case R_PPC64_ADDR14: {
1049 checkAlignment<4>(SA, Type);
1050 // Preserve the AA/LK bits in the branch instruction
1051 uint8_t AALK = Loc[3];
1052 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1053 break;
1054 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001056 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001057 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001058 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001060 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001061 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001063 case R_PPC64_ADDR16_HA:
1064 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 break;
1066 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001067 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001069 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001070 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071 break;
1072 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001073 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001074 break;
1075 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001076 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001077 break;
1078 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001079 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001081 case R_PPC64_ADDR16_LO:
1082 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001083 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001084 case R_PPC64_ADDR16_LO_DS:
1085 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001086 break;
1087 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001088 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001089 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001090 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001091 case R_PPC64_ADDR64:
1092 write64be(Loc, SA);
1093 break;
1094 case R_PPC64_REL16_HA:
1095 write16be(Loc, applyPPCHa(SA - P));
1096 break;
1097 case R_PPC64_REL16_HI:
1098 write16be(Loc, applyPPCHi(SA - P));
1099 break;
1100 case R_PPC64_REL16_LO:
1101 write16be(Loc, applyPPCLo(SA - P));
1102 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001104 // If we have an undefined weak symbol, we might get here with a symbol
1105 // address of zero. That could overflow, but the code must be unreachable,
1106 // so don't bother doing anything at all.
1107 if (!SA)
1108 break;
1109
Hal Finkeldaedc122015-10-12 23:16:53 +00001110 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1111 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001112 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001113
1114 if (!InPlt && Out<ELF64BE>::Opd) {
1115 // If this is a local call, and we currently have the address of a
1116 // function-descriptor, get the underlying code address instead.
1117 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1118 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001119 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001120
1121 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001122 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001123 }
1124
Hal Finkel3c8cc672015-10-12 20:56:18 +00001125 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001126 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001127 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001128
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 uint32_t Nop = 0x60000000;
1130 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1131 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 break;
1133 }
1134 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001135 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001136 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001137 break;
1138 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001139 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001140 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001141 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001142 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001143 break;
1144 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001145 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001146 }
1147}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001148
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001149AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001150 CopyRel = R_AARCH64_COPY;
1151 IRelativeRel = R_AARCH64_IRELATIVE;
1152 GotRel = R_AARCH64_GLOB_DAT;
1153 PltRel = R_AARCH64_JUMP_SLOT;
1154 TlsGotRel = R_AARCH64_TLS_TPREL64;
1155 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001156 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001157 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001158}
George Rimar648a2c32015-10-20 08:54:27 +00001159
Rui Ueyamac516ae12016-01-29 02:33:45 +00001160unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001161 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1162 return Type;
1163 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001164 fatal("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001165 "recompile with -fPIC.");
1166}
1167
Rui Ueyamac516ae12016-01-29 02:33:45 +00001168void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001169 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1170}
1171
Rui Ueyama900e2d22016-01-29 03:51:49 +00001172void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001173 const uint8_t PltData[] = {
1174 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1175 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1176 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1177 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1178 0x20, 0x02, 0x1f, 0xd6, // br x17
1179 0x1f, 0x20, 0x03, 0xd5, // nop
1180 0x1f, 0x20, 0x03, 0xd5, // nop
1181 0x1f, 0x20, 0x03, 0xd5 // nop
1182 };
1183 memcpy(Buf, PltData, sizeof(PltData));
1184
Rui Ueyama900e2d22016-01-29 03:51:49 +00001185 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1186 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1187 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1188 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1189 Got + 16);
1190 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1191 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001192}
1193
Rui Ueyama9398f862016-01-29 04:15:02 +00001194void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1195 uint64_t PltEntryAddr, int32_t Index,
1196 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001197 const uint8_t Inst[] = {
1198 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1199 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1200 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1201 0x20, 0x02, 0x1f, 0xd6 // br x17
1202 };
1203 memcpy(Buf, Inst, sizeof(Inst));
1204
1205 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1206 GotEntryAddr);
1207 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1208 GotEntryAddr);
1209 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1210 GotEntryAddr);
1211}
1212
Rui Ueyama724d6252016-01-29 01:49:32 +00001213unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar3d737e42016-01-13 13:04:46 +00001214 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1215 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1216 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +00001217 return TlsGotRel;
George Rimar3d737e42016-01-13 13:04:46 +00001218}
1219
Rui Ueyamac516ae12016-01-29 02:33:45 +00001220bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001221 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1222 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1223}
1224
Rui Ueyama02dfd492015-12-17 01:18:40 +00001225bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001226 if (Config->Shared)
1227 return false;
1228 switch (Type) {
1229 default:
1230 return false;
1231 case R_AARCH64_ABS16:
1232 case R_AARCH64_ABS32:
1233 case R_AARCH64_ABS64:
1234 case R_AARCH64_ADD_ABS_LO12_NC:
1235 case R_AARCH64_ADR_PREL_LO21:
1236 case R_AARCH64_ADR_PREL_PG_HI21:
1237 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001238 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001239 case R_AARCH64_LDST32_ABS_LO12_NC:
1240 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001241 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001242 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1243 return SS->Sym.getType() == STT_OBJECT;
1244 return false;
1245 }
1246}
1247
Rui Ueyamac516ae12016-01-29 02:33:45 +00001248bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001249 switch (Type) {
1250 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1251 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1252 case R_AARCH64_ADR_GOT_PAGE:
1253 case R_AARCH64_LD64_GOT_LO12_NC:
1254 return true;
1255 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001256 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001257 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001258}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001259
Rui Ueyamac516ae12016-01-29 02:33:45 +00001260bool AArch64TargetInfo::needsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001261 if (isGnuIFunc<ELF64LE>(S))
1262 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001263 switch (Type) {
1264 default:
1265 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001266 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001267 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001268 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001269 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001270 return canBePreempted(&S, true);
1271 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001272}
Davide Italiano1d750a62015-09-27 08:45:38 +00001273
Davide Italianoef4be6b2015-10-06 19:01:32 +00001274static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001275 uint32_t ImmLo = (Imm & 0x3) << 29;
1276 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1277 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001278 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001279}
1280
Davide Italiano318ca222015-10-02 22:13:51 +00001281// Page(Expr) is the page address of the expression Expr, defined
1282// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001283// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001284static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001285 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001286}
1287
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001288void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001289 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001290 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001291 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001292 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001293 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001294 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001295 break;
1296 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001297 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001298 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001299 break;
1300 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001301 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001302 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001303 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001304 // This relocation stores 12 bits and there's no instruction
1305 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001306 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1307 // bits in Loc are zero.
1308 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001309 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001310 case R_AARCH64_ADR_GOT_PAGE: {
1311 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1312 checkInt<33>(X, Type);
1313 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1314 break;
1315 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001316 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001317 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001318 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001319 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001320 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001321 }
George Rimar3d737e42016-01-13 13:04:46 +00001322 case R_AARCH64_ADR_PREL_PG_HI21:
1323 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001324 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001325 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001326 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001327 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001328 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001329 case R_AARCH64_CALL26:
1330 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001331 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001332 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001333 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1334 break;
1335 }
George Rimar4102bfb2016-01-11 14:22:00 +00001336 case R_AARCH64_CONDBR19: {
1337 uint64_t X = SA - P;
1338 checkInt<21>(X, Type);
1339 or32le(Loc, (X & 0x1FFFFC) << 3);
1340 break;
1341 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001342 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001343 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001344 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001345 or32le(Loc, (SA & 0xFF8) << 7);
1346 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001347 case R_AARCH64_LDST128_ABS_LO12_NC:
1348 or32le(Loc, (SA & 0x0FF8) << 6);
1349 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001350 case R_AARCH64_LDST16_ABS_LO12_NC:
1351 or32le(Loc, (SA & 0x0FFC) << 9);
1352 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001353 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001354 or32le(Loc, (SA & 0xFFF) << 10);
1355 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001356 case R_AARCH64_LDST32_ABS_LO12_NC:
1357 or32le(Loc, (SA & 0xFFC) << 8);
1358 break;
1359 case R_AARCH64_LDST64_ABS_LO12_NC:
1360 or32le(Loc, (SA & 0xFF8) << 7);
1361 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001362 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001363 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001364 write16le(Loc, SA - P);
1365 break;
1366 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001367 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001368 write32le(Loc, SA - P);
1369 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001370 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001371 write64le(Loc, SA - P);
1372 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001373 case R_AARCH64_TSTBR14: {
1374 uint64_t X = SA - P;
1375 checkInt<16>(X, Type);
1376 or32le(Loc, (X & 0xFFFC) << 3);
1377 break;
1378 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001379 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001380 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001381 }
1382}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001383
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001384// Implementing relocations for AMDGPU is low priority since most
1385// programs don't use relocations now. Thus, this function is not
1386// actually called (relocateOne is called for each relocation).
1387// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001388void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1389 uint64_t P, uint64_t SA, uint64_t ZA,
1390 uint8_t *PairedLoc) const {
1391 llvm_unreachable("not implemented");
1392}
1393
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001394template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +00001395 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001396 GotHeaderEntriesNum = 2;
Rui Ueyama724d6252016-01-29 01:49:32 +00001397 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001398}
1399
1400template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001401unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001402 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1403 return R_MIPS_REL32;
1404 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001405 fatal("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001406 "recompile with -fPIC.");
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001407}
1408
1409template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001410void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001411 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001412 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1413
1414 // Set the MSB of the second GOT slot. This is not required by any
1415 // MIPS ABI documentation, though.
1416 //
1417 // There is a comment in glibc saying that "The MSB of got[1] of a
1418 // gnu object is set to identify gnu objects," and in GNU gold it
1419 // says "the second entry will be used by some runtime loaders".
1420 // But how this field is being used is unclear.
1421 //
1422 // We are not really willing to mimic other linkers behaviors
1423 // without understanding why they do that, but because all files
1424 // generated by GNU tools have this special GOT value, and because
1425 // we've been doing this for years, it is probably a safe bet to
1426 // keep doing this for now. We really need to revisit this to see
1427 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001428 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001429 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001430}
1431
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001432template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001433bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +00001434 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001435}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001436
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001437template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001438bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001439 return false;
1440}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001441
Simon Atanasyan35031192015-12-15 06:06:34 +00001442static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001443
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001444template <endianness E, uint8_t BSIZE>
1445static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
1446 uint64_t SA) {
1447 uint32_t Mask = ~(0xffffffff << BSIZE);
1448 uint32_t Instr = read32<E>(Loc);
1449 int64_t A = SignExtend64<BSIZE + 2>((Instr & Mask) << 2);
1450 checkAlignment<4>(SA + A, Type);
1451 int64_t V = SA + A - P;
1452 checkInt<BSIZE + 2>(V, Type);
1453 write32<E>(Loc, (Instr & ~Mask) | ((V >> 2) & Mask));
1454}
1455
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001456template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001457void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001458 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001459 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001460 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001461 switch (Type) {
1462 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001463 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001464 break;
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001465 case R_MIPS_CALL16:
1466 case R_MIPS_GOT16: {
1467 int64_t V = SA - getMipsGpAddr<ELFT>();
1468 if (Type == R_MIPS_GOT16)
1469 checkInt<16>(V, Type);
1470 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1471 break;
1472 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001473 case R_MIPS_GPREL16: {
1474 uint32_t Instr = read32<E>(Loc);
1475 int64_t V = SA + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
1476 checkInt<16>(V, Type);
1477 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1478 break;
1479 }
1480 case R_MIPS_GPREL32:
1481 write32<E>(Loc, SA + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
1482 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001483 case R_MIPS_HI16: {
1484 uint32_t Instr = read32<E>(Loc);
1485 if (PairedLoc) {
1486 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001487 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001488 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001489 } else {
1490 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001491 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001492 }
1493 break;
1494 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001495 case R_MIPS_JALR:
1496 // Ignore this optimization relocation for now
1497 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001498 case R_MIPS_LO16: {
1499 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001500 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001501 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff));
1502 break;
1503 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001504 case R_MIPS_PC16:
1505 applyMipsPcReloc<E, 16>(Loc, Type, P, SA);
1506 break;
1507 case R_MIPS_PC19_S2:
1508 applyMipsPcReloc<E, 19>(Loc, Type, P, SA);
1509 break;
1510 case R_MIPS_PC21_S2:
1511 applyMipsPcReloc<E, 21>(Loc, Type, P, SA);
1512 break;
1513 case R_MIPS_PC26_S2:
1514 applyMipsPcReloc<E, 26>(Loc, Type, P, SA);
1515 break;
1516 case R_MIPS_PCHI16: {
1517 uint32_t Instr = read32<E>(Loc);
1518 if (PairedLoc) {
1519 uint64_t AHL = ((Instr & 0xffff) << 16) +
1520 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
1521 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA + AHL - P));
1522 } else {
1523 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
1524 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(SA - P));
1525 }
1526 break;
1527 }
1528 case R_MIPS_PCLO16: {
1529 uint32_t Instr = read32<E>(Loc);
1530 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
1531 write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL - P) & 0xffff));
1532 break;
1533 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001534 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001535 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001536 }
1537}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001538
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001539template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001540bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001541 return Type == R_MIPS_JALR;
1542}
1543
1544template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001545bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1546 switch (Type) {
1547 default:
1548 return false;
1549 case R_MIPS_PC16:
1550 case R_MIPS_PC19_S2:
1551 case R_MIPS_PC21_S2:
1552 case R_MIPS_PC26_S2:
1553 case R_MIPS_PCHI16:
1554 case R_MIPS_PCLO16:
1555 return true;
1556 }
1557}
1558
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001559// _gp is a MIPS-specific ABI-defined symbol which points to
1560// a location that is relative to GOT. This function returns
1561// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001562template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001563 unsigned GPOffset = 0x7ff0;
1564 if (uint64_t V = Out<ELFT>::Got->getVA())
1565 return V + GPOffset;
1566 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001567}
1568
Simon Atanasyan56ab5f02016-01-21 05:33:23 +00001569bool needsMipsLocalGot(uint32_t Type, SymbolBody *Body) {
1570 // The R_MIPS_GOT16 relocation requires creation of entry in the local part
1571 // of GOT if its target is a local symbol or non-local symbol with 'local'
1572 // visibility.
1573 if (Type != R_MIPS_GOT16)
1574 return false;
1575 if (!Body)
1576 return true;
1577 uint8_t V = Body->getVisibility();
1578 if (V != STV_DEFAULT && V != STV_PROTECTED)
1579 return true;
1580 return !Config->Shared;
1581}
1582
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001583template uint32_t getMipsGpAddr<ELF32LE>();
1584template uint32_t getMipsGpAddr<ELF32BE>();
1585template uint64_t getMipsGpAddr<ELF64LE>();
1586template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001587}
1588}