blob: d56984be7ee79a74528bd2825e2bd9cbef86aef0 [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 {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000035
Rui Ueyamac1c282a2016-02-11 21:18:01 +000036TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000037
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000039
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000040template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
41 if (isInt<N>(V))
42 return;
43 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000044 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045}
46
47template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
48 if (isUInt<N>(V))
49 return;
50 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000051 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000052}
53
Igor Kudrinfea8ed52015-11-26 10:05:24 +000054template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
55 if (isInt<N>(V) || isUInt<N>(V))
56 return;
57 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000058 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059}
60
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000061template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
62 if ((V & (N - 1)) == 0)
63 return;
64 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000065 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066}
67
Rui Ueyamaefc23de2015-10-14 21:30:32 +000068namespace {
69class X86TargetInfo final : public TargetInfo {
70public:
71 X86TargetInfo();
Rafael Espindolada99df32016-03-30 12:40:38 +000072 uint64_t getImplicitAddend(uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000073 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000074 uint32_t getDynRel(uint32_t Type) const override;
75 uint32_t getTlsGotRel(uint32_t Type) const override;
76 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
77 bool isTlsLocalDynamicRel(uint32_t Type) const override;
78 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
79 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000080 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000081 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000082 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
83 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000084 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000085 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000086 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +000087 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000088 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000089 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +000090 uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000091
92 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
93 uint64_t P, uint64_t SA) const override;
94 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +000095 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000096 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +000097 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000098 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
99 uint64_t P, uint64_t SA) const override;
100
George Rimarbfb7bf72015-12-21 10:00:12 +0000101 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000102 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000103};
104
105class X86_64TargetInfo final : public TargetInfo {
106public:
107 X86_64TargetInfo();
George Rimar86971052016-03-29 08:35:42 +0000108 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000109 uint32_t getTlsGotRel(uint32_t Type) const override;
110 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
111 bool isTlsLocalDynamicRel(uint32_t Type) const override;
112 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
113 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000114 void writeGotPltHeader(uint8_t *Buf) const override;
115 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000116 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000117 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
118 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000119 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000120 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000121 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000122 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000123 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000124 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000125 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000126 bool isSizeRel(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000127
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000128 size_t relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
129 uint64_t P, uint64_t SA) const override;
130 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000131 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000132 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000133 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000134 size_t relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
135 uint64_t P, uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000136};
137
Davide Italiano8c3444362016-01-11 19:45:33 +0000138class PPCTargetInfo final : public TargetInfo {
139public:
140 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000141 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000142 uint64_t SA) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000143 bool isRelRelative(uint32_t Type) const override;
144};
145
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000146class PPC64TargetInfo final : public TargetInfo {
147public:
148 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000149 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
150 int32_t Index, unsigned RelOff) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000151 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000152 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000153 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000154 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155 bool isRelRelative(uint32_t Type) const override;
156};
157
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000158class AArch64TargetInfo final : public TargetInfo {
159public:
160 AArch64TargetInfo();
George Rimar98b060d2016-03-06 06:01:07 +0000161 uint32_t getDynRel(uint32_t Type) const override;
162 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
163 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000164 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000165 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000166 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
167 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000168 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000169 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000170 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000171 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000172 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000173 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000174 uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000175 size_t relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000176 uint64_t P, uint64_t SA) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000177 size_t relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000178 uint64_t P, uint64_t SA) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000179
180private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000181 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000182};
183
Tom Stellard80efb162016-01-07 03:59:08 +0000184class AMDGPUTargetInfo final : public TargetInfo {
185public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000186 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000187 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000188 uint64_t SA) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000189};
190
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000191template <class ELFT> class MipsTargetInfo final : public TargetInfo {
192public:
193 MipsTargetInfo();
Rafael Espindola8cc68c32016-03-30 13:18:08 +0000194 uint64_t getImplicitAddend(uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000195 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000196 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
197 void writePltZero(uint8_t *Buf) const override;
198 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
199 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000200 void writeGotHeader(uint8_t *Buf) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000201 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000202 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000203 bool needsPltImpl(uint32_t Type) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000204 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rafael Espindola287e1002016-03-30 13:27:50 +0000205 uint64_t SA) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000206 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000207 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000208 bool refersToGotEntry(uint32_t Type) 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();
Tom Stellard80efb162016-01-07 03:59:08 +0000218 case EM_AMDGPU:
219 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000220 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000221 switch (Config->EKind) {
222 case ELF32LEKind:
223 return new MipsTargetInfo<ELF32LE>();
224 case ELF32BEKind:
225 return new MipsTargetInfo<ELF32BE>();
226 default:
George Rimar777f9632016-03-12 08:31:34 +0000227 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000228 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000229 case EM_PPC:
230 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000231 case EM_PPC64:
232 return new PPC64TargetInfo();
233 case EM_X86_64:
234 return new X86_64TargetInfo();
235 }
George Rimar777f9632016-03-12 08:31:34 +0000236 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000237}
238
Rafael Espindola01205f72015-09-22 18:19:46 +0000239TargetInfo::~TargetInfo() {}
240
Rafael Espindolada99df32016-03-30 12:40:38 +0000241uint64_t TargetInfo::getImplicitAddend(uint8_t *Buf, uint32_t Type) const {
242 return 0;
243}
244
George Rimar98b060d2016-03-06 06:01:07 +0000245bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000246 if (Config->Shared || (S && !S->IsTls))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000247 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000248
Rafael Espindolad405f472016-03-04 21:37:09 +0000249 // We know we are producing an executable.
250
251 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
252 // depending on the symbol being locally defined or not.
253 if (isTlsGlobalDynamicRel(Type))
254 return true;
255
256 // Local-Dynamic relocs can be relaxed to Local-Exec.
257 if (isTlsLocalDynamicRel(Type))
258 return true;
259
260 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
261 // defined.
262 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000263 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000264
George Rimar77d1cb12015-11-24 09:00:06 +0000265 return false;
266}
267
George Rimar786e8662016-03-17 05:57:33 +0000268uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000269
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000270bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
271
272template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
273 if (Config->Shared)
274 return false;
275 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
276 if (!SS)
277 return false;
278 return SS->Sym.getType() == STT_OBJECT;
279}
280
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000281template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000282bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000283 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000284}
285
George Rimarbfb7bf72015-12-21 10:00:12 +0000286bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000287bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000288bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000289bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000290
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000291bool TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
292 return false;
293}
Rui Ueyama012eb782016-01-29 04:05:09 +0000294
Rafael Espindola993f0272016-02-26 14:27:47 +0000295bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000296
297bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
298
Rafael Espindola852860e2016-02-12 15:47:37 +0000299TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
300 const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +0000301 if (S.IsGnuIFunc)
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000302 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000303 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000304 return Plt_Explicit;
305
306 // This handles a non PIC program call to function in a shared library.
307 // In an ideal world, we could just report an error saying the relocation
308 // can overflow at runtime.
309 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
310 // libc.so.
311 //
312 // The general idea on how to handle such cases is to create a PLT entry
313 // and use that as the function value.
314 //
315 // For the static linking part, we just return true and everything else
316 // will use the the PLT entry as the address.
317 //
318 // The remaining problem is making sure pointer equality still works. We
319 // need the help of the dynamic linker for that. We let it know that we have
320 // a direct reference to a so symbol by creating an undefined symbol with a
321 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
322 // the value of the symbol we created. This is true even for got entries, so
323 // pointer equality is maintained. To avoid an infinite loop, the only entry
324 // that points to the real function is a dedicated got entry used by the
325 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
326 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000327 if (S.isShared())
328 if (!Config->Pic && S.IsFunc && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000329 return Plt_Implicit;
330
Rafael Espindola852860e2016-02-12 15:47:37 +0000331 return Plt_No;
332}
Rui Ueyama012eb782016-01-29 04:05:09 +0000333
George Rimar98b060d2016-03-06 06:01:07 +0000334bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000335
George Rimar98b060d2016-03-06 06:01:07 +0000336bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000337 return false;
338}
339
George Rimar98b060d2016-03-06 06:01:07 +0000340bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000341
George Rimar98b060d2016-03-06 06:01:07 +0000342bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000343 return false;
344}
345
George Rimar7b8850f2016-03-06 06:09:50 +0000346size_t TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
347 uint64_t P, uint64_t SA,
Rafael Espindola67d72c02016-03-11 12:06:30 +0000348 const SymbolBody &S) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000349 if (isTlsGlobalDynamicRel(Type)) {
350 if (S.isPreemptible())
351 return relaxTlsGdToIe(Loc, BufEnd, Type, P, SA);
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000352 return relaxTlsGdToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000353 }
354 if (isTlsLocalDynamicRel(Type))
355 return relaxTlsLdToLe(Loc, BufEnd, Type, P, SA);
356 assert(isTlsInitialExecRel(Type));
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000357 return relaxTlsIeToLe(Loc, BufEnd, Type, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000358}
359
360size_t TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000361 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000362 llvm_unreachable("Should not have claimed to be relaxable");
363}
364
365size_t TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
366 uint64_t P, uint64_t SA) const {
367 llvm_unreachable("Should not have claimed to be relaxable");
368}
369
370size_t TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000371 uint64_t P, uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000372 llvm_unreachable("Should not have claimed to be relaxable");
373}
374
375size_t TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
376 uint64_t P, uint64_t SA) const {
377 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000378}
George Rimar77d1cb12015-11-24 09:00:06 +0000379
Rafael Espindola7f074422015-09-22 21:35:51 +0000380X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000381 CopyRel = R_386_COPY;
382 GotRel = R_386_GLOB_DAT;
383 PltRel = R_386_JUMP_SLOT;
384 IRelativeRel = R_386_IRELATIVE;
385 RelativeRel = R_386_RELATIVE;
386 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000387 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
388 TlsOffsetRel = R_386_TLS_DTPOFF32;
389 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000390 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000391 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000392}
393
Rafael Espindolaffcad442016-03-23 14:58:25 +0000394bool X86TargetInfo::isRelRelative(uint32_t Type) const {
395 switch (Type) {
396 default:
397 return false;
398 case R_386_PC32:
399 case R_386_PLT32:
400 case R_386_TLS_LDO_32:
401 return true;
402 }
403}
404
Rui Ueyamac516ae12016-01-29 02:33:45 +0000405void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000406 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
407}
408
Rui Ueyamac516ae12016-01-29 02:33:45 +0000409void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000410 // Entries in .got.plt initially points back to the corresponding
411 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000412 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000413}
Rafael Espindola01205f72015-09-22 18:19:46 +0000414
George Rimar98b060d2016-03-06 06:01:07 +0000415uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000416 if (Type == R_386_TLS_LE)
417 return R_386_TLS_TPOFF;
418 if (Type == R_386_TLS_LE_32)
419 return R_386_TLS_TPOFF32;
420 return Type;
421}
422
George Rimar98b060d2016-03-06 06:01:07 +0000423uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000424 if (Type == R_386_TLS_IE)
425 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000426 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000427}
428
George Rimar98b060d2016-03-06 06:01:07 +0000429bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000430 return Type == R_386_TLS_GD;
431}
432
George Rimar98b060d2016-03-06 06:01:07 +0000433bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000434 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
435}
436
George Rimar98b060d2016-03-06 06:01:07 +0000437bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000438 return Type == R_386_TLS_LDM;
439}
440
George Rimar98b060d2016-03-06 06:01:07 +0000441bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000442 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
443}
444
Rui Ueyama900e2d22016-01-29 03:51:49 +0000445void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000446 // Executable files and shared object files have
447 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000448 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000449 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000450 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000451 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
452 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000453 };
454 memcpy(Buf, V, sizeof(V));
455 return;
456 }
George Rimar648a2c32015-10-20 08:54:27 +0000457
George Rimar77b77792015-11-25 22:15:01 +0000458 const uint8_t PltData[] = {
459 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000460 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
461 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000462 };
463 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000464 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000465 write32le(Buf + 2, Got + 4);
466 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000467}
468
Rui Ueyama9398f862016-01-29 04:15:02 +0000469void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
470 uint64_t PltEntryAddr, int32_t Index,
471 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000472 const uint8_t Inst[] = {
473 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
474 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
475 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
476 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000477 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000478
George Rimar77b77792015-11-25 22:15:01 +0000479 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000480 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000481 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
482 : Out<ELF32LE>::Got->getVA();
483 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000484 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000485 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000486}
487
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000488bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
489 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000490}
491
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000492bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar2f0fab52016-03-06 06:26:18 +0000493 if (S.IsTls && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000494 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000495 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000496 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000497 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000498}
499
Rafael Espindola993f0272016-02-26 14:27:47 +0000500bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
501 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000502}
503
George Rimarbfb7bf72015-12-21 10:00:12 +0000504bool X86TargetInfo::isGotRelative(uint32_t Type) const {
505 // This relocation does not require got entry,
506 // but it is relative to got and needs it to be created.
507 // Here we request for that.
508 return Type == R_386_GOTOFF;
509}
510
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000511bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
512 return Type == R_386_GOT32;
513}
514
Rafael Espindolada99df32016-03-30 12:40:38 +0000515uint64_t X86TargetInfo::getImplicitAddend(uint8_t *Buf, uint32_t Type) const {
516 switch (Type) {
517 default:
518 return 0;
519 case R_386_32:
520 case R_386_GOT32:
521 case R_386_GOTOFF:
522 case R_386_GOTPC:
523 case R_386_PC32:
524 case R_386_PLT32:
525 return read32le(Buf);
526 }
527}
528
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000529void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +0000530 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000531 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000532 case R_386_32:
Rafael Espindolada99df32016-03-30 12:40:38 +0000533 write32le(Loc, SA);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000534 break;
George Rimarffb67352016-01-19 11:00:48 +0000535 case R_386_GOT32: {
536 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
537 Out<ELF32LE>::Got->getNumEntries() * 4;
538 checkInt<32>(V, Type);
Rafael Espindolada99df32016-03-30 12:40:38 +0000539 write32le(Loc, V);
George Rimarffb67352016-01-19 11:00:48 +0000540 break;
541 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000542 case R_386_GOTOFF:
Rafael Espindolada99df32016-03-30 12:40:38 +0000543 write32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000544 break;
George Rimar13934772015-11-25 20:20:31 +0000545 case R_386_GOTPC:
Rafael Espindolada99df32016-03-30 12:40:38 +0000546 write32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
George Rimar13934772015-11-25 20:20:31 +0000547 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000548 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000549 case R_386_PLT32:
Rafael Espindolada99df32016-03-30 12:40:38 +0000550 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000551 break;
George Rimar9db204a2015-12-02 09:58:20 +0000552 case R_386_TLS_GD:
553 case R_386_TLS_LDM:
554 case R_386_TLS_TPOFF: {
555 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
556 Out<ELF32LE>::Got->getNumEntries() * 4;
557 checkInt<32>(V, Type);
558 write32le(Loc, V);
559 break;
560 }
George Rimar6f17e092015-12-17 09:32:21 +0000561 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000562 case R_386_TLS_LDO_32:
563 write32le(Loc, SA);
564 break;
George Rimard23970f2015-11-25 20:41:53 +0000565 case R_386_TLS_LE:
566 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
567 break;
568 case R_386_TLS_LE_32:
569 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
570 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000571 default:
George Rimar57610422016-03-11 14:43:02 +0000572 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000573 }
574}
575
George Rimar98b060d2016-03-06 06:01:07 +0000576bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000577 return Config->Shared && Type == R_386_TLS_IE;
578}
579
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000580size_t X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000581 uint32_t Type, uint64_t P,
582 uint64_t SA) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000583 // GD can be optimized to LE:
584 // leal x@tlsgd(, %ebx, 1),
585 // call __tls_get_addr@plt
586 // Can be converted to:
587 // movl %gs:0,%eax
588 // addl $x@ntpoff,%eax
589 // But gold emits subl $foo@tpoff,%eax instead of addl.
590 // These instructions are completely equal in behavior.
591 // This method generates subl to be consistent with gold.
592 const uint8_t Inst[] = {
593 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
594 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
595 };
596 memcpy(Loc - 3, Inst, sizeof(Inst));
597 relocateOne(Loc + 5, BufEnd, R_386_32, P,
598 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
599
600 // The next relocation should be against __tls_get_addr, so skip it
601 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000602}
603
604// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
605// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
606// how GD can be optimized to IE:
607// leal x@tlsgd(, %ebx, 1),
608// call __tls_get_addr@plt
609// Is converted to:
610// movl %gs:0, %eax
611// addl x@gotntpoff(%ebx), %eax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000612size_t X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
613 uint32_t Type, uint64_t P,
614 uint64_t SA) const {
George Rimar2558e122015-12-09 09:55:54 +0000615 const uint8_t Inst[] = {
616 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
617 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
618 };
619 memcpy(Loc - 3, Inst, sizeof(Inst));
620 relocateOne(Loc + 5, BufEnd, R_386_32, P,
621 SA - Out<ELF32LE>::Got->getVA() -
622 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000623
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000624 // The next relocation should be against __tls_get_addr, so skip it
625 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000626}
627
George Rimar6f17e092015-12-17 09:32:21 +0000628// In some conditions, relocations can be optimized to avoid using GOT.
629// This function does that for Initial Exec to Local Exec case.
630// Read "ELF Handling For Thread-Local Storage, 5.1
631// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000632// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000633
634size_t X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000635 uint32_t Type, uint64_t P,
636 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000637 // Ulrich's document section 6.2 says that @gotntpoff can
638 // be used with MOVL or ADDL instructions.
639 // @indntpoff is similar to @gotntpoff, but for use in
640 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000641 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000642 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000643 uint8_t Reg = (Loc[-1] >> 3) & 7;
644 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000645 if (Type == R_386_TLS_IE) {
646 // For R_386_TLS_IE relocation we perform the next transformations:
647 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
648 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
649 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
650 // First one is special because when EAX is used the sequence is 5 bytes
651 // long, otherwise it is 6 bytes.
652 if (*Op == 0xa1) {
653 *Op = 0xb8;
654 } else {
655 *Inst = IsMov ? 0xc7 : 0x81;
656 *Op = 0xc0 | ((*Op >> 3) & 7);
657 }
658 } else {
659 // R_386_TLS_GOTIE relocation can be optimized to
660 // R_386_TLS_LE so that it does not use GOT.
661 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
662 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
663 // Note: gold converts to ADDL instead of LEAL.
664 *Inst = IsMov ? 0xc7 : 0x8d;
665 if (IsMov)
666 *Op = 0xc0 | ((*Op >> 3) & 7);
667 else
668 *Op = 0x80 | Reg | (Reg << 3);
669 }
George Rimar2558e122015-12-09 09:55:54 +0000670 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000671
672 return 0;
673}
674
675size_t X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
676 uint32_t Type, uint64_t P,
677 uint64_t SA) const {
678 if (Type == R_386_TLS_LDO_32) {
679 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
680 return 0;
681 }
682
683 // LD can be optimized to LE:
684 // leal foo(%reg),%eax
685 // call ___tls_get_addr
686 // Is converted to:
687 // movl %gs:0,%eax
688 // nop
689 // leal 0(%esi,1),%esi
690 const uint8_t Inst[] = {
691 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
692 0x90, // nop
693 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
694 };
695 memcpy(Loc - 2, Inst, sizeof(Inst));
696
697 // The next relocation should be against __tls_get_addr, so skip it
698 return 1;
George Rimar2558e122015-12-09 09:55:54 +0000699}
700
Rafael Espindola7f074422015-09-22 21:35:51 +0000701X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000702 CopyRel = R_X86_64_COPY;
703 GotRel = R_X86_64_GLOB_DAT;
704 PltRel = R_X86_64_JUMP_SLOT;
705 RelativeRel = R_X86_64_RELATIVE;
706 IRelativeRel = R_X86_64_IRELATIVE;
707 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000708 TlsModuleIndexRel = R_X86_64_DTPMOD64;
709 TlsOffsetRel = R_X86_64_DTPOFF64;
710 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000711 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000712 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000713}
714
Rui Ueyamac516ae12016-01-29 02:33:45 +0000715void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000716 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
717}
718
Rui Ueyamac516ae12016-01-29 02:33:45 +0000719void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000720 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000721 write32le(Buf, Plt + 6);
722}
723
Rui Ueyama900e2d22016-01-29 03:51:49 +0000724void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000725 const uint8_t PltData[] = {
726 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
727 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
728 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
729 };
730 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000731 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
732 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
733 write32le(Buf + 2, Got - Plt + 2); // GOT+8
734 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000735}
Rafael Espindola01205f72015-09-22 18:19:46 +0000736
Rui Ueyama9398f862016-01-29 04:15:02 +0000737void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
738 uint64_t PltEntryAddr, int32_t Index,
739 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000740 const uint8_t Inst[] = {
741 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
742 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
743 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
744 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000745 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000746
George Rimar648a2c32015-10-20 08:54:27 +0000747 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
748 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000749 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000750}
751
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000752bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
753 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
754 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000755}
756
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000757bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000758 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
759 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000760}
761
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000762bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000763 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000764 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000765 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000766 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000767 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000768}
769
George Rimar86971052016-03-29 08:35:42 +0000770uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
771 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
772 if (Config->Shared)
773 error(getELFRelocationTypeName(EM_X86_64, Type) +
774 " cannot be a dynamic relocation");
775 return Type;
776}
777
George Rimar98b060d2016-03-06 06:01:07 +0000778uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000779 // No other types of TLS relocations requiring GOT should
780 // reach here.
781 assert(Type == R_X86_64_GOTTPOFF);
782 return R_X86_64_PC32;
783}
784
George Rimar98b060d2016-03-06 06:01:07 +0000785bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000786 return Type == R_X86_64_GOTTPOFF;
787}
788
George Rimar98b060d2016-03-06 06:01:07 +0000789bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000790 return Type == R_X86_64_TLSGD;
791}
792
George Rimar98b060d2016-03-06 06:01:07 +0000793bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000794 return Type == R_X86_64_TLSLD;
795}
796
George Rimar98b060d2016-03-06 06:01:07 +0000797bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000798 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
799 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000800}
801
Rafael Espindola993f0272016-02-26 14:27:47 +0000802bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
803 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000804}
Rafael Espindolac4010882015-09-22 20:54:08 +0000805
Rafael Espindolaae244002015-10-05 19:30:12 +0000806bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
807 switch (Type) {
808 default:
809 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000810 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000811 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000812 case R_X86_64_PC8:
813 case R_X86_64_PC16:
814 case R_X86_64_PC32:
815 case R_X86_64_PC64:
816 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000817 return true;
818 }
819}
820
Rui Ueyamac516ae12016-01-29 02:33:45 +0000821bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000822 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000823}
824
George Rimar6713cf82015-11-25 21:46:05 +0000825// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
826// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000827// how GD can be optimized to LE:
828// .byte 0x66
829// leaq x@tlsgd(%rip), %rdi
830// .word 0x6666
831// rex64
832// call __tls_get_addr@plt
833// Is converted to:
834// mov %fs:0x0,%rax
835// lea x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000836size_t X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000837 uint32_t Type, uint64_t P,
838 uint64_t SA) const {
George Rimar6713cf82015-11-25 21:46:05 +0000839 const uint8_t Inst[] = {
840 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
841 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
842 };
843 memcpy(Loc - 4, Inst, sizeof(Inst));
844 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000845
846 // The next relocation should be against __tls_get_addr, so skip it
847 return 1;
George Rimar77d1cb12015-11-24 09:00:06 +0000848}
849
George Rimar25411f252015-12-04 11:20:13 +0000850// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
851// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
852// how GD can be optimized to IE:
853// .byte 0x66
854// leaq x@tlsgd(%rip), %rdi
855// .word 0x6666
856// rex64
857// call __tls_get_addr@plt
858// Is converted to:
859// mov %fs:0x0,%rax
860// addq x@tpoff,%rax
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000861size_t X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
862 uint32_t Type, uint64_t P,
863 uint64_t SA) const {
George Rimar25411f252015-12-04 11:20:13 +0000864 const uint8_t Inst[] = {
865 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
866 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
867 };
868 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000869 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000870
871 // The next relocation should be against __tls_get_addr, so skip it
872 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000873}
874
George Rimar77d1cb12015-11-24 09:00:06 +0000875// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000876// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000877// This function does that. Read "ELF Handling For Thread-Local Storage,
878// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
879// by Ulrich Drepper for details.
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000880size_t X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +0000881 uint32_t Type, uint64_t P,
882 uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000883 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
884 // used in MOVQ or ADDQ instructions only.
885 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
886 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
887 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
888 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
889 uint8_t *Prefix = Loc - 3;
890 uint8_t *Inst = Loc - 2;
891 uint8_t *RegSlot = Loc - 1;
892 uint8_t Reg = Loc[-1] >> 3;
893 bool IsMov = *Inst == 0x8b;
894 bool RspAdd = !IsMov && Reg == 4;
895 // r12 and rsp registers requires special handling.
896 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
897 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
898 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
899 // The same true for rsp. So we convert to addq for them, saving 1 byte that
900 // we dont have.
901 if (RspAdd)
902 *Inst = 0x81;
903 else
904 *Inst = IsMov ? 0xc7 : 0x8d;
905 if (*Prefix == 0x4c)
906 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
907 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
908 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000909 return 0;
George Rimar77d1cb12015-11-24 09:00:06 +0000910}
911
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000912// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
913// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
914// how LD can be optimized to LE:
915// leaq bar@tlsld(%rip), %rdi
916// callq __tls_get_addr@PLT
917// leaq bar@dtpoff(%rax), %rcx
918// Is converted to:
919// .word 0x6666
920// .byte 0x66
921// mov %fs:0,%rax
922// leaq bar@tpoff(%rax), %rcx
923size_t X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
924 uint32_t Type, uint64_t P,
925 uint64_t SA) const {
926 if (Type == R_X86_64_DTPOFF64) {
George Rimar1452f482016-03-10 18:57:17 +0000927 write64le(Loc, SA - Out<ELF64LE>::TlsPhdr->p_memsz);
928 return 0;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000929 }
930 if (Type == R_X86_64_DTPOFF32) {
931 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000932 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000933 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000934
935 const uint8_t Inst[] = {
936 0x66, 0x66, //.word 0x6666
937 0x66, //.byte 0x66
938 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
939 };
940 memcpy(Loc - 3, Inst, sizeof(Inst));
941 // The next relocation should be against __tls_get_addr, so skip it
942 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000943}
944
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000945void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +0000946 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000947 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000948 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000949 checkUInt<32>(SA, Type);
950 write32le(Loc, SA);
951 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000952 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000953 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000954 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000955 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000956 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000957 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000958 write64le(Loc, SA);
959 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000960 case R_X86_64_DTPOFF32:
961 write32le(Loc, SA);
962 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000963 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000964 case R_X86_64_GOTPCRELX:
965 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000966 case R_X86_64_PC32:
967 case R_X86_64_PLT32:
968 case R_X86_64_TLSGD:
969 case R_X86_64_TLSLD:
970 write32le(Loc, SA - P);
971 break;
George Rimar48651482015-12-11 08:59:37 +0000972 case R_X86_64_SIZE32:
Rafael Espindola287e1002016-03-30 13:27:50 +0000973 write32le(Loc, SA);
George Rimar48651482015-12-11 08:59:37 +0000974 break;
975 case R_X86_64_SIZE64:
Rafael Espindola287e1002016-03-30 13:27:50 +0000976 write64le(Loc, SA);
George Rimar48651482015-12-11 08:59:37 +0000977 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000978 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000979 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000980 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000981 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000982 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000983 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000984 default:
George Rimar57610422016-03-11 14:43:02 +0000985 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000986 }
987}
988
Hal Finkel3c8cc672015-10-12 20:56:18 +0000989// Relocation masks following the #lo(value), #hi(value), #ha(value),
990// #higher(value), #highera(value), #highest(value), and #highesta(value)
991// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
992// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000993static uint16_t applyPPCLo(uint64_t V) { return V; }
994static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
995static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
996static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
997static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000998static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000999static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
1000
Davide Italiano8c3444362016-01-11 19:45:33 +00001001PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +00001002bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
1003
1004void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +00001005 uint64_t P, uint64_t SA) const {
Davide Italiano8c3444362016-01-11 19:45:33 +00001006 switch (Type) {
1007 case R_PPC_ADDR16_HA:
1008 write16be(Loc, applyPPCHa(SA));
1009 break;
1010 case R_PPC_ADDR16_LO:
1011 write16be(Loc, applyPPCLo(SA));
1012 break;
1013 default:
George Rimar57610422016-03-11 14:43:02 +00001014 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +00001015 }
1016}
1017
Rafael Espindolac4010882015-09-22 20:54:08 +00001018PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001019 GotRel = R_PPC64_GLOB_DAT;
1020 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001021 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001022
1023 // We need 64K pages (at least under glibc/Linux, the loader won't
1024 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001025 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001026
1027 // The PPC64 ELF ABI v1 spec, says:
1028 //
1029 // It is normally desirable to put segments with different characteristics
1030 // in separate 256 Mbyte portions of the address space, to give the
1031 // operating system full paging flexibility in the 64-bit address space.
1032 //
1033 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1034 // use 0x10000000 as the starting address.
1035 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001036}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001037
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001038uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001039 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1040 // order. The TOC starts where the first of these sections starts.
1041
1042 // FIXME: This obviously does not do the right thing when there is no .got
1043 // section, but there is a .toc or .tocbss section.
1044 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1045 if (!TocVA)
1046 TocVA = Out<ELF64BE>::Plt->getVA();
1047
1048 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1049 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1050 // code (crt1.o) assumes that you can get from the TOC base to the
1051 // start of the .toc section with only a single (signed) 16-bit relocation.
1052 return TocVA + 0x8000;
1053}
1054
Rui Ueyama9398f862016-01-29 04:15:02 +00001055void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1056 uint64_t PltEntryAddr, int32_t Index,
1057 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1059
1060 // FIXME: What we should do, in theory, is get the offset of the function
1061 // descriptor in the .opd section, and use that as the offset from %r2 (the
1062 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1063 // be a pointer to the function descriptor in the .opd section. Using
1064 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1065
Hal Finkelfa92f682015-10-13 21:47:34 +00001066 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001067 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1068 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1069 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1070 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1071 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1072 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1073 write32be(Buf + 28, 0x4e800420); // bctr
1074}
1075
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001076bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001077 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 return true;
1079
1080 switch (Type) {
1081 default: return false;
1082 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001083 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001084 case R_PPC64_GOT16_HA:
1085 case R_PPC64_GOT16_HI:
1086 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 case R_PPC64_GOT16_LO_DS:
1088 return true;
1089 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001090}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001091
Rafael Espindola993f0272016-02-26 14:27:47 +00001092bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001093 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001094 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001095}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001096
Hal Finkelbe0823d2015-10-12 20:58:52 +00001097bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1098 switch (Type) {
1099 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001100 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001101 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001102 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001103 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001104 }
1105}
1106
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001107void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +00001108 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109 uint64_t TB = getPPC64TocBase();
1110
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1112 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001113 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001114 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1115 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001116 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1117 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001118 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1119 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 default: break;
1121 }
1122
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001124 case R_PPC64_ADDR14: {
1125 checkAlignment<4>(SA, Type);
1126 // Preserve the AA/LK bits in the branch instruction
1127 uint8_t AALK = Loc[3];
1128 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1129 break;
1130 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001132 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001133 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001134 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001135 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001136 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001137 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001138 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001139 case R_PPC64_ADDR16_HA:
1140 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001141 break;
1142 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001143 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001144 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001146 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001147 break;
1148 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001149 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001150 break;
1151 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001152 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001153 break;
1154 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001155 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001156 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001157 case R_PPC64_ADDR16_LO:
1158 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001159 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001160 case R_PPC64_ADDR16_LO_DS:
1161 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001162 break;
1163 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001164 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001165 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001166 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001167 case R_PPC64_ADDR64:
1168 write64be(Loc, SA);
1169 break;
1170 case R_PPC64_REL16_HA:
1171 write16be(Loc, applyPPCHa(SA - P));
1172 break;
1173 case R_PPC64_REL16_HI:
1174 write16be(Loc, applyPPCHi(SA - P));
1175 break;
1176 case R_PPC64_REL16_LO:
1177 write16be(Loc, applyPPCLo(SA - P));
1178 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001179 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001180 // If we have an undefined weak symbol, we might get here with a symbol
1181 // address of zero. That could overflow, but the code must be unreachable,
1182 // so don't bother doing anything at all.
1183 if (!SA)
1184 break;
1185
Hal Finkeldaedc122015-10-12 23:16:53 +00001186 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1187 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001188 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001189
1190 if (!InPlt && Out<ELF64BE>::Opd) {
1191 // If this is a local call, and we currently have the address of a
1192 // function-descriptor, get the underlying code address instead.
1193 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1194 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001195 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001196
1197 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001198 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001199 }
1200
Hal Finkel3c8cc672015-10-12 20:56:18 +00001201 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001202 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001203 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001204
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001205 uint32_t Nop = 0x60000000;
1206 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1207 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001208 break;
1209 }
1210 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001211 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001212 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001213 break;
1214 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001215 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001216 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001217 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001218 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001219 break;
1220 default:
George Rimar57610422016-03-11 14:43:02 +00001221 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001222 }
1223}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001224
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001225AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001226 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001227 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001228 IRelativeRel = R_AARCH64_IRELATIVE;
1229 GotRel = R_AARCH64_GLOB_DAT;
1230 PltRel = R_AARCH64_JUMP_SLOT;
1231 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001232 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1233 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001234 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001235 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001236 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001237}
George Rimar648a2c32015-10-20 08:54:27 +00001238
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001239bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001240 switch (Type) {
1241 default:
1242 return false;
1243 case R_AARCH64_PREL32:
1244 case R_AARCH64_ADR_PREL_LO21:
1245 case R_AARCH64_ADR_PREL_PG_HI21:
1246 case R_AARCH64_ADR_GOT_PAGE:
1247 case R_AARCH64_LDST8_ABS_LO12_NC:
1248 case R_AARCH64_LDST16_ABS_LO12_NC:
1249 case R_AARCH64_LDST32_ABS_LO12_NC:
1250 case R_AARCH64_LDST64_ABS_LO12_NC:
1251 case R_AARCH64_LDST128_ABS_LO12_NC:
1252 case R_AARCH64_ADD_ABS_LO12_NC:
1253 case R_AARCH64_CALL26:
1254 case R_AARCH64_JUMP26:
1255 case R_AARCH64_CONDBR19:
1256 case R_AARCH64_TSTBR14:
Rafael Espindola07275532016-03-28 01:31:11 +00001257 case R_AARCH64_PREL64:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001258 return true;
1259 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001260}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001261
George Rimar98b060d2016-03-06 06:01:07 +00001262bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001263 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1264 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1265 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1266 Type == R_AARCH64_TLSDESC_CALL;
1267}
1268
George Rimar98b060d2016-03-06 06:01:07 +00001269bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001270 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1271 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1272}
1273
George Rimar98b060d2016-03-06 06:01:07 +00001274uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001275 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1276 return Type;
1277 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001278 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001279 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001280 // Keep it going with a dummy value so that we can find more reloc errors.
1281 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001282}
1283
Rui Ueyamac516ae12016-01-29 02:33:45 +00001284void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001285 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1286}
1287
Rui Ueyama900e2d22016-01-29 03:51:49 +00001288void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001289 const uint8_t PltData[] = {
1290 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1291 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1292 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1293 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1294 0x20, 0x02, 0x1f, 0xd6, // br x17
1295 0x1f, 0x20, 0x03, 0xd5, // nop
1296 0x1f, 0x20, 0x03, 0xd5, // nop
1297 0x1f, 0x20, 0x03, 0xd5 // nop
1298 };
1299 memcpy(Buf, PltData, sizeof(PltData));
1300
Rui Ueyama900e2d22016-01-29 03:51:49 +00001301 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1302 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1303 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1304 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1305 Got + 16);
1306 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1307 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001308}
1309
Rui Ueyama9398f862016-01-29 04:15:02 +00001310void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1311 uint64_t PltEntryAddr, int32_t Index,
1312 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001313 const uint8_t Inst[] = {
1314 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1315 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1316 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1317 0x20, 0x02, 0x1f, 0xd6 // br x17
1318 };
1319 memcpy(Buf, Inst, sizeof(Inst));
1320
1321 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1322 GotEntryAddr);
1323 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1324 GotEntryAddr);
1325 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1326 GotEntryAddr);
1327}
1328
George Rimar98b060d2016-03-06 06:01:07 +00001329uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001330 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001331 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1332 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001333}
1334
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001335bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001336 switch (Type) {
1337 default:
1338 return false;
1339 case R_AARCH64_ABS16:
1340 case R_AARCH64_ABS32:
1341 case R_AARCH64_ABS64:
1342 case R_AARCH64_ADD_ABS_LO12_NC:
1343 case R_AARCH64_ADR_PREL_LO21:
1344 case R_AARCH64_ADR_PREL_PG_HI21:
1345 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001346 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001347 case R_AARCH64_LDST32_ABS_LO12_NC:
1348 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001349 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001350 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001351 }
1352}
1353
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001354bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001355 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001356 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001357 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001358 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001359 case R_AARCH64_ADR_GOT_PAGE:
1360 case R_AARCH64_LD64_GOT_LO12_NC:
1361 return true;
1362 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001363 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001364 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001365}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001366
Rafael Espindola993f0272016-02-26 14:27:47 +00001367bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001368 switch (Type) {
1369 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001370 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001371 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001372 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001373 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001374 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001375 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001376 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001377}
Davide Italiano1d750a62015-09-27 08:45:38 +00001378
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001379static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001380 uint32_t ImmLo = (Imm & 0x3) << 29;
1381 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1382 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001383 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001384}
1385
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001386static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1387 or32le(L, (Imm & 0xFFF) << 10);
1388}
1389
Davide Italiano318ca222015-10-02 22:13:51 +00001390// Page(Expr) is the page address of the expression Expr, defined
1391// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001392// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001393static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001394 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001395}
1396
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001397void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola287e1002016-03-30 13:27:50 +00001398 uint32_t Type, uint64_t P,
1399 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001400 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001401 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001402 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001403 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001404 break;
1405 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001406 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001407 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001408 break;
1409 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001410 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001411 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001412 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001413 // This relocation stores 12 bits and there's no instruction
1414 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001415 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1416 // bits in Loc are zero.
1417 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001418 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001419 case R_AARCH64_ADR_GOT_PAGE: {
1420 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1421 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001422 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001423 break;
1424 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001425 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001426 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001427 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001428 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001429 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001430 }
George Rimar3d737e42016-01-13 13:04:46 +00001431 case R_AARCH64_ADR_PREL_PG_HI21:
1432 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001433 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001434 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001435 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001436 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001437 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001438 case R_AARCH64_CALL26:
1439 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001440 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001441 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001442 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1443 break;
1444 }
George Rimar4102bfb2016-01-11 14:22:00 +00001445 case R_AARCH64_CONDBR19: {
1446 uint64_t X = SA - P;
1447 checkInt<21>(X, Type);
1448 or32le(Loc, (X & 0x1FFFFC) << 3);
1449 break;
1450 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001451 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001452 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001453 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001454 or32le(Loc, (SA & 0xFF8) << 7);
1455 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001456 case R_AARCH64_LDST128_ABS_LO12_NC:
1457 or32le(Loc, (SA & 0x0FF8) << 6);
1458 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001459 case R_AARCH64_LDST16_ABS_LO12_NC:
1460 or32le(Loc, (SA & 0x0FFC) << 9);
1461 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001462 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001463 or32le(Loc, (SA & 0xFFF) << 10);
1464 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001465 case R_AARCH64_LDST32_ABS_LO12_NC:
1466 or32le(Loc, (SA & 0xFFC) << 8);
1467 break;
1468 case R_AARCH64_LDST64_ABS_LO12_NC:
1469 or32le(Loc, (SA & 0xFF8) << 7);
1470 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001471 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001472 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001473 write16le(Loc, SA - P);
1474 break;
1475 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001476 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001477 write32le(Loc, SA - P);
1478 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001479 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001480 write64le(Loc, SA - P);
1481 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001482 case R_AARCH64_TSTBR14: {
1483 uint64_t X = SA - P;
1484 checkInt<16>(X, Type);
1485 or32le(Loc, (X & 0xFFFC) << 3);
1486 break;
1487 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001488 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1489 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1490 checkInt<24>(V, Type);
1491 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1492 break;
1493 }
1494 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1495 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1496 updateAArch64Add(Loc, V & 0xFFF);
1497 break;
1498 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001499 default:
George Rimar57610422016-03-11 14:43:02 +00001500 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001501 }
1502}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001503
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001504size_t AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001505 uint32_t Type, uint64_t P,
1506 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001507 // TLSDESC Global-Dynamic relocation are in the form:
1508 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1509 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1510 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1511 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1512 // And it can optimized to:
1513 // movz x0, #0x0, lsl #16
1514 // movk x0, #0x10
1515 // nop
1516 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001517 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1518 uint64_t X = SA + TPOff;
1519 checkUInt<32>(X, Type);
1520
1521 uint32_t NewInst;
1522 switch (Type) {
1523 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1524 case R_AARCH64_TLSDESC_CALL:
1525 // nop
1526 NewInst = 0xd503201f;
1527 break;
1528 case R_AARCH64_TLSDESC_ADR_PAGE21:
1529 // movz
1530 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1531 break;
1532 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1533 // movk
1534 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1535 break;
1536 default:
George Rimar777f9632016-03-12 08:31:34 +00001537 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001538 }
1539 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001540
1541 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001542}
1543
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001544size_t AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001545 uint32_t Type, uint64_t P,
1546 uint64_t SA) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001547 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1548 uint64_t X = SA + TPOff;
1549 checkUInt<32>(X, Type);
1550
George Rimar4d1d16d2016-03-06 06:16:05 +00001551 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001552 uint32_t NewInst;
1553 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1554 // Generate movz.
1555 unsigned RegNo = (Inst & 0x1f);
1556 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1557 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1558 // Generate movk
1559 unsigned RegNo = (Inst & 0x1f);
1560 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1561 } else {
George Rimar777f9632016-03-12 08:31:34 +00001562 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001563 }
1564 write32le(Loc, NewInst);
Rafael Espindola89cc14f2016-03-16 19:03:58 +00001565
1566 return 0;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001567}
1568
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001569// Implementing relocations for AMDGPU is low priority since most
1570// programs don't use relocations now. Thus, this function is not
1571// actually called (relocateOne is called for each relocation).
1572// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001573void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
Rafael Espindola287e1002016-03-30 13:27:50 +00001574 uint64_t P, uint64_t SA) const {
George Rimar57610422016-03-11 14:43:02 +00001575 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001576}
1577
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001578template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001579 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001580 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001581 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001582 PltEntrySize = 16;
1583 PltZeroSize = 32;
1584 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001585 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001586 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001587 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001588}
1589
1590template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001591uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001592 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1593 return R_MIPS_REL32;
1594 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001595 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001596 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001597 // Keep it going with a dummy value so that we can find more reloc errors.
1598 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001599}
1600
1601template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001602void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001603 typedef typename ELFT::Off Elf_Off;
1604 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001605
1606 // Set the MSB of the second GOT slot. This is not required by any
1607 // MIPS ABI documentation, though.
1608 //
1609 // There is a comment in glibc saying that "The MSB of got[1] of a
1610 // gnu object is set to identify gnu objects," and in GNU gold it
1611 // says "the second entry will be used by some runtime loaders".
1612 // But how this field is being used is unclear.
1613 //
1614 // We are not really willing to mimic other linkers behaviors
1615 // without understanding why they do that, but because all files
1616 // generated by GNU tools have this special GOT value, and because
1617 // we've been doing this for years, it is probably a safe bet to
1618 // keep doing this for now. We really need to revisit this to see
1619 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001620 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001621 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001622}
1623
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001624template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001625void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1626 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001627}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001628
Simon Atanasyan35031192015-12-15 06:06:34 +00001629static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001630
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001631template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001632static int64_t getPcRelocAddend(uint8_t *Loc) {
1633 uint32_t Instr = read32<E>(Loc);
1634 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1635 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1636}
1637
1638template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001639static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001640 uint64_t SA) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001641 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001642 uint32_t Instr = read32<E>(Loc);
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001643 int64_t V = SA - P;
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001644 if (SHIFT > 0)
1645 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001646 checkInt<BSIZE + SHIFT>(V, Type);
1647 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001648}
1649
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001650template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001651static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001652 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001653 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001654}
1655
Simon Atanasyan3b377852016-03-04 10:55:20 +00001656template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001657static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1658 uint32_t Instr = read32<E>(Loc);
1659 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1660}
1661
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001662template <endianness E> static int16_t readSignedLo16(uint8_t *Loc) {
1663 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1664}
1665
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001666template <class ELFT>
1667void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1668 const endianness E = ELFT::TargetEndianness;
1669 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1670 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1671 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1672 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1673 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1674 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1675 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1676 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1677 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001678 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001679 writeMipsLo16<E>(Buf + 4, Got);
1680 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001681}
1682
1683template <class ELFT>
1684void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1685 uint64_t PltEntryAddr, int32_t Index,
1686 unsigned RelOff) const {
1687 const endianness E = ELFT::TargetEndianness;
1688 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1689 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1690 write32<E>(Buf + 8, 0x03200008); // jr $25
1691 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001692 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001693 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1694 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001695}
1696
1697template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001698bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001699 return !isRelRelative(Type);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001700}
1701
1702template <class ELFT>
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001703bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001704 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001705}
1706
1707template <class ELFT>
1708bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1709 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001710}
1711
1712template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001713bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1714 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001715}
1716
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001717template <class ELFT>
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001718uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(uint8_t *Buf,
1719 uint32_t Type) const {
1720 const endianness E = ELFT::TargetEndianness;
1721 switch (Type) {
1722 default:
1723 return 0;
1724 case R_MIPS_32:
1725 case R_MIPS_GPREL32:
1726 return read32<E>(Buf);
1727 case R_MIPS_26:
1728 // FIXME (simon): If the relocation target symbol is not a PLT entry
1729 // we should use another expression for calculation:
1730 // ((A << 2) | (P & 0xf0000000)) >> 2
1731 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1732 case R_MIPS_GPREL16:
1733 case R_MIPS_LO16:
1734 case R_MIPS_PCLO16:
1735 case R_MIPS_TLS_DTPREL_HI16:
1736 case R_MIPS_TLS_DTPREL_LO16:
1737 case R_MIPS_TLS_TPREL_HI16:
1738 case R_MIPS_TLS_TPREL_LO16:
1739 return readSignedLo16<E>(Buf);
1740 case R_MIPS_PC16:
1741 return getPcRelocAddend<E, 16, 2>(Buf);
1742 case R_MIPS_PC19_S2:
1743 return getPcRelocAddend<E, 19, 2>(Buf);
1744 case R_MIPS_PC21_S2:
1745 return getPcRelocAddend<E, 21, 2>(Buf);
1746 case R_MIPS_PC26_S2:
1747 return getPcRelocAddend<E, 26, 2>(Buf);
1748 case R_MIPS_PC32:
1749 return getPcRelocAddend<E, 32, 0>(Buf);
1750 }
1751}
1752
1753template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001754void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Rafael Espindola287e1002016-03-30 13:27:50 +00001755 uint32_t Type, uint64_t P,
1756 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001757 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001758 // Thread pointer and DRP offsets from the start of TLS data area.
1759 // https://www.linux-mips.org/wiki/NPTL
1760 const uint32_t TPOffset = 0x7000;
1761 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001762 switch (Type) {
1763 case R_MIPS_32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001764 write32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001765 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001766 case R_MIPS_26: {
1767 uint32_t Instr = read32<E>(Loc);
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001768 write32<E>(Loc, (Instr & ~0x3ffffff) | (SA >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001769 break;
1770 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001771 case R_MIPS_CALL16:
1772 case R_MIPS_GOT16: {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001773 int64_t V = SA - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001774 if (Type == R_MIPS_GOT16)
1775 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001776 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001777 break;
1778 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001779 case R_MIPS_GPREL16: {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001780 int64_t V = SA - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001781 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001782 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001783 break;
1784 }
1785 case R_MIPS_GPREL32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001786 write32<E>(Loc, SA - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001787 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001788 case R_MIPS_HI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001789 writeMipsHi16<E>(Loc, SA);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001790 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001791 case R_MIPS_JALR:
1792 // Ignore this optimization relocation for now
1793 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001794 case R_MIPS_LO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001795 writeMipsLo16<E>(Loc, SA);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001796 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001797 case R_MIPS_PC16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001798 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001799 break;
1800 case R_MIPS_PC19_S2:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001801 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001802 break;
1803 case R_MIPS_PC21_S2:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001804 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001805 break;
1806 case R_MIPS_PC26_S2:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001807 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, SA);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001808 break;
1809 case R_MIPS_PC32:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001810 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, SA);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001811 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001812 case R_MIPS_PCHI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001813 writeMipsHi16<E>(Loc, SA - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001814 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001815 case R_MIPS_PCLO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001816 writeMipsLo16<E>(Loc, SA - P);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001817 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001818 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001819 writeMipsHi16<E>(Loc, SA - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001820 break;
1821 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001822 writeMipsLo16<E>(Loc, SA - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001823 break;
1824 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001825 writeMipsHi16<E>(Loc, SA - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001826 break;
1827 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001828 writeMipsLo16<E>(Loc, SA - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001829 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001830 default:
George Rimar57610422016-03-11 14:43:02 +00001831 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001832 }
1833}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001834
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001835template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001836bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001837 return Type == R_MIPS_JALR;
1838}
1839
1840template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001841bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1842 switch (Type) {
1843 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001844 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001845 case R_MIPS_26:
1846 case R_MIPS_32:
1847 case R_MIPS_64:
1848 case R_MIPS_HI16:
Simon Atanasyancf8c42f2016-03-30 22:43:14 +00001849 case R_MIPS_LO16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001850 case R_MIPS_TLS_DTPREL_HI16:
1851 case R_MIPS_TLS_DTPREL_LO16:
1852 case R_MIPS_TLS_TPREL_HI16:
1853 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001854 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001855 }
1856}
1857
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001858// _gp is a MIPS-specific ABI-defined symbol which points to
1859// a location that is relative to GOT. This function returns
1860// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001861template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001862 unsigned GPOffset = 0x7ff0;
1863 if (uint64_t V = Out<ELFT>::Got->getVA())
1864 return V + GPOffset;
1865 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001866}
1867
1868template uint32_t getMipsGpAddr<ELF32LE>();
1869template uint32_t getMipsGpAddr<ELF32BE>();
1870template uint64_t getMipsGpAddr<ELF64LE>();
1871template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001872
1873template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1874 const SymbolBody &) const;
1875template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1876 const SymbolBody &) const;
1877template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1878 const SymbolBody &) const;
1879template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1880 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001881}
1882}