blob: 4a7d4bed37fa090fbf2a50c4704b8ddb646a8765 [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;
Rafael Espindola852860e2016-02-12 15:47:37 +000096 PltNeed needsPlt(uint32_t Type, const 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;
Rafael Espindola852860e2016-02-12 15:47:37 +0000130 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000131 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000132 uint64_t SA, uint64_t ZA = 0,
133 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000135 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000136 bool isSizeRel(uint32_t Type) const override;
137 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
138 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000139
140private:
141 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
142 uint64_t SA) const;
143 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
144 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000145 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
146 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000147 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
148 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000149};
150
Davide Italiano8c3444362016-01-11 19:45:33 +0000151class PPCTargetInfo final : public TargetInfo {
152public:
153 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000154 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
155 uint64_t SA, uint64_t ZA = 0,
156 uint8_t *PairedLoc = nullptr) const override;
157 bool isRelRelative(uint32_t Type) const override;
158};
159
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000160class PPC64TargetInfo final : public TargetInfo {
161public:
162 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000163 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
164 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000165 bool needsGot(uint32_t Type, SymbolBody &S) const override;
Rafael Espindola852860e2016-02-12 15:47:37 +0000166 PltNeed needsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000167 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000168 uint64_t SA, uint64_t ZA = 0,
169 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000170 bool isRelRelative(uint32_t Type) const override;
171};
172
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000173class AArch64TargetInfo final : public TargetInfo {
174public:
175 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000176 unsigned getDynRel(unsigned Type) const override;
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;
Rafael Espindola852860e2016-02-12 15:47:37 +0000186 PltNeed needsPlt(uint32_t Type, const 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;
Rafael Espindola852860e2016-02-12 15:47:37 +0000222 PltNeed needsPlt(uint32_t Type, const 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 Espindola852860e2016-02-12 15:47:37 +0000281TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
282 const SymbolBody &S) const {
283 return Plt_No;
284}
Rui Ueyama012eb782016-01-29 04:05:09 +0000285
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000286bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
287 return false;
288}
289
290bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
291 return false;
292}
293
Rui Ueyamac516ae12016-01-29 02:33:45 +0000294unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
295 uint64_t P, uint64_t SA,
296 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000297 return 0;
298}
George Rimar77d1cb12015-11-24 09:00:06 +0000299
Rafael Espindola7f074422015-09-22 21:35:51 +0000300X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000301 CopyRel = R_386_COPY;
302 GotRel = R_386_GLOB_DAT;
303 PltRel = R_386_JUMP_SLOT;
304 IRelativeRel = R_386_IRELATIVE;
305 RelativeRel = R_386_RELATIVE;
306 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000307 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
308 TlsOffsetRel = R_386_TLS_DTPOFF32;
309 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000310 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000311 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000312}
313
Rui Ueyamac516ae12016-01-29 02:33:45 +0000314void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000315 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
316}
317
Rui Ueyamac516ae12016-01-29 02:33:45 +0000318void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000319 // Entries in .got.plt initially points back to the corresponding
320 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000321 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000322}
Rafael Espindola01205f72015-09-22 18:19:46 +0000323
Rui Ueyamac516ae12016-01-29 02:33:45 +0000324unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000325 if (Type == R_386_TLS_LE)
326 return R_386_TLS_TPOFF;
327 if (Type == R_386_TLS_LE_32)
328 return R_386_TLS_TPOFF32;
329 return Type;
330}
331
Rui Ueyama724d6252016-01-29 01:49:32 +0000332unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000333 if (Type == R_386_TLS_IE)
334 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000335 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000336}
337
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000338bool X86TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
339 return Type == R_386_TLS_GD;
340}
341
342bool X86TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
343 return Type == R_386_TLS_LDM;
344}
345
Rui Ueyamac516ae12016-01-29 02:33:45 +0000346bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000347 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
348 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000349 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000350 if (Type == R_386_TLS_IE)
351 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000352 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000353}
354
Rui Ueyama900e2d22016-01-29 03:51:49 +0000355void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000356 // Executable files and shared object files have
357 // separate procedure linkage tables.
358 if (Config->Shared) {
359 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000360 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000361 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
362 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000363 };
364 memcpy(Buf, V, sizeof(V));
365 return;
366 }
George Rimar648a2c32015-10-20 08:54:27 +0000367
George Rimar77b77792015-11-25 22:15:01 +0000368 const uint8_t PltData[] = {
369 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000370 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
371 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000372 };
373 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000374 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000375 write32le(Buf + 2, Got + 4);
376 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000377}
378
Rui Ueyama9398f862016-01-29 04:15:02 +0000379void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
380 uint64_t PltEntryAddr, int32_t Index,
381 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000382 const uint8_t Inst[] = {
383 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
384 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
385 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
386 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000387 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000388
George Rimar77b77792015-11-25 22:15:01 +0000389 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
390 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000391 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
392 : Out<ELF32LE>::Got->getVA();
393 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000394 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000395 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000396}
397
Rui Ueyama02dfd492015-12-17 01:18:40 +0000398bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000399 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
400 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
401 return SS->Sym.getType() == STT_OBJECT;
402 return false;
403}
404
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000405bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000406 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000407 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000408 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000409 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000410 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000411}
412
Rafael Espindola852860e2016-02-12 15:47:37 +0000413TargetInfo::PltNeed X86TargetInfo::needsPlt(uint32_t Type,
414 const SymbolBody &S) const {
415 if (isGnuIFunc<ELF32LE>(S) ||
416 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
417 (Type == R_386_PC32 && S.isShared()))
418 return Plt_Explicit;
419 return Plt_No;
Rafael Espindola01205f72015-09-22 18:19:46 +0000420}
421
George Rimarbfb7bf72015-12-21 10:00:12 +0000422bool X86TargetInfo::isGotRelative(uint32_t Type) const {
423 // This relocation does not require got entry,
424 // but it is relative to got and needs it to be created.
425 // Here we request for that.
426 return Type == R_386_GOTOFF;
427}
428
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000429void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000430 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000431 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000432 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000433 case R_386_32:
434 add32le(Loc, SA);
435 break;
George Rimarffb67352016-01-19 11:00:48 +0000436 case R_386_GOT32: {
437 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
438 Out<ELF32LE>::Got->getNumEntries() * 4;
439 checkInt<32>(V, Type);
440 add32le(Loc, V);
441 break;
442 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000443 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000444 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000445 break;
George Rimar13934772015-11-25 20:20:31 +0000446 case R_386_GOTPC:
447 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
448 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000449 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000450 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000451 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000452 break;
George Rimar9db204a2015-12-02 09:58:20 +0000453 case R_386_TLS_GD:
454 case R_386_TLS_LDM:
455 case R_386_TLS_TPOFF: {
456 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
457 Out<ELF32LE>::Got->getNumEntries() * 4;
458 checkInt<32>(V, Type);
459 write32le(Loc, V);
460 break;
461 }
George Rimar6f17e092015-12-17 09:32:21 +0000462 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000463 case R_386_TLS_LDO_32:
464 write32le(Loc, SA);
465 break;
George Rimard23970f2015-11-25 20:41:53 +0000466 case R_386_TLS_LE:
467 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
468 break;
469 case R_386_TLS_LE_32:
470 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
471 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000472 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000473 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000474 }
475}
476
Rui Ueyamabaf16512016-01-29 00:20:12 +0000477bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000478 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000479 return false;
480 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
481 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000482 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000483 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
484}
485
Rui Ueyamac516ae12016-01-29 02:33:45 +0000486bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000487 return Config->Shared && Type == R_386_TLS_IE;
488}
489
Rui Ueyamac516ae12016-01-29 02:33:45 +0000490unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
491 uint64_t P, uint64_t SA,
492 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000493 switch (Type) {
494 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000495 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000496 relocateTlsGdToIe(Loc, BufEnd, P, SA);
497 else
498 relocateTlsGdToLe(Loc, BufEnd, P, SA);
499 // The next relocation should be against __tls_get_addr, so skip it
500 return 1;
501 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000502 case R_386_TLS_IE:
503 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000504 return 0;
505 case R_386_TLS_LDM:
506 relocateTlsLdToLe(Loc, BufEnd, P, SA);
507 // The next relocation should be against __tls_get_addr, so skip it
508 return 1;
509 case R_386_TLS_LDO_32:
510 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
511 return 0;
512 }
513 llvm_unreachable("Unknown TLS optimization");
514}
515
516// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
517// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
518// how GD can be optimized to IE:
519// leal x@tlsgd(, %ebx, 1),
520// call __tls_get_addr@plt
521// Is converted to:
522// movl %gs:0, %eax
523// addl x@gotntpoff(%ebx), %eax
524void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
525 uint64_t SA) const {
526 const uint8_t Inst[] = {
527 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
528 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
529 };
530 memcpy(Loc - 3, Inst, sizeof(Inst));
531 relocateOne(Loc + 5, BufEnd, R_386_32, P,
532 SA - Out<ELF32LE>::Got->getVA() -
533 Out<ELF32LE>::Got->getNumEntries() * 4);
534}
535
536// GD can be optimized to LE:
537// leal x@tlsgd(, %ebx, 1),
538// call __tls_get_addr@plt
539// Can be converted to:
540// movl %gs:0,%eax
541// addl $x@ntpoff,%eax
542// But gold emits subl $foo@tpoff,%eax instead of addl.
543// These instructions are completely equal in behavior.
544// This method generates subl to be consistent with gold.
545void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
546 uint64_t SA) const {
547 const uint8_t Inst[] = {
548 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
549 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
550 };
551 memcpy(Loc - 3, Inst, sizeof(Inst));
552 relocateOne(Loc + 5, BufEnd, R_386_32, P,
553 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
554}
555
556// LD can be optimized to LE:
557// leal foo(%reg),%eax
558// call ___tls_get_addr
559// Is converted to:
560// movl %gs:0,%eax
561// nop
562// leal 0(%esi,1),%esi
563void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
564 uint64_t SA) const {
565 const uint8_t Inst[] = {
566 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
567 0x90, // nop
568 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
569 };
570 memcpy(Loc - 2, Inst, sizeof(Inst));
571}
572
George Rimar6f17e092015-12-17 09:32:21 +0000573// In some conditions, relocations can be optimized to avoid using GOT.
574// This function does that for Initial Exec to Local Exec case.
575// Read "ELF Handling For Thread-Local Storage, 5.1
576// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000577// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000578void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
579 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000580 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000581 // Ulrich's document section 6.2 says that @gotntpoff can
582 // be used with MOVL or ADDL instructions.
583 // @indntpoff is similar to @gotntpoff, but for use in
584 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000585 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000586 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000587 uint8_t Reg = (Loc[-1] >> 3) & 7;
588 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000589 if (Type == R_386_TLS_IE) {
590 // For R_386_TLS_IE relocation we perform the next transformations:
591 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
592 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
593 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
594 // First one is special because when EAX is used the sequence is 5 bytes
595 // long, otherwise it is 6 bytes.
596 if (*Op == 0xa1) {
597 *Op = 0xb8;
598 } else {
599 *Inst = IsMov ? 0xc7 : 0x81;
600 *Op = 0xc0 | ((*Op >> 3) & 7);
601 }
602 } else {
603 // R_386_TLS_GOTIE relocation can be optimized to
604 // R_386_TLS_LE so that it does not use GOT.
605 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
606 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
607 // Note: gold converts to ADDL instead of LEAL.
608 *Inst = IsMov ? 0xc7 : 0x8d;
609 if (IsMov)
610 *Op = 0xc0 | ((*Op >> 3) & 7);
611 else
612 *Op = 0x80 | Reg | (Reg << 3);
613 }
George Rimar2558e122015-12-09 09:55:54 +0000614 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
615}
616
Rafael Espindola7f074422015-09-22 21:35:51 +0000617X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000618 CopyRel = R_X86_64_COPY;
619 GotRel = R_X86_64_GLOB_DAT;
620 PltRel = R_X86_64_JUMP_SLOT;
621 RelativeRel = R_X86_64_RELATIVE;
622 IRelativeRel = R_X86_64_IRELATIVE;
623 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000624 TlsModuleIndexRel = R_X86_64_DTPMOD64;
625 TlsOffsetRel = R_X86_64_DTPOFF64;
626 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000627 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000628 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000629}
630
Rui Ueyamac516ae12016-01-29 02:33:45 +0000631void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000632 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
633}
634
Rui Ueyamac516ae12016-01-29 02:33:45 +0000635void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000636 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000637 write32le(Buf, Plt + 6);
638}
639
Rui Ueyama900e2d22016-01-29 03:51:49 +0000640void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000641 const uint8_t PltData[] = {
642 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
643 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
644 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
645 };
646 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000647 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
648 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
649 write32le(Buf + 2, Got - Plt + 2); // GOT+8
650 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000651}
Rafael Espindola01205f72015-09-22 18:19:46 +0000652
Rui Ueyama9398f862016-01-29 04:15:02 +0000653void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
654 uint64_t PltEntryAddr, int32_t Index,
655 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000656 const uint8_t Inst[] = {
657 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
658 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
659 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
660 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000661 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000662
George Rimar648a2c32015-10-20 08:54:27 +0000663 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
664 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000665 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000666}
667
Rui Ueyama02dfd492015-12-17 01:18:40 +0000668bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000669 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
670 Type == R_X86_64_64)
671 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
672 return SS->Sym.getType() == STT_OBJECT;
673 return false;
674}
675
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000676bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000677 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000678 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000679 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000680 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000681 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000682}
683
George Rimar2960c982016-02-11 11:14:46 +0000684unsigned X86_64TargetInfo::getTlsGotRel(unsigned Type) const {
685 // No other types of TLS relocations requiring GOT should
686 // reach here.
687 assert(Type == R_X86_64_GOTTPOFF);
688 return R_X86_64_PC32;
689}
690
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000691bool X86_64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
692 return Type == R_X86_64_TLSGD;
693}
694
695bool X86_64TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
696 return Type == R_X86_64_TLSLD;
697}
698
Rui Ueyamac516ae12016-01-29 02:33:45 +0000699bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000700 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000701}
702
Rafael Espindola852860e2016-02-12 15:47:37 +0000703TargetInfo::PltNeed X86_64TargetInfo::needsPlt(uint32_t Type,
704 const SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000705 if (needsCopyRel(Type, S))
Rafael Espindola852860e2016-02-12 15:47:37 +0000706 return Plt_No;
George Rimara07ff662015-12-21 10:12:06 +0000707 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola852860e2016-02-12 15:47:37 +0000708 return Plt_Explicit;
George Rimarbc590fe2015-10-28 16:48:58 +0000709
Rafael Espindola01205f72015-09-22 18:19:46 +0000710 switch (Type) {
711 default:
Rafael Espindola852860e2016-02-12 15:47:37 +0000712 return Plt_No;
Rafael Espindola227556e2015-10-14 16:15:46 +0000713 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000714 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000715 case R_X86_64_PC32:
716 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000717 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000718 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000719 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000720 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000721 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
722 // libc.so.
723 //
724 // The general idea on how to handle such cases is to create a PLT entry
725 // and use that as the function value.
726 //
727 // For the static linking part, we just return true and everything else
728 // will use the the PLT entry as the address.
729 //
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000730 // The remaining problem is making sure pointer equality still works. We
731 // need the help of the dynamic linker for that. We let it know that we have
732 // a direct reference to a so symbol by creating an undefined symbol with a
733 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
734 // the value of the symbol we created. This is true even for got entries, so
735 // pointer equality is maintained. To avoid an infinite loop, the only entry
736 // that points to the real function is a dedicated got entry used by the
737 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
Rafael Espindola3c412e12015-09-30 12:30:58 +0000738 // R_386_JMP_SLOT, etc).
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000739 if (!S.isShared())
Rafael Espindola852860e2016-02-12 15:47:37 +0000740 return Plt_No;
741 return Plt_Implicit;
Rafael Espindola01205f72015-09-22 18:19:46 +0000742 case R_X86_64_PLT32:
Rafael Espindola852860e2016-02-12 15:47:37 +0000743 if (canBePreempted(&S, true))
744 return Plt_Explicit;
745 return Plt_No;
Rafael Espindola01205f72015-09-22 18:19:46 +0000746 }
747}
Rafael Espindolac4010882015-09-22 20:54:08 +0000748
Rafael Espindolaae244002015-10-05 19:30:12 +0000749bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
750 switch (Type) {
751 default:
752 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000753 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000754 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000755 case R_X86_64_PC8:
756 case R_X86_64_PC16:
757 case R_X86_64_PC32:
758 case R_X86_64_PC64:
759 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000760 return true;
761 }
762}
763
Rui Ueyamac516ae12016-01-29 02:33:45 +0000764bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000765 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000766}
767
Rui Ueyamabaf16512016-01-29 00:20:12 +0000768bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000769 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000770 return false;
George Rimar25411f252015-12-04 11:20:13 +0000771 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
772 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000773 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
774}
775
776// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
777// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
778// how LD can be optimized to LE:
779// leaq bar@tlsld(%rip), %rdi
780// callq __tls_get_addr@PLT
781// leaq bar@dtpoff(%rax), %rcx
782// Is converted to:
783// .word 0x6666
784// .byte 0x66
785// mov %fs:0,%rax
786// leaq bar@tpoff(%rax), %rcx
787void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
788 uint64_t P, uint64_t SA) const {
789 const uint8_t Inst[] = {
790 0x66, 0x66, //.word 0x6666
791 0x66, //.byte 0x66
792 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
793 };
794 memcpy(Loc - 3, Inst, sizeof(Inst));
795}
796
797// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
798// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
799// how GD can be optimized to LE:
800// .byte 0x66
801// leaq x@tlsgd(%rip), %rdi
802// .word 0x6666
803// rex64
804// call __tls_get_addr@plt
805// Is converted to:
806// mov %fs:0x0,%rax
807// lea x@tpoff,%rax
808void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
809 uint64_t P, uint64_t SA) const {
810 const uint8_t Inst[] = {
811 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
812 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
813 };
814 memcpy(Loc - 4, Inst, sizeof(Inst));
815 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000816}
817
George Rimar25411f252015-12-04 11:20:13 +0000818// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
819// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
820// how GD can be optimized to IE:
821// .byte 0x66
822// leaq x@tlsgd(%rip), %rdi
823// .word 0x6666
824// rex64
825// call __tls_get_addr@plt
826// Is converted to:
827// mov %fs:0x0,%rax
828// addq x@tpoff,%rax
829void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
830 uint64_t P, uint64_t SA) const {
831 const uint8_t Inst[] = {
832 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
833 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
834 };
835 memcpy(Loc - 4, Inst, sizeof(Inst));
George Rimar2960c982016-02-11 11:14:46 +0000836 relocateOne(Loc + 8, BufEnd, R_X86_64_PC32, P + 12, SA);
George Rimar25411f252015-12-04 11:20:13 +0000837}
838
George Rimar77d1cb12015-11-24 09:00:06 +0000839// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000840// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000841// This function does that. Read "ELF Handling For Thread-Local Storage,
842// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
843// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000844void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
845 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000846 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
847 // used in MOVQ or ADDQ instructions only.
848 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
849 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
850 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
851 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
852 uint8_t *Prefix = Loc - 3;
853 uint8_t *Inst = Loc - 2;
854 uint8_t *RegSlot = Loc - 1;
855 uint8_t Reg = Loc[-1] >> 3;
856 bool IsMov = *Inst == 0x8b;
857 bool RspAdd = !IsMov && Reg == 4;
858 // r12 and rsp registers requires special handling.
859 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
860 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
861 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
862 // The same true for rsp. So we convert to addq for them, saving 1 byte that
863 // we dont have.
864 if (RspAdd)
865 *Inst = 0x81;
866 else
867 *Inst = IsMov ? 0xc7 : 0x8d;
868 if (*Prefix == 0x4c)
869 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
870 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
871 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
872}
873
George Rimar6713cf82015-11-25 21:46:05 +0000874// This function applies a TLS relocation with an optimization as described
875// in the Ulrich's document. As a result of rewriting instructions at the
876// relocation target, relocations immediately follow the TLS relocation (which
877// would be applied to rewritten instructions) may have to be skipped.
878// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000879unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
880 uint32_t Type, uint64_t P, uint64_t SA,
881 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000882 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000883 case R_X86_64_DTPOFF32:
884 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
885 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000886 case R_X86_64_GOTTPOFF:
887 relocateTlsIeToLe(Loc, BufEnd, P, SA);
888 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000889 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000890 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000891 relocateTlsGdToIe(Loc, BufEnd, P, SA);
892 else
893 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000894 // The next relocation should be against __tls_get_addr, so skip it
895 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000896 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000897 case R_X86_64_TLSLD:
898 relocateTlsLdToLe(Loc, BufEnd, P, SA);
899 // The next relocation should be against __tls_get_addr, so skip it
900 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000901 }
902 llvm_unreachable("Unknown TLS optimization");
903}
904
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000905void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000906 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000907 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000908 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000909 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000910 checkUInt<32>(SA, Type);
911 write32le(Loc, SA);
912 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000913 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000914 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000915 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000916 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000917 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000918 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000919 write64le(Loc, SA);
920 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000921 case R_X86_64_DTPOFF32:
922 write32le(Loc, SA);
923 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000924 case R_X86_64_GOTPCREL:
925 case R_X86_64_PC32:
926 case R_X86_64_PLT32:
927 case R_X86_64_TLSGD:
928 case R_X86_64_TLSLD:
929 write32le(Loc, SA - P);
930 break;
George Rimar48651482015-12-11 08:59:37 +0000931 case R_X86_64_SIZE32:
932 write32le(Loc, ZA);
933 break;
934 case R_X86_64_SIZE64:
935 write64le(Loc, ZA);
936 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000937 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000938 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000939 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000940 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000941 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000942 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000943 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000944 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000945 }
946}
947
Hal Finkel3c8cc672015-10-12 20:56:18 +0000948// Relocation masks following the #lo(value), #hi(value), #ha(value),
949// #higher(value), #highera(value), #highest(value), and #highesta(value)
950// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
951// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000952static uint16_t applyPPCLo(uint64_t V) { return V; }
953static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
954static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
955static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
956static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000957static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000958static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
959
Davide Italiano8c3444362016-01-11 19:45:33 +0000960PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000961bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
962
963void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
964 uint64_t P, uint64_t SA, uint64_t ZA,
965 uint8_t *PairedLoc) const {
966 switch (Type) {
967 case R_PPC_ADDR16_HA:
968 write16be(Loc, applyPPCHa(SA));
969 break;
970 case R_PPC_ADDR16_LO:
971 write16be(Loc, applyPPCLo(SA));
972 break;
973 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000974 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000975 }
976}
977
Rafael Espindolac4010882015-09-22 20:54:08 +0000978PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000979 GotRel = R_PPC64_GLOB_DAT;
980 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000981 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000982
983 // We need 64K pages (at least under glibc/Linux, the loader won't
984 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000985 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000986
987 // The PPC64 ELF ABI v1 spec, says:
988 //
989 // It is normally desirable to put segments with different characteristics
990 // in separate 256 Mbyte portions of the address space, to give the
991 // operating system full paging flexibility in the 64-bit address space.
992 //
993 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
994 // use 0x10000000 as the starting address.
995 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000996}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000997
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000998uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000999 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1000 // order. The TOC starts where the first of these sections starts.
1001
1002 // FIXME: This obviously does not do the right thing when there is no .got
1003 // section, but there is a .toc or .tocbss section.
1004 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1005 if (!TocVA)
1006 TocVA = Out<ELF64BE>::Plt->getVA();
1007
1008 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1009 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1010 // code (crt1.o) assumes that you can get from the TOC base to the
1011 // start of the .toc section with only a single (signed) 16-bit relocation.
1012 return TocVA + 0x8000;
1013}
1014
Rui Ueyama9398f862016-01-29 04:15:02 +00001015void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1016 uint64_t PltEntryAddr, int32_t Index,
1017 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001018 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1019
1020 // FIXME: What we should do, in theory, is get the offset of the function
1021 // descriptor in the .opd section, and use that as the offset from %r2 (the
1022 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1023 // be a pointer to the function descriptor in the .opd section. Using
1024 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1025
Hal Finkelfa92f682015-10-13 21:47:34 +00001026 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001027 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1028 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1029 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1030 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1031 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1032 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1033 write32be(Buf + 28, 0x4e800420); // bctr
1034}
1035
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001036bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyamac516ae12016-01-29 02:33:45 +00001037 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001038 return true;
1039
1040 switch (Type) {
1041 default: return false;
1042 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001044 case R_PPC64_GOT16_HA:
1045 case R_PPC64_GOT16_HI:
1046 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001047 case R_PPC64_GOT16_LO_DS:
1048 return true;
1049 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001050}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051
Rafael Espindola852860e2016-02-12 15:47:37 +00001052TargetInfo::PltNeed PPC64TargetInfo::needsPlt(uint32_t Type,
1053 const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001054 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola852860e2016-02-12 15:47:37 +00001055 if (Type == R_PPC64_REL24 && canBePreempted(&S, false))
1056 return Plt_Explicit;
1057 return Plt_No;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001058}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059
Hal Finkelbe0823d2015-10-12 20:58:52 +00001060bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1061 switch (Type) {
1062 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001063 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001064 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001065 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001066 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001067 }
1068}
1069
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001070void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001071 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001072 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 uint64_t TB = getPPC64TocBase();
1074
Hal Finkel3c8cc672015-10-12 20:56:18 +00001075 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1076 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001077 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001078 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1079 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001080 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1081 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001082 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1083 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 default: break;
1085 }
1086
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001088 case R_PPC64_ADDR14: {
1089 checkAlignment<4>(SA, Type);
1090 // Preserve the AA/LK bits in the branch instruction
1091 uint8_t AALK = Loc[3];
1092 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1093 break;
1094 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001095 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001096 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001097 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001098 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001099 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001100 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001101 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001102 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001103 case R_PPC64_ADDR16_HA:
1104 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001105 break;
1106 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001107 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001108 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001109 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001110 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001111 break;
1112 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001113 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001114 break;
1115 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001116 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001117 break;
1118 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001119 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001121 case R_PPC64_ADDR16_LO:
1122 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001124 case R_PPC64_ADDR16_LO_DS:
1125 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001126 break;
1127 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001128 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001129 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001131 case R_PPC64_ADDR64:
1132 write64be(Loc, SA);
1133 break;
1134 case R_PPC64_REL16_HA:
1135 write16be(Loc, applyPPCHa(SA - P));
1136 break;
1137 case R_PPC64_REL16_HI:
1138 write16be(Loc, applyPPCHi(SA - P));
1139 break;
1140 case R_PPC64_REL16_LO:
1141 write16be(Loc, applyPPCLo(SA - P));
1142 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001143 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001144 // If we have an undefined weak symbol, we might get here with a symbol
1145 // address of zero. That could overflow, but the code must be unreachable,
1146 // so don't bother doing anything at all.
1147 if (!SA)
1148 break;
1149
Hal Finkeldaedc122015-10-12 23:16:53 +00001150 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1151 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001152 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001153
1154 if (!InPlt && Out<ELF64BE>::Opd) {
1155 // If this is a local call, and we currently have the address of a
1156 // function-descriptor, get the underlying code address instead.
1157 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1158 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001159 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001160
1161 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001162 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001163 }
1164
Hal Finkel3c8cc672015-10-12 20:56:18 +00001165 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001166 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001167 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001168
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001169 uint32_t Nop = 0x60000000;
1170 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1171 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001172 break;
1173 }
1174 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001175 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001176 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001177 break;
1178 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001179 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001180 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001181 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001182 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001183 break;
1184 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001185 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001186 }
1187}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001188
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001189AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001190 CopyRel = R_AARCH64_COPY;
1191 IRelativeRel = R_AARCH64_IRELATIVE;
1192 GotRel = R_AARCH64_GLOB_DAT;
1193 PltRel = R_AARCH64_JUMP_SLOT;
1194 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001195 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1196 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001197 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001198 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001199 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001200}
George Rimar648a2c32015-10-20 08:54:27 +00001201
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001202bool AArch64TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
1203 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1204 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1205 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1206 Type == R_AARCH64_TLSDESC_CALL;
1207}
1208
Rui Ueyamac516ae12016-01-29 02:33:45 +00001209unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001210 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1211 return Type;
1212 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001213 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001214 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001215 // Keep it going with a dummy value so that we can find more reloc errors.
1216 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001217}
1218
Rui Ueyamac516ae12016-01-29 02:33:45 +00001219void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001220 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1221}
1222
Rui Ueyama900e2d22016-01-29 03:51:49 +00001223void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001224 const uint8_t PltData[] = {
1225 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1226 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1227 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1228 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1229 0x20, 0x02, 0x1f, 0xd6, // br x17
1230 0x1f, 0x20, 0x03, 0xd5, // nop
1231 0x1f, 0x20, 0x03, 0xd5, // nop
1232 0x1f, 0x20, 0x03, 0xd5 // nop
1233 };
1234 memcpy(Buf, PltData, sizeof(PltData));
1235
Rui Ueyama900e2d22016-01-29 03:51:49 +00001236 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1237 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1238 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1239 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1240 Got + 16);
1241 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1242 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001243}
1244
Rui Ueyama9398f862016-01-29 04:15:02 +00001245void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1246 uint64_t PltEntryAddr, int32_t Index,
1247 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001248 const uint8_t Inst[] = {
1249 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1250 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1251 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1252 0x20, 0x02, 0x1f, 0xd6 // br x17
1253 };
1254 memcpy(Buf, Inst, sizeof(Inst));
1255
1256 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1257 GotEntryAddr);
1258 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1259 GotEntryAddr);
1260 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1261 GotEntryAddr);
1262}
1263
Rui Ueyama724d6252016-01-29 01:49:32 +00001264unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001265 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1266 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
George Rimar3d737e42016-01-13 13:04:46 +00001267 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001268}
1269
Rui Ueyamac516ae12016-01-29 02:33:45 +00001270bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001271 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1272 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1273 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1274 Type == R_AARCH64_TLSDESC_CALL ||
1275 Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar3d737e42016-01-13 13:04:46 +00001276 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1277}
1278
Rui Ueyama02dfd492015-12-17 01:18:40 +00001279bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001280 if (Config->Shared)
1281 return false;
1282 switch (Type) {
1283 default:
1284 return false;
1285 case R_AARCH64_ABS16:
1286 case R_AARCH64_ABS32:
1287 case R_AARCH64_ABS64:
1288 case R_AARCH64_ADD_ABS_LO12_NC:
1289 case R_AARCH64_ADR_PREL_LO21:
1290 case R_AARCH64_ADR_PREL_PG_HI21:
1291 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001292 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001293 case R_AARCH64_LDST32_ABS_LO12_NC:
1294 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001295 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001296 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1297 return SS->Sym.getType() == STT_OBJECT;
1298 return false;
1299 }
1300}
1301
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001302bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001303 switch (Type) {
1304 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1305 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1306 case R_AARCH64_ADR_GOT_PAGE:
1307 case R_AARCH64_LD64_GOT_LO12_NC:
1308 return true;
1309 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001310 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001311 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001312}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001313
Rafael Espindola852860e2016-02-12 15:47:37 +00001314TargetInfo::PltNeed AArch64TargetInfo::needsPlt(uint32_t Type,
1315 const SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001316 if (isGnuIFunc<ELF64LE>(S))
Rafael Espindola852860e2016-02-12 15:47:37 +00001317 return Plt_Explicit;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001318 switch (Type) {
1319 default:
Rafael Espindola852860e2016-02-12 15:47:37 +00001320 return Plt_No;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001321 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001322 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001323 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001324 case R_AARCH64_TSTBR14:
Rafael Espindola852860e2016-02-12 15:47:37 +00001325 if (canBePreempted(&S, true))
1326 return Plt_Explicit;
1327 return Plt_No;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001328 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001329}
Davide Italiano1d750a62015-09-27 08:45:38 +00001330
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001331static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001332 uint32_t ImmLo = (Imm & 0x3) << 29;
1333 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1334 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001335 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001336}
1337
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001338static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1339 or32le(L, (Imm & 0xFFF) << 10);
1340}
1341
Davide Italiano318ca222015-10-02 22:13:51 +00001342// Page(Expr) is the page address of the expression Expr, defined
1343// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001344// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001345static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001346 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001347}
1348
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001349void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001350 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001351 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001352 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001353 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001354 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001355 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001356 break;
1357 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001358 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001359 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001360 break;
1361 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001362 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001363 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001364 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001365 // This relocation stores 12 bits and there's no instruction
1366 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001367 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1368 // bits in Loc are zero.
1369 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001370 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001371 case R_AARCH64_ADR_GOT_PAGE: {
1372 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1373 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001374 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001375 break;
1376 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001377 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001378 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001379 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001380 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001381 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001382 }
George Rimar3d737e42016-01-13 13:04:46 +00001383 case R_AARCH64_ADR_PREL_PG_HI21:
1384 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001385 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001386 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001387 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001388 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001389 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001390 case R_AARCH64_CALL26:
1391 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001392 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001393 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001394 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1395 break;
1396 }
George Rimar4102bfb2016-01-11 14:22:00 +00001397 case R_AARCH64_CONDBR19: {
1398 uint64_t X = SA - P;
1399 checkInt<21>(X, Type);
1400 or32le(Loc, (X & 0x1FFFFC) << 3);
1401 break;
1402 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001403 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001404 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001405 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001406 or32le(Loc, (SA & 0xFF8) << 7);
1407 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001408 case R_AARCH64_LDST128_ABS_LO12_NC:
1409 or32le(Loc, (SA & 0x0FF8) << 6);
1410 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001411 case R_AARCH64_LDST16_ABS_LO12_NC:
1412 or32le(Loc, (SA & 0x0FFC) << 9);
1413 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001414 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001415 or32le(Loc, (SA & 0xFFF) << 10);
1416 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001417 case R_AARCH64_LDST32_ABS_LO12_NC:
1418 or32le(Loc, (SA & 0xFFC) << 8);
1419 break;
1420 case R_AARCH64_LDST64_ABS_LO12_NC:
1421 or32le(Loc, (SA & 0xFF8) << 7);
1422 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001423 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001424 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001425 write16le(Loc, SA - P);
1426 break;
1427 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001428 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001429 write32le(Loc, SA - P);
1430 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001431 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001432 write64le(Loc, SA - P);
1433 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001434 case R_AARCH64_TSTBR14: {
1435 uint64_t X = SA - P;
1436 checkInt<16>(X, Type);
1437 or32le(Loc, (X & 0xFFFC) << 3);
1438 break;
1439 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001440 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
1441 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1442 checkInt<24>(V, Type);
1443 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1444 break;
1445 }
1446 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
1447 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + SA;
1448 updateAArch64Add(Loc, V & 0xFFF);
1449 break;
1450 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001451 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001452 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001453 }
1454}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001455
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001456bool AArch64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
1457 if (Config->Shared || (S && !S->isTls()))
1458 return false;
1459
1460 // Global-Dynamic relocs can be relaxed to Initial-Exec if the target is
1461 // an executable. And if the target is local it can also be fully relaxed to
1462 // Local-Exec.
1463 if (isTlsGlobalDynamicRel(Type))
1464 return !canBePreempted(S, true);
1465
1466 // Initial-Exec relocs can be relaxed to Local-Exec if the target is a local
1467 // symbol.
1468 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1469 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1470 return !canBePreempted(S, true);
1471
1472 return false;
1473}
1474
1475unsigned AArch64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
1476 uint32_t Type, uint64_t P, uint64_t SA,
1477 const SymbolBody *S) const {
1478 switch (Type) {
1479 case R_AARCH64_TLSDESC_ADR_PAGE21:
1480 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1481 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1482 case R_AARCH64_TLSDESC_CALL: {
1483 if (canBePreempted(S, true))
1484 fatal("Unsupported TLS optimization");
1485 uint64_t X = S ? S->getVA<ELF64LE>() : SA;
1486 relocateTlsGdToLe(Type, Loc, BufEnd, P, X);
1487 return 0;
1488 }
1489 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1490 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1491 relocateTlsIeToLe(Type, Loc, BufEnd, P, S->getVA<ELF64LE>());
1492 return 0;
1493 }
1494 llvm_unreachable("Unknown TLS optimization");
1495}
1496
1497// Global-Dynamic relocations can be relaxed to Local-Exec if both binary is
1498// an executable and target is final (can notbe preempted).
1499void AArch64TargetInfo::relocateTlsGdToLe(unsigned Type, uint8_t *Loc,
1500 uint8_t *BufEnd, uint64_t P,
1501 uint64_t SA) const {
1502 // TLSDESC Global-Dynamic relocation are in the form:
1503 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1504 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1505 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1506 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1507 // And it can optimized to:
1508 // movz x0, #0x0, lsl #16
1509 // movk x0, #0x10
1510 // nop
1511 // nop
1512
1513 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1514 uint64_t X = SA + TPOff;
1515 checkUInt<32>(X, Type);
1516
1517 uint32_t NewInst;
1518 switch (Type) {
1519 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1520 case R_AARCH64_TLSDESC_CALL:
1521 // nop
1522 NewInst = 0xd503201f;
1523 break;
1524 case R_AARCH64_TLSDESC_ADR_PAGE21:
1525 // movz
1526 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1527 break;
1528 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1529 // movk
1530 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1531 break;
1532 default:
1533 llvm_unreachable("Unsupported Relocation for TLS GD to LE relax");
1534 }
1535 write32le(Loc, NewInst);
1536}
1537
1538// Initial-Exec relocations can be relaxed to Local-Exec if symbol is final
1539// (can not be preempted).
1540void AArch64TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
1541 uint8_t *BufEnd, uint64_t P,
1542 uint64_t SA) const {
1543 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
1544 uint64_t X = SA + TPOff;
1545 checkUInt<32>(X, Type);
1546
1547 uint32_t Inst = read32le (Loc);
1548 uint32_t NewInst;
1549 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1550 // Generate movz.
1551 unsigned RegNo = (Inst & 0x1f);
1552 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1553 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1554 // Generate movk
1555 unsigned RegNo = (Inst & 0x1f);
1556 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1557 } else {
1558 llvm_unreachable("Invalid Relocation for TLS IE to LE Relax");
1559 }
1560 write32le(Loc, NewInst);
1561}
1562
1563
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001564// Implementing relocations for AMDGPU is low priority since most
1565// programs don't use relocations now. Thus, this function is not
1566// actually called (relocateOne is called for each relocation).
1567// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001568void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1569 uint64_t P, uint64_t SA, uint64_t ZA,
1570 uint8_t *PairedLoc) const {
1571 llvm_unreachable("not implemented");
1572}
1573
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001574template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001575 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001576 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001577 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001578 PltEntrySize = 16;
1579 PltZeroSize = 32;
1580 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001581 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001582 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001583 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001584}
1585
1586template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001587unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001588 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1589 return R_MIPS_REL32;
1590 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001591 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001592 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001593 // Keep it going with a dummy value so that we can find more reloc errors.
1594 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001595}
1596
1597template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001598void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001599 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001600 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1601
1602 // Set the MSB of the second GOT slot. This is not required by any
1603 // MIPS ABI documentation, though.
1604 //
1605 // There is a comment in glibc saying that "The MSB of got[1] of a
1606 // gnu object is set to identify gnu objects," and in GNU gold it
1607 // says "the second entry will be used by some runtime loaders".
1608 // But how this field is being used is unclear.
1609 //
1610 // We are not really willing to mimic other linkers behaviors
1611 // without understanding why they do that, but because all files
1612 // generated by GNU tools have this special GOT value, and because
1613 // we've been doing this for years, it is probably a safe bet to
1614 // keep doing this for now. We really need to revisit this to see
1615 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001616 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001617 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001618}
1619
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001620template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001621void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1622 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001623}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001624
Simon Atanasyan35031192015-12-15 06:06:34 +00001625static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001626
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001627template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001628static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001629 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001630 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001631 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001632 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1633 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001634 checkAlignment<(1 << SHIFT)>(S + A, Type);
1635 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001636 checkInt<BSIZE + SHIFT>(V, Type);
1637 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001638}
1639
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001640template <endianness E>
1641static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1642 uint32_t Instr = read32<E>(Loc);
1643 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1644}
1645
1646template <class ELFT>
1647void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1648 const endianness E = ELFT::TargetEndianness;
1649 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1650 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1651 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1652 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1653 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1654 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1655 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1656 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1657 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1658 uint64_t Plt = Out<ELFT>::Plt->getVA();
1659 applyMipsHi16Reloc<E>(Buf, Got, 0);
1660 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1661 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1662}
1663
1664template <class ELFT>
1665void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1666 uint64_t PltEntryAddr, int32_t Index,
1667 unsigned RelOff) const {
1668 const endianness E = ELFT::TargetEndianness;
1669 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1670 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1671 write32<E>(Buf + 8, 0x03200008); // jr $25
1672 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1673 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1674 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1675 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1676}
1677
1678template <class ELFT>
1679bool MipsTargetInfo<ELFT>::needsCopyRel(uint32_t Type,
1680 const SymbolBody &S) const {
1681 if (Config->Shared)
1682 return false;
1683 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1684 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
1685 return SS->Sym.getType() == STT_OBJECT;
1686 return false;
1687}
1688
1689template <class ELFT>
1690bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
1691 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
1692}
1693
1694template <class ELFT>
Rafael Espindola852860e2016-02-12 15:47:37 +00001695TargetInfo::PltNeed MipsTargetInfo<ELFT>::needsPlt(uint32_t Type,
1696 const SymbolBody &S) const {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001697 if (needsCopyRel(Type, S))
Rafael Espindola852860e2016-02-12 15:47:37 +00001698 return Plt_No;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001699 if (Type == R_MIPS_26 && canBePreempted(&S, false))
Rafael Espindola852860e2016-02-12 15:47:37 +00001700 return Plt_Explicit;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001701 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
Rafael Espindola852860e2016-02-12 15:47:37 +00001702 if (S.isShared())
1703 return Plt_Explicit;
1704 return Plt_No;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001705}
1706
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001707template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001708void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001709 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001710 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001711 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001712 switch (Type) {
1713 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001714 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001715 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001716 case R_MIPS_26: {
1717 uint32_t Instr = read32<E>(Loc);
1718 // FIXME (simon): If the relocation target symbol is not a PLT entry
1719 // we should use another expression for calculation:
1720 // ((A << 2) | (P & 0xf0000000)) >> 2
1721 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1722 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1723 break;
1724 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001725 case R_MIPS_CALL16:
1726 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001727 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001728 if (Type == R_MIPS_GOT16)
1729 checkInt<16>(V, Type);
1730 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1731 break;
1732 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001733 case R_MIPS_GPREL16: {
1734 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001735 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001736 checkInt<16>(V, Type);
1737 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1738 break;
1739 }
1740 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001741 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001742 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001743 case R_MIPS_HI16: {
1744 uint32_t Instr = read32<E>(Loc);
1745 if (PairedLoc) {
1746 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001747 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001748 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001749 } else {
1750 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001751 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001752 }
1753 break;
1754 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001755 case R_MIPS_JALR:
1756 // Ignore this optimization relocation for now
1757 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001758 case R_MIPS_LO16: {
1759 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001760 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001761 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001762 break;
1763 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001764 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001765 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001766 break;
1767 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001768 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001769 break;
1770 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001771 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001772 break;
1773 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001774 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001775 break;
1776 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001777 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001778 break;
1779 case R_MIPS_PCHI16: {
1780 uint32_t Instr = read32<E>(Loc);
1781 if (PairedLoc) {
1782 uint64_t AHL = ((Instr & 0xffff) << 16) +
1783 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001784 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001785 } else {
1786 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001787 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001788 }
1789 break;
1790 }
1791 case R_MIPS_PCLO16: {
1792 uint32_t Instr = read32<E>(Loc);
1793 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001794 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001795 break;
1796 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001797 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001798 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001799 }
1800}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001801
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001802template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001803bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001804 return Type == R_MIPS_JALR;
1805}
1806
1807template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001808bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1809 switch (Type) {
1810 default:
1811 return false;
1812 case R_MIPS_PC16:
1813 case R_MIPS_PC19_S2:
1814 case R_MIPS_PC21_S2:
1815 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001816 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001817 case R_MIPS_PCHI16:
1818 case R_MIPS_PCLO16:
1819 return true;
1820 }
1821}
1822
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001823// _gp is a MIPS-specific ABI-defined symbol which points to
1824// a location that is relative to GOT. This function returns
1825// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001826template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001827 unsigned GPOffset = 0x7ff0;
1828 if (uint64_t V = Out<ELFT>::Got->getVA())
1829 return V + GPOffset;
1830 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001831}
1832
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001833template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1834template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1835template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1836template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1837
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001838template uint32_t getMipsGpAddr<ELF32LE>();
1839template uint32_t getMipsGpAddr<ELF32BE>();
1840template uint64_t getMipsGpAddr<ELF64LE>();
1841template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001842}
1843}