blob: 58ab2268448d044118c3c1f22bd96c6334b70ce6 [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;
Rafael Espindolae149b482016-04-14 16:05:42 +0000425 return R_386_GOT32;
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:
Rafael Espindolae149b482016-04-14 16:05:42 +0000547 case R_386_TLS_LDM: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000548 uint64_t V = Val - Out<ELF32LE>::Got->getVA() -
George Rimar9db204a2015-12-02 09:58:20 +0000549 Out<ELF32LE>::Got->getNumEntries() * 4;
550 checkInt<32>(V, Type);
551 write32le(Loc, V);
552 break;
553 }
George Rimar6f17e092015-12-17 09:32:21 +0000554 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000555 case R_386_TLS_LDO_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000556 write32le(Loc, Val);
George Rimar9db204a2015-12-02 09:58:20 +0000557 break;
George Rimard23970f2015-11-25 20:41:53 +0000558 case R_386_TLS_LE:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000559 write32le(Loc, Val - Out<ELF32LE>::TlsPhdr->p_memsz);
George Rimard23970f2015-11-25 20:41:53 +0000560 break;
561 case R_386_TLS_LE_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000562 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimard23970f2015-11-25 20:41:53 +0000563 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000564 default:
George Rimar57610422016-03-11 14:43:02 +0000565 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000566 }
567}
568
George Rimar98b060d2016-03-06 06:01:07 +0000569bool X86TargetInfo::needsDynRelative(uint32_t Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000570 return Config->Shared && Type == R_386_TLS_IE;
571}
572
Rafael Espindola22ef9562016-04-13 01:40:19 +0000573void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
574 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000575 // GD can be optimized to LE:
576 // leal x@tlsgd(, %ebx, 1),
577 // call __tls_get_addr@plt
578 // Can be converted to:
579 // movl %gs:0,%eax
580 // addl $x@ntpoff,%eax
581 // But gold emits subl $foo@tpoff,%eax instead of addl.
582 // These instructions are completely equal in behavior.
583 // This method generates subl to be consistent with gold.
584 const uint8_t Inst[] = {
585 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
586 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
587 };
588 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000589 relocateOne(Loc + 5, R_386_32, Out<ELF32LE>::TlsPhdr->p_memsz - Val);
George Rimar2558e122015-12-09 09:55:54 +0000590}
591
592// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
593// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
594// how GD can be optimized to IE:
595// leal x@tlsgd(, %ebx, 1),
596// call __tls_get_addr@plt
597// Is converted to:
598// movl %gs:0, %eax
599// addl x@gotntpoff(%ebx), %eax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000600void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
601 uint64_t Val) const {
George Rimar2558e122015-12-09 09:55:54 +0000602 const uint8_t Inst[] = {
603 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
604 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
605 };
606 memcpy(Loc - 3, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000607 relocateOne(Loc + 5, R_386_32, Val - Out<ELF32LE>::Got->getVA() -
608 Out<ELF32LE>::Got->getNumEntries() * 4);
George Rimar2558e122015-12-09 09:55:54 +0000609}
610
George Rimar6f17e092015-12-17 09:32:21 +0000611// In some conditions, relocations can be optimized to avoid using GOT.
612// This function does that for Initial Exec to Local Exec case.
613// Read "ELF Handling For Thread-Local Storage, 5.1
614// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000615// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000616void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
617 uint64_t Val) const {
George Rimar6f17e092015-12-17 09:32:21 +0000618 // Ulrich's document section 6.2 says that @gotntpoff can
619 // be used with MOVL or ADDL instructions.
620 // @indntpoff is similar to @gotntpoff, but for use in
621 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000622 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000623 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000624 uint8_t Reg = (Loc[-1] >> 3) & 7;
625 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000626 if (Type == R_386_TLS_IE) {
627 // For R_386_TLS_IE relocation we perform the next transformations:
628 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
629 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
630 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
631 // First one is special because when EAX is used the sequence is 5 bytes
632 // long, otherwise it is 6 bytes.
633 if (*Op == 0xa1) {
634 *Op = 0xb8;
635 } else {
636 *Inst = IsMov ? 0xc7 : 0x81;
637 *Op = 0xc0 | ((*Op >> 3) & 7);
638 }
639 } else {
640 // R_386_TLS_GOTIE relocation can be optimized to
641 // R_386_TLS_LE so that it does not use GOT.
642 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
643 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
644 // Note: gold converts to ADDL instead of LEAL.
645 *Inst = IsMov ? 0xc7 : 0x8d;
646 if (IsMov)
647 *Op = 0xc0 | ((*Op >> 3) & 7);
648 else
649 *Op = 0x80 | Reg | (Reg << 3);
650 }
Rafael Espindola22ef9562016-04-13 01:40:19 +0000651 relocateOne(Loc, R_386_TLS_LE, Val);
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000652}
653
Rafael Espindola22ef9562016-04-13 01:40:19 +0000654void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
655 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000656 if (Type == R_386_TLS_LDO_32) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000657 relocateOne(Loc, R_386_TLS_LE, Val);
658 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000659 }
660
661 // LD can be optimized to LE:
662 // leal foo(%reg),%eax
663 // call ___tls_get_addr
664 // Is converted to:
665 // movl %gs:0,%eax
666 // nop
667 // leal 0(%esi,1),%esi
668 const uint8_t Inst[] = {
669 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
670 0x90, // nop
671 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
672 };
673 memcpy(Loc - 2, Inst, sizeof(Inst));
George Rimar2558e122015-12-09 09:55:54 +0000674}
675
Rafael Espindola7f074422015-09-22 21:35:51 +0000676X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000677 CopyRel = R_X86_64_COPY;
678 GotRel = R_X86_64_GLOB_DAT;
679 PltRel = R_X86_64_JUMP_SLOT;
680 RelativeRel = R_X86_64_RELATIVE;
681 IRelativeRel = R_X86_64_IRELATIVE;
682 TlsGotRel = R_X86_64_TPOFF64;
Rui Ueyama724d6252016-01-29 01:49:32 +0000683 TlsModuleIndexRel = R_X86_64_DTPMOD64;
684 TlsOffsetRel = R_X86_64_DTPOFF64;
685 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000686 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000687 PltZeroSize = 16;
Rafael Espindola22ef9562016-04-13 01:40:19 +0000688 TlsGdToLeSkip = 2;
689}
690
691RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
692 switch (Type) {
693 default:
694 return R_ABS;
695 case R_X86_64_GOTPCREL:
696 case R_X86_64_PLT32:
697 case R_X86_64_PC32:
698 case R_X86_64_TLSLD:
699 case R_X86_64_GOTTPOFF:
700 case R_X86_64_TLSGD:
701 return R_PC;
702 }
George Rimar648a2c32015-10-20 08:54:27 +0000703}
704
Rui Ueyamac516ae12016-01-29 02:33:45 +0000705void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000706 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
707}
708
Rui Ueyamac516ae12016-01-29 02:33:45 +0000709void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000710 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000711 write32le(Buf, Plt + 6);
712}
713
Rui Ueyama900e2d22016-01-29 03:51:49 +0000714void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000715 const uint8_t PltData[] = {
716 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
717 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
718 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
719 };
720 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000721 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
722 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
723 write32le(Buf + 2, Got - Plt + 2); // GOT+8
724 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000725}
Rafael Espindola01205f72015-09-22 18:19:46 +0000726
Rui Ueyama9398f862016-01-29 04:15:02 +0000727void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
728 uint64_t PltEntryAddr, int32_t Index,
729 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000730 const uint8_t Inst[] = {
731 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
732 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
733 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
734 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000735 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000736
George Rimar648a2c32015-10-20 08:54:27 +0000737 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
738 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000739 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000740}
741
Rafael Espindolafb1533b2016-02-22 21:23:29 +0000742bool X86_64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
743 return Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
744 Type == R_X86_64_64;
George Rimarbc590fe2015-10-28 16:48:58 +0000745}
746
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000747bool X86_64TargetInfo::refersToGotEntry(uint32_t Type) const {
George Rimar9f8f4e32016-03-22 12:15:26 +0000748 return Type == R_X86_64_GOTPCREL || Type == R_X86_64_GOTPCRELX ||
749 Type == R_X86_64_REX_GOTPCRELX;
Rafael Espindola795dc5a2016-02-24 18:24:23 +0000750}
751
Rafael Espindola4e5ab422016-03-31 13:38:28 +0000752bool X86_64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000753 if (Type == R_X86_64_TLSGD)
Rui Ueyamac4466602016-03-13 19:48:18 +0000754 return Target->canRelaxTls(Type, &S) && S.isPreemptible();
George Rimar77d1cb12015-11-24 09:00:06 +0000755 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000756 return !canRelaxTls(Type, &S);
Rafael Espindola54322872016-03-24 12:55:27 +0000757 return refersToGotEntry(Type) || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000758}
759
George Rimar86971052016-03-29 08:35:42 +0000760uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const {
761 if (Type == R_X86_64_PC32 || Type == R_X86_64_32)
762 if (Config->Shared)
763 error(getELFRelocationTypeName(EM_X86_64, Type) +
764 " cannot be a dynamic relocation");
765 return Type;
766}
767
George Rimar98b060d2016-03-06 06:01:07 +0000768uint32_t X86_64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +0000769 // No other types of TLS relocations requiring GOT should
770 // reach here.
771 assert(Type == R_X86_64_GOTTPOFF);
772 return R_X86_64_PC32;
773}
774
George Rimar98b060d2016-03-06 06:01:07 +0000775bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +0000776 return Type == R_X86_64_GOTTPOFF;
777}
778
George Rimar98b060d2016-03-06 06:01:07 +0000779bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000780 return Type == R_X86_64_TLSGD;
781}
782
George Rimar98b060d2016-03-06 06:01:07 +0000783bool X86_64TargetInfo::pointsToLocalDynamicGotEntry(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +0000784 return Type == R_X86_64_TLSLD;
785}
786
George Rimar98b060d2016-03-06 06:01:07 +0000787bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const {
Rafael Espindola1f04c442016-03-08 20:24:36 +0000788 return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 ||
789 Type == R_X86_64_TLSLD;
George Rimard23970f2015-11-25 20:41:53 +0000790}
791
Rafael Espindola993f0272016-02-26 14:27:47 +0000792bool X86_64TargetInfo::needsPltImpl(uint32_t Type) const {
793 return Type == R_X86_64_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +0000794}
Rafael Espindolac4010882015-09-22 20:54:08 +0000795
Rafael Espindolaae244002015-10-05 19:30:12 +0000796bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
797 switch (Type) {
798 default:
799 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000800 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000801 case R_X86_64_DTPOFF64:
Ed Schouten39aca422016-04-06 18:21:07 +0000802 case R_X86_64_GOTTPOFF:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000803 case R_X86_64_PC8:
804 case R_X86_64_PC16:
805 case R_X86_64_PC32:
806 case R_X86_64_PC64:
807 case R_X86_64_PLT32:
Ed Schouten39aca422016-04-06 18:21:07 +0000808 case R_X86_64_TPOFF32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000809 return true;
810 }
811}
812
Rui Ueyamac516ae12016-01-29 02:33:45 +0000813bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000814 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000815}
816
George Rimar6713cf82015-11-25 21:46:05 +0000817// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
818// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
George Rimar6713cf82015-11-25 21:46:05 +0000819// how GD can be optimized to LE:
820// .byte 0x66
821// leaq x@tlsgd(%rip), %rdi
822// .word 0x6666
823// rex64
824// call __tls_get_addr@plt
825// Is converted to:
826// mov %fs:0x0,%rax
827// lea x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000828void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
829 uint64_t Val) const {
George Rimar6713cf82015-11-25 21:46:05 +0000830 const uint8_t Inst[] = {
831 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
832 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
833 };
834 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000835 relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000836}
837
George Rimar25411f252015-12-04 11:20:13 +0000838// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
839// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
840// how GD can be optimized to IE:
841// .byte 0x66
842// leaq x@tlsgd(%rip), %rdi
843// .word 0x6666
844// rex64
845// call __tls_get_addr@plt
846// Is converted to:
847// mov %fs:0x0,%rax
848// addq x@tpoff,%rax
Rafael Espindola22ef9562016-04-13 01:40:19 +0000849void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type,
850 uint64_t Val) const {
George Rimar25411f252015-12-04 11:20:13 +0000851 const uint8_t Inst[] = {
852 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
853 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
854 };
855 memcpy(Loc - 4, Inst, sizeof(Inst));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000856 relocateOne(Loc + 8, R_X86_64_PC32, Val - 8);
George Rimar25411f252015-12-04 11:20:13 +0000857}
858
George Rimar77d1cb12015-11-24 09:00:06 +0000859// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000860// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000861// This function does that. Read "ELF Handling For Thread-Local Storage,
862// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
863// by Ulrich Drepper for details.
Rafael Espindola22ef9562016-04-13 01:40:19 +0000864void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
865 uint64_t Val) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000866 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
867 // used in MOVQ or ADDQ instructions only.
868 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
869 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
870 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
871 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
872 uint8_t *Prefix = Loc - 3;
873 uint8_t *Inst = Loc - 2;
874 uint8_t *RegSlot = Loc - 1;
875 uint8_t Reg = Loc[-1] >> 3;
876 bool IsMov = *Inst == 0x8b;
877 bool RspAdd = !IsMov && Reg == 4;
878 // r12 and rsp registers requires special handling.
879 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
880 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
881 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
882 // The same true for rsp. So we convert to addq for them, saving 1 byte that
883 // we dont have.
884 if (RspAdd)
885 *Inst = 0x81;
886 else
887 *Inst = IsMov ? 0xc7 : 0x8d;
888 if (*Prefix == 0x4c)
889 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
890 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
Rafael Espindola22ef9562016-04-13 01:40:19 +0000891 relocateOne(Loc, R_X86_64_TPOFF32, Val + 4);
George Rimar77d1cb12015-11-24 09:00:06 +0000892}
893
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000894// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
895// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
896// how LD can be optimized to LE:
897// leaq bar@tlsld(%rip), %rdi
898// callq __tls_get_addr@PLT
899// leaq bar@dtpoff(%rax), %rcx
900// Is converted to:
901// .word 0x6666
902// .byte 0x66
903// mov %fs:0,%rax
904// leaq bar@tpoff(%rax), %rcx
Rafael Espindola22ef9562016-04-13 01:40:19 +0000905void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type,
906 uint64_t Val) const {
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000907 if (Type == R_X86_64_DTPOFF64) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000908 write64le(Loc, Val - Out<ELF64LE>::TlsPhdr->p_memsz);
909 return;
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000910 }
911 if (Type == R_X86_64_DTPOFF32) {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000912 relocateOne(Loc, R_X86_64_TPOFF32, Val);
913 return;
George Rimar25411f252015-12-04 11:20:13 +0000914 }
Rafael Espindola89cc14f2016-03-16 19:03:58 +0000915
916 const uint8_t Inst[] = {
917 0x66, 0x66, //.word 0x6666
918 0x66, //.byte 0x66
919 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
920 };
921 memcpy(Loc - 3, Inst, sizeof(Inst));
George Rimar6713cf82015-11-25 21:46:05 +0000922}
923
Rafael Espindola22ef9562016-04-13 01:40:19 +0000924void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
925 uint64_t Val) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000926 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000927 case R_X86_64_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000928 checkUInt<32>(Val, Type);
929 write32le(Loc, Val);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000930 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000931 case R_X86_64_32S:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000932 checkInt<32>(Val, Type);
933 write32le(Loc, Val);
Rafael Espindolac4010882015-09-22 20:54:08 +0000934 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000935 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000936 case R_X86_64_DTPOFF64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000937 write64le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000938 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000939 case R_X86_64_DTPOFF32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000940 write32le(Loc, Val);
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000941 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000942 case R_X86_64_GOTPCREL:
George Rimar9f8f4e32016-03-22 12:15:26 +0000943 case R_X86_64_GOTPCRELX:
944 case R_X86_64_REX_GOTPCRELX:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000945 case R_X86_64_PC32:
946 case R_X86_64_PLT32:
947 case R_X86_64_TLSGD:
948 case R_X86_64_TLSLD:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000949 write32le(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +0000950 break;
George Rimar48651482015-12-11 08:59:37 +0000951 case R_X86_64_SIZE32:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000952 write32le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000953 break;
954 case R_X86_64_SIZE64:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000955 write64le(Loc, Val);
George Rimar48651482015-12-11 08:59:37 +0000956 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000957 case R_X86_64_TPOFF32: {
Rafael Espindola22ef9562016-04-13 01:40:19 +0000958 Val -= Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000959 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000960 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000961 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000962 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000963 default:
George Rimar57610422016-03-11 14:43:02 +0000964 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000965 }
966}
967
Hal Finkel3c8cc672015-10-12 20:56:18 +0000968// Relocation masks following the #lo(value), #hi(value), #ha(value),
969// #higher(value), #highera(value), #highest(value), and #highesta(value)
970// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
971// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000972static uint16_t applyPPCLo(uint64_t V) { return V; }
973static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
974static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
975static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
976static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000977static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000978static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
979
Davide Italiano8c3444362016-01-11 19:45:33 +0000980PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000981bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
982
Rafael Espindola22ef9562016-04-13 01:40:19 +0000983void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
984 uint64_t Val) const {
Davide Italiano8c3444362016-01-11 19:45:33 +0000985 switch (Type) {
986 case R_PPC_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000987 write16be(Loc, applyPPCHa(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000988 break;
989 case R_PPC_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +0000990 write16be(Loc, applyPPCLo(Val));
Davide Italiano8c3444362016-01-11 19:45:33 +0000991 break;
992 default:
George Rimar57610422016-03-11 14:43:02 +0000993 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000994 }
995}
996
Rafael Espindola22ef9562016-04-13 01:40:19 +0000997RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
998 return R_ABS;
999}
1000
Rafael Espindolac4010882015-09-22 20:54:08 +00001001PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001002 GotRel = R_PPC64_GLOB_DAT;
1003 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +00001004 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +00001005
1006 // We need 64K pages (at least under glibc/Linux, the loader won't
1007 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +00001008 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +00001009
1010 // The PPC64 ELF ABI v1 spec, says:
1011 //
1012 // It is normally desirable to put segments with different characteristics
1013 // in separate 256 Mbyte portions of the address space, to give the
1014 // operating system full paging flexibility in the 64-bit address space.
1015 //
1016 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
1017 // use 0x10000000 as the starting address.
1018 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +00001019}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001020
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001021uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001022 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
1023 // order. The TOC starts where the first of these sections starts.
1024
1025 // FIXME: This obviously does not do the right thing when there is no .got
1026 // section, but there is a .toc or .tocbss section.
1027 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
1028 if (!TocVA)
1029 TocVA = Out<ELF64BE>::Plt->getVA();
1030
1031 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
1032 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
1033 // code (crt1.o) assumes that you can get from the TOC base to the
1034 // start of the .toc section with only a single (signed) 16-bit relocation.
1035 return TocVA + 0x8000;
1036}
1037
Rafael Espindola22ef9562016-04-13 01:40:19 +00001038RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
1039 switch (Type) {
1040 default:
1041 return R_ABS;
1042 case R_PPC64_REL24:
1043 return R_PPC_OPD;
1044 }
1045}
1046
Rui Ueyama9398f862016-01-29 04:15:02 +00001047void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1048 uint64_t PltEntryAddr, int32_t Index,
1049 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001050 uint64_t Off = GotEntryAddr - getPPC64TocBase();
1051
1052 // FIXME: What we should do, in theory, is get the offset of the function
1053 // descriptor in the .opd section, and use that as the offset from %r2 (the
1054 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
1055 // be a pointer to the function descriptor in the .opd section. Using
1056 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
1057
Hal Finkelfa92f682015-10-13 21:47:34 +00001058 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001059 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
1060 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
1061 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
1062 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
1063 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
1064 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
1065 write32be(Buf + 28, 0x4e800420); // bctr
1066}
1067
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001068bool PPC64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001069 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +00001070 return true;
1071
1072 switch (Type) {
1073 default: return false;
1074 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001075 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001076 case R_PPC64_GOT16_HA:
1077 case R_PPC64_GOT16_HI:
1078 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001079 case R_PPC64_GOT16_LO_DS:
1080 return true;
1081 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001082}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001083
Rafael Espindola993f0272016-02-26 14:27:47 +00001084bool PPC64TargetInfo::needsPltImpl(uint32_t Type) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001085 // These are function calls that need to be redirected through a PLT stub.
Rafael Espindola993f0272016-02-26 14:27:47 +00001086 return Type == R_PPC64_REL24;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001087}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001088
Hal Finkelbe0823d2015-10-12 20:58:52 +00001089bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1090 switch (Type) {
1091 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001092 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001093 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001094 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001095 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001096 }
1097}
1098
Rafael Espindola22ef9562016-04-13 01:40:19 +00001099void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1100 uint64_t Val) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001101 uint64_t TB = getPPC64TocBase();
1102
Hal Finkel3c8cc672015-10-12 20:56:18 +00001103 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1104 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001105 switch (Type) {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001106 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TB; break;
1107 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TB; break;
1108 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TB; break;
1109 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TB; break;
1110 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TB; break;
1111 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001112 default: break;
1113 }
1114
Hal Finkel3c8cc672015-10-12 20:56:18 +00001115 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001116 case R_PPC64_ADDR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001117 checkAlignment<4>(Val, Type);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001118 // Preserve the AA/LK bits in the branch instruction
1119 uint8_t AALK = Loc[3];
Rafael Espindola22ef9562016-04-13 01:40:19 +00001120 write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001121 break;
1122 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001123 case R_PPC64_ADDR16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001124 checkInt<16>(Val, Type);
1125 write16be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001126 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001127 case R_PPC64_ADDR16_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001128 checkInt<16>(Val, Type);
1129 write16be(Loc, (read16be(Loc) & 3) | (Val & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001130 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001131 case R_PPC64_ADDR16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001132 write16be(Loc, applyPPCHa(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_ADDR16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001135 write16be(Loc, applyPPCHi(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001137 case R_PPC64_ADDR16_HIGHER:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001138 write16be(Loc, applyPPCHigher(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001139 break;
1140 case R_PPC64_ADDR16_HIGHERA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001141 write16be(Loc, applyPPCHighera(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001142 break;
1143 case R_PPC64_ADDR16_HIGHEST:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001144 write16be(Loc, applyPPCHighest(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001145 break;
1146 case R_PPC64_ADDR16_HIGHESTA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001147 write16be(Loc, applyPPCHighesta(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001148 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001149 case R_PPC64_ADDR16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001150 write16be(Loc, applyPPCLo(Val));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001151 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001152 case R_PPC64_ADDR16_LO_DS:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001153 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001154 break;
1155 case R_PPC64_ADDR32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001156 checkInt<32>(Val, Type);
1157 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001158 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001159 case R_PPC64_ADDR64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001160 write64be(Loc, Val);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001161 break;
1162 case R_PPC64_REL16_HA:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001163 write16be(Loc, applyPPCHa(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001164 break;
1165 case R_PPC64_REL16_HI:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001166 write16be(Loc, applyPPCHi(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001167 break;
1168 case R_PPC64_REL16_LO:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001169 write16be(Loc, applyPPCLo(Val));
Igor Kudrinb4a09272015-12-01 08:41:20 +00001170 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001171 case R_PPC64_REL24: {
1172 uint32_t Mask = 0x03FFFFFC;
Rafael Espindola22ef9562016-04-13 01:40:19 +00001173 checkInt<24>(Val, Type);
1174 write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001175 break;
1176 }
1177 case R_PPC64_REL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001178 checkInt<32>(Val, Type);
1179 write32be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001180 break;
1181 case R_PPC64_REL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001182 write64be(Loc, Val);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001183 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001184 case R_PPC64_TOC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001185 write64be(Loc, Val);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001186 break;
1187 default:
George Rimar57610422016-03-11 14:43:02 +00001188 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001189 }
1190}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001191
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001192AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001193 CopyRel = R_AARCH64_COPY;
Adhemerval Zanella668ad0f2016-02-23 16:54:40 +00001194 RelativeRel = R_AARCH64_RELATIVE;
Rui Ueyama724d6252016-01-29 01:49:32 +00001195 IRelativeRel = R_AARCH64_IRELATIVE;
1196 GotRel = R_AARCH64_GLOB_DAT;
1197 PltRel = R_AARCH64_JUMP_SLOT;
1198 TlsGotRel = R_AARCH64_TLS_TPREL64;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001199 TlsModuleIndexRel = R_AARCH64_TLS_DTPMOD64;
1200 TlsOffsetRel = R_AARCH64_TLS_DTPREL64;
Rui Ueyama724d6252016-01-29 01:49:32 +00001201 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001202 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001203 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001204}
George Rimar648a2c32015-10-20 08:54:27 +00001205
Rafael Espindola22ef9562016-04-13 01:40:19 +00001206RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type,
1207 const SymbolBody &S) const {
1208 switch (Type) {
1209 default:
1210 return R_ABS;
1211 case R_AARCH64_JUMP26:
1212 case R_AARCH64_CALL26:
1213 case R_AARCH64_PREL16:
1214 case R_AARCH64_PREL32:
1215 case R_AARCH64_PREL64:
1216 case R_AARCH64_CONDBR19:
1217 case R_AARCH64_ADR_PREL_LO21:
1218 case R_AARCH64_TSTBR14:
1219 return R_PC;
1220 case R_AARCH64_ADR_GOT_PAGE:
1221 case R_AARCH64_ADR_PREL_PG_HI21:
1222 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1223 return R_PAGE_PC;
1224 }
1225}
1226
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001227bool AArch64TargetInfo::isRelRelative(uint32_t Type) const {
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001228 switch (Type) {
1229 default:
1230 return false;
Ed Schouten39aca422016-04-06 18:21:07 +00001231 case R_AARCH64_ADD_ABS_LO12_NC:
1232 case R_AARCH64_ADR_GOT_PAGE:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001233 case R_AARCH64_ADR_PREL_LO21:
1234 case R_AARCH64_ADR_PREL_PG_HI21:
Ed Schouten39aca422016-04-06 18:21:07 +00001235 case R_AARCH64_CALL26:
1236 case R_AARCH64_CONDBR19:
1237 case R_AARCH64_JUMP26:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001238 case R_AARCH64_LDST8_ABS_LO12_NC:
1239 case R_AARCH64_LDST16_ABS_LO12_NC:
1240 case R_AARCH64_LDST32_ABS_LO12_NC:
1241 case R_AARCH64_LDST64_ABS_LO12_NC:
1242 case R_AARCH64_LDST128_ABS_LO12_NC:
Ed Schouten39aca422016-04-06 18:21:07 +00001243 case R_AARCH64_PREL32:
Rafael Espindola07275532016-03-28 01:31:11 +00001244 case R_AARCH64_PREL64:
Ed Schouten39aca422016-04-06 18:21:07 +00001245 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1246 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1247 case R_AARCH64_TLSLE_ADD_TPREL_HI12:
1248 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1249 case R_AARCH64_TSTBR14:
Adhemerval Zanella3db3f6d2016-03-24 19:12:14 +00001250 return true;
1251 }
Rafael Espindolaa4e35f72016-02-24 16:15:13 +00001252}
Rafael Espindola435c00f2016-02-23 20:19:44 +00001253
George Rimar98b060d2016-03-06 06:01:07 +00001254bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001255 return Type == R_AARCH64_TLSDESC_ADR_PAGE21 ||
1256 Type == R_AARCH64_TLSDESC_LD64_LO12_NC ||
1257 Type == R_AARCH64_TLSDESC_ADD_LO12_NC ||
1258 Type == R_AARCH64_TLSDESC_CALL;
1259}
1260
George Rimar98b060d2016-03-06 06:01:07 +00001261bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const {
Rafael Espindolad405f472016-03-04 21:37:09 +00001262 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1263 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1264}
1265
George Rimar98b060d2016-03-06 06:01:07 +00001266uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001267 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1268 return Type;
1269 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001270 error("relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001271 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001272 // Keep it going with a dummy value so that we can find more reloc errors.
1273 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001274}
1275
Rui Ueyamac516ae12016-01-29 02:33:45 +00001276void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001277 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1278}
1279
Rafael Espindola22ef9562016-04-13 01:40:19 +00001280static uint64_t getAArch64Page(uint64_t Expr) {
1281 return Expr & (~static_cast<uint64_t>(0xFFF));
1282}
1283
Rui Ueyama900e2d22016-01-29 03:51:49 +00001284void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001285 const uint8_t PltData[] = {
1286 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1287 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1288 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1289 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1290 0x20, 0x02, 0x1f, 0xd6, // br x17
1291 0x1f, 0x20, 0x03, 0xd5, // nop
1292 0x1f, 0x20, 0x03, 0xd5, // nop
1293 0x1f, 0x20, 0x03, 0xd5 // nop
1294 };
1295 memcpy(Buf, PltData, sizeof(PltData));
1296
Rui Ueyama900e2d22016-01-29 03:51:49 +00001297 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1298 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
Rafael Espindola22ef9562016-04-13 01:40:19 +00001299 relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21,
1300 getAArch64Page(Got + 16) - getAArch64Page(Plt + 4));
1301 relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16);
1302 relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001303}
1304
Rui Ueyama9398f862016-01-29 04:15:02 +00001305void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1306 uint64_t PltEntryAddr, int32_t Index,
1307 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001308 const uint8_t Inst[] = {
1309 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1310 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1311 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1312 0x20, 0x02, 0x1f, 0xd6 // br x17
1313 };
1314 memcpy(Buf, Inst, sizeof(Inst));
1315
Rafael Espindola22ef9562016-04-13 01:40:19 +00001316 relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21,
1317 getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr));
1318 relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr);
1319 relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001320}
1321
George Rimar98b060d2016-03-06 06:01:07 +00001322uint32_t AArch64TargetInfo::getTlsGotRel(uint32_t Type) const {
George Rimar2960c982016-02-11 11:14:46 +00001323 assert(Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
George Rimar4d1d16d2016-03-06 06:16:05 +00001324 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC);
1325 return Type;
George Rimar3d737e42016-01-13 13:04:46 +00001326}
1327
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001328bool AArch64TargetInfo::needsCopyRelImpl(uint32_t Type) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001329 switch (Type) {
1330 default:
1331 return false;
1332 case R_AARCH64_ABS16:
1333 case R_AARCH64_ABS32:
1334 case R_AARCH64_ABS64:
1335 case R_AARCH64_ADD_ABS_LO12_NC:
1336 case R_AARCH64_ADR_PREL_LO21:
1337 case R_AARCH64_ADR_PREL_PG_HI21:
1338 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001339 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001340 case R_AARCH64_LDST32_ABS_LO12_NC:
1341 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001342 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001343 return true;
Igor Kudrin9606d192015-12-03 08:05:35 +00001344 }
1345}
1346
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001347bool AArch64TargetInfo::needsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001348 switch (Type) {
George Rimar3d737e42016-01-13 13:04:46 +00001349 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
Rafael Espindola48b91022016-03-16 22:43:36 +00001350 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola54bf9e12016-03-16 23:01:56 +00001351 return !canRelaxTls(Type, &S);
George Rimar3d737e42016-01-13 13:04:46 +00001352 case R_AARCH64_ADR_GOT_PAGE:
1353 case R_AARCH64_LD64_GOT_LO12_NC:
1354 return true;
1355 default:
Rafael Espindola54322872016-03-24 12:55:27 +00001356 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001357 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001358}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001359
Adhemerval Zanella15cba9e2016-04-08 14:10:41 +00001360bool AArch64TargetInfo::refersToGotEntry(uint32_t Type) const {
1361 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC;
1362}
1363
Rafael Espindola993f0272016-02-26 14:27:47 +00001364bool AArch64TargetInfo::needsPltImpl(uint32_t Type) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001365 switch (Type) {
1366 default:
Rafael Espindola795dc5a2016-02-24 18:24:23 +00001367 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001368 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001369 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001370 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001371 case R_AARCH64_TSTBR14:
Rafael Espindola993f0272016-02-26 14:27:47 +00001372 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001373 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001374}
Davide Italiano1d750a62015-09-27 08:45:38 +00001375
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001376static void updateAArch64Addr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001377 uint32_t ImmLo = (Imm & 0x3) << 29;
1378 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1379 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001380 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001381}
1382
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001383static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) {
1384 or32le(L, (Imm & 0xFFF) << 10);
1385}
1386
Rafael Espindola22ef9562016-04-13 01:40:19 +00001387void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1388 uint64_t Val) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001389 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001390 case R_AARCH64_ABS16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001391 checkIntUInt<16>(Val, Type);
1392 write16le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001393 break;
1394 case R_AARCH64_ABS32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001395 checkIntUInt<32>(Val, Type);
1396 write32le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001397 break;
1398 case R_AARCH64_ABS64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001399 write64le(Loc, Val);
Davide Italianodf88f962015-10-04 00:59:16 +00001400 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001401 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001402 // This relocation stores 12 bits and there's no instruction
1403 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001404 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1405 // bits in Loc are zero.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001406 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001407 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001408 case R_AARCH64_ADR_GOT_PAGE: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001409 uint64_t X = Val;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001410 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001411 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Igor Kudrinb4a09272015-12-01 08:41:20 +00001412 break;
1413 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001414 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001415 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001416 checkInt<21>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001417 updateAArch64Addr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001418 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001419 }
George Rimar3d737e42016-01-13 13:04:46 +00001420 case R_AARCH64_ADR_PREL_PG_HI21:
1421 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001422 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001423 checkInt<33>(X, Type);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001424 updateAArch64Addr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001425 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001426 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001427 case R_AARCH64_CALL26:
1428 case R_AARCH64_JUMP26: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001429 uint64_t X = Val;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001430 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001431 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1432 break;
1433 }
George Rimar4102bfb2016-01-11 14:22:00 +00001434 case R_AARCH64_CONDBR19: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001435 uint64_t X = Val;
George Rimar4102bfb2016-01-11 14:22:00 +00001436 checkInt<21>(X, Type);
1437 or32le(Loc, (X & 0x1FFFFC) << 3);
1438 break;
1439 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001440 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001441 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001442 checkAlignment<8>(Val, Type);
1443 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001444 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001445 case R_AARCH64_LDST128_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001446 or32le(Loc, (Val & 0x0FF8) << 6);
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001447 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001448 case R_AARCH64_LDST16_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001449 or32le(Loc, (Val & 0x0FFC) << 9);
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001450 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001451 case R_AARCH64_LDST8_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001452 or32le(Loc, (Val & 0xFFF) << 10);
Davide Italianodc67f9b2015-11-20 21:35:38 +00001453 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001454 case R_AARCH64_LDST32_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001455 or32le(Loc, (Val & 0xFFC) << 8);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001456 break;
1457 case R_AARCH64_LDST64_ABS_LO12_NC:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001458 or32le(Loc, (Val & 0xFF8) << 7);
Igor Kudrinb4a09272015-12-01 08:41:20 +00001459 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001460 case R_AARCH64_PREL16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001461 checkIntUInt<16>(Val, Type);
1462 write16le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001463 break;
1464 case R_AARCH64_PREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001465 checkIntUInt<32>(Val, Type);
1466 write32le(Loc, Val);
Davide Italiano3300b792015-10-29 19:55:59 +00001467 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001468 case R_AARCH64_PREL64:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001469 write64le(Loc, Val);
Davide Italianob12d6682015-10-28 16:14:18 +00001470 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001471 case R_AARCH64_TSTBR14: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001472 uint64_t X = Val;
George Rimar1395dbd2016-01-11 14:27:05 +00001473 checkInt<16>(X, Type);
1474 or32le(Loc, (X & 0xFFFC) << 3);
1475 break;
1476 }
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001477 case R_AARCH64_TLSLE_ADD_TPREL_HI12: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001478 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001479 checkInt<24>(V, Type);
1480 updateAArch64Add(Loc, (V & 0xFFF000) >> 12);
1481 break;
1482 }
1483 case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001484 uint64_t V = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align) + Val;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001485 updateAArch64Add(Loc, V & 0xFFF);
1486 break;
1487 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001488 default:
George Rimar57610422016-03-11 14:43:02 +00001489 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001490 }
1491}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001492
Rafael Espindola22ef9562016-04-13 01:40:19 +00001493void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type,
1494 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001495 // TLSDESC Global-Dynamic relocation are in the form:
1496 // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21]
1497 // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC]
1498 // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC]
1499 // .tlsdesccall [R_AARCH64_TLSDESC_CALL]
1500 // And it can optimized to:
1501 // movz x0, #0x0, lsl #16
1502 // movk x0, #0x10
1503 // nop
1504 // nop
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001505 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001506 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001507 checkUInt<32>(X, Type);
1508
1509 uint32_t NewInst;
1510 switch (Type) {
1511 case R_AARCH64_TLSDESC_ADD_LO12_NC:
1512 case R_AARCH64_TLSDESC_CALL:
1513 // nop
1514 NewInst = 0xd503201f;
1515 break;
1516 case R_AARCH64_TLSDESC_ADR_PAGE21:
1517 // movz
1518 NewInst = 0xd2a00000 | (((X >> 16) & 0xffff) << 5);
1519 break;
1520 case R_AARCH64_TLSDESC_LD64_LO12_NC:
1521 // movk
1522 NewInst = 0xf2800000 | ((X & 0xffff) << 5);
1523 break;
1524 default:
George Rimar777f9632016-03-12 08:31:34 +00001525 llvm_unreachable("unsupported Relocation for TLS GD to LE relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001526 }
1527 write32le(Loc, NewInst);
1528}
1529
Rafael Espindola22ef9562016-04-13 01:40:19 +00001530void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type,
1531 uint64_t Val) const {
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001532 uint64_t TPOff = llvm::alignTo(TcbSize, Out<ELF64LE>::TlsPhdr->p_align);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001533 uint64_t X = Val + TPOff;
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001534 checkUInt<32>(X, Type);
1535
George Rimar4d1d16d2016-03-06 06:16:05 +00001536 uint32_t Inst = read32le(Loc);
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001537 uint32_t NewInst;
1538 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) {
1539 // Generate movz.
1540 unsigned RegNo = (Inst & 0x1f);
1541 NewInst = (0xd2a00000 | RegNo) | (((X >> 16) & 0xffff) << 5);
1542 } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) {
1543 // Generate movk
1544 unsigned RegNo = (Inst & 0x1f);
1545 NewInst = (0xf2800000 | RegNo) | ((X & 0xffff) << 5);
1546 } else {
George Rimar777f9632016-03-12 08:31:34 +00001547 llvm_unreachable("invalid Relocation for TLS IE to LE Relax");
Adhemerval Zanella74bcf032016-02-12 13:43:03 +00001548 }
1549 write32le(Loc, NewInst);
1550}
1551
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001552// Implementing relocations for AMDGPU is low priority since most
1553// programs don't use relocations now. Thus, this function is not
1554// actually called (relocateOne is called for each relocation).
1555// That's why the AMDGPU port works without implementing this function.
Rafael Espindola22ef9562016-04-13 01:40:19 +00001556void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1557 uint64_t Val) const {
1558 llvm_unreachable("not implemented");
1559}
1560
1561RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
George Rimar57610422016-03-11 14:43:02 +00001562 llvm_unreachable("not implemented");
Tom Stellard80efb162016-01-07 03:59:08 +00001563}
1564
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001565template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001566 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001567 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001568 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001569 PltEntrySize = 16;
1570 PltZeroSize = 32;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001571 ThunkSize = 16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001572 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001573 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001574 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001575 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001576}
1577
1578template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001579RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
1580 const SymbolBody &S) const {
1581 switch (Type) {
1582 default:
1583 return R_ABS;
1584 case R_MIPS_HI16:
1585 // MIPS _gp_disp designates offset between start of function and 'gp'
1586 // pointer into GOT. __gnu_local_gp is equal to the current value of
1587 // the 'gp'. Therefore any relocations against them do not require
1588 // dynamic relocation.
1589 if (&S == ElfSym<ELFT>::MipsGpDisp)
1590 return R_PC;
1591 return R_ABS;
1592 case R_MIPS_PC32:
1593 case R_MIPS_PC16:
1594 case R_MIPS_PC19_S2:
1595 case R_MIPS_PC21_S2:
1596 case R_MIPS_PC26_S2:
1597 case R_MIPS_PCHI16:
1598 case R_MIPS_PCLO16:
1599 return R_PC;
1600 }
1601}
1602
1603template <class ELFT>
George Rimar98b060d2016-03-06 06:01:07 +00001604uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001605 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1606 return R_MIPS_REL32;
1607 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
George Rimarca1d1fb2016-03-15 14:00:22 +00001608 error("relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001609 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001610 // Keep it going with a dummy value so that we can find more reloc errors.
1611 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001612}
1613
1614template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001615void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001616 typedef typename ELFT::Off Elf_Off;
1617 typedef typename ELFT::uint uintX_t;
Rui Ueyama8364c622016-01-29 22:55:38 +00001618
1619 // Set the MSB of the second GOT slot. This is not required by any
1620 // MIPS ABI documentation, though.
1621 //
1622 // There is a comment in glibc saying that "The MSB of got[1] of a
1623 // gnu object is set to identify gnu objects," and in GNU gold it
1624 // says "the second entry will be used by some runtime loaders".
1625 // But how this field is being used is unclear.
1626 //
1627 // We are not really willing to mimic other linkers behaviors
1628 // without understanding why they do that, but because all files
1629 // generated by GNU tools have this special GOT value, and because
1630 // we've been doing this for years, it is probably a safe bet to
1631 // keep doing this for now. We really need to revisit this to see
1632 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001633 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001634 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001635}
1636
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001637template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001638void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1639 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001640}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001641
Simon Atanasyan35031192015-12-15 06:06:34 +00001642static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001643
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001644template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001645static int64_t getPcRelocAddend(const uint8_t *Loc) {
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001646 uint32_t Instr = read32<E>(Loc);
1647 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
1648 return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1649}
1650
1651template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001652static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001653 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001654 uint32_t Instr = read32<E>(Loc);
Rafael Espindola66ea7bb2016-03-31 12:09:36 +00001655 if (SHIFT > 0)
1656 checkAlignment<(1 << SHIFT)>(V, Type);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001657 checkInt<BSIZE + SHIFT>(V, Type);
1658 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001659}
1660
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001661template <endianness E>
Simon Atanasyana888e672016-03-04 10:55:12 +00001662static void writeMipsHi16(uint8_t *Loc, uint64_t V) {
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001663 uint32_t Instr = read32<E>(Loc);
Simon Atanasyana888e672016-03-04 10:55:12 +00001664 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001665}
1666
Simon Atanasyan3b377852016-03-04 10:55:20 +00001667template <endianness E>
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001668static void writeMipsLo16(uint8_t *Loc, uint64_t V) {
1669 uint32_t Instr = read32<E>(Loc);
1670 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1671}
1672
Rafael Espindola666625b2016-04-01 14:36:09 +00001673template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) {
Simon Atanasyan4e18a312016-03-04 10:55:29 +00001674 return SignExtend32<16>(read32<E>(Loc) & 0xffff);
1675}
1676
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001677template <class ELFT>
1678void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1679 const endianness E = ELFT::TargetEndianness;
1680 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1681 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1682 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1683 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1684 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1685 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1686 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1687 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1688 uint64_t Got = Out<ELFT>::GotPlt->getVA();
Simon Atanasyana888e672016-03-04 10:55:12 +00001689 writeMipsHi16<E>(Buf, Got);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001690 writeMipsLo16<E>(Buf + 4, Got);
1691 writeMipsLo16<E>(Buf + 8, Got);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001692}
1693
1694template <class ELFT>
1695void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1696 uint64_t PltEntryAddr, int32_t Index,
1697 unsigned RelOff) const {
1698 const endianness E = ELFT::TargetEndianness;
1699 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1700 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1701 write32<E>(Buf + 8, 0x03200008); // jr $25
1702 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
Simon Atanasyana888e672016-03-04 10:55:12 +00001703 writeMipsHi16<E>(Buf, GotEntryAddr);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001704 writeMipsLo16<E>(Buf + 4, GotEntryAddr);
1705 writeMipsLo16<E>(Buf + 12, GotEntryAddr);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001706}
1707
1708template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001709void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const {
1710 // Write MIPS LA25 thunk code to call PIC function from the non-PIC one.
1711 // See MipsTargetInfo::writeThunk for details.
1712 const endianness E = ELFT::TargetEndianness;
1713 write32<E>(Buf, 0x3c190000); // lui $25, %hi(func)
1714 write32<E>(Buf + 4, 0x08000000); // j func
1715 write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func)
1716 write32<E>(Buf + 12, 0x00000000); // nop
1717 writeMipsHi16<E>(Buf, S);
1718 write32<E>(Buf + 4, 0x08000000 | (S >> 2));
1719 writeMipsLo16<E>(Buf + 8, S);
1720}
1721
1722template <class ELFT>
Rafael Espindolafb1533b2016-02-22 21:23:29 +00001723bool MipsTargetInfo<ELFT>::needsCopyRelImpl(uint32_t Type) const {
Rafael Espindola790db9c2016-04-01 17:00:36 +00001724 return !isRelRelative(Type) || Type == R_MIPS_LO16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001725}
1726
1727template <class ELFT>
Rafael Espindola4e5ab422016-03-31 13:38:28 +00001728bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola54322872016-03-24 12:55:27 +00001729 return needsPlt(Type, S) || refersToGotEntry(Type);
Simon Atanasyand040a582016-02-25 16:19:15 +00001730}
1731
1732template <class ELFT>
1733bool MipsTargetInfo<ELFT>::refersToGotEntry(uint32_t Type) const {
1734 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001735}
1736
1737template <class ELFT>
Rafael Espindola993f0272016-02-26 14:27:47 +00001738bool MipsTargetInfo<ELFT>::needsPltImpl(uint32_t Type) const {
1739 return Type == R_MIPS_26;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001740}
1741
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001742template <class ELFT>
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001743bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File,
1744 const SymbolBody &S) const {
1745 // Any MIPS PIC code function is invoked with its address in register $t9.
1746 // So if we have a branch instruction from non-PIC code to the PIC one
1747 // we cannot make the jump directly and need to create a small stubs
1748 // to save the target function address.
1749 // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1750 if (Type != R_MIPS_26)
1751 return false;
1752 auto *F = dyn_cast<ELFFileBase<ELFT>>(&File);
1753 if (!F)
1754 return false;
1755 // If current file has PIC code, LA25 stub is not required.
1756 if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC)
1757 return false;
1758 auto *D = dyn_cast<DefinedRegular<ELFT>>(&S);
1759 if (!D || !D->Section)
1760 return false;
1761 // LA25 is required if target file has PIC code
1762 // or target symbol is a PIC symbol.
1763 return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) ||
Rui Ueyamab5792b22016-04-04 19:09:08 +00001764 (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC;
Simon Atanasyan13f6da12016-03-31 21:26:23 +00001765}
1766
1767template <class ELFT>
Rafael Espindola666625b2016-04-01 14:36:09 +00001768uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf,
Rafael Espindola8cc68c32016-03-30 13:18:08 +00001769 uint32_t Type) const {
1770 const endianness E = ELFT::TargetEndianness;
1771 switch (Type) {
1772 default:
1773 return 0;
1774 case R_MIPS_32:
1775 case R_MIPS_GPREL32:
1776 return read32<E>(Buf);
1777 case R_MIPS_26:
1778 // FIXME (simon): If the relocation target symbol is not a PLT entry
1779 // we should use another expression for calculation:
1780 // ((A << 2) | (P & 0xf0000000)) >> 2
1781 return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2);
1782 case R_MIPS_GPREL16:
1783 case R_MIPS_LO16:
1784 case R_MIPS_PCLO16:
1785 case R_MIPS_TLS_DTPREL_HI16:
1786 case R_MIPS_TLS_DTPREL_LO16:
1787 case R_MIPS_TLS_TPREL_HI16:
1788 case R_MIPS_TLS_TPREL_LO16:
1789 return readSignedLo16<E>(Buf);
1790 case R_MIPS_PC16:
1791 return getPcRelocAddend<E, 16, 2>(Buf);
1792 case R_MIPS_PC19_S2:
1793 return getPcRelocAddend<E, 19, 2>(Buf);
1794 case R_MIPS_PC21_S2:
1795 return getPcRelocAddend<E, 21, 2>(Buf);
1796 case R_MIPS_PC26_S2:
1797 return getPcRelocAddend<E, 26, 2>(Buf);
1798 case R_MIPS_PC32:
1799 return getPcRelocAddend<E, 32, 0>(Buf);
1800 }
1801}
1802
1803template <class ELFT>
Rafael Espindola22ef9562016-04-13 01:40:19 +00001804void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type,
1805 uint64_t Val) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001806 const endianness E = ELFT::TargetEndianness;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001807 // Thread pointer and DRP offsets from the start of TLS data area.
1808 // https://www.linux-mips.org/wiki/NPTL
1809 const uint32_t TPOffset = 0x7000;
1810 const uint32_t DTPOffset = 0x8000;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001811 switch (Type) {
1812 case R_MIPS_32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001813 write32<E>(Loc, Val);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001814 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001815 case R_MIPS_26: {
1816 uint32_t Instr = read32<E>(Loc);
Rafael Espindola22ef9562016-04-13 01:40:19 +00001817 write32<E>(Loc, (Instr & ~0x3ffffff) | (Val >> 2));
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001818 break;
1819 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001820 case R_MIPS_CALL16:
1821 case R_MIPS_GOT16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001822 int64_t V = Val - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001823 if (Type == R_MIPS_GOT16)
1824 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001825 writeMipsLo16<E>(Loc, V);
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001826 break;
1827 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001828 case R_MIPS_GPREL16: {
Rafael Espindola22ef9562016-04-13 01:40:19 +00001829 int64_t V = Val - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001830 checkInt<16>(V, Type);
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001831 writeMipsLo16<E>(Loc, V);
Simon Atanasyan57830b62015-12-25 13:02:13 +00001832 break;
1833 }
1834 case R_MIPS_GPREL32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001835 write32<E>(Loc, Val - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001836 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001837 case R_MIPS_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001838 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001839 break;
Simon Atanasyane4361852015-12-13 06:49:14 +00001840 case R_MIPS_JALR:
1841 // Ignore this optimization relocation for now
1842 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001843 case R_MIPS_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001844 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001845 break;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001846 case R_MIPS_PC16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001847 applyMipsPcReloc<E, 16, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001848 break;
1849 case R_MIPS_PC19_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001850 applyMipsPcReloc<E, 19, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001851 break;
1852 case R_MIPS_PC21_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001853 applyMipsPcReloc<E, 21, 2>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001854 break;
1855 case R_MIPS_PC26_S2:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001856 applyMipsPcReloc<E, 26, 2>(Loc, Type, Val);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001857 break;
1858 case R_MIPS_PC32:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001859 applyMipsPcReloc<E, 32, 0>(Loc, Type, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001860 break;
Simon Atanasyan3b377852016-03-04 10:55:20 +00001861 case R_MIPS_PCHI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001862 writeMipsHi16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001863 break;
Simon Atanasyan0dcf5412016-03-04 10:55:24 +00001864 case R_MIPS_PCLO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001865 writeMipsLo16<E>(Loc, Val);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001866 break;
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001867 case R_MIPS_TLS_DTPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001868 writeMipsHi16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001869 break;
1870 case R_MIPS_TLS_DTPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001871 writeMipsLo16<E>(Loc, Val - DTPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001872 break;
1873 case R_MIPS_TLS_TPREL_HI16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001874 writeMipsHi16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001875 break;
1876 case R_MIPS_TLS_TPREL_LO16:
Rafael Espindola22ef9562016-04-13 01:40:19 +00001877 writeMipsLo16<E>(Loc, Val - TPOffset);
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001878 break;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001879 default:
George Rimar57610422016-03-11 14:43:02 +00001880 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001881 }
1882}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001883
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001884template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001885bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001886 return Type == R_MIPS_JALR;
1887}
1888
1889template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001890bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1891 switch (Type) {
1892 default:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001893 return true;
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001894 case R_MIPS_26:
1895 case R_MIPS_32:
1896 case R_MIPS_64:
1897 case R_MIPS_HI16:
Simon Atanasyana8e9cb32016-03-17 12:36:08 +00001898 case R_MIPS_TLS_DTPREL_HI16:
1899 case R_MIPS_TLS_DTPREL_LO16:
1900 case R_MIPS_TLS_TPREL_HI16:
1901 case R_MIPS_TLS_TPREL_LO16:
Simon Atanasyan49bc69b2016-02-25 05:03:52 +00001902 return false;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001903 }
1904}
1905
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001906// _gp is a MIPS-specific ABI-defined symbol which points to
1907// a location that is relative to GOT. This function returns
1908// the value for the symbol.
Rui Ueyama9328b2c2016-03-14 23:16:09 +00001909template <class ELFT> typename ELFT::uint getMipsGpAddr() {
Rafael Espindola6f92e142016-04-12 13:26:51 +00001910 return Out<ELFT>::Got->getVA() + MipsGPOffset;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001911}
1912
1913template uint32_t getMipsGpAddr<ELF32LE>();
1914template uint32_t getMipsGpAddr<ELF32BE>();
1915template uint64_t getMipsGpAddr<ELF64LE>();
1916template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindolaf7ae3592016-02-23 18:53:29 +00001917
1918template bool TargetInfo::needsCopyRel<ELF32LE>(uint32_t,
1919 const SymbolBody &) const;
1920template bool TargetInfo::needsCopyRel<ELF32BE>(uint32_t,
1921 const SymbolBody &) const;
1922template bool TargetInfo::needsCopyRel<ELF64LE>(uint32_t,
1923 const SymbolBody &) const;
1924template bool TargetInfo::needsCopyRel<ELF64BE>(uint32_t,
1925 const SymbolBody &) const;
Rafael Espindola01205f72015-09-22 18:19:46 +00001926}
1927}