blob: afaa9b5ad4b9d36fa6c9c1eb7a1d2aaa4a11bd39 [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
34namespace elf2 {
35
Rui Ueyamac1c282a2016-02-11 21:18:01 +000036TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000037
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000049 error("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050}
51
52template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
53 if (isUInt<N>(V))
54 return;
55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000056 error("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000057}
58
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
60 if (isInt<N>(V) || isUInt<N>(V))
61 return;
62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000063 error("Relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064}
65
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
67 if ((V & (N - 1)) == 0)
68 return;
69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000070 error("Improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
George Rimara07ff662015-12-21 10:12:06 +000073template <class ELFT> bool isGnuIFunc(const SymbolBody &S) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000074 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S))
George Rimara07ff662015-12-21 10:12:06 +000075 return SS->Sym.getType() == STT_GNU_IFUNC;
76 return false;
77}
78
Rui Ueyamaefc23de2015-10-14 21:30:32 +000079namespace {
80class X86TargetInfo final : public TargetInfo {
81public:
82 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000083 void writeGotPltHeader(uint8_t *Buf) const override;
84 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000085 unsigned getTlsGotRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +000086 bool isTlsLocalDynamicRel(unsigned Type) const override;
87 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000088 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
89 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000090 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000091 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
92 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000093 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000094 bool needsDynRelative(unsigned Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000095 bool needsGot(uint32_t Type, SymbolBody &S) const override;
96 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000097 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000098 uint64_t SA, uint64_t ZA = 0,
99 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000100 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000101 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
102 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000103 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000104
105private:
106 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
107 uint64_t SA) const;
108 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
109 uint64_t SA) const;
110 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
111 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000112 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
113 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000114};
115
116class X86_64TargetInfo final : public TargetInfo {
117public:
118 X86_64TargetInfo();
George Rimar2960c982016-02-11 11:14:46 +0000119 unsigned getTlsGotRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000120 bool isTlsLocalDynamicRel(unsigned Type) const override;
121 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000122 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
123 void writeGotPltHeader(uint8_t *Buf) const override;
124 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000125 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000126 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
127 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000128 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000129 bool needsGot(uint32_t Type, SymbolBody &S) const override;
130 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000131 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000132 uint64_t SA, uint64_t ZA = 0,
133 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000135 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000136 bool isSizeRel(uint32_t Type) const override;
137 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
138 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000139
140private:
141 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
142 uint64_t SA) const;
143 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000145 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000147 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000149};
150
Davide Italiano8c3444362016-01-11 19:45:33 +0000151class PPCTargetInfo final : public TargetInfo {
152public:
153 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
155 uint64_t SA, uint64_t ZA = 0,
156 uint8_t *PairedLoc = nullptr) const override;
157 bool isRelRelative(uint32_t Type) const override;
158};
159
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000160class PPC64TargetInfo final : public TargetInfo {
161public:
162 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
164 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000165 bool needsGot(uint32_t Type, SymbolBody &S) const override;
166 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000167 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000168 uint64_t SA, uint64_t ZA = 0,
169 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170 bool isRelRelative(uint32_t Type) const override;
171};
172
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173class AArch64TargetInfo final : public TargetInfo {
174public:
175 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000176 unsigned getDynRel(unsigned Type) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000177 bool isTlsGlobalDynamicRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000178 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000179 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000180 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
181 int32_t Index, unsigned RelOff) const override;
George Rimar2960c982016-02-11 11:14:46 +0000182 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000183 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000184 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000185 bool needsGot(uint32_t Type, SymbolBody &S) const override;
186 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000187 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000188 uint64_t SA, uint64_t ZA = 0,
189 uint8_t *PairedLoc = nullptr) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000190 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
191 uint64_t SA, const SymbolBody *S) const override;
192 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
193
194private:
195 void relocateTlsGdToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
196 uint64_t P, uint64_t SA) const;
197 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
198 uint64_t P, uint64_t SA) const;
199
200 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000201};
202
Tom Stellard80efb162016-01-07 03:59:08 +0000203class AMDGPUTargetInfo final : public TargetInfo {
204public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000205 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000206 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
207 uint64_t SA, uint64_t ZA = 0,
208 uint8_t *PairedLoc = nullptr) const override;
209};
210
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000211template <class ELFT> class MipsTargetInfo final : public TargetInfo {
212public:
213 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000214 unsigned getDynRel(unsigned Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000215 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
216 void writePltZero(uint8_t *Buf) const override;
217 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
218 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000219 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyane1bfc2e2016-02-08 10:05:13 +0000220 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000221 bool needsGot(uint32_t Type, SymbolBody &S) const override;
222 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000223 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000224 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000225 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000226 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000227 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000228};
229} // anonymous namespace
230
Rui Ueyama91004392015-10-13 16:08:15 +0000231TargetInfo *createTarget() {
232 switch (Config->EMachine) {
233 case EM_386:
234 return new X86TargetInfo();
235 case EM_AARCH64:
236 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000237 case EM_AMDGPU:
238 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000239 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000240 switch (Config->EKind) {
241 case ELF32LEKind:
242 return new MipsTargetInfo<ELF32LE>();
243 case ELF32BEKind:
244 return new MipsTargetInfo<ELF32BE>();
245 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000246 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000247 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000248 case EM_PPC:
249 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000250 case EM_PPC64:
251 return new PPC64TargetInfo();
252 case EM_X86_64:
253 return new X86_64TargetInfo();
254 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000255 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000256}
257
Rafael Espindola01205f72015-09-22 18:19:46 +0000258TargetInfo::~TargetInfo() {}
259
Rui Ueyamabaf16512016-01-29 00:20:12 +0000260bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000261 return false;
262}
263
Igor Kudrinf6f45472015-11-10 08:39:27 +0000264uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
265
Rui Ueyama02dfd492015-12-17 01:18:40 +0000266bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000267 return false;
268}
269
Rui Ueyama012eb782016-01-29 04:05:09 +0000270bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
271 return false;
272}
273
George Rimarbfb7bf72015-12-21 10:00:12 +0000274bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000275bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000276bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000277bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000278
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000279bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000280
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000281bool TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000282
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000283bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
284 return false;
285}
286
287bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
288 return false;
289}
290
Rui Ueyamac516ae12016-01-29 02:33:45 +0000291unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
292 uint64_t P, uint64_t SA,
293 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000294 return 0;
295}
George Rimar77d1cb12015-11-24 09:00:06 +0000296
Rafael Espindola7f074422015-09-22 21:35:51 +0000297X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000298 CopyRel = R_386_COPY;
299 GotRel = R_386_GLOB_DAT;
300 PltRel = R_386_JUMP_SLOT;
301 IRelativeRel = R_386_IRELATIVE;
302 RelativeRel = R_386_RELATIVE;
303 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000304 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
305 TlsOffsetRel = R_386_TLS_DTPOFF32;
306 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000307 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000308 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000309}
310
Rui Ueyamac516ae12016-01-29 02:33:45 +0000311void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000312 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
313}
314
Rui Ueyamac516ae12016-01-29 02:33:45 +0000315void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000316 // Entries in .got.plt initially points back to the corresponding
317 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000318 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000319}
Rafael Espindola01205f72015-09-22 18:19:46 +0000320
Rui Ueyamac516ae12016-01-29 02:33:45 +0000321unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000322 if (Type == R_386_TLS_LE)
323 return R_386_TLS_TPOFF;
324 if (Type == R_386_TLS_LE_32)
325 return R_386_TLS_TPOFF32;
326 return Type;
327}
328
Rui Ueyama724d6252016-01-29 01:49:32 +0000329unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000330 if (Type == R_386_TLS_IE)
331 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000332 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000333}
334
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000335bool X86TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
336 return Type == R_386_TLS_GD;
337}
338
339bool X86TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
340 return Type == R_386_TLS_LDM;
341}
342
Rui Ueyamac516ae12016-01-29 02:33:45 +0000343bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000344 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
345 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000346 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000347 if (Type == R_386_TLS_IE)
348 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000349 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000350}
351
Rui Ueyama900e2d22016-01-29 03:51:49 +0000352void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000353 // Executable files and shared object files have
354 // separate procedure linkage tables.
355 if (Config->Shared) {
356 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000357 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000358 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
359 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000360 };
361 memcpy(Buf, V, sizeof(V));
362 return;
363 }
George Rimar648a2c32015-10-20 08:54:27 +0000364
George Rimar77b77792015-11-25 22:15:01 +0000365 const uint8_t PltData[] = {
366 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000367 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
368 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000369 };
370 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000371 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000372 write32le(Buf + 2, Got + 4);
373 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000374}
375
Rui Ueyama9398f862016-01-29 04:15:02 +0000376void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
377 uint64_t PltEntryAddr, int32_t Index,
378 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000379 const uint8_t Inst[] = {
380 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
381 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
382 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
383 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000384 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000385
George Rimar77b77792015-11-25 22:15:01 +0000386 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
387 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000388 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
389 : Out<ELF32LE>::Got->getVA();
390 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000391 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000392 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000393}
394
Rui Ueyama02dfd492015-12-17 01:18:40 +0000395bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000396 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
397 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
398 return SS->Sym.getType() == STT_OBJECT;
399 return false;
400}
401
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000402bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000403 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000404 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000405 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000406 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000407 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000408}
409
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000410bool X86TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000411 return isGnuIFunc<ELF32LE>(S) ||
412 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000413 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000414}
415
George Rimarbfb7bf72015-12-21 10:00:12 +0000416bool X86TargetInfo::isGotRelative(uint32_t Type) const {
417 // This relocation does not require got entry,
418 // but it is relative to got and needs it to be created.
419 // Here we request for that.
420 return Type == R_386_GOTOFF;
421}
422
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000423void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000424 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000425 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000426 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000427 case R_386_32:
428 add32le(Loc, SA);
429 break;
George Rimarffb67352016-01-19 11:00:48 +0000430 case R_386_GOT32: {
431 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
432 Out<ELF32LE>::Got->getNumEntries() * 4;
433 checkInt<32>(V, Type);
434 add32le(Loc, V);
435 break;
436 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000437 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000438 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000439 break;
George Rimar13934772015-11-25 20:20:31 +0000440 case R_386_GOTPC:
441 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
442 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000443 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000444 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000445 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000446 break;
George Rimar9db204a2015-12-02 09:58:20 +0000447 case R_386_TLS_GD:
448 case R_386_TLS_LDM:
449 case R_386_TLS_TPOFF: {
450 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
451 Out<ELF32LE>::Got->getNumEntries() * 4;
452 checkInt<32>(V, Type);
453 write32le(Loc, V);
454 break;
455 }
George Rimar6f17e092015-12-17 09:32:21 +0000456 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000457 case R_386_TLS_LDO_32:
458 write32le(Loc, SA);
459 break;
George Rimard23970f2015-11-25 20:41:53 +0000460 case R_386_TLS_LE:
461 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
462 break;
463 case R_386_TLS_LE_32:
464 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
465 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000466 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000467 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000468 }
469}
470
Rui Ueyamabaf16512016-01-29 00:20:12 +0000471bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000472 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000473 return false;
474 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
475 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000476 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000477 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
478}
479
Rui Ueyamac516ae12016-01-29 02:33:45 +0000480bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000481 return Config->Shared && Type == R_386_TLS_IE;
482}
483
Rui Ueyamac516ae12016-01-29 02:33:45 +0000484unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
485 uint64_t P, uint64_t SA,
486 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000487 switch (Type) {
488 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000489 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000490 relocateTlsGdToIe(Loc, BufEnd, P, SA);
491 else
492 relocateTlsGdToLe(Loc, BufEnd, P, SA);
493 // The next relocation should be against __tls_get_addr, so skip it
494 return 1;
495 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000496 case R_386_TLS_IE:
497 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000498 return 0;
499 case R_386_TLS_LDM:
500 relocateTlsLdToLe(Loc, BufEnd, P, SA);
501 // The next relocation should be against __tls_get_addr, so skip it
502 return 1;
503 case R_386_TLS_LDO_32:
504 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
505 return 0;
506 }
507 llvm_unreachable("Unknown TLS optimization");
508}
509
510// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
511// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
512// how GD can be optimized to IE:
513// leal x@tlsgd(, %ebx, 1),
514// call __tls_get_addr@plt
515// Is converted to:
516// movl %gs:0, %eax
517// addl x@gotntpoff(%ebx), %eax
518void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
519 uint64_t SA) const {
520 const uint8_t Inst[] = {
521 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
522 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
523 };
524 memcpy(Loc - 3, Inst, sizeof(Inst));
525 relocateOne(Loc + 5, BufEnd, R_386_32, P,
526 SA - Out<ELF32LE>::Got->getVA() -
527 Out<ELF32LE>::Got->getNumEntries() * 4);
528}
529
530// GD can be optimized to LE:
531// leal x@tlsgd(, %ebx, 1),
532// call __tls_get_addr@plt
533// Can be converted to:
534// movl %gs:0,%eax
535// addl $x@ntpoff,%eax
536// But gold emits subl $foo@tpoff,%eax instead of addl.
537// These instructions are completely equal in behavior.
538// This method generates subl to be consistent with gold.
539void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
540 uint64_t SA) const {
541 const uint8_t Inst[] = {
542 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
543 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
544 };
545 memcpy(Loc - 3, Inst, sizeof(Inst));
546 relocateOne(Loc + 5, BufEnd, R_386_32, P,
547 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
548}
549
550// LD can be optimized to LE:
551// leal foo(%reg),%eax
552// call ___tls_get_addr
553// Is converted to:
554// movl %gs:0,%eax
555// nop
556// leal 0(%esi,1),%esi
557void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
558 uint64_t SA) const {
559 const uint8_t Inst[] = {
560 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
561 0x90, // nop
562 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
563 };
564 memcpy(Loc - 2, Inst, sizeof(Inst));
565}
566
George Rimar6f17e092015-12-17 09:32:21 +0000567// In some conditions, relocations can be optimized to avoid using GOT.
568// This function does that for Initial Exec to Local Exec case.
569// Read "ELF Handling For Thread-Local Storage, 5.1
570// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000571// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000572void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
573 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000574 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000575 // Ulrich's document section 6.2 says that @gotntpoff can
576 // be used with MOVL or ADDL instructions.
577 // @indntpoff is similar to @gotntpoff, but for use in
578 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000579 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000580 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000581 uint8_t Reg = (Loc[-1] >> 3) & 7;
582 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000583 if (Type == R_386_TLS_IE) {
584 // For R_386_TLS_IE relocation we perform the next transformations:
585 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
586 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
587 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
588 // First one is special because when EAX is used the sequence is 5 bytes
589 // long, otherwise it is 6 bytes.
590 if (*Op == 0xa1) {
591 *Op = 0xb8;
592 } else {
593 *Inst = IsMov ? 0xc7 : 0x81;
594 *Op = 0xc0 | ((*Op >> 3) & 7);
595 }
596 } else {
597 // R_386_TLS_GOTIE relocation can be optimized to
598 // R_386_TLS_LE so that it does not use GOT.
599 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
600 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
601 // Note: gold converts to ADDL instead of LEAL.
602 *Inst = IsMov ? 0xc7 : 0x8d;
603 if (IsMov)
604 *Op = 0xc0 | ((*Op >> 3) & 7);
605 else
606 *Op = 0x80 | Reg | (Reg << 3);
607 }
George Rimar2558e122015-12-09 09:55:54 +0000608 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
609}
610
Rafael Espindola7f074422015-09-22 21:35:51 +0000611X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000612 CopyRel = R_X86_64_COPY;
613 GotRel = R_X86_64_GLOB_DAT;
614 PltRel = R_X86_64_JUMP_SLOT;
615 RelativeRel = R_X86_64_RELATIVE;
616 IRelativeRel = R_X86_64_IRELATIVE;
617 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000618 TlsModuleIndexRel = R_X86_64_DTPMOD64;
619 TlsOffsetRel = R_X86_64_DTPOFF64;
620 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000621 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000622 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000623}
624
Rui Ueyamac516ae12016-01-29 02:33:45 +0000625void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000626 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
627}
628
Rui Ueyamac516ae12016-01-29 02:33:45 +0000629void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000630 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000631 write32le(Buf, Plt + 6);
632}
633
Rui Ueyama900e2d22016-01-29 03:51:49 +0000634void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000635 const uint8_t PltData[] = {
636 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
637 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
638 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
639 };
640 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000641 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
642 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
643 write32le(Buf + 2, Got - Plt + 2); // GOT+8
644 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000645}
Rafael Espindola01205f72015-09-22 18:19:46 +0000646
Rui Ueyama9398f862016-01-29 04:15:02 +0000647void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
648 uint64_t PltEntryAddr, int32_t Index,
649 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000650 const uint8_t Inst[] = {
651 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
652 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
653 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
654 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000655 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000656
George Rimar648a2c32015-10-20 08:54:27 +0000657 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
658 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000659 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000660}
661
Rui Ueyama02dfd492015-12-17 01:18:40 +0000662bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000663 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
664 Type == R_X86_64_64)
665 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
666 return SS->Sym.getType() == STT_OBJECT;
667 return false;
668}
669
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000670bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000671 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000672 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000673 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000674 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000675 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000676}
677
George Rimar2960c982016-02-11 11:14:46 +0000678unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const {
679 // No other types of TLS relocations requiring GOT should
680 // reach here.
681 assert(Type == R_X86_64_GOTTPOFF);
682 return R_X86_64_PC32;
683}
684
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000685bool X86_64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
686 return Type == R_X86_64_TLSGD;
687}
688
689bool X86_64TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
690 return Type == R_X86_64_TLSLD;
691}
692
Rui Ueyamac516ae12016-01-29 02:33:45 +0000693bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000694 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000695}
696
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000697bool X86_64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000698 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000699 return false;
George Rimara07ff662015-12-21 10:12:06 +0000700 if (isGnuIFunc<ELF64LE>(S))
701 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000702
Rafael Espindola01205f72015-09-22 18:19:46 +0000703 switch (Type) {
704 default:
705 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000706 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000707 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000708 case R_X86_64_PC32:
709 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000710 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000711 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000712 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000713 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000714 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
715 // libc.so.
716 //
717 // The general idea on how to handle such cases is to create a PLT entry
718 // and use that as the function value.
719 //
720 // For the static linking part, we just return true and everything else
721 // will use the the PLT entry as the address.
722 //
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000723 // The remaining problem is making sure pointer equality still works. We
724 // need the help of the dynamic linker for that. We let it know that we have
725 // a direct reference to a so symbol by creating an undefined symbol with a
726 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
727 // the value of the symbol we created. This is true even for got entries, so
728 // pointer equality is maintained. To avoid an infinite loop, the only entry
729 // that points to the real function is a dedicated got entry used by the
730 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
Rafael Espindola3c412e12015-09-30 12:30:58 +0000731 // R_386_JMP_SLOT, etc).
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000732 if (!S.isShared())
733 return false;
734 S.NeedsCopyOrPltAddr = true;
735 return true;
Rafael Espindola01205f72015-09-22 18:19:46 +0000736 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000737 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000738 }
739}
Rafael Espindolac4010882015-09-22 20:54:08 +0000740
Rafael Espindolaae244002015-10-05 19:30:12 +0000741bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
742 switch (Type) {
743 default:
744 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000745 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000746 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000747 case R_X86_64_PC8:
748 case R_X86_64_PC16:
749 case R_X86_64_PC32:
750 case R_X86_64_PC64:
751 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000752 return true;
753 }
754}
755
Rui Ueyamac516ae12016-01-29 02:33:45 +0000756bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000757 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000758}
759
Rui Ueyamabaf16512016-01-29 00:20:12 +0000760bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000761 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000762 return false;
George Rimar25411f252015-12-04 11:20:13 +0000763 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
764 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000765 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
766}
767
768// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
769// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
770// how LD can be optimized to LE:
771// leaq bar@tlsld(%rip), %rdi
772// callq __tls_get_addr@PLT
773// leaq bar@dtpoff(%rax), %rcx
774// Is converted to:
775// .word 0x6666
776// .byte 0x66
777// mov %fs:0,%rax
778// leaq bar@tpoff(%rax), %rcx
779void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
780 uint64_t P, uint64_t SA) const {
781 const uint8_t Inst[] = {
782 0x66, 0x66, //.word 0x6666
783 0x66, //.byte 0x66
784 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
785 };
786 memcpy(Loc - 3, Inst, sizeof(Inst));
787}
788
789// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
790// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
791// how GD can be optimized to LE:
792// .byte 0x66
793// leaq x@tlsgd(%rip), %rdi
794// .word 0x6666
795// rex64
796// call __tls_get_addr@plt
797// Is converted to:
798// mov %fs:0x0,%rax
799// lea x@tpoff,%rax
800void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
801 uint64_t P, uint64_t SA) const {
802 const uint8_t Inst[] = {
803 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
804 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
805 };
806 memcpy(Loc - 4, Inst, sizeof(Inst));
807 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000808}
809
George Rimar25411f252015-12-04 11:20:13 +0000810// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
811// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
812// how GD can be optimized to IE:
813// .byte 0x66
814// leaq x@tlsgd(%rip), %rdi
815// .word 0x6666
816// rex64
817// call __tls_get_addr@plt
818// Is converted to:
819// mov %fs:0x0,%rax
820// addq x@tpoff,%rax
821void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
822 uint64_t P, uint64_t SA) const {
823 const uint8_t Inst[] = {
824 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
825 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
826 };
827 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000828 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000829}
830
George Rimar77d1cb12015-11-24 09:00:06 +0000831// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000832// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000833// This function does that. Read "ELF Handling For Thread-Local Storage,
834// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
835// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000836void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
837 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000838 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
839 // used in MOVQ or ADDQ instructions only.
840 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
841 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
842 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
843 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
844 uint8_t *Prefix = Loc - 3;
845 uint8_t *Inst = Loc - 2;
846 uint8_t *RegSlot = Loc - 1;
847 uint8_t Reg = Loc[-1] >> 3;
848 bool IsMov = *Inst == 0x8b;
849 bool RspAdd = !IsMov && Reg == 4;
850 // r12 and rsp registers requires special handling.
851 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
852 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
853 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
854 // The same true for rsp. So we convert to addq for them, saving 1 byte that
855 // we dont have.
856 if (RspAdd)
857 *Inst = 0x81;
858 else
859 *Inst = IsMov ? 0xc7 : 0x8d;
860 if (*Prefix == 0x4c)
861 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
862 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
863 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
864}
865
George Rimar6713cf82015-11-25 21:46:05 +0000866// This function applies a TLS relocation with an optimization as described
867// in the Ulrich's document. As a result of rewriting instructions at the
868// relocation target, relocations immediately follow the TLS relocation (which
869// would be applied to rewritten instructions) may have to be skipped.
870// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000871unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
872 uint32_t Type, uint64_t P, uint64_t SA,
873 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000874 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000875 case R_X86_64_DTPOFF32:
876 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
877 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000878 case R_X86_64_GOTTPOFF:
879 relocateTlsIeToLe(Loc, BufEnd, P, SA);
880 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000881 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000882 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000883 relocateTlsGdToIe(Loc, BufEnd, P, SA);
884 else
885 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000886 // The next relocation should be against __tls_get_addr, so skip it
887 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000888 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000889 case R_X86_64_TLSLD:
890 relocateTlsLdToLe(Loc, BufEnd, P, SA);
891 // The next relocation should be against __tls_get_addr, so skip it
892 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000893 }
894 llvm_unreachable("Unknown TLS optimization");
895}
896
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000897void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000898 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000899 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000900 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000901 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000902 checkUInt<32>(SA, Type);
903 write32le(Loc, SA);
904 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000905 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000906 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000907 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000908 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000909 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000910 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000911 write64le(Loc, SA);
912 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000913 case R_X86_64_DTPOFF32:
914 write32le(Loc, SA);
915 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000916 case R_X86_64_GOTPCREL:
917 case R_X86_64_PC32:
918 case R_X86_64_PLT32:
919 case R_X86_64_TLSGD:
920 case R_X86_64_TLSLD:
921 write32le(Loc, SA - P);
922 break;
George Rimar48651482015-12-11 08:59:37 +0000923 case R_X86_64_SIZE32:
924 write32le(Loc, ZA);
925 break;
926 case R_X86_64_SIZE64:
927 write64le(Loc, ZA);
928 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000929 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000930 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000931 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000932 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000933 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000934 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000935 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000936 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000937 }
938}
939
Hal Finkel3c8cc672015-10-12 20:56:18 +0000940// Relocation masks following the #lo(value), #hi(value), #ha(value),
941// #higher(value), #highera(value), #highest(value), and #highesta(value)
942// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
943// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000944static uint16_t applyPPCLo(uint64_t V) { return V; }
945static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
946static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
947static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
948static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000949static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000950static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
951
Davide Italiano8c3444362016-01-11 19:45:33 +0000952PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000953bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
954
955void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
956 uint64_t P, uint64_t SA, uint64_t ZA,
957 uint8_t *PairedLoc) const {
958 switch (Type) {
959 case R_PPC_ADDR16_HA:
960 write16be(Loc, applyPPCHa(SA));
961 break;
962 case R_PPC_ADDR16_LO:
963 write16be(Loc, applyPPCLo(SA));
964 break;
965 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000966 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000967 }
968}
969
Rafael Espindolac4010882015-09-22 20:54:08 +0000970PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000971 GotRel = R_PPC64_GLOB_DAT;
972 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000973 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000974
975 // We need 64K pages (at least under glibc/Linux, the loader won't
976 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000977 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000978
979 // The PPC64 ELF ABI v1 spec, says:
980 //
981 // It is normally desirable to put segments with different characteristics
982 // in separate 256 Mbyte portions of the address space, to give the
983 // operating system full paging flexibility in the 64-bit address space.
984 //
985 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
986 // use 0x10000000 as the starting address.
987 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000988}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000989
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000990uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000991 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
992 // order. The TOC starts where the first of these sections starts.
993
994 // FIXME: This obviously does not do the right thing when there is no .got
995 // section, but there is a .toc or .tocbss section.
996 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
997 if (!TocVA)
998 TocVA = Out<ELF64BE>::Plt->getVA();
999
1000 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1001 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1002 // code (crt1.o) assumes that you can get from the TOC base to the
1003 // start of the .toc section with only a single (signed) 16-bit relocation.
1004 return TocVA + 0x8000;
1005}
1006
Rui Ueyama9398f862016-01-29 04:15:02 +00001007void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1008 uint64_t PltEntryAddr, int32_t Index,
1009 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1011
1012 // FIXME: What we should do, in theory, is get the offset of the function
1013 // descriptor in the .opd section, and use that as the offset from %r2 (the
1014 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1015 // be a pointer to the function descriptor in the .opd section. Using
1016 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1017
Hal Finkelfa92f682015-10-13 21:47:34 +00001018 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001019 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1020 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1021 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1022 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1023 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1024 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1025 write32be(Buf + 28, 0x4e800420); // bctr
1026}
1027
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001028bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyamac516ae12016-01-29 02:33:45 +00001029 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 return true;
1031
1032 switch (Type) {
1033 default: return false;
1034 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001035 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001036 case R_PPC64_GOT16_HA:
1037 case R_PPC64_GOT16_HI:
1038 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001039 case R_PPC64_GOT16_LO_DS:
1040 return true;
1041 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001042}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001044bool PPC64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001045 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001046 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001047}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048
Hal Finkelbe0823d2015-10-12 20:58:52 +00001049bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1050 switch (Type) {
1051 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001052 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001053 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001054 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001055 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001056 }
1057}
1058
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001059void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001060 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001061 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001062 uint64_t TB = getPPC64TocBase();
1063
Hal Finkel3c8cc672015-10-12 20:56:18 +00001064 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1065 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001066 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001067 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1068 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001069 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1070 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001071 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1072 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 default: break;
1074 }
1075
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001077 case R_PPC64_ADDR14: {
1078 checkAlignment<4>(SA, Type);
1079 // Preserve the AA/LK bits in the branch instruction
1080 uint8_t AALK = Loc[3];
1081 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1082 break;
1083 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001085 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001086 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001087 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001088 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001089 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001090 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001091 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001092 case R_PPC64_ADDR16_HA:
1093 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001094 break;
1095 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001096 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001097 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001098 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001099 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001100 break;
1101 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001102 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 break;
1104 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001105 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001106 break;
1107 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001108 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001110 case R_PPC64_ADDR16_LO:
1111 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001112 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001113 case R_PPC64_ADDR16_LO_DS:
1114 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 break;
1116 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001117 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001118 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001119 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001120 case R_PPC64_ADDR64:
1121 write64be(Loc, SA);
1122 break;
1123 case R_PPC64_REL16_HA:
1124 write16be(Loc, applyPPCHa(SA - P));
1125 break;
1126 case R_PPC64_REL16_HI:
1127 write16be(Loc, applyPPCHi(SA - P));
1128 break;
1129 case R_PPC64_REL16_LO:
1130 write16be(Loc, applyPPCLo(SA - P));
1131 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001132 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001133 // If we have an undefined weak symbol, we might get here with a symbol
1134 // address of zero. That could overflow, but the code must be unreachable,
1135 // so don't bother doing anything at all.
1136 if (!SA)
1137 break;
1138
Hal Finkeldaedc122015-10-12 23:16:53 +00001139 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1140 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001141 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001142
1143 if (!InPlt && Out<ELF64BE>::Opd) {
1144 // If this is a local call, and we currently have the address of a
1145 // function-descriptor, get the underlying code address instead.
1146 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1147 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001148 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001149
1150 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001151 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001152 }
1153
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001155 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001156 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001157
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001158 uint32_t Nop = 0x60000000;
1159 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1160 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001161 break;
1162 }
1163 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001164 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001165 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001166 break;
1167 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001168 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001169 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001170 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001171 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001172 break;
1173 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001174 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001175 }
1176}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001177
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001178AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001179 CopyRel = R_AARCH64_COPY;
1180 IRelativeRel = R_AARCH64_IRELATIVE;
1181 GotRel = R_AARCH64_GLOB_DAT;
1182 PltRel = R_AARCH64_JUMP_SLOT;
1183 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001184 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1185 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001186 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001187 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001188 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001189}
George Rimar648a2c32015-10-20 08:54:27 +00001190
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001191bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
1192 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1193 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1194 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1195 Type == R_AARCH64_TLSDESC_CALL;
1196}
1197
Rui Ueyamac516ae12016-01-29 02:33:45 +00001198unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001199 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1200 return Type;
1201 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001202 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001203 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001204 // Keep it going with a dummy value so that we can find more reloc errors.
1205 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001206}
1207
Rui Ueyamac516ae12016-01-29 02:33:45 +00001208void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001209 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1210}
1211
Rui Ueyama900e2d22016-01-29 03:51:49 +00001212void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001213 const uint8_t PltData[] = {
1214 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1215 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1216 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1217 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1218 0x20, 0x02, 0x1f, 0xd6, // br x17
1219 0x1f, 0x20, 0x03, 0xd5, // nop
1220 0x1f, 0x20, 0x03, 0xd5, // nop
1221 0x1f, 0x20, 0x03, 0xd5 // nop
1222 };
1223 memcpy(Buf, PltData, sizeof(PltData));
1224
Rui Ueyama900e2d22016-01-29 03:51:49 +00001225 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1226 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1227 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1228 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1229 Got + 16);
1230 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1231 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001232}
1233
Rui Ueyama9398f862016-01-29 04:15:02 +00001234void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1235 uint64_t PltEntryAddr, int32_t Index,
1236 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001237 const uint8_t Inst[] = {
1238 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1239 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1240 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1241 0x20, 0x02, 0x1f, 0xd6 // br x17
1242 };
1243 memcpy(Buf, Inst, sizeof(Inst));
1244
1245 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1246 GotEntryAddr);
1247 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1248 GotEntryAddr);
1249 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1250 GotEntryAddr);
1251}
1252
Rui Ueyama724d6252016-01-29 01:49:32 +00001253unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001254 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1255 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001256 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001257}
1258
Rui Ueyamac516ae12016-01-29 02:33:45 +00001259bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001260 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1261 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1262 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1263 Type == R_AARCH64_TLSDESC_CALL ||
1264 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001265 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1266}
1267
Rui Ueyama02dfd492015-12-17 01:18:40 +00001268bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001269 if (Config->Shared)
1270 return false;
1271 switch (Type) {
1272 default:
1273 return false;
1274 case R_AARCH64_ABS16:
1275 case R_AARCH64_ABS32:
1276 case R_AARCH64_ABS64:
1277 case R_AARCH64_ADD_ABS_LO12_NC:
1278 case R_AARCH64_ADR_PREL_LO21:
1279 case R_AARCH64_ADR_PREL_PG_HI21:
1280 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001281 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001282 case R_AARCH64_LDST32_ABS_LO12_NC:
1283 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001284 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001285 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1286 return SS->Sym.getType() == STT_OBJECT;
1287 return false;
1288 }
1289}
1290
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001291bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001292 switch (Type) {
1293 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1294 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1295 case R_AARCH64_ADR_GOT_PAGE:
1296 case R_AARCH64_LD64_GOT_LO12_NC:
1297 return true;
1298 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001299 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001300 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001301}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001302
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001303bool AArch64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001304 if (isGnuIFunc<ELF64LE>(S))
1305 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001306 switch (Type) {
1307 default:
1308 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001309 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001310 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001311 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001312 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001313 return canBePreempted(&S, true);
1314 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001315}
Davide Italiano1d750a62015-09-27 08:45:38 +00001316
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001317static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001318 uint32_t ImmLo = (Imm & 0x3) << 29;
1319 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1320 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001321 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001322}
1323
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001324static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1325 or32le(L, (Imm & 0xFFF) << 10);
1326}
1327
Davide Italiano318ca222015-10-02 22:13:51 +00001328// Page(Expr) is the page address of the expression Expr, defined
1329// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001330// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001331static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001332 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001333}
1334
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001335void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001336 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001337 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001338 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001339 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001340 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001341 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001342 break;
1343 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001344 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001345 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001346 break;
1347 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001348 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001349 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001350 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001351 // This relocation stores 12 bits and there's no instruction
1352 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001353 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1354 // bits in Loc are zero.
1355 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001356 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001357 case R_AARCH64_ADR_GOT_PAGE: {
1358 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1359 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001360 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001361 break;
1362 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001363 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001364 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001365 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001366 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001367 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001368 }
George Rimar3d737e42016-01-13 13:04:46 +00001369 case R_AARCH64_ADR_PREL_PG_HI21:
1370 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001371 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001372 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001373 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001374 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001375 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001376 case R_AARCH64_CALL26:
1377 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001378 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001379 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001380 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1381 break;
1382 }
George Rimar4102bfb2016-01-11 14:22:00 +00001383 case R_AARCH64_CONDBR19: {
1384 uint64_t X = SA - P;
1385 checkInt<21>(X, Type);
1386 or32le(Loc, (X & 0x1FFFFC) << 3);
1387 break;
1388 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001389 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001390 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001391 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001392 or32le(Loc, (SA & 0xFF8) << 7);
1393 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001394 case R_AARCH64_LDST128_ABS_LO12_NC:
1395 or32le(Loc, (SA & 0x0FF8) << 6);
1396 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001397 case R_AARCH64_LDST16_ABS_LO12_NC:
1398 or32le(Loc, (SA & 0x0FFC) << 9);
1399 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001400 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001401 or32le(Loc, (SA & 0xFFF) << 10);
1402 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001403 case R_AARCH64_LDST32_ABS_LO12_NC:
1404 or32le(Loc, (SA & 0xFFC) << 8);
1405 break;
1406 case R_AARCH64_LDST64_ABS_LO12_NC:
1407 or32le(Loc, (SA & 0xFF8) << 7);
1408 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001409 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001410 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001411 write16le(Loc, SA - P);
1412 break;
1413 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001414 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001415 write32le(Loc, SA - P);
1416 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001417 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001418 write64le(Loc, SA - P);
1419 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001420 case R_AARCH64_TSTBR14: {
1421 uint64_t X = SA - P;
1422 checkInt<16>(X, Type);
1423 or32le(Loc, (X & 0xFFFC) << 3);
1424 break;
1425 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001426 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1427 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1428 checkInt<24>(V, Type);
1429 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1430 break;
1431 }
1432 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1433 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1434 updateAArch64Add(Loc, V & 0xFFF);
1435 break;
1436 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001437 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001438 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001439 }
1440}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001441
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001442bool AArch64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
1443 if (Config->Shared || (S && !S->isTls()))
1444 return false;
1445
1446 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is
1447 // an executable. And if the target is local it can also be fully relaxed to
1448 // Local-Exec.
1449 if (isTlsGlobalDynamicRel(Type))
1450 return !canBePreempted(S, true);
1451
1452 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local
1453 // symbol.
1454 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1455 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1456 return !canBePreempted(S, true);
1457
1458 return false;
1459}
1460
1461unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1462 uint32_t Type, uint64_t P, uint64_t SA,
1463 const SymbolBody *S) const {
1464 switch (Type) {
1465 case R_AARCH64_TLSDESC_ADR_PAGE21:
1466 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1467 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1468 case R_AARCH64_TLSDESC_CALL: {
1469 if (canBePreempted(S, true))
1470 fatal("Unsupported TLS optimization");
1471 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1472 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1473 return 0;
1474 }
1475 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1476 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1477 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1478 return 0;
1479 }
1480 llvm_unreachable("Unknown TLS optimization");
1481}
1482
1483// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1484// an executable and target is final (can notbe preempted).
1485void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc,
1486 uint8_t *BufEnd, uint64_t P,
1487 uint64_t SA) const {
1488 // TLSDESC Global-Dynamic relocation are in the form:
1489 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1490 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1491 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1492 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1493 // And it can optimized to:
1494 // movz x0, #0x0, lsl #16
1495 // movk x0, #0x10
1496 // nop
1497 // nop
1498
1499 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1500 uint64_t X = SA + TPOff;
1501 checkUInt<32>(X, Type);
1502
1503 uint32_t NewInst;
1504 switch (Type) {
1505 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1506 case R_AARCH64_TLSDESC_CALL:
1507 // nop
1508 NewInst = 0xd503201f;
1509 break;
1510 case R_AARCH64_TLSDESC_ADR_PAGE21:
1511 // movz
1512 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1513 break;
1514 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1515 // movk
1516 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1517 break;
1518 default:
1519 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1520 }
1521 write32le(Loc, NewInst);
1522}
1523
1524// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1525// (can not be preempted).
1526void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
1527 uint8_t *BufEnd, uint64_t P,
1528 uint64_t SA) const {
1529 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1530 uint64_t X = SA + TPOff;
1531 checkUInt<32>(X, Type);
1532
1533 uint32_t Inst = read32le (Loc);
1534 uint32_t NewInst;
1535 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1536 // Generate movz.
1537 unsigned RegNo = (Inst & 0x1f);
1538 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1539 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1540 // Generate movk
1541 unsigned RegNo = (Inst & 0x1f);
1542 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1543 } else {
1544 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1545 }
1546 write32le(Loc, NewInst);
1547}
1548
1549
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001550// Implementing relocations for AMDGPU is low priority since most
1551// programs don't use relocations now. Thus, this function is not
1552// actually called (relocateOne is called for each relocation).
1553// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001554void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1555 uint64_t P, uint64_t SA, uint64_t ZA,
1556 uint8_t *PairedLoc) const {
1557 llvm_unreachable("not implemented");
1558}
1559
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001560template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001561 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001562 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001563 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001564 PltEntrySize = 16;
1565 PltZeroSize = 32;
1566 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001567 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001568 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001569 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001570}
1571
1572template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001573unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001574 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1575 return R_MIPS_REL32;
1576 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001577 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001578 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001579 // Keep it going with a dummy value so that we can find more reloc errors.
1580 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001581}
1582
1583template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001584void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001585 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001586 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1587
1588 // Set the MSB of the second GOT slot. This is not required by any
1589 // MIPS ABI documentation, though.
1590 //
1591 // There is a comment in glibc saying that "The MSB of got[1] of a
1592 // gnu object is set to identify gnu objects," and in GNU gold it
1593 // says "the second entry will be used by some runtime loaders".
1594 // But how this field is being used is unclear.
1595 //
1596 // We are not really willing to mimic other linkers behaviors
1597 // without understanding why they do that, but because all files
1598 // generated by GNU tools have this special GOT value, and because
1599 // we've been doing this for years, it is probably a safe bet to
1600 // keep doing this for now. We really need to revisit this to see
1601 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001602 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001603 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001604}
1605
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001606template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001607void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1608 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001609}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001610
Simon Atanasyan35031192015-12-15 06:06:34 +00001611static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001612
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001613template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001614static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001615 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001616 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001617 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001618 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1619 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001620 checkAlignment<(1 << SHIFT)>(S + A, Type);
1621 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001622 checkInt<BSIZE + SHIFT>(V, Type);
1623 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001624}
1625
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001626template <endianness E>
1627static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1628 uint32_t Instr = read32<E>(Loc);
1629 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1630}
1631
1632template <class ELFT>
1633void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1634 const endianness E = ELFT::TargetEndianness;
1635 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1636 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1637 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1638 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1639 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1640 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1641 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1642 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1643 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1644 uint64_t Plt = Out<ELFT>::Plt->getVA();
1645 applyMipsHi16Reloc<E>(Buf, Got, 0);
1646 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1647 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1648}
1649
1650template <class ELFT>
1651void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1652 uint64_t PltEntryAddr, int32_t Index,
1653 unsigned RelOff) const {
1654 const endianness E = ELFT::TargetEndianness;
1655 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1656 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1657 write32<E>(Buf + 8, 0x03200008); // jr $25
1658 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1659 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1660 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1661 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1662}
1663
1664template <class ELFT>
1665bool MipsTargetInfo<ELFT>::needsCopyRel(uint32_t Type,
1666 const SymbolBody &S) const {
1667 if (Config->Shared)
1668 return false;
1669 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1670 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
1671 return SS->Sym.getType() == STT_OBJECT;
1672 return false;
1673}
1674
1675template <class ELFT>
1676bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
1677 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
1678}
1679
1680template <class ELFT>
1681bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, SymbolBody &S) const {
1682 if (needsCopyRel(Type, S))
1683 return false;
1684 if (Type == R_MIPS_26 && canBePreempted(&S, false))
1685 return true;
1686 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1687 return S.isShared();
1688 return false;
1689}
1690
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001691template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001692void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001693 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001694 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001695 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001696 switch (Type) {
1697 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001698 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001699 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001700 case R_MIPS_26: {
1701 uint32_t Instr = read32<E>(Loc);
1702 // FIXME (simon): If the relocation target symbol is not a PLT entry
1703 // we should use another expression for calculation:
1704 // ((A << 2) | (P & 0xf0000000)) >> 2
1705 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1706 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1707 break;
1708 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001709 case R_MIPS_CALL16:
1710 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001711 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001712 if (Type == R_MIPS_GOT16)
1713 checkInt<16>(V, Type);
1714 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1715 break;
1716 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001717 case R_MIPS_GPREL16: {
1718 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001719 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001720 checkInt<16>(V, Type);
1721 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1722 break;
1723 }
1724 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001725 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001726 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001727 case R_MIPS_HI16: {
1728 uint32_t Instr = read32<E>(Loc);
1729 if (PairedLoc) {
1730 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001731 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001732 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001733 } else {
1734 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001735 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001736 }
1737 break;
1738 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001739 case R_MIPS_JALR:
1740 // Ignore this optimization relocation for now
1741 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001742 case R_MIPS_LO16: {
1743 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001744 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001745 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001746 break;
1747 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001748 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001749 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001750 break;
1751 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001752 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001753 break;
1754 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001755 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001756 break;
1757 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001758 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001759 break;
1760 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001761 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001762 break;
1763 case R_MIPS_PCHI16: {
1764 uint32_t Instr = read32<E>(Loc);
1765 if (PairedLoc) {
1766 uint64_t AHL = ((Instr & 0xffff) << 16) +
1767 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001768 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001769 } else {
1770 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001771 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001772 }
1773 break;
1774 }
1775 case R_MIPS_PCLO16: {
1776 uint32_t Instr = read32<E>(Loc);
1777 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001778 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001779 break;
1780 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001781 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001782 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001783 }
1784}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001785
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001786template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001787bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001788 return Type == R_MIPS_JALR;
1789}
1790
1791template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001792bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1793 switch (Type) {
1794 default:
1795 return false;
1796 case R_MIPS_PC16:
1797 case R_MIPS_PC19_S2:
1798 case R_MIPS_PC21_S2:
1799 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001800 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001801 case R_MIPS_PCHI16:
1802 case R_MIPS_PCLO16:
1803 return true;
1804 }
1805}
1806
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001807// _gp is a MIPS-specific ABI-defined symbol which points to
1808// a location that is relative to GOT. This function returns
1809// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001810template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001811 unsigned GPOffset = 0x7ff0;
1812 if (uint64_t V = Out<ELFT>::Got->getVA())
1813 return V + GPOffset;
1814 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001815}
1816
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001817template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1818template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1819template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1820template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1821
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001822template uint32_t getMipsGpAddr<ELF32LE>();
1823template uint32_t getMipsGpAddr<ELF32BE>();
1824template uint64_t getMipsGpAddr<ELF64LE>();
1825template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001826}
1827}