blob: d62fa189af53110476ef8a2347ea0a29e0534227 [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
Rafael Espindola22ef9562016-04-13 01:40:19 +000014// in this file.
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"
Simon Atanasyan13f6da12016-03-31 21:26:23 +000020#include "InputFiles.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000021#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000022#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000023
24#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000025#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000026#include "llvm/Support/Endian.h"
27#include "llvm/Support/ELF.h"
28
29using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000030using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000031using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000032using namespace llvm::ELF;
33
34namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000035namespace elf {
Rafael Espindola01205f72015-09-22 18:19:46 +000036
Rui Ueyamac1c282a2016-02-11 21:18:01 +000037TargetInfo *Target;
Rafael Espindola01205f72015-09-22 18:19:46 +000038
Rafael Espindolae7e57b22015-11-09 21:43:00 +000039static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000040
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000041template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
42 if (isInt<N>(V))
43 return;
44 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000045 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000046}
47
48template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
49 if (isUInt<N>(V))
50 return;
51 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000052 error("relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000053}
54
Igor Kudrinfea8ed52015-11-26 10:05:24 +000055template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
56 if (isInt<N>(V) || isUInt<N>(V))
57 return;
58 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000059 error("relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000060}
61
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000062template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
63 if ((V & (N - 1)) == 0)
64 return;
65 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
George Rimar777f9632016-03-12 08:31:34 +000066 error("improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000067}
68
Rui Ueyamaefc23de2015-10-14 21:30:32 +000069namespace {
70class X86TargetInfo final : public TargetInfo {
71public:
72 X86TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +000073 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +000074 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000075 void writeGotPltHeader(uint8_t *Buf) const override;
George Rimar98b060d2016-03-06 06:01:07 +000076 uint32_t getDynRel(uint32_t Type) const override;
77 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000078 bool isTlsLocalDynamicRel(uint32_t Type) const override;
79 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
80 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000081 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000082 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000083 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
84 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000085 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000086 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000087 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000088 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000089 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000090
Rafael Espindola22ef9562016-04-13 01:40:19 +000091 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
92 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
93 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
94 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000095
George Rimarbfb7bf72015-12-21 10:00:12 +000096 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +000097 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000098};
99
100class X86_64TargetInfo final : public TargetInfo {
101public:
102 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000103 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000104 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000105 uint32_t getTlsGotRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000106 bool isTlsLocalDynamicRel(uint32_t Type) const override;
107 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
108 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000109 void writeGotPltHeader(uint8_t *Buf) const override;
110 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000111 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000112 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
113 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000114 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000115 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000116 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000117 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000118 bool isRelRelative(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000119
Rafael Espindola22ef9562016-04-13 01:40:19 +0000120 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
121 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
122 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
123 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000124};
125
Davide Italiano8c3444362016-01-11 19:45:33 +0000126class PPCTargetInfo final : public TargetInfo {
127public:
128 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000129 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000130 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000131 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000132};
133
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000134class PPC64TargetInfo final : public TargetInfo {
135public:
136 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000137 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000138 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
139 int32_t Index, unsigned RelOff) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000140 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000141 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000142 bool isRelRelative(uint32_t Type) const override;
143};
144
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000145class AArch64TargetInfo final : public TargetInfo {
146public:
147 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000148 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000149 uint32_t getDynRel(uint32_t Type) const override;
150 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
151 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000152 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000153 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000154 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
155 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000156 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000157 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000158 bool needsCopyRelImpl(uint32_t Type) const override;
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +0000159 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000160 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000161 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
162 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
163 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000164
165private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000166 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000167};
168
Tom Stellard80efb162016-01-07 03:59:08 +0000169class AMDGPUTargetInfo final : public TargetInfo {
170public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000171 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000172 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
173 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000174};
175
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000176template <class ELFT> class MipsTargetInfo final : public TargetInfo {
177public:
178 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000179 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000180 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000181 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000182 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
183 void writePltZero(uint8_t *Buf) const override;
184 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
185 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000186 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000187 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000188 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000189 bool needsPltImpl(uint32_t Type) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000190 bool needsThunk(uint32_t Type, const InputFile &File,
191 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000192 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000193 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000194 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000195 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000196};
197} // anonymous namespace
198
Rui Ueyama91004392015-10-13 16:08:15 +0000199TargetInfo *createTarget() {
200 switch (Config->EMachine) {
201 case EM_386:
202 return new X86TargetInfo();
203 case EM_AARCH64:
204 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000205 case EM_AMDGPU:
206 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000207 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000208 switch (Config->EKind) {
209 case ELF32LEKind:
210 return new MipsTargetInfo<ELF32LE>();
211 case ELF32BEKind:
212 return new MipsTargetInfo<ELF32BE>();
213 default:
George Rimar777f9632016-03-12 08:31:34 +0000214 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000215 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000216 case EM_PPC:
217 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000218 case EM_PPC64:
219 return new PPC64TargetInfo();
220 case EM_X86_64:
221 return new X86_64TargetInfo();
222 }
George Rimar777f9632016-03-12 08:31:34 +0000223 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000224}
225
Rafael Espindola01205f72015-09-22 18:19:46 +0000226TargetInfo::~TargetInfo() {}
227
Rafael Espindola666625b2016-04-01 14:36:09 +0000228uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
229 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000230 return 0;
231}
232
George Rimar98b060d2016-03-06 06:01:07 +0000233bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000234 if (Config->Shared || (S && !S->isTls()))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000235 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000236
Rafael Espindolad405f472016-03-04 21:37:09 +0000237 // We know we are producing an executable.
238
239 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
240 // depending on the symbol being locally defined or not.
241 if (isTlsGlobalDynamicRel(Type))
242 return true;
243
244 // Local-Dynamic relocs can be relaxed to Local-Exec.
245 if (isTlsLocalDynamicRel(Type))
246 return true;
247
248 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
249 // defined.
250 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000251 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000252
George Rimar77d1cb12015-11-24 09:00:06 +0000253 return false;
254}
255
George Rimar786e8662016-03-17 05:57:33 +0000256uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000257
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000258bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
259
260template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
261 if (Config->Shared)
262 return false;
263 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
264 if (!SS)
265 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000266 return SS->isObject();
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000267}
268
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000269template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000270bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000271 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000272}
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; }
George Rimar48651482015-12-11 08:59:37 +0000277
Rafael Espindola993f0272016-02-26 14:27:47 +0000278bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000279
280bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
281
Rafael Espindola852860e2016-02-12 15:47:37 +0000282TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
283 const SymbolBody &S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000284 if (S.isGnuIFunc())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000285 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000286 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000287 return Plt_Explicit;
288
289 // This handles a non PIC program call to function in a shared library.
290 // In an ideal world, we could just report an error saying the relocation
291 // can overflow at runtime.
292 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
293 // libc.so.
294 //
295 // The general idea on how to handle such cases is to create a PLT entry
296 // and use that as the function value.
297 //
298 // For the static linking part, we just return true and everything else
299 // will use the the PLT entry as the address.
300 //
301 // The remaining problem is making sure pointer equality still works. We
302 // need the help of the dynamic linker for that. We let it know that we have
303 // a direct reference to a so symbol by creating an undefined symbol with a
304 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
305 // the value of the symbol we created. This is true even for got entries, so
306 // pointer equality is maintained. To avoid an infinite loop, the only entry
307 // that points to the real function is a dedicated got entry used by the
308 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
309 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000310 if (S.isShared())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000311 if (!Config->Pic && S.isFunc() && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000312 return Plt_Implicit;
313
Rafael Espindola852860e2016-02-12 15:47:37 +0000314 return Plt_No;
315}
Rui Ueyama012eb782016-01-29 04:05:09 +0000316
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000317bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
318 const SymbolBody &S) const {
319 return false;
320}
321
George Rimar98b060d2016-03-06 06:01:07 +0000322bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000323
George Rimar98b060d2016-03-06 06:01:07 +0000324bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000325
George Rimar98b060d2016-03-06 06:01:07 +0000326bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000327 return false;
328}
329
Rafael Espindola22ef9562016-04-13 01:40:19 +0000330void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
331 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000332 llvm_unreachable("Should not have claimed to be relaxable");
333}
334
Rafael Espindola22ef9562016-04-13 01:40:19 +0000335void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
336 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000337 llvm_unreachable("Should not have claimed to be relaxable");
338}
339
Rafael Espindola22ef9562016-04-13 01:40:19 +0000340void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
341 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000342 llvm_unreachable("Should not have claimed to be relaxable");
343}
344
Rafael Espindola22ef9562016-04-13 01:40:19 +0000345void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
346 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000347 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000348}
George Rimar77d1cb12015-11-24 09:00:06 +0000349
Rafael Espindola7f074422015-09-22 21:35:51 +0000350X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000351 CopyRel = R_386_COPY;
352 GotRel = R_386_GLOB_DAT;
353 PltRel = R_386_JUMP_SLOT;
354 IRelativeRel = R_386_IRELATIVE;
355 RelativeRel = R_386_RELATIVE;
356 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000357 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
358 TlsOffsetRel = R_386_TLS_DTPOFF32;
359 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000360 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000361 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000362 TlsGdToLeSkip = 2;
363}
364
365RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
366 switch (Type) {
367 default:
368 return R_ABS;
Rafael Espindoladf172772016-04-18 01:29:15 +0000369 case R_386_TLS_GD:
370 return R_TLSGD;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000371 case R_386_TLS_LDM:
372 return R_TLSLD;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000373 case R_386_PLT32:
374 case R_386_PC32:
375 case R_386_GOTPC:
376 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000377 case R_386_GOT32:
378 case R_386_TLS_GOTIE:
379 case R_386_TLS_IE:
380 return R_GOT;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000381 }
George Rimar77b77792015-11-25 22:15:01 +0000382}
383
Rafael Espindolaffcad442016-03-23 14:58:25 +0000384bool X86TargetInfo::isRelRelative(uint32_t Type) const {
385 switch (Type) {
386 default:
387 return false;
388 case R_386_PC32:
389 case R_386_PLT32:
390 case R_386_TLS_LDO_32:
391 return true;
392 }
393}
394
Rui Ueyamac516ae12016-01-29 02:33:45 +0000395void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000396 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
397}
398
Rui Ueyamac516ae12016-01-29 02:33:45 +0000399void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000400 // Entries in .got.plt initially points back to the corresponding
401 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000402 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000403}
Rafael Espindola01205f72015-09-22 18:19:46 +0000404
George Rimar98b060d2016-03-06 06:01:07 +0000405uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000406 if (Type == R_386_TLS_LE)
407 return R_386_TLS_TPOFF;
408 if (Type == R_386_TLS_LE_32)
409 return R_386_TLS_TPOFF32;
410 return Type;
411}
412
George Rimar98b060d2016-03-06 06:01:07 +0000413uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000414 if (Type == R_386_TLS_IE)
415 return Type;
Rafael Espindolae149b482016-04-14 16:05:42 +0000416 return R_386_GOT32;
George Rimar6f17e092015-12-17 09:32:21 +0000417}
418
George Rimar98b060d2016-03-06 06:01:07 +0000419bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000420 return Type == R_386_TLS_GD;
421}
422
George Rimar98b060d2016-03-06 06:01:07 +0000423bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000424 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
425}
426
George Rimar98b060d2016-03-06 06:01:07 +0000427bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000428 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
429}
430
Rui Ueyama900e2d22016-01-29 03:51:49 +0000431void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000432 // Executable files and shared object files have
433 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000434 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000435 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000436 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000437 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
438 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000439 };
440 memcpy(Buf, V, sizeof(V));
441 return;
442 }
George Rimar648a2c32015-10-20 08:54:27 +0000443
George Rimar77b77792015-11-25 22:15:01 +0000444 const uint8_t PltData[] = {
445 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000446 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
447 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000448 };
449 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000450 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000451 write32le(Buf + 2, Got + 4);
452 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000453}
454
Rui Ueyama9398f862016-01-29 04:15:02 +0000455void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
456 uint64_t PltEntryAddr, int32_t Index,
457 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000458 const uint8_t Inst[] = {
459 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
460 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
461 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
462 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000463 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000464
George Rimar77b77792015-11-25 22:15:01 +0000465 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000466 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000467 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
468 : Out<ELF32LE>::Got->getVA();
469 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000470 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000471 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000472}
473
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000474bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
475 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000476}
477
Rafael Espindola993f0272016-02-26 14:27:47 +0000478bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
479 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000480}
481
George Rimarbfb7bf72015-12-21 10:00:12 +0000482bool X86TargetInfo::isGotRelative(uint32_t Type) const {
483 // This relocation does not require got entry,
484 // but it is relative to got and needs it to be created.
485 // Here we request for that.
486 return Type == R_386_GOTOFF;
487}
488
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000489bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
490 return Type == R_386_GOT32;
491}
492
Rafael Espindola666625b2016-04-01 14:36:09 +0000493uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
494 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000495 switch (Type) {
496 default:
497 return 0;
498 case R_386_32:
499 case R_386_GOT32:
500 case R_386_GOTOFF:
501 case R_386_GOTPC:
502 case R_386_PC32:
503 case R_386_PLT32:
504 return read32le(Buf);
505 }
506}
507
Rafael Espindola22ef9562016-04-13 01:40:19 +0000508void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
509 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000510 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000511 case R_386_32:
Rafael Espindola7dd20562016-04-15 17:57:27 +0000512 case R_386_PC32:
513 case R_386_PLT32:
514 case R_386_TLS_IE:
515 case R_386_TLS_LDO_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000516 write32le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000517 break;
George Rimarbfb7bf72015-12-21 10:00:12 +0000518 case R_386_GOTOFF:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000519 write32le(Loc, Val - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000520 break;
George Rimar13934772015-11-25 20:20:31 +0000521 case R_386_GOTPC:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000522 write32le(Loc, Val + Out<ELF32LE>::Got->getVA());
George Rimar13934772015-11-25 20:20:31 +0000523 break;
Rafael Espindolac1c14a22016-04-14 15:56:14 +0000524 case R_386_GOT32:
George Rimar9db204a2015-12-02 09:58:20 +0000525 case R_386_TLS_GD:
Rafael Espindolae149b482016-04-14 16:05:42 +0000526 case R_386_TLS_LDM: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000527 uint64_t V = Val - Out<ELF32LE>::Got->getVA() -
George Rimar9db204a2015-12-02 09:58:20 +0000528 Out<ELF32LE>::Got->getNumEntries() * 4;
529 checkInt<32>(V, Type);
530 write32le(Loc, V);
531 break;
532 }
George Rimard23970f2015-11-25 20:41:53 +0000533 case R_386_TLS_LE:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000534 write32le(Loc, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
George Rimard23970f2015-11-25 20:41:53 +0000535 break;
536 case R_386_TLS_LE_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000537 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimard23970f2015-11-25 20:41:53 +0000538 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000539 default:
George Rimar57610422016-03-11 14:43:02 +0000540 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000541 }
542}
543
George Rimar98b060d2016-03-06 06:01:07 +0000544bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000545 return Config->Shared && Type == R_386_TLS_IE;
546}
547
Rafael Espindola22ef9562016-04-13 01:40:19 +0000548void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
549 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000550 // GD can be optimized to LE:
551 // leal x@tlsgd(, %ebx, 1),
552 // call __tls_get_addr@plt
553 // Can be converted to:
554 // movl %gs:0,%eax
555 // addl $x@ntpoff,%eax
556 // But gold emits subl $foo@tpoff,%eax instead of addl.
557 // These instructions are completely equal in behavior.
558 // This method generates subl to be consistent with gold.
559 const uint8_t Inst[] = {
560 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
561 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
562 };
563 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000564 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000565}
566
567// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
568// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
569// how GD can be optimized to IE:
570// leal x@tlsgd(, %ebx, 1),
571// call __tls_get_addr@plt
572// Is converted to:
573// movl %gs:0, %eax
574// addl x@gotntpoff(%ebx), %eax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000575void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
576 uint64_t Val) const {
George Rimar2558e122015-12-09 09:55:54 +0000577 const uint8_t Inst[] = {
578 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
579 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
580 };
581 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000582 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
583 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000584}
585
George Rimar6f17e092015-12-17 09:32:21 +0000586// In some conditions, relocations can be optimized to avoid using GOT.
587// This function does that for Initial Exec to Local Exec case.
588// Read "ELF Handling For Thread-Local Storage, 5.1
589// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000590// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000591void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
592 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000593 // Ulrich's document section 6.2 says that @gotntpoff can
594 // be used with MOVL or ADDL instructions.
595 // @indntpoff is similar to @gotntpoff, but for use in
596 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000597 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000598 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000599 uint8_t Reg = (Loc[-1] >> 3) & 7;
600 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000601 if (Type == R_386_TLS_IE) {
602 // For R_386_TLS_IE relocation we perform the next transformations:
603 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
604 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
605 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
606 // First one is special because when EAX is used the sequence is 5 bytes
607 // long, otherwise it is 6 bytes.
608 if (*Op == 0xa1) {
609 *Op = 0xb8;
610 } else {
611 *Inst = IsMov ? 0xc7 : 0x81;
612 *Op = 0xc0 | ((*Op >> 3) & 7);
613 }
614 } else {
615 // R_386_TLS_GOTIE relocation can be optimized to
616 // R_386_TLS_LE so that it does not use GOT.
617 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
618 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
619 // Note: gold converts to ADDL instead of LEAL.
620 *Inst = IsMov ? 0xc7 : 0x8d;
621 if (IsMov)
622 *Op = 0xc0 | ((*Op >> 3) & 7);
623 else
624 *Op = 0x80 | Reg | (Reg << 3);
625 }
Rafael Espindola22ef9562016-04-13 01:40:19 +0000626 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000627}
628
Rafael Espindola22ef9562016-04-13 01:40:19 +0000629void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
630 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000631 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000632 relocateOne(Loc, R_386_TLS_LE, Val);
633 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000634 }
635
636 // LD can be optimized to LE:
637 // leal foo(%reg),%eax
638 // call ___tls_get_addr
639 // Is converted to:
640 // movl %gs:0,%eax
641 // nop
642 // leal 0(%esi,1),%esi
643 const uint8_t Inst[] = {
644 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
645 0x90, // nop
646 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
647 };
648 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000649}
650
Rafael Espindola7f074422015-09-22 21:35:51 +0000651X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000652 CopyRel = R_X86_64_COPY;
653 GotRel = R_X86_64_GLOB_DAT;
654 PltRel = R_X86_64_JUMP_SLOT;
655 RelativeRel = R_X86_64_RELATIVE;
656 IRelativeRel = R_X86_64_IRELATIVE;
657 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000658 TlsModuleIndexRel = R_X86_64_DTPMOD64;
659 TlsOffsetRel = R_X86_64_DTPOFF64;
660 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000661 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000662 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000663 TlsGdToLeSkip = 2;
664}
665
666RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
667 switch (Type) {
668 default:
669 return R_ABS;
Rafael Espindolac4d56972016-04-18 00:28:57 +0000670 case R_X86_64_TLSLD:
671 return R_TLSLD_PC;
Rafael Espindoladf172772016-04-18 01:29:15 +0000672 case R_X86_64_TLSGD:
673 return R_TLSGD_PC;
Rafael Espindola3151d892016-04-14 18:39:44 +0000674 case R_X86_64_SIZE32:
675 case R_X86_64_SIZE64:
676 return R_SIZE;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000677 case R_X86_64_PLT32:
678 case R_X86_64_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000679 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +0000680 case R_X86_64_GOT32:
681 return R_GOT;
682 case R_X86_64_GOTPCREL:
683 case R_X86_64_GOTTPOFF:
684 return R_GOT_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000685 }
George Rimar648a2c32015-10-20 08:54:27 +0000686}
687
Rui Ueyamac516ae12016-01-29 02:33:45 +0000688void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000689 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
690}
691
Rui Ueyamac516ae12016-01-29 02:33:45 +0000692void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000693 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000694 write32le(Buf, Plt + 6);
695}
696
Rui Ueyama900e2d22016-01-29 03:51:49 +0000697void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000698 const uint8_t PltData[] = {
699 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
700 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
701 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
702 };
703 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000704 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
705 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
706 write32le(Buf + 2, Got - Plt + 2); // GOT+8
707 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000708}
Rafael Espindola01205f72015-09-22 18:19:46 +0000709
Rui Ueyama9398f862016-01-29 04:15:02 +0000710void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
711 uint64_t PltEntryAddr, int32_t Index,
712 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000713 const uint8_t Inst[] = {
714 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
715 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
716 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
717 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000718 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000719
George Rimar648a2c32015-10-20 08:54:27 +0000720 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
721 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000722 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000723}
724
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000725bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
726 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
727 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000728}
729
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000730bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000731 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
732 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000733}
734
George Rimar86971052016-03-29 08:35:42 +0000735uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
736 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
737 if (Config->Shared)
738 error(getELFRelocationTypeName(EM_X86_64, Type) +
739 " cannot be a dynamic relocation");
740 return Type;
741}
742
George Rimar98b060d2016-03-06 06:01:07 +0000743uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000744 // No other types of TLS relocations requiring GOT should
745 // reach here.
746 assert(Type == R_X86_64_GOTTPOFF);
747 return R_X86_64_PC32;
748}
749
George Rimar98b060d2016-03-06 06:01:07 +0000750bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000751 return Type == R_X86_64_GOTTPOFF;
752}
753
George Rimar98b060d2016-03-06 06:01:07 +0000754bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000755 return Type == R_X86_64_TLSGD;
756}
757
George Rimar98b060d2016-03-06 06:01:07 +0000758bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000759 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
760 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000761}
762
Rafael Espindola993f0272016-02-26 14:27:47 +0000763bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
764 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000765}
Rafael Espindolac4010882015-09-22 20:54:08 +0000766
Rafael Espindolaae244002015-10-05 19:30:12 +0000767bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
768 switch (Type) {
769 default:
770 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000771 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000772 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000773 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000774 case R_X86_64_PC8:
775 case R_X86_64_PC16:
776 case R_X86_64_PC32:
777 case R_X86_64_PC64:
778 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000779 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000780 return true;
781 }
782}
783
George Rimar6713cf82015-11-25 21:46:05 +0000784// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
785// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000786// how GD can be optimized to LE:
787// .byte 0x66
788// leaq x@tlsgd(%rip), %rdi
789// .word 0x6666
790// rex64
791// call __tls_get_addr@plt
792// Is converted to:
793// mov %fs:0x0,%rax
794// lea x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000795void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
796 uint64_t Val) const {
George Rimar6713cf82015-11-25 21:46:05 +0000797 const uint8_t Inst[] = {
798 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
799 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
800 };
801 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000802 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000803}
804
George Rimar25411f252015-12-04 11:20:13 +0000805// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
806// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
807// how GD can be optimized to IE:
808// .byte 0x66
809// leaq x@tlsgd(%rip), %rdi
810// .word 0x6666
811// rex64
812// call __tls_get_addr@plt
813// Is converted to:
814// mov %fs:0x0,%rax
815// addq x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000816void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
817 uint64_t Val) const {
George Rimar25411f252015-12-04 11:20:13 +0000818 const uint8_t Inst[] = {
819 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
820 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
821 };
822 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000823 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000824}
825
George Rimar77d1cb12015-11-24 09:00:06 +0000826// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000827// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000828// This function does that. Read "ELF Handling For Thread-Local Storage,
829// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
830// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000831void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
832 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000833 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
834 // used in MOVQ or ADDQ instructions only.
835 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
836 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
837 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
838 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
839 uint8_t *Prefix = Loc - 3;
840 uint8_t *Inst = Loc - 2;
841 uint8_t *RegSlot = Loc - 1;
842 uint8_t Reg = Loc[-1] >> 3;
843 bool IsMov = *Inst == 0x8b;
844 bool RspAdd = !IsMov && Reg == 4;
845 // r12 and rsp registers requires special handling.
846 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
847 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
848 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
849 // The same true for rsp. So we convert to addq for them, saving 1 byte that
850 // we dont have.
851 if (RspAdd)
852 *Inst = 0x81;
853 else
854 *Inst = IsMov ? 0xc7 : 0x8d;
855 if (*Prefix == 0x4c)
856 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
857 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000858 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000859}
860
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000861// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
862// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
863// how LD can be optimized to LE:
864// leaq bar@tlsld(%rip), %rdi
865// callq __tls_get_addr@PLT
866// leaq bar@dtpoff(%rax), %rcx
867// Is converted to:
868// .word 0x6666
869// .byte 0x66
870// mov %fs:0,%rax
871// leaq bar@tpoff(%rax), %rcx
Rafael Espindola22ef9562016-04-13 01:40:19 +0000872void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
873 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000874 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000875 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
876 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000877 }
878 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000879 relocateOne(Loc, R_X86_64_TPOFF32, Val);
880 return;
George Rimar25411f252015-12-04 11:20:13 +0000881 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000882
883 const uint8_t Inst[] = {
884 0x66, 0x66, //.word 0x6666
885 0x66, //.byte 0x66
886 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
887 };
888 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000889}
890
Rafael Espindola22ef9562016-04-13 01:40:19 +0000891void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
892 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000893 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000894 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000895 checkUInt<32>(Val, Type);
896 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000897 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000898 case R_X86_64_32S:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000899 checkInt<32>(Val, Type);
900 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000901 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000902 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000903 case R_X86_64_DTPOFF64:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000904 case R_X86_64_SIZE64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000905 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000906 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000907 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000908 case R_X86_64_GOTPCRELX:
909 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000910 case R_X86_64_PC32:
911 case R_X86_64_PLT32:
912 case R_X86_64_TLSGD:
913 case R_X86_64_TLSLD:
Rafael Espindola3c20fb42016-04-18 11:53:42 +0000914 case R_X86_64_DTPOFF32:
George Rimar48651482015-12-11 08:59:37 +0000915 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000916 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000917 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000918 case R_X86_64_TPOFF32: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000919 Val -= Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000920 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000921 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000922 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000923 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000924 default:
George Rimar57610422016-03-11 14:43:02 +0000925 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000926 }
927}
928
Hal Finkel3c8cc672015-10-12 20:56:18 +0000929// Relocation masks following the #lo(value), #hi(value), #ha(value),
930// #higher(value), #highera(value), #highest(value), and #highesta(value)
931// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
932// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000933static uint16_t applyPPCLo(uint64_t V) { return V; }
934static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
935static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
936static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
937static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000938static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000939static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
940
Davide Italiano8c3444362016-01-11 19:45:33 +0000941PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000942bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
943
Rafael Espindola22ef9562016-04-13 01:40:19 +0000944void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
945 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000946 switch (Type) {
947 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000948 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000949 break;
950 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000951 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000952 break;
953 default:
George Rimar57610422016-03-11 14:43:02 +0000954 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000955 }
956}
957
Rafael Espindola22ef9562016-04-13 01:40:19 +0000958RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
959 return R_ABS;
960}
961
Rafael Espindolac4010882015-09-22 20:54:08 +0000962PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000963 GotRel = R_PPC64_GLOB_DAT;
964 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000965 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000966
967 // We need 64K pages (at least under glibc/Linux, the loader won't
968 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000969 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000970
971 // The PPC64 ELF ABI v1 spec, says:
972 //
973 // It is normally desirable to put segments with different characteristics
974 // in separate 256 Mbyte portions of the address space, to give the
975 // operating system full paging flexibility in the 64-bit address space.
976 //
977 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
978 // use 0x10000000 as the starting address.
979 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000980}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000981
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000982uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000983 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
984 // order. The TOC starts where the first of these sections starts.
985
986 // FIXME: This obviously does not do the right thing when there is no .got
987 // section, but there is a .toc or .tocbss section.
988 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
989 if (!TocVA)
990 TocVA = Out<ELF64BE>::Plt->getVA();
991
992 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
993 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
994 // code (crt1.o) assumes that you can get from the TOC base to the
995 // start of the .toc section with only a single (signed) 16-bit relocation.
996 return TocVA + 0x8000;
997}
998
Rafael Espindola22ef9562016-04-13 01:40:19 +0000999RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1000 switch (Type) {
1001 default:
1002 return R_ABS;
1003 case R_PPC64_REL24:
1004 return R_PPC_OPD;
1005 }
1006}
1007
Rui Ueyama9398f862016-01-29 04:15:02 +00001008void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1009 uint64_t PltEntryAddr, int32_t Index,
1010 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001011 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1012
1013 // FIXME: What we should do, in theory, is get the offset of the function
1014 // descriptor in the .opd section, and use that as the offset from %r2 (the
1015 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1016 // be a pointer to the function descriptor in the .opd section. Using
1017 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1018
Hal Finkelfa92f682015-10-13 21:47:34 +00001019 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001020 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1021 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1022 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1023 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1024 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1025 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1026 write32be(Buf + 28, 0x4e800420); // bctr
1027}
1028
Rafael Espindola993f0272016-02-26 14:27:47 +00001029bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001030 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001031 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001032}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001033
Hal Finkelbe0823d2015-10-12 20:58:52 +00001034bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1035 switch (Type) {
1036 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001037 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001038 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001039 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001040 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001041 }
1042}
1043
Rafael Espindola22ef9562016-04-13 01:40:19 +00001044void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1045 uint64_t Val) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001046 uint64_t TB = getPPC64TocBase();
1047
Hal Finkel3c8cc672015-10-12 20:56:18 +00001048 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1049 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001050 switch (Type) {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001051 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TB; break;
1052 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TB; break;
1053 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TB; break;
1054 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TB; break;
1055 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TB; break;
1056 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001057 default: break;
1058 }
1059
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001061 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001062 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001063 // Preserve the AA/LK bits in the branch instruction
1064 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001065 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001066 break;
1067 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001068 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001069 checkInt<16>(Val, Type);
1070 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001071 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001072 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001073 checkInt<16>(Val, Type);
1074 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001075 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001076 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001077 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001078 break;
1079 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001080 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001081 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001083 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084 break;
1085 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001086 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001087 break;
1088 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001089 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001090 break;
1091 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001092 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001093 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001094 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001095 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001096 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001097 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001098 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001099 break;
1100 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001101 checkInt<32>(Val, Type);
1102 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001104 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001105 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001106 break;
1107 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001108 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001109 break;
1110 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001111 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001112 break;
1113 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001114 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001115 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001116 case R_PPC64_REL24: {
1117 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001118 checkInt<24>(Val, Type);
1119 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001120 break;
1121 }
1122 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001123 checkInt<32>(Val, Type);
1124 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001125 break;
1126 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001127 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001128 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001129 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001130 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001131 break;
1132 default:
George Rimar57610422016-03-11 14:43:02 +00001133 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001134 }
1135}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001136
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001137AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001138 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001139 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001140 IRelativeRel = R_AARCH64_IRELATIVE;
1141 GotRel = R_AARCH64_GLOB_DAT;
1142 PltRel = R_AARCH64_JUMP_SLOT;
1143 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001144 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1145 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001146 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001147 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001148 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001149}
George Rimar648a2c32015-10-20 08:54:27 +00001150
Rafael Espindola22ef9562016-04-13 01:40:19 +00001151RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1152 const SymbolBody &S) const {
1153 switch (Type) {
1154 default:
1155 return R_ABS;
1156 case R_AARCH64_JUMP26:
1157 case R_AARCH64_CALL26:
1158 case R_AARCH64_PREL16:
1159 case R_AARCH64_PREL32:
1160 case R_AARCH64_PREL64:
1161 case R_AARCH64_CONDBR19:
1162 case R_AARCH64_ADR_PREL_LO21:
1163 case R_AARCH64_TSTBR14:
1164 return R_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001165 case R_AARCH64_ADR_PREL_PG_HI21:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001166 return R_PAGE_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001167 case R_AARCH64_LD64_GOT_LO12_NC:
1168 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1169 return R_GOT;
1170 case R_AARCH64_ADR_GOT_PAGE:
1171 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1172 return R_GOT_PAGE_PC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001173 }
1174}
1175
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001176bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001177 switch (Type) {
1178 default:
1179 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001180 case R_AARCH64_ADD_ABS_LO12_NC:
1181 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001182 case R_AARCH64_ADR_PREL_LO21:
1183 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001184 case R_AARCH64_CALL26:
1185 case R_AARCH64_CONDBR19:
1186 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001187 case R_AARCH64_LDST8_ABS_LO12_NC:
1188 case R_AARCH64_LDST16_ABS_LO12_NC:
1189 case R_AARCH64_LDST32_ABS_LO12_NC:
1190 case R_AARCH64_LDST64_ABS_LO12_NC:
1191 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001192 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001193 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001194 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1195 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1196 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1197 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1198 case R_AARCH64_TSTBR14:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001199 return true;
1200 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001201}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001202
George Rimar98b060d2016-03-06 06:01:07 +00001203bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001204 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1205 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1206 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1207 Type == R_AARCH64_TLSDESC_CALL;
1208}
1209
George Rimar98b060d2016-03-06 06:01:07 +00001210bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001211 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1212 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1213}
1214
George Rimar98b060d2016-03-06 06:01:07 +00001215uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001216 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1217 return Type;
1218 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001219 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001220 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001221 // Keep it going with a dummy value so that we can find more reloc errors.
1222 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001223}
1224
Rui Ueyamac516ae12016-01-29 02:33:45 +00001225void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001226 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1227}
1228
Rafael Espindola22ef9562016-04-13 01:40:19 +00001229static uint64_t getAArch64Page(uint64_t Expr) {
1230 return Expr & (~static_cast<uint64_t>(0xFFF));
1231}
1232
Rui Ueyama900e2d22016-01-29 03:51:49 +00001233void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001234 const uint8_t PltData[] = {
1235 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1236 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1237 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1238 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1239 0x20, 0x02, 0x1f, 0xd6, // br x17
1240 0x1f, 0x20, 0x03, 0xd5, // nop
1241 0x1f, 0x20, 0x03, 0xd5, // nop
1242 0x1f, 0x20, 0x03, 0xd5 // nop
1243 };
1244 memcpy(Buf, PltData, sizeof(PltData));
1245
Rui Ueyama900e2d22016-01-29 03:51:49 +00001246 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1247 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001248 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1249 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1250 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1251 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001252}
1253
Rui Ueyama9398f862016-01-29 04:15:02 +00001254void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1255 uint64_t PltEntryAddr, int32_t Index,
1256 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001257 const uint8_t Inst[] = {
1258 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1259 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1260 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1261 0x20, 0x02, 0x1f, 0xd6 // br x17
1262 };
1263 memcpy(Buf, Inst, sizeof(Inst));
1264
Rafael Espindola22ef9562016-04-13 01:40:19 +00001265 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1266 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1267 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1268 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001269}
1270
George Rimar98b060d2016-03-06 06:01:07 +00001271uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001272 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001273 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1274 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001275}
1276
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001277bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001278 switch (Type) {
1279 default:
1280 return false;
1281 case R_AARCH64_ABS16:
1282 case R_AARCH64_ABS32:
1283 case R_AARCH64_ABS64:
1284 case R_AARCH64_ADD_ABS_LO12_NC:
1285 case R_AARCH64_ADR_PREL_LO21:
1286 case R_AARCH64_ADR_PREL_PG_HI21:
1287 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001288 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001289 case R_AARCH64_LDST32_ABS_LO12_NC:
1290 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001291 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001292 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001293 }
1294}
1295
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +00001296bool AArch64TargetInfo::refersToGotEntry(uint32_t Type) const {
1297 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC;
1298}
1299
Rafael Espindola993f0272016-02-26 14:27:47 +00001300bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001301 switch (Type) {
1302 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001303 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001304 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001305 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001306 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001307 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001308 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001309 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001310}
Davide Italiano1d750a62015-09-27 08:45:38 +00001311
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001312static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001313 uint32_t ImmLo = (Imm & 0x3) << 29;
1314 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1315 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001316 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001317}
1318
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001319static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1320 or32le(L, (Imm & 0xFFF) << 10);
1321}
1322
Rafael Espindola22ef9562016-04-13 01:40:19 +00001323void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1324 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001325 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001326 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001327 checkIntUInt<16>(Val, Type);
1328 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001329 break;
1330 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001331 checkIntUInt<32>(Val, Type);
1332 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001333 break;
1334 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001335 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001336 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001337 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001338 // This relocation stores 12 bits and there's no instruction
1339 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001340 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1341 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001342 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001343 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001344 case R_AARCH64_ADR_GOT_PAGE: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001345 uint64_t X = Val;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001346 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001347 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001348 break;
1349 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001350 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001351 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001352 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001353 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001354 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001355 }
George Rimar3d737e42016-01-13 13:04:46 +00001356 case R_AARCH64_ADR_PREL_PG_HI21:
1357 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001358 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001359 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001360 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001361 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001362 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001363 case R_AARCH64_CALL26:
1364 case R_AARCH64_JUMP26: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001365 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001366 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001367 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1368 break;
1369 }
George Rimar4102bfb2016-01-11 14:22:00 +00001370 case R_AARCH64_CONDBR19: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001371 uint64_t X = Val;
George Rimar4102bfb2016-01-11 14:22:00 +00001372 checkInt<21>(X, Type);
1373 or32le(Loc, (X & 0x1FFFFC) << 3);
1374 break;
1375 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001376 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001377 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001378 checkAlignment<8>(Val, Type);
1379 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001380 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001381 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001382 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001383 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001384 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001385 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001386 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001387 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001388 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001389 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001390 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001391 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001392 break;
1393 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001394 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001395 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001396 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001397 checkIntUInt<16>(Val, Type);
1398 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001399 break;
1400 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001401 checkIntUInt<32>(Val, Type);
1402 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001403 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001404 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001405 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001406 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001407 case R_AARCH64_TSTBR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001408 uint64_t X = Val;
George Rimar1395dbd2016-01-11 14:27:05 +00001409 checkInt<16>(X, Type);
1410 or32le(Loc, (X & 0xFFFC) << 3);
1411 break;
1412 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001413 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001414 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001415 checkInt<24>(V, Type);
1416 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1417 break;
1418 }
1419 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001420 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001421 updateAArch64Add(Loc, V & 0xFFF);
1422 break;
1423 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001424 default:
George Rimar57610422016-03-11 14:43:02 +00001425 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001426 }
1427}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001428
Rafael Espindola22ef9562016-04-13 01:40:19 +00001429void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1430 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001431 // TLSDESC Global-Dynamic relocation are in the form:
1432 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1433 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1434 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1435 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1436 // And it can optimized to:
1437 // movz x0, #0x0, lsl #16
1438 // movk x0, #0x10
1439 // nop
1440 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001441 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001442 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001443 checkUInt<32>(X, Type);
1444
1445 uint32_t NewInst;
1446 switch (Type) {
1447 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1448 case R_AARCH64_TLSDESC_CALL:
1449 // nop
1450 NewInst = 0xd503201f;
1451 break;
1452 case R_AARCH64_TLSDESC_ADR_PAGE21:
1453 // movz
1454 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1455 break;
1456 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1457 // movk
1458 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1459 break;
1460 default:
George Rimar777f9632016-03-12 08:31:34 +00001461 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001462 }
1463 write32le(Loc, NewInst);
1464}
1465
Rafael Espindola22ef9562016-04-13 01:40:19 +00001466void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1467 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001468 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001469 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001470 checkUInt<32>(X, Type);
1471
George Rimar4d1d16d2016-03-06 06:16:05 +00001472 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001473 uint32_t NewInst;
1474 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1475 // Generate movz.
1476 unsigned RegNo = (Inst & 0x1f);
1477 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1478 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1479 // Generate movk
1480 unsigned RegNo = (Inst & 0x1f);
1481 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1482 } else {
George Rimar777f9632016-03-12 08:31:34 +00001483 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001484 }
1485 write32le(Loc, NewInst);
1486}
1487
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001488// Implementing relocations for AMDGPU is low priority since most
1489// programs don't use relocations now. Thus, this function is not
1490// actually called (relocateOne is called for each relocation).
1491// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001492void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1493 uint64_t Val) const {
1494 llvm_unreachable("not implemented");
1495}
1496
1497RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001498 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001499}
1500
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001501template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001502 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001503 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001504 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001505 PltEntrySize = 16;
1506 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001507 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001508 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001509 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001510 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001511 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001512}
1513
1514template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001515RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1516 const SymbolBody &S) const {
1517 switch (Type) {
1518 default:
1519 return R_ABS;
1520 case R_MIPS_HI16:
Simon Atanasyan1ca263c2016-04-14 21:10:05 +00001521 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001522 // MIPS _gp_disp designates offset between start of function and 'gp'
1523 // pointer into GOT. __gnu_local_gp is equal to the current value of
1524 // the 'gp'. Therefore any relocations against them do not require
1525 // dynamic relocation.
1526 if (&S == ElfSym<ELFT>::MipsGpDisp)
1527 return R_PC;
1528 return R_ABS;
1529 case R_MIPS_PC32:
1530 case R_MIPS_PC16:
1531 case R_MIPS_PC19_S2:
1532 case R_MIPS_PC21_S2:
1533 case R_MIPS_PC26_S2:
1534 case R_MIPS_PCHI16:
1535 case R_MIPS_PCLO16:
1536 return R_PC;
Rafael Espindola5628ee72016-04-15 19:14:18 +00001537 case R_MIPS_GOT16:
1538 case R_MIPS_CALL16:
1539 if (S.isLocal())
1540 return R_MIPS_GOT_LOCAL;
1541 if (!S.isPreemptible())
1542 return R_MIPS_GOT;
1543 return R_GOT;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001544 }
1545}
1546
1547template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001548uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001549 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1550 return R_MIPS_REL32;
1551 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001552 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001553 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001554 // Keep it going with a dummy value so that we can find more reloc errors.
1555 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001556}
1557
1558template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001559void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001560 typedef typename ELFT::Off Elf_Off;
1561 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001562
1563 // Set the MSB of the second GOT slot. This is not required by any
1564 // MIPS ABI documentation, though.
1565 //
1566 // There is a comment in glibc saying that "The MSB of got[1] of a
1567 // gnu object is set to identify gnu objects," and in GNU gold it
1568 // says "the second entry will be used by some runtime loaders".
1569 // But how this field is being used is unclear.
1570 //
1571 // We are not really willing to mimic other linkers behaviors
1572 // without understanding why they do that, but because all files
1573 // generated by GNU tools have this special GOT value, and because
1574 // we've been doing this for years, it is probably a safe bet to
1575 // keep doing this for now. We really need to revisit this to see
1576 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001577 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001578 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001579}
1580
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001581template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001582void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1583 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001584}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001585
Simon Atanasyan35031192015-12-15 06:06:34 +00001586static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001587
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001588template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001589static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001590 uint32_t Instr = read32<E>(Loc);
1591 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1592 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1593}
1594
1595template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001596static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001597 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001598 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001599 if (SHIFT > 0)
1600 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001601 checkInt<BSIZE + SHIFT>(V, Type);
1602 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001603}
1604
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001605template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001606static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001607 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001608 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001609}
1610
Simon Atanasyan3b377852016-03-04 10:55:20 +00001611template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001612static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1613 uint32_t Instr = read32<E>(Loc);
1614 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1615}
1616
Rafael Espindola666625b2016-04-01 14:36:09 +00001617template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001618 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1619}
1620
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001621template <class ELFT>
1622void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1623 const endianness E = ELFT::TargetEndianness;
1624 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1625 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1626 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1627 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1628 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1629 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1630 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1631 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1632 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001633 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001634 writeMipsLo16<E>(Buf + 4, Got);
1635 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001636}
1637
1638template <class ELFT>
1639void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1640 uint64_t PltEntryAddr, int32_t Index,
1641 unsigned RelOff) const {
1642 const endianness E = ELFT::TargetEndianness;
1643 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1644 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1645 write32<E>(Buf + 8, 0x03200008); // jr $25
1646 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001647 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001648 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1649 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001650}
1651
1652template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001653void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1654 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1655 // See MipsTargetInfo::writeThunk for details.
1656 const endianness E = ELFT::TargetEndianness;
1657 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1658 write32<E>(Buf + 4, 0x08000000); // j func
1659 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1660 write32<E>(Buf + 12, 0x00000000); // nop
1661 writeMipsHi16<E>(Buf, S);
1662 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1663 writeMipsLo16<E>(Buf + 8, S);
1664}
1665
1666template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001667bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Rafael Espindola790db9c2016-04-01 17:00:36 +00001668 return !isRelRelative(Type) || Type == R_MIPS_LO16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001669}
1670
1671template <class ELFT>
Simon Atanasyand040a582016-02-25 16:19:15 +00001672bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1673 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001674}
1675
1676template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001677bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1678 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001679}
1680
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001681template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001682bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1683 const SymbolBody &S) const {
1684 // Any MIPS PIC code function is invoked with its address in register $t9.
1685 // So if we have a branch instruction from non-PIC code to the PIC one
1686 // we cannot make the jump directly and need to create a small stubs
1687 // to save the target function address.
1688 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1689 if (Type != R_MIPS_26)
1690 return false;
1691 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1692 if (!F)
1693 return false;
1694 // If current file has PIC code, LA25 stub is not required.
1695 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1696 return false;
1697 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1698 if (!D || !D->Section)
1699 return false;
1700 // LA25 is required if target file has PIC code
1701 // or target symbol is a PIC symbol.
1702 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001703 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001704}
1705
1706template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001707uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001708 uint32_t Type) const {
1709 const endianness E = ELFT::TargetEndianness;
1710 switch (Type) {
1711 default:
1712 return 0;
1713 case R_MIPS_32:
1714 case R_MIPS_GPREL32:
1715 return read32<E>(Buf);
1716 case R_MIPS_26:
1717 // FIXME (simon): If the relocation target symbol is not a PLT entry
1718 // we should use another expression for calculation:
1719 // ((A << 2) | (P & 0xf0000000)) >> 2
1720 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1721 case R_MIPS_GPREL16:
1722 case R_MIPS_LO16:
1723 case R_MIPS_PCLO16:
1724 case R_MIPS_TLS_DTPREL_HI16:
1725 case R_MIPS_TLS_DTPREL_LO16:
1726 case R_MIPS_TLS_TPREL_HI16:
1727 case R_MIPS_TLS_TPREL_LO16:
1728 return readSignedLo16<E>(Buf);
1729 case R_MIPS_PC16:
1730 return getPcRelocAddend<E, 16, 2>(Buf);
1731 case R_MIPS_PC19_S2:
1732 return getPcRelocAddend<E, 19, 2>(Buf);
1733 case R_MIPS_PC21_S2:
1734 return getPcRelocAddend<E, 21, 2>(Buf);
1735 case R_MIPS_PC26_S2:
1736 return getPcRelocAddend<E, 26, 2>(Buf);
1737 case R_MIPS_PC32:
1738 return getPcRelocAddend<E, 32, 0>(Buf);
1739 }
1740}
1741
1742template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001743void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1744 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001745 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001746 // Thread pointer and DRP offsets from the start of TLS data area.
1747 // https://www.linux-mips.org/wiki/NPTL
1748 const uint32_t TPOffset = 0x7000;
1749 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001750 switch (Type) {
1751 case R_MIPS_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001752 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001753 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001754 case R_MIPS_26: {
1755 uint32_t Instr = read32<E>(Loc);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001756 write32<E>(Loc, (Instr & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001757 break;
1758 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001759 case R_MIPS_CALL16:
1760 case R_MIPS_GOT16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001761 int64_t V = Val - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001762 if (Type == R_MIPS_GOT16)
1763 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001764 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001765 break;
1766 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001767 case R_MIPS_GPREL16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001768 int64_t V = Val - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001769 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001770 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001771 break;
1772 }
1773 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001774 write32<E>(Loc, Val - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001775 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001776 case R_MIPS_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001777 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001778 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001779 case R_MIPS_JALR:
1780 // Ignore this optimization relocation for now
1781 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001782 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001783 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001784 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001785 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001786 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001787 break;
1788 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001789 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001790 break;
1791 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001792 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001793 break;
1794 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001795 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001796 break;
1797 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001798 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001799 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001800 case R_MIPS_PCHI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001801 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001802 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001803 case R_MIPS_PCLO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001804 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001805 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001806 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001807 writeMipsHi16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001808 break;
1809 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001810 writeMipsLo16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001811 break;
1812 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001813 writeMipsHi16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001814 break;
1815 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001816 writeMipsLo16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001817 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001818 default:
George Rimar57610422016-03-11 14:43:02 +00001819 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001820 }
1821}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001822
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001823template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001824bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001825 return Type == R_MIPS_JALR;
1826}
1827
1828template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001829bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1830 switch (Type) {
1831 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001832 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001833 case R_MIPS_26:
1834 case R_MIPS_32:
1835 case R_MIPS_64:
1836 case R_MIPS_HI16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001837 case R_MIPS_TLS_DTPREL_HI16:
1838 case R_MIPS_TLS_DTPREL_LO16:
1839 case R_MIPS_TLS_TPREL_HI16:
1840 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001841 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001842 }
1843}
1844
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001845// _gp is a MIPS-specific ABI-defined symbol which points to
1846// a location that is relative to GOT. This function returns
1847// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001848template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rafael Espindola6f92e142016-04-12 13:26:51 +00001849 return Out<ELFT>::Got->getVA() + MipsGPOffset;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001850}
1851
1852template uint32_t getMipsGpAddr<ELF32LE>();
1853template uint32_t getMipsGpAddr<ELF32BE>();
1854template uint64_t getMipsGpAddr<ELF64LE>();
1855template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001856
1857template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1858 const SymbolBody &) const;
1859template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1860 const SymbolBody &) const;
1861template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1862 const SymbolBody &) const;
1863template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1864 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001865}
1866}