blob: 2f9e3525ce2ca56e652e07a9cbb93e386a61346f [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;
78 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
79 bool isTlsLocalDynamicRel(uint32_t Type) const override;
80 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
81 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000082 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000083 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000084 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
85 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaffcad442016-03-23 14:58:25 +000086 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +000087 bool needsCopyRelImpl(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +000088 bool needsDynRelative(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +000089 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +000090 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +000091 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000092
Rafael Espindola22ef9562016-04-13 01:40:19 +000093 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
94 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
95 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
96 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rafael Espindola89cc14f2016-03-16 19:03:58 +000097
George Rimarbfb7bf72015-12-21 10:00:12 +000098 bool isGotRelative(uint32_t Type) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +000099 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000100};
101
102class X86_64TargetInfo final : public TargetInfo {
103public:
104 X86_64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000105 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar86971052016-03-29 08:35:42 +0000106 uint32_t getDynRel(uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000107 uint32_t getTlsGotRel(uint32_t Type) const override;
108 bool pointsToLocalDynamicGotEntry(uint32_t Type) const override;
109 bool isTlsLocalDynamicRel(uint32_t Type) const override;
110 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
111 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000112 void writeGotPltHeader(uint8_t *Buf) const override;
113 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000114 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000115 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
116 int32_t Index, unsigned RelOff) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000117 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000118 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000119 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000120 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000121 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000122 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000123 bool isSizeRel(uint32_t Type) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000124
Rafael Espindola22ef9562016-04-13 01:40:19 +0000125 void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
126 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
127 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
128 void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129};
130
Davide Italiano8c3444362016-01-11 19:45:33 +0000131class PPCTargetInfo final : public TargetInfo {
132public:
133 PPCTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000134 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000135 bool isRelRelative(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000136 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Davide Italiano8c3444362016-01-11 19:45:33 +0000137};
138
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000139class PPC64TargetInfo final : public TargetInfo {
140public:
141 PPC64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000142 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000143 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
144 int32_t Index, unsigned RelOff) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000145 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000146 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000147 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000148 bool isRelRelative(uint32_t Type) const override;
149};
150
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000151class AArch64TargetInfo final : public TargetInfo {
152public:
153 AArch64TargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000154 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000155 uint32_t getDynRel(uint32_t Type) const override;
156 bool isTlsGlobalDynamicRel(uint32_t Type) const override;
157 bool isTlsInitialExecRel(uint32_t Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000158 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000159 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000160 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
161 int32_t Index, unsigned RelOff) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000162 uint32_t getTlsGotRel(uint32_t Type) const override;
Rafael Espindola435c00f2016-02-23 20:19:44 +0000163 bool isRelRelative(uint32_t Type) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000164 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000165 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +0000166 bool refersToGotEntry(uint32_t Type) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000167 bool needsPltImpl(uint32_t Type) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000168 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
169 void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
170 void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000171
172private:
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000173 static const uint64_t TcbSize = 16;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000174};
175
Tom Stellard80efb162016-01-07 03:59:08 +0000176class AMDGPUTargetInfo final : public TargetInfo {
177public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000178 AMDGPUTargetInfo() {}
Rafael Espindola22ef9562016-04-13 01:40:19 +0000179 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
180 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Tom Stellard80efb162016-01-07 03:59:08 +0000181};
182
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000183template <class ELFT> class MipsTargetInfo final : public TargetInfo {
184public:
185 MipsTargetInfo();
Rafael Espindola22ef9562016-04-13 01:40:19 +0000186 RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola666625b2016-04-01 14:36:09 +0000187 uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override;
George Rimar98b060d2016-03-06 06:01:07 +0000188 uint32_t getDynRel(uint32_t Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000189 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
190 void writePltZero(uint8_t *Buf) const override;
191 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
192 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000193 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000194 void writeThunk(uint8_t *Buf, uint64_t S) const override;
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000195 bool needsCopyRelImpl(uint32_t Type) const override;
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000196 bool needsGot(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindola993f0272016-02-26 14:27:47 +0000197 bool needsPltImpl(uint32_t Type) const override;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000198 bool needsThunk(uint32_t Type, const InputFile &File,
199 const SymbolBody &S) const override;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000200 void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000201 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000202 bool isRelRelative(uint32_t Type) const override;
Simon Atanasyand040a582016-02-25 16:19:15 +0000203 bool refersToGotEntry(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000204};
205} // anonymous namespace
206
Rui Ueyama91004392015-10-13 16:08:15 +0000207TargetInfo *createTarget() {
208 switch (Config->EMachine) {
209 case EM_386:
210 return new X86TargetInfo();
211 case EM_AARCH64:
212 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000213 case EM_AMDGPU:
214 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000215 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000216 switch (Config->EKind) {
217 case ELF32LEKind:
218 return new MipsTargetInfo<ELF32LE>();
219 case ELF32BEKind:
220 return new MipsTargetInfo<ELF32BE>();
221 default:
George Rimar777f9632016-03-12 08:31:34 +0000222 fatal("unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000223 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000224 case EM_PPC:
225 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000226 case EM_PPC64:
227 return new PPC64TargetInfo();
228 case EM_X86_64:
229 return new X86_64TargetInfo();
230 }
George Rimar777f9632016-03-12 08:31:34 +0000231 fatal("unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000232}
233
Rafael Espindola01205f72015-09-22 18:19:46 +0000234TargetInfo::~TargetInfo() {}
235
Rafael Espindola666625b2016-04-01 14:36:09 +0000236uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf,
237 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000238 return 0;
239}
240
George Rimar98b060d2016-03-06 06:01:07 +0000241bool TargetInfo::canRelaxTls(uint32_t Type, const SymbolBody *S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000242 if (Config->Shared || (S && !S->isTls()))
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000243 return false;
Rafael Espindola1ea51d22016-03-04 16:14:19 +0000244
Rafael Espindolad405f472016-03-04 21:37:09 +0000245 // We know we are producing an executable.
246
247 // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec
248 // depending on the symbol being locally defined or not.
249 if (isTlsGlobalDynamicRel(Type))
250 return true;
251
252 // Local-Dynamic relocs can be relaxed to Local-Exec.
253 if (isTlsLocalDynamicRel(Type))
254 return true;
255
256 // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally
257 // defined.
258 if (isTlsInitialExecRel(Type))
Rui Ueyamac4466602016-03-13 19:48:18 +0000259 return !S->isPreemptible();
Rafael Espindolad405f472016-03-04 21:37:09 +0000260
George Rimar77d1cb12015-11-24 09:00:06 +0000261 return false;
262}
263
George Rimar786e8662016-03-17 05:57:33 +0000264uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; }
Igor Kudrinf6f45472015-11-10 08:39:27 +0000265
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000266bool TargetInfo::needsCopyRelImpl(uint32_t Type) const { return false; }
267
268template <typename ELFT> static bool mayNeedCopy(const SymbolBody &S) {
269 if (Config->Shared)
270 return false;
271 auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S);
272 if (!SS)
273 return false;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000274 return SS->isObject();
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000275}
276
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000277template <class ELFT>
Rui Ueyama02dfd492015-12-17 01:18:40 +0000278bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaf7ae3592016-02-23 18:53:29 +0000279 return mayNeedCopy<ELFT>(S) && needsCopyRelImpl(Type);
George Rimarbc590fe2015-10-28 16:48:58 +0000280}
281
George Rimarbfb7bf72015-12-21 10:00:12 +0000282bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000283bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000284bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000285bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000286
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000287bool TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
288 return false;
289}
Rui Ueyama012eb782016-01-29 04:05:09 +0000290
Rafael Espindola993f0272016-02-26 14:27:47 +0000291bool TargetInfo::needsPltImpl(uint32_t Type) const { return false; }
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000292
293bool TargetInfo::refersToGotEntry(uint32_t Type) const { return false; }
294
Rafael Espindola852860e2016-02-12 15:47:37 +0000295TargetInfo::PltNeed TargetInfo::needsPlt(uint32_t Type,
296 const SymbolBody &S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000297 if (S.isGnuIFunc())
Rafael Espindoladd7f4e32016-02-25 23:03:55 +0000298 return Plt_Explicit;
Rui Ueyamac4466602016-03-13 19:48:18 +0000299 if (S.isPreemptible() && needsPltImpl(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000300 return Plt_Explicit;
301
302 // This handles a non PIC program call to function in a shared library.
303 // In an ideal world, we could just report an error saying the relocation
304 // can overflow at runtime.
305 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
306 // libc.so.
307 //
308 // The general idea on how to handle such cases is to create a PLT entry
309 // and use that as the function value.
310 //
311 // For the static linking part, we just return true and everything else
312 // will use the the PLT entry as the address.
313 //
314 // The remaining problem is making sure pointer equality still works. We
315 // need the help of the dynamic linker for that. We let it know that we have
316 // a direct reference to a so symbol by creating an undefined symbol with a
317 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
318 // the value of the symbol we created. This is true even for got entries, so
319 // pointer equality is maintained. To avoid an infinite loop, the only entry
320 // that points to the real function is a dedicated got entry used by the
321 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
322 // R_386_JMP_SLOT, etc).
Rafael Espindola54322872016-03-24 12:55:27 +0000323 if (S.isShared())
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000324 if (!Config->Pic && S.isFunc() && !refersToGotEntry(Type))
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000325 return Plt_Implicit;
326
Rafael Espindola852860e2016-02-12 15:47:37 +0000327 return Plt_No;
328}
Rui Ueyama012eb782016-01-29 04:05:09 +0000329
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000330bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File,
331 const SymbolBody &S) const {
332 return false;
333}
334
George Rimar98b060d2016-03-06 06:01:07 +0000335bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000336
George Rimar98b060d2016-03-06 06:01:07 +0000337bool TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000338 return false;
339}
340
George Rimar98b060d2016-03-06 06:01:07 +0000341bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; }
Rafael Espindolad405f472016-03-04 21:37:09 +0000342
George Rimar98b060d2016-03-06 06:01:07 +0000343bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000344 return false;
345}
346
Rafael Espindola22ef9562016-04-13 01:40:19 +0000347void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
348 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000349 llvm_unreachable("Should not have claimed to be relaxable");
350}
351
Rafael Espindola22ef9562016-04-13 01:40:19 +0000352void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
353 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000354 llvm_unreachable("Should not have claimed to be relaxable");
355}
356
Rafael Espindola22ef9562016-04-13 01:40:19 +0000357void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
358 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000359 llvm_unreachable("Should not have claimed to be relaxable");
360}
361
Rafael Espindola22ef9562016-04-13 01:40:19 +0000362void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
363 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000364 llvm_unreachable("Should not have claimed to be relaxable");
George Rimar6713cf82015-11-25 21:46:05 +0000365}
George Rimar77d1cb12015-11-24 09:00:06 +0000366
Rafael Espindola7f074422015-09-22 21:35:51 +0000367X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000368 CopyRel = R_386_COPY;
369 GotRel = R_386_GLOB_DAT;
370 PltRel = R_386_JUMP_SLOT;
371 IRelativeRel = R_386_IRELATIVE;
372 RelativeRel = R_386_RELATIVE;
373 TlsGotRel = R_386_TLS_TPOFF;
Rui Ueyama724d6252016-01-29 01:49:32 +0000374 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
375 TlsOffsetRel = R_386_TLS_DTPOFF32;
376 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000377 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000378 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000379 TlsGdToLeSkip = 2;
380}
381
382RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
383 switch (Type) {
384 default:
385 return R_ABS;
386 case R_386_PLT32:
387 case R_386_PC32:
388 case R_386_GOTPC:
389 return R_PC;
390 }
George Rimar77b77792015-11-25 22:15:01 +0000391}
392
Rafael Espindolaffcad442016-03-23 14:58:25 +0000393bool X86TargetInfo::isRelRelative(uint32_t Type) const {
394 switch (Type) {
395 default:
396 return false;
397 case R_386_PC32:
398 case R_386_PLT32:
399 case R_386_TLS_LDO_32:
400 return true;
401 }
402}
403
Rui Ueyamac516ae12016-01-29 02:33:45 +0000404void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000405 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
406}
407
Rui Ueyamac516ae12016-01-29 02:33:45 +0000408void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000409 // Entries in .got.plt initially points back to the corresponding
410 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000411 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000412}
Rafael Espindola01205f72015-09-22 18:19:46 +0000413
George Rimar98b060d2016-03-06 06:01:07 +0000414uint32_t X86TargetInfo::getDynRel(uint32_t Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000415 if (Type == R_386_TLS_LE)
416 return R_386_TLS_TPOFF;
417 if (Type == R_386_TLS_LE_32)
418 return R_386_TLS_TPOFF32;
419 return Type;
420}
421
George Rimar98b060d2016-03-06 06:01:07 +0000422uint32_t X86TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000423 if (Type == R_386_TLS_IE)
424 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000425 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000426}
427
George Rimar98b060d2016-03-06 06:01:07 +0000428bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000429 return Type == R_386_TLS_GD;
430}
431
George Rimar98b060d2016-03-06 06:01:07 +0000432bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000433 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM;
434}
435
George Rimar98b060d2016-03-06 06:01:07 +0000436bool X86TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000437 return Type == R_386_TLS_LDM;
438}
439
George Rimar98b060d2016-03-06 06:01:07 +0000440bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000441 return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE;
442}
443
Rui Ueyama900e2d22016-01-29 03:51:49 +0000444void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000445 // Executable files and shared object files have
446 // separate procedure linkage tables.
George Rimar786e8662016-03-17 05:57:33 +0000447 if (Config->Pic) {
George Rimar77b77792015-11-25 22:15:01 +0000448 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000449 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000450 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
451 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000452 };
453 memcpy(Buf, V, sizeof(V));
454 return;
455 }
George Rimar648a2c32015-10-20 08:54:27 +0000456
George Rimar77b77792015-11-25 22:15:01 +0000457 const uint8_t PltData[] = {
458 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000459 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
460 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000461 };
462 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000463 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000464 write32le(Buf + 2, Got + 4);
465 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000466}
467
Rui Ueyama9398f862016-01-29 04:15:02 +0000468void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
469 uint64_t PltEntryAddr, int32_t Index,
470 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000471 const uint8_t Inst[] = {
472 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
473 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
474 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
475 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000476 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000477
George Rimar77b77792015-11-25 22:15:01 +0000478 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
George Rimar786e8662016-03-17 05:57:33 +0000479 Buf[1] = Config->Pic ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000480 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
481 : Out<ELF32LE>::Got->getVA();
482 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000483 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000484 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000485}
486
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000487bool X86TargetInfo::needsCopyRelImpl(uint32_t Type) const {
488 return Type == R_386_32 || Type == R_386_16 || Type == R_386_8;
George Rimar70e25082015-11-25 11:27:40 +0000489}
490
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000491bool X86TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000492 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000493 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar6f17e092015-12-17 09:32:21 +0000494 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000495 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000496 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000497}
498
Rafael Espindola993f0272016-02-26 14:27:47 +0000499bool X86TargetInfo::needsPltImpl(uint32_t Type) const {
500 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000501}
502
George Rimarbfb7bf72015-12-21 10:00:12 +0000503bool X86TargetInfo::isGotRelative(uint32_t Type) const {
504 // This relocation does not require got entry,
505 // but it is relative to got and needs it to be created.
506 // Here we request for that.
507 return Type == R_386_GOTOFF;
508}
509
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000510bool X86TargetInfo::refersToGotEntry(uint32_t Type) const {
511 return Type == R_386_GOT32;
512}
513
Rafael Espindola666625b2016-04-01 14:36:09 +0000514uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf,
515 uint32_t Type) const {
Rafael Espindolada99df32016-03-30 12:40:38 +0000516 switch (Type) {
517 default:
518 return 0;
519 case R_386_32:
520 case R_386_GOT32:
521 case R_386_GOTOFF:
522 case R_386_GOTPC:
523 case R_386_PC32:
524 case R_386_PLT32:
525 return read32le(Buf);
526 }
527}
528
Rafael Espindola22ef9562016-04-13 01:40:19 +0000529void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
530 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000531 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000532 case R_386_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000533 write32le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000534 break;
George Rimarbfb7bf72015-12-21 10:00:12 +0000535 case R_386_GOTOFF:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000536 write32le(Loc, Val - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000537 break;
George Rimar13934772015-11-25 20:20:31 +0000538 case R_386_GOTPC:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000539 write32le(Loc, Val + Out<ELF32LE>::Got->getVA());
George Rimar13934772015-11-25 20:20:31 +0000540 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000541 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000542 case R_386_PLT32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000543 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000544 break;
Rafael Espindolac1c14a22016-04-14 15:56:14 +0000545 case R_386_GOT32:
George Rimar9db204a2015-12-02 09:58:20 +0000546 case R_386_TLS_GD:
547 case R_386_TLS_LDM:
548 case R_386_TLS_TPOFF: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000549 uint64_t V = Val - Out<ELF32LE>::Got->getVA() -
George Rimar9db204a2015-12-02 09:58:20 +0000550 Out<ELF32LE>::Got->getNumEntries() * 4;
551 checkInt<32>(V, Type);
552 write32le(Loc, V);
553 break;
554 }
George Rimar6f17e092015-12-17 09:32:21 +0000555 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000556 case R_386_TLS_LDO_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000557 write32le(Loc, Val);
George Rimar9db204a2015-12-02 09:58:20 +0000558 break;
George Rimard23970f2015-11-25 20:41:53 +0000559 case R_386_TLS_LE:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000560 write32le(Loc, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
George Rimard23970f2015-11-25 20:41:53 +0000561 break;
562 case R_386_TLS_LE_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000563 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimard23970f2015-11-25 20:41:53 +0000564 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000565 default:
George Rimar57610422016-03-11 14:43:02 +0000566 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000567 }
568}
569
George Rimar98b060d2016-03-06 06:01:07 +0000570bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000571 return Config->Shared && Type == R_386_TLS_IE;
572}
573
Rafael Espindola22ef9562016-04-13 01:40:19 +0000574void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
575 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000576 // GD can be optimized to LE:
577 // leal x@tlsgd(, %ebx, 1),
578 // call __tls_get_addr@plt
579 // Can be converted to:
580 // movl %gs:0,%eax
581 // addl $x@ntpoff,%eax
582 // But gold emits subl $foo@tpoff,%eax instead of addl.
583 // These instructions are completely equal in behavior.
584 // This method generates subl to be consistent with gold.
585 const uint8_t Inst[] = {
586 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
587 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
588 };
589 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000590 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000591}
592
593// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
594// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
595// how GD can be optimized to IE:
596// leal x@tlsgd(, %ebx, 1),
597// call __tls_get_addr@plt
598// Is converted to:
599// movl %gs:0, %eax
600// addl x@gotntpoff(%ebx), %eax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000601void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
602 uint64_t Val) const {
George Rimar2558e122015-12-09 09:55:54 +0000603 const uint8_t Inst[] = {
604 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
605 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
606 };
607 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000608 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
609 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000610}
611
George Rimar6f17e092015-12-17 09:32:21 +0000612// In some conditions, relocations can be optimized to avoid using GOT.
613// This function does that for Initial Exec to Local Exec case.
614// Read "ELF Handling For Thread-Local Storage, 5.1
615// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000616// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000617void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
618 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000619 // Ulrich's document section 6.2 says that @gotntpoff can
620 // be used with MOVL or ADDL instructions.
621 // @indntpoff is similar to @gotntpoff, but for use in
622 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000623 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000624 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000625 uint8_t Reg = (Loc[-1] >> 3) & 7;
626 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000627 if (Type == R_386_TLS_IE) {
628 // For R_386_TLS_IE relocation we perform the next transformations:
629 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
630 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
631 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
632 // First one is special because when EAX is used the sequence is 5 bytes
633 // long, otherwise it is 6 bytes.
634 if (*Op == 0xa1) {
635 *Op = 0xb8;
636 } else {
637 *Inst = IsMov ? 0xc7 : 0x81;
638 *Op = 0xc0 | ((*Op >> 3) & 7);
639 }
640 } else {
641 // R_386_TLS_GOTIE relocation can be optimized to
642 // R_386_TLS_LE so that it does not use GOT.
643 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
644 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
645 // Note: gold converts to ADDL instead of LEAL.
646 *Inst = IsMov ? 0xc7 : 0x8d;
647 if (IsMov)
648 *Op = 0xc0 | ((*Op >> 3) & 7);
649 else
650 *Op = 0x80 | Reg | (Reg << 3);
651 }
Rafael Espindola22ef9562016-04-13 01:40:19 +0000652 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000653}
654
Rafael Espindola22ef9562016-04-13 01:40:19 +0000655void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
656 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000657 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000658 relocateOne(Loc, R_386_TLS_LE, Val);
659 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000660 }
661
662 // LD can be optimized to LE:
663 // leal foo(%reg),%eax
664 // call ___tls_get_addr
665 // Is converted to:
666 // movl %gs:0,%eax
667 // nop
668 // leal 0(%esi,1),%esi
669 const uint8_t Inst[] = {
670 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
671 0x90, // nop
672 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
673 };
674 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000675}
676
Rafael Espindola7f074422015-09-22 21:35:51 +0000677X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000678 CopyRel = R_X86_64_COPY;
679 GotRel = R_X86_64_GLOB_DAT;
680 PltRel = R_X86_64_JUMP_SLOT;
681 RelativeRel = R_X86_64_RELATIVE;
682 IRelativeRel = R_X86_64_IRELATIVE;
683 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000684 TlsModuleIndexRel = R_X86_64_DTPMOD64;
685 TlsOffsetRel = R_X86_64_DTPOFF64;
686 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000687 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000688 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000689 TlsGdToLeSkip = 2;
690}
691
692RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
693 switch (Type) {
694 default:
695 return R_ABS;
696 case R_X86_64_GOTPCREL:
697 case R_X86_64_PLT32:
698 case R_X86_64_PC32:
699 case R_X86_64_TLSLD:
700 case R_X86_64_GOTTPOFF:
701 case R_X86_64_TLSGD:
702 return R_PC;
703 }
George Rimar648a2c32015-10-20 08:54:27 +0000704}
705
Rui Ueyamac516ae12016-01-29 02:33:45 +0000706void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000707 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
708}
709
Rui Ueyamac516ae12016-01-29 02:33:45 +0000710void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000711 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000712 write32le(Buf, Plt + 6);
713}
714
Rui Ueyama900e2d22016-01-29 03:51:49 +0000715void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000716 const uint8_t PltData[] = {
717 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
718 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
719 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
720 };
721 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000722 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
723 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
724 write32le(Buf + 2, Got - Plt + 2); // GOT+8
725 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000726}
Rafael Espindola01205f72015-09-22 18:19:46 +0000727
Rui Ueyama9398f862016-01-29 04:15:02 +0000728void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
729 uint64_t PltEntryAddr, int32_t Index,
730 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000731 const uint8_t Inst[] = {
732 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
733 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
734 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
735 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000736 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000737
George Rimar648a2c32015-10-20 08:54:27 +0000738 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
739 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000740 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000741}
742
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000743bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
744 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
745 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000746}
747
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000748bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000749 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
750 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000751}
752
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000753bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000754 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000755 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000756 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000757 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000758 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000759}
760
George Rimar86971052016-03-29 08:35:42 +0000761uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
762 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
763 if (Config->Shared)
764 error(getELFRelocationTypeName(EM_X86_64, Type) +
765 " cannot be a dynamic relocation");
766 return Type;
767}
768
George Rimar98b060d2016-03-06 06:01:07 +0000769uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000770 // No other types of TLS relocations requiring GOT should
771 // reach here.
772 assert(Type == R_X86_64_GOTTPOFF);
773 return R_X86_64_PC32;
774}
775
George Rimar98b060d2016-03-06 06:01:07 +0000776bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000777 return Type == R_X86_64_GOTTPOFF;
778}
779
George Rimar98b060d2016-03-06 06:01:07 +0000780bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000781 return Type == R_X86_64_TLSGD;
782}
783
George Rimar98b060d2016-03-06 06:01:07 +0000784bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000785 return Type == R_X86_64_TLSLD;
786}
787
George Rimar98b060d2016-03-06 06:01:07 +0000788bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000789 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
790 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000791}
792
Rafael Espindola993f0272016-02-26 14:27:47 +0000793bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
794 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000795}
Rafael Espindolac4010882015-09-22 20:54:08 +0000796
Rafael Espindolaae244002015-10-05 19:30:12 +0000797bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
798 switch (Type) {
799 default:
800 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000801 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000802 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000803 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000804 case R_X86_64_PC8:
805 case R_X86_64_PC16:
806 case R_X86_64_PC32:
807 case R_X86_64_PC64:
808 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000809 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000810 return true;
811 }
812}
813
Rui Ueyamac516ae12016-01-29 02:33:45 +0000814bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000815 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000816}
817
George Rimar6713cf82015-11-25 21:46:05 +0000818// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
819// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000820// how GD can be optimized to LE:
821// .byte 0x66
822// leaq x@tlsgd(%rip), %rdi
823// .word 0x6666
824// rex64
825// call __tls_get_addr@plt
826// Is converted to:
827// mov %fs:0x0,%rax
828// lea x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000829void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
830 uint64_t Val) const {
George Rimar6713cf82015-11-25 21:46:05 +0000831 const uint8_t Inst[] = {
832 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
833 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
834 };
835 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000836 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000837}
838
George Rimar25411f252015-12-04 11:20:13 +0000839// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
840// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
841// how GD can be optimized to IE:
842// .byte 0x66
843// leaq x@tlsgd(%rip), %rdi
844// .word 0x6666
845// rex64
846// call __tls_get_addr@plt
847// Is converted to:
848// mov %fs:0x0,%rax
849// addq x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000850void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
851 uint64_t Val) const {
George Rimar25411f252015-12-04 11:20:13 +0000852 const uint8_t Inst[] = {
853 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
854 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
855 };
856 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000857 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000858}
859
George Rimar77d1cb12015-11-24 09:00:06 +0000860// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000861// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000862// This function does that. Read "ELF Handling For Thread-Local Storage,
863// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
864// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000865void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
866 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000867 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
868 // used in MOVQ or ADDQ instructions only.
869 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
870 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
871 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
872 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
873 uint8_t *Prefix = Loc - 3;
874 uint8_t *Inst = Loc - 2;
875 uint8_t *RegSlot = Loc - 1;
876 uint8_t Reg = Loc[-1] >> 3;
877 bool IsMov = *Inst == 0x8b;
878 bool RspAdd = !IsMov && Reg == 4;
879 // r12 and rsp registers requires special handling.
880 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
881 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
882 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
883 // The same true for rsp. So we convert to addq for them, saving 1 byte that
884 // we dont have.
885 if (RspAdd)
886 *Inst = 0x81;
887 else
888 *Inst = IsMov ? 0xc7 : 0x8d;
889 if (*Prefix == 0x4c)
890 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
891 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000892 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000893}
894
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000895// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
896// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
897// how LD can be optimized to LE:
898// leaq bar@tlsld(%rip), %rdi
899// callq __tls_get_addr@PLT
900// leaq bar@dtpoff(%rax), %rcx
901// Is converted to:
902// .word 0x6666
903// .byte 0x66
904// mov %fs:0,%rax
905// leaq bar@tpoff(%rax), %rcx
Rafael Espindola22ef9562016-04-13 01:40:19 +0000906void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
907 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000908 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000909 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
910 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000911 }
912 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000913 relocateOne(Loc, R_X86_64_TPOFF32, Val);
914 return;
George Rimar25411f252015-12-04 11:20:13 +0000915 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000916
917 const uint8_t Inst[] = {
918 0x66, 0x66, //.word 0x6666
919 0x66, //.byte 0x66
920 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
921 };
922 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000923}
924
Rafael Espindola22ef9562016-04-13 01:40:19 +0000925void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
926 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000927 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000928 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000929 checkUInt<32>(Val, Type);
930 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000931 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000932 case R_X86_64_32S:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000933 checkInt<32>(Val, Type);
934 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000935 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000936 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000937 case R_X86_64_DTPOFF64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000938 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000939 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000940 case R_X86_64_DTPOFF32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000941 write32le(Loc, Val);
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000942 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000943 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000944 case R_X86_64_GOTPCRELX:
945 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000946 case R_X86_64_PC32:
947 case R_X86_64_PLT32:
948 case R_X86_64_TLSGD:
949 case R_X86_64_TLSLD:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000950 write32le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000951 break;
George Rimar48651482015-12-11 08:59:37 +0000952 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000953 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000954 break;
955 case R_X86_64_SIZE64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000956 write64le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000957 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000958 case R_X86_64_TPOFF32: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000959 Val -= Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000960 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000961 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000962 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000963 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000964 default:
George Rimar57610422016-03-11 14:43:02 +0000965 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000966 }
967}
968
Hal Finkel3c8cc672015-10-12 20:56:18 +0000969// Relocation masks following the #lo(value), #hi(value), #ha(value),
970// #higher(value), #highera(value), #highest(value), and #highesta(value)
971// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
972// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000973static uint16_t applyPPCLo(uint64_t V) { return V; }
974static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
975static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
976static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
977static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000978static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000979static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
980
Davide Italiano8c3444362016-01-11 19:45:33 +0000981PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000982bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
983
Rafael Espindola22ef9562016-04-13 01:40:19 +0000984void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
985 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000986 switch (Type) {
987 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000988 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000989 break;
990 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000991 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000992 break;
993 default:
George Rimar57610422016-03-11 14:43:02 +0000994 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000995 }
996}
997
Rafael Espindola22ef9562016-04-13 01:40:19 +0000998RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
999 return R_ABS;
1000}
1001
Rafael Espindolac4010882015-09-22 20:54:08 +00001002PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001003 GotRel = R_PPC64_GLOB_DAT;
1004 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001005 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001006
1007 // We need 64K pages (at least under glibc/Linux, the loader won't
1008 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001009 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001010
1011 // The PPC64 ELF ABI v1 spec, says:
1012 //
1013 // It is normally desirable to put segments with different characteristics
1014 // in separate 256 Mbyte portions of the address space, to give the
1015 // operating system full paging flexibility in the 64-bit address space.
1016 //
1017 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1018 // use 0x10000000 as the starting address.
1019 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001020}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001021
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001022uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001023 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1024 // order. The TOC starts where the first of these sections starts.
1025
1026 // FIXME: This obviously does not do the right thing when there is no .got
1027 // section, but there is a .toc or .tocbss section.
1028 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1029 if (!TocVA)
1030 TocVA = Out<ELF64BE>::Plt->getVA();
1031
1032 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1033 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1034 // code (crt1.o) assumes that you can get from the TOC base to the
1035 // start of the .toc section with only a single (signed) 16-bit relocation.
1036 return TocVA + 0x8000;
1037}
1038
Rafael Espindola22ef9562016-04-13 01:40:19 +00001039RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1040 switch (Type) {
1041 default:
1042 return R_ABS;
1043 case R_PPC64_REL24:
1044 return R_PPC_OPD;
1045 }
1046}
1047
Rui Ueyama9398f862016-01-29 04:15:02 +00001048void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1049 uint64_t PltEntryAddr, int32_t Index,
1050 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1052
1053 // FIXME: What we should do, in theory, is get the offset of the function
1054 // descriptor in the .opd section, and use that as the offset from %r2 (the
1055 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1056 // be a pointer to the function descriptor in the .opd section. Using
1057 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1058
Hal Finkelfa92f682015-10-13 21:47:34 +00001059 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001060 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1061 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1062 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1063 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1064 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1065 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1066 write32be(Buf + 28, 0x4e800420); // bctr
1067}
1068
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001069bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001070 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001071 return true;
1072
1073 switch (Type) {
1074 default: return false;
1075 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001077 case R_PPC64_GOT16_HA:
1078 case R_PPC64_GOT16_HI:
1079 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001080 case R_PPC64_GOT16_LO_DS:
1081 return true;
1082 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001083}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001084
Rafael Espindola993f0272016-02-26 14:27:47 +00001085bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001086 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001087 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001088}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001089
Hal Finkelbe0823d2015-10-12 20:58:52 +00001090bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1091 switch (Type) {
1092 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001093 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001094 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001095 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001096 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001097 }
1098}
1099
Rafael Espindola22ef9562016-04-13 01:40:19 +00001100void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1101 uint64_t Val) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001102 uint64_t TB = getPPC64TocBase();
1103
Hal Finkel3c8cc672015-10-12 20:56:18 +00001104 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1105 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001106 switch (Type) {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001107 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TB; break;
1108 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TB; break;
1109 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TB; break;
1110 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TB; break;
1111 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TB; break;
1112 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001113 default: break;
1114 }
1115
Hal Finkel3c8cc672015-10-12 20:56:18 +00001116 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001117 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001118 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001119 // Preserve the AA/LK bits in the branch instruction
1120 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001121 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001122 break;
1123 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001124 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001125 checkInt<16>(Val, Type);
1126 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001127 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001128 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001129 checkInt<16>(Val, Type);
1130 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001131 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001132 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001133 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001134 break;
1135 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001136 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001137 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001138 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001139 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001140 break;
1141 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001142 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001143 break;
1144 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001145 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001146 break;
1147 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001148 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001149 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001150 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001151 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001152 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001153 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001154 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001155 break;
1156 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001157 checkInt<32>(Val, Type);
1158 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001159 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001160 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001161 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001162 break;
1163 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001164 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001165 break;
1166 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001167 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001168 break;
1169 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001170 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001171 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001172 case R_PPC64_REL24: {
1173 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001174 checkInt<24>(Val, Type);
1175 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001176 break;
1177 }
1178 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001179 checkInt<32>(Val, Type);
1180 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001181 break;
1182 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001183 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001184 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001185 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001186 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001187 break;
1188 default:
George Rimar57610422016-03-11 14:43:02 +00001189 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001190 }
1191}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001192
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001193AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001194 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001195 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001196 IRelativeRel = R_AARCH64_IRELATIVE;
1197 GotRel = R_AARCH64_GLOB_DAT;
1198 PltRel = R_AARCH64_JUMP_SLOT;
1199 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001200 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1201 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001202 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001203 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001204 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001205}
George Rimar648a2c32015-10-20 08:54:27 +00001206
Rafael Espindola22ef9562016-04-13 01:40:19 +00001207RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1208 const SymbolBody &S) const {
1209 switch (Type) {
1210 default:
1211 return R_ABS;
1212 case R_AARCH64_JUMP26:
1213 case R_AARCH64_CALL26:
1214 case R_AARCH64_PREL16:
1215 case R_AARCH64_PREL32:
1216 case R_AARCH64_PREL64:
1217 case R_AARCH64_CONDBR19:
1218 case R_AARCH64_ADR_PREL_LO21:
1219 case R_AARCH64_TSTBR14:
1220 return R_PC;
1221 case R_AARCH64_ADR_GOT_PAGE:
1222 case R_AARCH64_ADR_PREL_PG_HI21:
1223 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1224 return R_PAGE_PC;
1225 }
1226}
1227
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001228bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001229 switch (Type) {
1230 default:
1231 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001232 case R_AARCH64_ADD_ABS_LO12_NC:
1233 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001234 case R_AARCH64_ADR_PREL_LO21:
1235 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001236 case R_AARCH64_CALL26:
1237 case R_AARCH64_CONDBR19:
1238 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001239 case R_AARCH64_LDST8_ABS_LO12_NC:
1240 case R_AARCH64_LDST16_ABS_LO12_NC:
1241 case R_AARCH64_LDST32_ABS_LO12_NC:
1242 case R_AARCH64_LDST64_ABS_LO12_NC:
1243 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001244 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001245 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001246 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1247 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1248 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1249 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1250 case R_AARCH64_TSTBR14:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001251 return true;
1252 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001253}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001254
George Rimar98b060d2016-03-06 06:01:07 +00001255bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001256 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1257 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1258 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1259 Type == R_AARCH64_TLSDESC_CALL;
1260}
1261
George Rimar98b060d2016-03-06 06:01:07 +00001262bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001263 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1264 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1265}
1266
George Rimar98b060d2016-03-06 06:01:07 +00001267uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001268 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1269 return Type;
1270 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001271 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001272 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001273 // Keep it going with a dummy value so that we can find more reloc errors.
1274 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001275}
1276
Rui Ueyamac516ae12016-01-29 02:33:45 +00001277void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001278 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1279}
1280
Rafael Espindola22ef9562016-04-13 01:40:19 +00001281static uint64_t getAArch64Page(uint64_t Expr) {
1282 return Expr & (~static_cast<uint64_t>(0xFFF));
1283}
1284
Rui Ueyama900e2d22016-01-29 03:51:49 +00001285void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001286 const uint8_t PltData[] = {
1287 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1288 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1289 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1290 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1291 0x20, 0x02, 0x1f, 0xd6, // br x17
1292 0x1f, 0x20, 0x03, 0xd5, // nop
1293 0x1f, 0x20, 0x03, 0xd5, // nop
1294 0x1f, 0x20, 0x03, 0xd5 // nop
1295 };
1296 memcpy(Buf, PltData, sizeof(PltData));
1297
Rui Ueyama900e2d22016-01-29 03:51:49 +00001298 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1299 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001300 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1301 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1302 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1303 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001304}
1305
Rui Ueyama9398f862016-01-29 04:15:02 +00001306void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1307 uint64_t PltEntryAddr, int32_t Index,
1308 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001309 const uint8_t Inst[] = {
1310 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1311 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1312 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1313 0x20, 0x02, 0x1f, 0xd6 // br x17
1314 };
1315 memcpy(Buf, Inst, sizeof(Inst));
1316
Rafael Espindola22ef9562016-04-13 01:40:19 +00001317 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1318 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1319 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1320 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001321}
1322
George Rimar98b060d2016-03-06 06:01:07 +00001323uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001324 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001325 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1326 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001327}
1328
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001329bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001330 switch (Type) {
1331 default:
1332 return false;
1333 case R_AARCH64_ABS16:
1334 case R_AARCH64_ABS32:
1335 case R_AARCH64_ABS64:
1336 case R_AARCH64_ADD_ABS_LO12_NC:
1337 case R_AARCH64_ADR_PREL_LO21:
1338 case R_AARCH64_ADR_PREL_PG_HI21:
1339 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001340 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001341 case R_AARCH64_LDST32_ABS_LO12_NC:
1342 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001343 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001344 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001345 }
1346}
1347
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001348bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001349 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001350 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001351 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001352 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001353 case R_AARCH64_ADR_GOT_PAGE:
1354 case R_AARCH64_LD64_GOT_LO12_NC:
1355 return true;
1356 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001357 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001358 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001359}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001360
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +00001361bool AArch64TargetInfo::refersToGotEntry(uint32_t Type) const {
1362 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC;
1363}
1364
Rafael Espindola993f0272016-02-26 14:27:47 +00001365bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001366 switch (Type) {
1367 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001368 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001369 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001370 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001371 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001372 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001373 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001374 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001375}
Davide Italiano1d750a62015-09-27 08:45:38 +00001376
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001377static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001378 uint32_t ImmLo = (Imm & 0x3) << 29;
1379 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1380 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001381 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001382}
1383
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001384static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1385 or32le(L, (Imm & 0xFFF) << 10);
1386}
1387
Rafael Espindola22ef9562016-04-13 01:40:19 +00001388void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1389 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001390 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001391 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001392 checkIntUInt<16>(Val, Type);
1393 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001394 break;
1395 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001396 checkIntUInt<32>(Val, Type);
1397 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001398 break;
1399 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001400 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001401 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001402 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001403 // This relocation stores 12 bits and there's no instruction
1404 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001405 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1406 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001407 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001408 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001409 case R_AARCH64_ADR_GOT_PAGE: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001410 uint64_t X = Val;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001411 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001412 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001413 break;
1414 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001415 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001416 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001417 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001418 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001419 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001420 }
George Rimar3d737e42016-01-13 13:04:46 +00001421 case R_AARCH64_ADR_PREL_PG_HI21:
1422 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001423 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001424 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001425 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001426 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001427 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001428 case R_AARCH64_CALL26:
1429 case R_AARCH64_JUMP26: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001430 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001431 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001432 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1433 break;
1434 }
George Rimar4102bfb2016-01-11 14:22:00 +00001435 case R_AARCH64_CONDBR19: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001436 uint64_t X = Val;
George Rimar4102bfb2016-01-11 14:22:00 +00001437 checkInt<21>(X, Type);
1438 or32le(Loc, (X & 0x1FFFFC) << 3);
1439 break;
1440 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001441 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001442 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001443 checkAlignment<8>(Val, Type);
1444 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001445 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001446 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001447 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001448 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001449 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001450 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001451 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001452 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001453 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001454 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001455 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001456 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001457 break;
1458 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001459 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001460 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001461 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001462 checkIntUInt<16>(Val, Type);
1463 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001464 break;
1465 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001466 checkIntUInt<32>(Val, Type);
1467 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001468 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001469 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001470 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001471 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001472 case R_AARCH64_TSTBR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001473 uint64_t X = Val;
George Rimar1395dbd2016-01-11 14:27:05 +00001474 checkInt<16>(X, Type);
1475 or32le(Loc, (X & 0xFFFC) << 3);
1476 break;
1477 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001478 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001479 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001480 checkInt<24>(V, Type);
1481 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1482 break;
1483 }
1484 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001485 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001486 updateAArch64Add(Loc, V & 0xFFF);
1487 break;
1488 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001489 default:
George Rimar57610422016-03-11 14:43:02 +00001490 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001491 }
1492}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001493
Rafael Espindola22ef9562016-04-13 01:40:19 +00001494void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1495 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001496 // TLSDESC Global-Dynamic relocation are in the form:
1497 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1498 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1499 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1500 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1501 // And it can optimized to:
1502 // movz x0, #0x0, lsl #16
1503 // movk x0, #0x10
1504 // nop
1505 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001506 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001507 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001508 checkUInt<32>(X, Type);
1509
1510 uint32_t NewInst;
1511 switch (Type) {
1512 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1513 case R_AARCH64_TLSDESC_CALL:
1514 // nop
1515 NewInst = 0xd503201f;
1516 break;
1517 case R_AARCH64_TLSDESC_ADR_PAGE21:
1518 // movz
1519 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1520 break;
1521 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1522 // movk
1523 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1524 break;
1525 default:
George Rimar777f9632016-03-12 08:31:34 +00001526 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001527 }
1528 write32le(Loc, NewInst);
1529}
1530
Rafael Espindola22ef9562016-04-13 01:40:19 +00001531void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1532 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001533 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001534 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001535 checkUInt<32>(X, Type);
1536
George Rimar4d1d16d2016-03-06 06:16:05 +00001537 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001538 uint32_t NewInst;
1539 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1540 // Generate movz.
1541 unsigned RegNo = (Inst & 0x1f);
1542 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1543 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1544 // Generate movk
1545 unsigned RegNo = (Inst & 0x1f);
1546 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1547 } else {
George Rimar777f9632016-03-12 08:31:34 +00001548 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001549 }
1550 write32le(Loc, NewInst);
1551}
1552
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001553// Implementing relocations for AMDGPU is low priority since most
1554// programs don't use relocations now. Thus, this function is not
1555// actually called (relocateOne is called for each relocation).
1556// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001557void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1558 uint64_t Val) const {
1559 llvm_unreachable("not implemented");
1560}
1561
1562RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001563 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001564}
1565
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001566template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001567 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001568 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001569 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001570 PltEntrySize = 16;
1571 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001572 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001573 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001574 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001575 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001576 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001577}
1578
1579template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001580RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1581 const SymbolBody &S) const {
1582 switch (Type) {
1583 default:
1584 return R_ABS;
1585 case R_MIPS_HI16:
1586 // MIPS _gp_disp designates offset between start of function and 'gp'
1587 // pointer into GOT. __gnu_local_gp is equal to the current value of
1588 // the 'gp'. Therefore any relocations against them do not require
1589 // dynamic relocation.
1590 if (&S == ElfSym<ELFT>::MipsGpDisp)
1591 return R_PC;
1592 return R_ABS;
1593 case R_MIPS_PC32:
1594 case R_MIPS_PC16:
1595 case R_MIPS_PC19_S2:
1596 case R_MIPS_PC21_S2:
1597 case R_MIPS_PC26_S2:
1598 case R_MIPS_PCHI16:
1599 case R_MIPS_PCLO16:
1600 return R_PC;
1601 }
1602}
1603
1604template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001605uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001606 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1607 return R_MIPS_REL32;
1608 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001609 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001610 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001611 // Keep it going with a dummy value so that we can find more reloc errors.
1612 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001613}
1614
1615template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001616void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001617 typedef typename ELFT::Off Elf_Off;
1618 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001619
1620 // Set the MSB of the second GOT slot. This is not required by any
1621 // MIPS ABI documentation, though.
1622 //
1623 // There is a comment in glibc saying that "The MSB of got[1] of a
1624 // gnu object is set to identify gnu objects," and in GNU gold it
1625 // says "the second entry will be used by some runtime loaders".
1626 // But how this field is being used is unclear.
1627 //
1628 // We are not really willing to mimic other linkers behaviors
1629 // without understanding why they do that, but because all files
1630 // generated by GNU tools have this special GOT value, and because
1631 // we've been doing this for years, it is probably a safe bet to
1632 // keep doing this for now. We really need to revisit this to see
1633 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001634 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001635 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001636}
1637
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001638template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001639void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1640 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001641}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001642
Simon Atanasyan35031192015-12-15 06:06:34 +00001643static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001644
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001645template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001646static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001647 uint32_t Instr = read32<E>(Loc);
1648 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1649 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1650}
1651
1652template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001653static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001654 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001655 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001656 if (SHIFT > 0)
1657 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001658 checkInt<BSIZE + SHIFT>(V, Type);
1659 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001660}
1661
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001662template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001663static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001664 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001665 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001666}
1667
Simon Atanasyan3b377852016-03-04 10:55:20 +00001668template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001669static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1670 uint32_t Instr = read32<E>(Loc);
1671 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1672}
1673
Rafael Espindola666625b2016-04-01 14:36:09 +00001674template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001675 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1676}
1677
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001678template <class ELFT>
1679void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1680 const endianness E = ELFT::TargetEndianness;
1681 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1682 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1683 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1684 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1685 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1686 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1687 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1688 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1689 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001690 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001691 writeMipsLo16<E>(Buf + 4, Got);
1692 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001693}
1694
1695template <class ELFT>
1696void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1697 uint64_t PltEntryAddr, int32_t Index,
1698 unsigned RelOff) const {
1699 const endianness E = ELFT::TargetEndianness;
1700 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1701 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1702 write32<E>(Buf + 8, 0x03200008); // jr $25
1703 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001704 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001705 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1706 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001707}
1708
1709template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001710void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1711 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1712 // See MipsTargetInfo::writeThunk for details.
1713 const endianness E = ELFT::TargetEndianness;
1714 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1715 write32<E>(Buf + 4, 0x08000000); // j func
1716 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1717 write32<E>(Buf + 12, 0x00000000); // nop
1718 writeMipsHi16<E>(Buf, S);
1719 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1720 writeMipsLo16<E>(Buf + 8, S);
1721}
1722
1723template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001724bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Rafael Espindola790db9c2016-04-01 17:00:36 +00001725 return !isRelRelative(Type) || Type == R_MIPS_LO16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001726}
1727
1728template <class ELFT>
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001729bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001730 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001731}
1732
1733template <class ELFT>
1734bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1735 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001736}
1737
1738template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001739bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1740 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001741}
1742
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001743template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001744bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1745 const SymbolBody &S) const {
1746 // Any MIPS PIC code function is invoked with its address in register $t9.
1747 // So if we have a branch instruction from non-PIC code to the PIC one
1748 // we cannot make the jump directly and need to create a small stubs
1749 // to save the target function address.
1750 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1751 if (Type != R_MIPS_26)
1752 return false;
1753 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1754 if (!F)
1755 return false;
1756 // If current file has PIC code, LA25 stub is not required.
1757 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1758 return false;
1759 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1760 if (!D || !D->Section)
1761 return false;
1762 // LA25 is required if target file has PIC code
1763 // or target symbol is a PIC symbol.
1764 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001765 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001766}
1767
1768template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001769uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001770 uint32_t Type) const {
1771 const endianness E = ELFT::TargetEndianness;
1772 switch (Type) {
1773 default:
1774 return 0;
1775 case R_MIPS_32:
1776 case R_MIPS_GPREL32:
1777 return read32<E>(Buf);
1778 case R_MIPS_26:
1779 // FIXME (simon): If the relocation target symbol is not a PLT entry
1780 // we should use another expression for calculation:
1781 // ((A << 2) | (P & 0xf0000000)) >> 2
1782 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1783 case R_MIPS_GPREL16:
1784 case R_MIPS_LO16:
1785 case R_MIPS_PCLO16:
1786 case R_MIPS_TLS_DTPREL_HI16:
1787 case R_MIPS_TLS_DTPREL_LO16:
1788 case R_MIPS_TLS_TPREL_HI16:
1789 case R_MIPS_TLS_TPREL_LO16:
1790 return readSignedLo16<E>(Buf);
1791 case R_MIPS_PC16:
1792 return getPcRelocAddend<E, 16, 2>(Buf);
1793 case R_MIPS_PC19_S2:
1794 return getPcRelocAddend<E, 19, 2>(Buf);
1795 case R_MIPS_PC21_S2:
1796 return getPcRelocAddend<E, 21, 2>(Buf);
1797 case R_MIPS_PC26_S2:
1798 return getPcRelocAddend<E, 26, 2>(Buf);
1799 case R_MIPS_PC32:
1800 return getPcRelocAddend<E, 32, 0>(Buf);
1801 }
1802}
1803
1804template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001805void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1806 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001807 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001808 // Thread pointer and DRP offsets from the start of TLS data area.
1809 // https://www.linux-mips.org/wiki/NPTL
1810 const uint32_t TPOffset = 0x7000;
1811 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001812 switch (Type) {
1813 case R_MIPS_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001814 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001815 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001816 case R_MIPS_26: {
1817 uint32_t Instr = read32<E>(Loc);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001818 write32<E>(Loc, (Instr & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001819 break;
1820 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001821 case R_MIPS_CALL16:
1822 case R_MIPS_GOT16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001823 int64_t V = Val - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001824 if (Type == R_MIPS_GOT16)
1825 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001826 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001827 break;
1828 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001829 case R_MIPS_GPREL16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001830 int64_t V = Val - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001831 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001832 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001833 break;
1834 }
1835 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001836 write32<E>(Loc, Val - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001837 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001838 case R_MIPS_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001839 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001840 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001841 case R_MIPS_JALR:
1842 // Ignore this optimization relocation for now
1843 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001844 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001845 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001846 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001847 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001848 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001849 break;
1850 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001851 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001852 break;
1853 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001854 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001855 break;
1856 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001857 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001858 break;
1859 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001860 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001861 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001862 case R_MIPS_PCHI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001863 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001864 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001865 case R_MIPS_PCLO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001866 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001867 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001868 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001869 writeMipsHi16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001870 break;
1871 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001872 writeMipsLo16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001873 break;
1874 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001875 writeMipsHi16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001876 break;
1877 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001878 writeMipsLo16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001879 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001880 default:
George Rimar57610422016-03-11 14:43:02 +00001881 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001882 }
1883}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001884
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001885template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001886bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001887 return Type == R_MIPS_JALR;
1888}
1889
1890template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001891bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1892 switch (Type) {
1893 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001894 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001895 case R_MIPS_26:
1896 case R_MIPS_32:
1897 case R_MIPS_64:
1898 case R_MIPS_HI16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001899 case R_MIPS_TLS_DTPREL_HI16:
1900 case R_MIPS_TLS_DTPREL_LO16:
1901 case R_MIPS_TLS_TPREL_HI16:
1902 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001903 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001904 }
1905}
1906
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001907// _gp is a MIPS-specific ABI-defined symbol which points to
1908// a location that is relative to GOT. This function returns
1909// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001910template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rafael Espindola6f92e142016-04-12 13:26:51 +00001911 return Out<ELFT>::Got->getVA() + MipsGPOffset;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001912}
1913
1914template uint32_t getMipsGpAddr<ELF32LE>();
1915template uint32_t getMipsGpAddr<ELF32BE>();
1916template uint64_t getMipsGpAddr<ELF64LE>();
1917template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001918
1919template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1920 const SymbolBody &) const;
1921template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1922 const SymbolBody &) const;
1923template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1924 const SymbolBody &) const;
1925template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1926 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001927}
1928}