blob: b4ef5d64b74f63e4233cc25dae8c07e91a22469b [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:46 +00001//===- Target.cpp ---------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Rui Ueyama34f29242015-10-13 19:51:57 +00009//
Rui Ueyama66072272015-10-15 19:52:27 +000010// Machine-specific things, such as applying relocations, creation of
11// GOT or PLT entries, etc., are handled in this file.
12//
13// Refer the ELF spec for the single letter varaibles, S, A or P, used
14// in this file. SA is S+A.
Rui Ueyama34f29242015-10-13 19:51:57 +000015//
16//===----------------------------------------------------------------------===//
Rafael Espindola01205f72015-09-22 18:19:46 +000017
18#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000019#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000020#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000021#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000022
23#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000024#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000025#include "llvm/Support/Endian.h"
26#include "llvm/Support/ELF.h"
27
28using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000029using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000030using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000031using namespace llvm::ELF;
32
33namespace lld {
34namespace elf2 {
35
36std::unique_ptr<TargetInfo> Target;
37
Rafael Espindolae7e57b22015-11-09 21:43:00 +000038template <endianness E> static void add32(void *P, int32_t V) {
39 write32<E>(P, read32<E>(P) + V);
40}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +000041
Rafael Espindolae7e57b22015-11-09 21:43:00 +000042static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); }
43static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
Rui Ueyamaefc23de2015-10-14 21:30:32 +000044
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000045template <unsigned N> static void checkInt(int64_t V, uint32_t Type) {
46 if (isInt<N>(V))
47 return;
48 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000049 error("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000050}
51
52template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) {
53 if (isUInt<N>(V))
54 return;
55 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000056 error("Relocation " + S + " out of range");
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000057}
58
Igor Kudrinfea8ed52015-11-26 10:05:24 +000059template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) {
60 if (isInt<N>(V) || isUInt<N>(V))
61 return;
62 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000063 error("Relocation " + S + " out of range");
Igor Kudrinfea8ed52015-11-26 10:05:24 +000064}
65
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000066template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) {
67 if ((V & (N - 1)) == 0)
68 return;
69 StringRef S = getELFRelocationTypeName(Config->EMachine, Type);
Rui Ueyama21923992016-02-01 23:28:21 +000070 error("Improper alignment for relocation " + S);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +000071}
72
George Rimara07ff662015-12-21 10:12:06 +000073template <class ELFT> bool isGnuIFunc(const SymbolBody &S) {
Rafael Espindola4d4b06a2015-12-24 00:47:42 +000074 if (auto *SS = dyn_cast<DefinedElf<ELFT>>(&S))
George Rimara07ff662015-12-21 10:12:06 +000075 return SS->Sym.getType() == STT_GNU_IFUNC;
76 return false;
77}
78
Rui Ueyamaefc23de2015-10-14 21:30:32 +000079namespace {
80class X86TargetInfo final : public TargetInfo {
81public:
82 X86TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +000083 void writeGotPltHeader(uint8_t *Buf) const override;
84 unsigned getDynRel(unsigned Type) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +000085 unsigned getTlsGotRel(unsigned Type) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000086 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
87 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +000088 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +000089 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
90 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +000091 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000092 bool needsDynRelative(unsigned Type) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +000093 bool needsGot(uint32_t Type, SymbolBody &S) const override;
94 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000095 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +000096 uint64_t SA, uint64_t ZA = 0,
97 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +000098 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +000099 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
100 uint64_t SA, const SymbolBody *S) const override;
George Rimarbfb7bf72015-12-21 10:00:12 +0000101 bool isGotRelative(uint32_t Type) const override;
George Rimar2558e122015-12-09 09:55:54 +0000102
103private:
104 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
105 uint64_t SA) const;
106 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
107 uint64_t SA) const;
108 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
109 uint64_t SA) const;
George Rimar6f17e092015-12-17 09:32:21 +0000110 void relocateTlsIeToLe(unsigned Type, uint8_t *Loc, uint8_t *BufEnd,
111 uint64_t P, uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000112};
113
114class X86_64TargetInfo final : public TargetInfo {
115public:
116 X86_64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000117 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
118 void writeGotPltHeader(uint8_t *Buf) const override;
119 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000120 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000121 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
122 int32_t Index, unsigned RelOff) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000123 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000124 bool needsGot(uint32_t Type, SymbolBody &S) const override;
125 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000126 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000127 uint64_t SA, uint64_t ZA = 0,
128 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000129 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamabaf16512016-01-29 00:20:12 +0000130 bool canRelaxTls(unsigned Type, const SymbolBody *S) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000131 bool isSizeRel(uint32_t Type) const override;
132 unsigned relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
133 uint64_t SA, const SymbolBody *S) const override;
George Rimar6713cf82015-11-25 21:46:05 +0000134
135private:
136 void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
137 uint64_t SA) const;
138 void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
139 uint64_t SA) const;
George Rimar25411f252015-12-04 11:20:13 +0000140 void relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
141 uint64_t SA) const;
George Rimar6713cf82015-11-25 21:46:05 +0000142 void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
143 uint64_t SA) const;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000144};
145
Davide Italiano8c3444362016-01-11 19:45:33 +0000146class PPCTargetInfo final : public TargetInfo {
147public:
148 PPCTargetInfo();
Davide Italiano8c3444362016-01-11 19:45:33 +0000149 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
150 uint64_t SA, uint64_t ZA = 0,
151 uint8_t *PairedLoc = nullptr) const override;
152 bool isRelRelative(uint32_t Type) const override;
153};
154
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000155class PPC64TargetInfo final : public TargetInfo {
156public:
157 PPC64TargetInfo();
Rui Ueyama9398f862016-01-29 04:15:02 +0000158 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
159 int32_t Index, unsigned RelOff) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000160 bool needsGot(uint32_t Type, SymbolBody &S) const override;
161 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000162 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000163 uint64_t SA, uint64_t ZA = 0,
164 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000165 bool isRelRelative(uint32_t Type) const override;
166};
167
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000168class AArch64TargetInfo final : public TargetInfo {
169public:
170 AArch64TargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000171 unsigned getDynRel(unsigned Type) const override;
172 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
Rui Ueyama900e2d22016-01-29 03:51:49 +0000173 void writePltZero(uint8_t *Buf) const override;
Rui Ueyama9398f862016-01-29 04:15:02 +0000174 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
175 int32_t Index, unsigned RelOff) const override;
Rui Ueyama724d6252016-01-29 01:49:32 +0000176 unsigned getTlsGotRel(unsigned Type = -1) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000177 bool isTlsDynRel(unsigned Type, const SymbolBody &S) const override;
Rui Ueyama02dfd492015-12-17 01:18:40 +0000178 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000179 bool needsGot(uint32_t Type, SymbolBody &S) const override;
180 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000181 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
George Rimar48651482015-12-11 08:59:37 +0000182 uint64_t SA, uint64_t ZA = 0,
183 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000184};
185
Tom Stellard80efb162016-01-07 03:59:08 +0000186class AMDGPUTargetInfo final : public TargetInfo {
187public:
Rui Ueyama012eb782016-01-29 04:05:09 +0000188 AMDGPUTargetInfo() {}
Tom Stellard80efb162016-01-07 03:59:08 +0000189 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
190 uint64_t SA, uint64_t ZA = 0,
191 uint8_t *PairedLoc = nullptr) const override;
192};
193
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000194template <class ELFT> class MipsTargetInfo final : public TargetInfo {
195public:
196 MipsTargetInfo();
Rui Ueyamac516ae12016-01-29 02:33:45 +0000197 unsigned getDynRel(unsigned Type) const override;
Simon Atanasyan2287dc32016-02-10 19:57:19 +0000198 void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override;
199 void writePltZero(uint8_t *Buf) const override;
200 void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr,
201 int32_t Index, unsigned RelOff) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000202 void writeGotHeader(uint8_t *Buf) const override;
Simon Atanasyane1bfc2e2016-02-08 10:05:13 +0000203 bool needsCopyRel(uint32_t Type, const SymbolBody &S) const override;
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000204 bool needsGot(uint32_t Type, SymbolBody &S) const override;
205 bool needsPlt(uint32_t Type, SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000206 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +0000207 uint64_t S, uint64_t ZA = 0,
George Rimar48651482015-12-11 08:59:37 +0000208 uint8_t *PairedLoc = nullptr) const override;
Rui Ueyamac516ae12016-01-29 02:33:45 +0000209 bool isHintRel(uint32_t Type) const override;
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +0000210 bool isRelRelative(uint32_t Type) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000211};
212} // anonymous namespace
213
Rui Ueyama91004392015-10-13 16:08:15 +0000214TargetInfo *createTarget() {
215 switch (Config->EMachine) {
216 case EM_386:
217 return new X86TargetInfo();
218 case EM_AARCH64:
219 return new AArch64TargetInfo();
Tom Stellard80efb162016-01-07 03:59:08 +0000220 case EM_AMDGPU:
221 return new AMDGPUTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000222 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000223 switch (Config->EKind) {
224 case ELF32LEKind:
225 return new MipsTargetInfo<ELF32LE>();
226 case ELF32BEKind:
227 return new MipsTargetInfo<ELF32BE>();
228 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000229 fatal("Unsupported MIPS target");
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000230 }
Davide Italiano8c3444362016-01-11 19:45:33 +0000231 case EM_PPC:
232 return new PPCTargetInfo();
Rui Ueyama91004392015-10-13 16:08:15 +0000233 case EM_PPC64:
234 return new PPC64TargetInfo();
235 case EM_X86_64:
236 return new X86_64TargetInfo();
237 }
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000238 fatal("Unknown target machine");
Rui Ueyama91004392015-10-13 16:08:15 +0000239}
240
Rafael Espindola01205f72015-09-22 18:19:46 +0000241TargetInfo::~TargetInfo() {}
242
Rui Ueyamabaf16512016-01-29 00:20:12 +0000243bool TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000244 return false;
245}
246
Igor Kudrinf6f45472015-11-10 08:39:27 +0000247uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
248
Rui Ueyama02dfd492015-12-17 01:18:40 +0000249bool TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000250 return false;
251}
252
Rui Ueyama012eb782016-01-29 04:05:09 +0000253bool TargetInfo::isTlsLocalDynamicRel(unsigned Type) const {
254 return Type == TlsLocalDynamicRel;
255}
256
257bool TargetInfo::isTlsGlobalDynamicRel(unsigned Type) const {
258 return Type == TlsGlobalDynamicRel;
259}
260
261bool TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
262 return false;
263}
264
George Rimarbfb7bf72015-12-21 10:00:12 +0000265bool TargetInfo::isGotRelative(uint32_t Type) const { return false; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000266bool TargetInfo::isHintRel(uint32_t Type) const { return false; }
Rafael Espindolaae244002015-10-05 19:30:12 +0000267bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
Rui Ueyamac516ae12016-01-29 02:33:45 +0000268bool TargetInfo::isSizeRel(uint32_t Type) const { return false; }
George Rimar48651482015-12-11 08:59:37 +0000269
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000270bool TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000271
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000272bool TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const { return false; }
Rui Ueyama012eb782016-01-29 04:05:09 +0000273
Rui Ueyamac516ae12016-01-29 02:33:45 +0000274unsigned TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
275 uint64_t P, uint64_t SA,
276 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000277 return 0;
278}
George Rimar77d1cb12015-11-24 09:00:06 +0000279
Rafael Espindola7f074422015-09-22 21:35:51 +0000280X86TargetInfo::X86TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000281 CopyRel = R_386_COPY;
282 GotRel = R_386_GLOB_DAT;
283 PltRel = R_386_JUMP_SLOT;
284 IRelativeRel = R_386_IRELATIVE;
285 RelativeRel = R_386_RELATIVE;
286 TlsGotRel = R_386_TLS_TPOFF;
287 TlsGlobalDynamicRel = R_386_TLS_GD;
288 TlsLocalDynamicRel = R_386_TLS_LDM;
289 TlsModuleIndexRel = R_386_TLS_DTPMOD32;
290 TlsOffsetRel = R_386_TLS_DTPOFF32;
291 UseLazyBinding = true;
George Rimar77b77792015-11-25 22:15:01 +0000292 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000293 PltZeroSize = 16;
George Rimar77b77792015-11-25 22:15:01 +0000294}
295
Rui Ueyamac516ae12016-01-29 02:33:45 +0000296void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000297 write32le(Buf, Out<ELF32LE>::Dynamic->getVA());
298}
299
Rui Ueyamac516ae12016-01-29 02:33:45 +0000300void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000301 // Entries in .got.plt initially points back to the corresponding
302 // PLT entries with a fixed offset to skip the first instruction.
George Rimar77b77792015-11-25 22:15:01 +0000303 write32le(Buf, Plt + 6);
Rafael Espindola7f074422015-09-22 21:35:51 +0000304}
Rafael Espindola01205f72015-09-22 18:19:46 +0000305
Rui Ueyamac516ae12016-01-29 02:33:45 +0000306unsigned X86TargetInfo::getDynRel(unsigned Type) const {
George Rimard23970f2015-11-25 20:41:53 +0000307 if (Type == R_386_TLS_LE)
308 return R_386_TLS_TPOFF;
309 if (Type == R_386_TLS_LE_32)
310 return R_386_TLS_TPOFF32;
311 return Type;
312}
313
Rui Ueyama724d6252016-01-29 01:49:32 +0000314unsigned X86TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000315 if (Type == R_386_TLS_IE)
316 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +0000317 return TlsGotRel;
George Rimar6f17e092015-12-17 09:32:21 +0000318}
319
Rui Ueyamac516ae12016-01-29 02:33:45 +0000320bool X86TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar9db204a2015-12-02 09:58:20 +0000321 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 ||
322 Type == R_386_TLS_GOTIE)
George Rimard23970f2015-11-25 20:41:53 +0000323 return Config->Shared;
George Rimar6f17e092015-12-17 09:32:21 +0000324 if (Type == R_386_TLS_IE)
325 return canBePreempted(&S, true);
George Rimar2558e122015-12-09 09:55:54 +0000326 return Type == R_386_TLS_GD;
George Rimard23970f2015-11-25 20:41:53 +0000327}
328
Rui Ueyama900e2d22016-01-29 03:51:49 +0000329void X86TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar77b77792015-11-25 22:15:01 +0000330 // Executable files and shared object files have
331 // separate procedure linkage tables.
332 if (Config->Shared) {
333 const uint8_t V[] = {
Rui Ueyamaf53b1b72016-01-05 16:35:46 +0000334 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx)
Rui Ueyamacf375932016-01-29 23:58:03 +0000335 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx)
336 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000337 };
338 memcpy(Buf, V, sizeof(V));
339 return;
340 }
George Rimar648a2c32015-10-20 08:54:27 +0000341
George Rimar77b77792015-11-25 22:15:01 +0000342 const uint8_t PltData[] = {
343 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4)
Rui Ueyamacf375932016-01-29 23:58:03 +0000344 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8)
345 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop
George Rimar77b77792015-11-25 22:15:01 +0000346 };
347 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000348 uint32_t Got = Out<ELF32LE>::GotPlt->getVA();
Rui Ueyamacf375932016-01-29 23:58:03 +0000349 write32le(Buf + 2, Got + 4);
350 write32le(Buf + 8, Got + 8);
George Rimar77b77792015-11-25 22:15:01 +0000351}
352
Rui Ueyama9398f862016-01-29 04:15:02 +0000353void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
354 uint64_t PltEntryAddr, int32_t Index,
355 unsigned RelOff) const {
George Rimar77b77792015-11-25 22:15:01 +0000356 const uint8_t Inst[] = {
357 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx)
358 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset
359 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC
360 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000361 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyama9398f862016-01-29 04:15:02 +0000362
George Rimar77b77792015-11-25 22:15:01 +0000363 // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT
364 Buf[1] = Config->Shared ? 0xa3 : 0x25;
Rui Ueyama9398f862016-01-29 04:15:02 +0000365 uint32_t Got = UseLazyBinding ? Out<ELF32LE>::GotPlt->getVA()
366 : Out<ELF32LE>::Got->getVA();
367 write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr);
George Rimar77b77792015-11-25 22:15:01 +0000368 write32le(Buf + 7, RelOff);
Rui Ueyama62515452016-01-29 03:00:32 +0000369 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000370}
371
Rui Ueyama02dfd492015-12-17 01:18:40 +0000372bool X86TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimar70e25082015-11-25 11:27:40 +0000373 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
374 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
375 return SS->Sym.getType() == STT_OBJECT;
376 return false;
377}
378
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000379bool X86TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000380 if (S.isTls() && Type == R_386_TLS_GD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000381 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar6f17e092015-12-17 09:32:21 +0000382 if (Type == R_386_TLS_GOTIE || Type == R_386_TLS_IE)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000383 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000384 return Type == R_386_GOT32 || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000385}
386
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000387bool X86TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara07ff662015-12-21 10:12:06 +0000388 return isGnuIFunc<ELF32LE>(S) ||
389 (Type == R_386_PLT32 && canBePreempted(&S, true)) ||
George Rimarb72a9c62015-12-10 09:03:39 +0000390 (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000391}
392
George Rimarbfb7bf72015-12-21 10:00:12 +0000393bool X86TargetInfo::isGotRelative(uint32_t Type) const {
394 // This relocation does not require got entry,
395 // but it is relative to got and needs it to be created.
396 // Here we request for that.
397 return Type == R_386_GOTOFF;
398}
399
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000400void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000401 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000402 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000403 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000404 case R_386_32:
405 add32le(Loc, SA);
406 break;
George Rimarffb67352016-01-19 11:00:48 +0000407 case R_386_GOT32: {
408 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
409 Out<ELF32LE>::Got->getNumEntries() * 4;
410 checkInt<32>(V, Type);
411 add32le(Loc, V);
412 break;
413 }
George Rimarbfb7bf72015-12-21 10:00:12 +0000414 case R_386_GOTOFF:
Rui Ueyama66072272015-10-15 19:52:27 +0000415 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000416 break;
George Rimar13934772015-11-25 20:20:31 +0000417 case R_386_GOTPC:
418 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
419 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000420 case R_386_PC32:
George Rimarb72a9c62015-12-10 09:03:39 +0000421 case R_386_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000422 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000423 break;
George Rimar9db204a2015-12-02 09:58:20 +0000424 case R_386_TLS_GD:
425 case R_386_TLS_LDM:
426 case R_386_TLS_TPOFF: {
427 uint64_t V = SA - Out<ELF32LE>::Got->getVA() -
428 Out<ELF32LE>::Got->getNumEntries() * 4;
429 checkInt<32>(V, Type);
430 write32le(Loc, V);
431 break;
432 }
George Rimar6f17e092015-12-17 09:32:21 +0000433 case R_386_TLS_IE:
George Rimar9db204a2015-12-02 09:58:20 +0000434 case R_386_TLS_LDO_32:
435 write32le(Loc, SA);
436 break;
George Rimard23970f2015-11-25 20:41:53 +0000437 case R_386_TLS_LE:
438 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
439 break;
440 case R_386_TLS_LE_32:
441 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
442 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000443 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000444 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000445 }
446}
447
Rui Ueyamabaf16512016-01-29 00:20:12 +0000448bool X86TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000449 if (Config->Shared || (S && !S->isTls()))
George Rimar2558e122015-12-09 09:55:54 +0000450 return false;
451 return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM ||
452 Type == R_386_TLS_GD ||
George Rimar6f17e092015-12-17 09:32:21 +0000453 (Type == R_386_TLS_IE && !canBePreempted(S, true)) ||
George Rimar2558e122015-12-09 09:55:54 +0000454 (Type == R_386_TLS_GOTIE && !canBePreempted(S, true));
455}
456
Rui Ueyamac516ae12016-01-29 02:33:45 +0000457bool X86TargetInfo::needsDynRelative(unsigned Type) const {
George Rimar6f17e092015-12-17 09:32:21 +0000458 return Config->Shared && Type == R_386_TLS_IE;
459}
460
Rui Ueyamac516ae12016-01-29 02:33:45 +0000461unsigned X86TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
462 uint64_t P, uint64_t SA,
463 const SymbolBody *S) const {
George Rimar2558e122015-12-09 09:55:54 +0000464 switch (Type) {
465 case R_386_TLS_GD:
George Rimar237b2182016-01-22 18:02:28 +0000466 if (canBePreempted(S, true))
George Rimar2558e122015-12-09 09:55:54 +0000467 relocateTlsGdToIe(Loc, BufEnd, P, SA);
468 else
469 relocateTlsGdToLe(Loc, BufEnd, P, SA);
470 // The next relocation should be against __tls_get_addr, so skip it
471 return 1;
472 case R_386_TLS_GOTIE:
George Rimar6f17e092015-12-17 09:32:21 +0000473 case R_386_TLS_IE:
474 relocateTlsIeToLe(Type, Loc, BufEnd, P, SA);
George Rimar2558e122015-12-09 09:55:54 +0000475 return 0;
476 case R_386_TLS_LDM:
477 relocateTlsLdToLe(Loc, BufEnd, P, SA);
478 // The next relocation should be against __tls_get_addr, so skip it
479 return 1;
480 case R_386_TLS_LDO_32:
481 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
482 return 0;
483 }
484 llvm_unreachable("Unknown TLS optimization");
485}
486
487// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.1
488// IA-32 Linker Optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
489// how GD can be optimized to IE:
490// leal x@tlsgd(, %ebx, 1),
491// call __tls_get_addr@plt
492// Is converted to:
493// movl %gs:0, %eax
494// addl x@gotntpoff(%ebx), %eax
495void X86TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
496 uint64_t SA) const {
497 const uint8_t Inst[] = {
498 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
499 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax
500 };
501 memcpy(Loc - 3, Inst, sizeof(Inst));
502 relocateOne(Loc + 5, BufEnd, R_386_32, P,
503 SA - Out<ELF32LE>::Got->getVA() -
504 Out<ELF32LE>::Got->getNumEntries() * 4);
505}
506
507// GD can be optimized to LE:
508// leal x@tlsgd(, %ebx, 1),
509// call __tls_get_addr@plt
510// Can be converted to:
511// movl %gs:0,%eax
512// addl $x@ntpoff,%eax
513// But gold emits subl $foo@tpoff,%eax instead of addl.
514// These instructions are completely equal in behavior.
515// This method generates subl to be consistent with gold.
516void X86TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
517 uint64_t SA) const {
518 const uint8_t Inst[] = {
519 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax
520 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax
521 };
522 memcpy(Loc - 3, Inst, sizeof(Inst));
523 relocateOne(Loc + 5, BufEnd, R_386_32, P,
524 Out<ELF32LE>::TlsPhdr->p_memsz - SA);
525}
526
527// LD can be optimized to LE:
528// leal foo(%reg),%eax
529// call ___tls_get_addr
530// Is converted to:
531// movl %gs:0,%eax
532// nop
533// leal 0(%esi,1),%esi
534void X86TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
535 uint64_t SA) const {
536 const uint8_t Inst[] = {
537 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax
538 0x90, // nop
539 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi
540 };
541 memcpy(Loc - 2, Inst, sizeof(Inst));
542}
543
George Rimar6f17e092015-12-17 09:32:21 +0000544// In some conditions, relocations can be optimized to avoid using GOT.
545// This function does that for Initial Exec to Local Exec case.
546// Read "ELF Handling For Thread-Local Storage, 5.1
547// IA-32 Linker Optimizations" (http://www.akkadia.org/drepper/tls.pdf)
George Rimar2558e122015-12-09 09:55:54 +0000548// by Ulrich Drepper for details.
George Rimar6f17e092015-12-17 09:32:21 +0000549void X86TargetInfo::relocateTlsIeToLe(unsigned Type, uint8_t *Loc,
550 uint8_t *BufEnd, uint64_t P,
George Rimar2558e122015-12-09 09:55:54 +0000551 uint64_t SA) const {
George Rimar6f17e092015-12-17 09:32:21 +0000552 // Ulrich's document section 6.2 says that @gotntpoff can
553 // be used with MOVL or ADDL instructions.
554 // @indntpoff is similar to @gotntpoff, but for use in
555 // position dependent code.
George Rimar2558e122015-12-09 09:55:54 +0000556 uint8_t *Inst = Loc - 2;
George Rimar6f17e092015-12-17 09:32:21 +0000557 uint8_t *Op = Loc - 1;
George Rimar2558e122015-12-09 09:55:54 +0000558 uint8_t Reg = (Loc[-1] >> 3) & 7;
559 bool IsMov = *Inst == 0x8b;
George Rimar6f17e092015-12-17 09:32:21 +0000560 if (Type == R_386_TLS_IE) {
561 // For R_386_TLS_IE relocation we perform the next transformations:
562 // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX
563 // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG
564 // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG
565 // First one is special because when EAX is used the sequence is 5 bytes
566 // long, otherwise it is 6 bytes.
567 if (*Op == 0xa1) {
568 *Op = 0xb8;
569 } else {
570 *Inst = IsMov ? 0xc7 : 0x81;
571 *Op = 0xc0 | ((*Op >> 3) & 7);
572 }
573 } else {
574 // R_386_TLS_GOTIE relocation can be optimized to
575 // R_386_TLS_LE so that it does not use GOT.
576 // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG".
577 // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG"
578 // Note: gold converts to ADDL instead of LEAL.
579 *Inst = IsMov ? 0xc7 : 0x8d;
580 if (IsMov)
581 *Op = 0xc0 | ((*Op >> 3) & 7);
582 else
583 *Op = 0x80 | Reg | (Reg << 3);
584 }
George Rimar2558e122015-12-09 09:55:54 +0000585 relocateOne(Loc, BufEnd, R_386_TLS_LE, P, SA);
586}
587
Rafael Espindola7f074422015-09-22 21:35:51 +0000588X86_64TargetInfo::X86_64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000589 CopyRel = R_X86_64_COPY;
590 GotRel = R_X86_64_GLOB_DAT;
591 PltRel = R_X86_64_JUMP_SLOT;
592 RelativeRel = R_X86_64_RELATIVE;
593 IRelativeRel = R_X86_64_IRELATIVE;
594 TlsGotRel = R_X86_64_TPOFF64;
595 TlsLocalDynamicRel = R_X86_64_TLSLD;
596 TlsGlobalDynamicRel = R_X86_64_TLSGD;
597 TlsModuleIndexRel = R_X86_64_DTPMOD64;
598 TlsOffsetRel = R_X86_64_DTPOFF64;
599 UseLazyBinding = true;
George Rimar648a2c32015-10-20 08:54:27 +0000600 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +0000601 PltZeroSize = 16;
George Rimar648a2c32015-10-20 08:54:27 +0000602}
603
Rui Ueyamac516ae12016-01-29 02:33:45 +0000604void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const {
Igor Kudrin351b41d2015-11-16 17:44:08 +0000605 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
606}
607
Rui Ueyamac516ae12016-01-29 02:33:45 +0000608void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Rui Ueyamacf375932016-01-29 23:58:03 +0000609 // See comments in X86TargetInfo::writeGotPlt.
George Rimar648a2c32015-10-20 08:54:27 +0000610 write32le(Buf, Plt + 6);
611}
612
Rui Ueyama900e2d22016-01-29 03:51:49 +0000613void X86_64TargetInfo::writePltZero(uint8_t *Buf) const {
George Rimar648a2c32015-10-20 08:54:27 +0000614 const uint8_t PltData[] = {
615 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
616 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
617 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
618 };
619 memcpy(Buf, PltData, sizeof(PltData));
Rui Ueyama900e2d22016-01-29 03:51:49 +0000620 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
621 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
622 write32le(Buf + 2, Got - Plt + 2); // GOT+8
623 write32le(Buf + 8, Got - Plt + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000624}
Rafael Espindola01205f72015-09-22 18:19:46 +0000625
Rui Ueyama9398f862016-01-29 04:15:02 +0000626void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
627 uint64_t PltEntryAddr, int32_t Index,
628 unsigned RelOff) const {
George Rimar648a2c32015-10-20 08:54:27 +0000629 const uint8_t Inst[] = {
630 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
631 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
632 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
633 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000634 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000635
George Rimar648a2c32015-10-20 08:54:27 +0000636 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
637 write32le(Buf + 7, Index);
Rui Ueyama62515452016-01-29 03:00:32 +0000638 write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000639}
640
Rui Ueyama02dfd492015-12-17 01:18:40 +0000641bool X86_64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000642 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
643 Type == R_X86_64_64)
644 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
645 return SS->Sym.getType() == STT_OBJECT;
646 return false;
647}
648
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000649bool X86_64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000650 if (Type == R_X86_64_TLSGD)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000651 return Target->canRelaxTls(Type, &S) && canBePreempted(&S, true);
George Rimar77d1cb12015-11-24 09:00:06 +0000652 if (Type == R_X86_64_GOTTPOFF)
Rui Ueyamabaf16512016-01-29 00:20:12 +0000653 return !canRelaxTls(Type, &S);
Rui Ueyamac516ae12016-01-29 02:33:45 +0000654 return Type == R_X86_64_GOTPCREL || needsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000655}
656
Rui Ueyamac516ae12016-01-29 02:33:45 +0000657bool X86_64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar25411f252015-12-04 11:20:13 +0000658 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_TLSGD;
George Rimard23970f2015-11-25 20:41:53 +0000659}
660
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000661bool X86_64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Rui Ueyama02dfd492015-12-17 01:18:40 +0000662 if (needsCopyRel(Type, S))
George Rimarbc590fe2015-10-28 16:48:58 +0000663 return false;
George Rimara07ff662015-12-21 10:12:06 +0000664 if (isGnuIFunc<ELF64LE>(S))
665 return true;
George Rimarbc590fe2015-10-28 16:48:58 +0000666
Rafael Espindola01205f72015-09-22 18:19:46 +0000667 switch (Type) {
668 default:
669 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000670 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000671 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000672 case R_X86_64_PC32:
673 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000674 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000675 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000676 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000677 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000678 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
679 // libc.so.
680 //
681 // The general idea on how to handle such cases is to create a PLT entry
682 // and use that as the function value.
683 //
684 // For the static linking part, we just return true and everything else
685 // will use the the PLT entry as the address.
686 //
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000687 // The remaining problem is making sure pointer equality still works. We
688 // need the help of the dynamic linker for that. We let it know that we have
689 // a direct reference to a so symbol by creating an undefined symbol with a
690 // non zero st_value. Seeing that, the dynamic linker resolves the symbol to
691 // the value of the symbol we created. This is true even for got entries, so
692 // pointer equality is maintained. To avoid an infinite loop, the only entry
693 // that points to the real function is a dedicated got entry used by the
694 // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,
Rafael Espindola3c412e12015-09-30 12:30:58 +0000695 // R_386_JMP_SLOT, etc).
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000696 if (!S.isShared())
697 return false;
698 S.NeedsCopyOrPltAddr = true;
699 return true;
Rafael Espindola01205f72015-09-22 18:19:46 +0000700 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000701 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000702 }
703}
Rafael Espindolac4010882015-09-22 20:54:08 +0000704
Rafael Espindolaae244002015-10-05 19:30:12 +0000705bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
706 switch (Type) {
707 default:
708 return false;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000709 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000710 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000711 case R_X86_64_PC8:
712 case R_X86_64_PC16:
713 case R_X86_64_PC32:
714 case R_X86_64_PC64:
715 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000716 return true;
717 }
718}
719
Rui Ueyamac516ae12016-01-29 02:33:45 +0000720bool X86_64TargetInfo::isSizeRel(uint32_t Type) const {
Rui Ueyama3a7c2f62016-01-08 00:13:23 +0000721 return Type == R_X86_64_SIZE32 || Type == R_X86_64_SIZE64;
George Rimar48651482015-12-11 08:59:37 +0000722}
723
Rui Ueyamabaf16512016-01-29 00:20:12 +0000724bool X86_64TargetInfo::canRelaxTls(unsigned Type, const SymbolBody *S) const {
Rui Ueyama62d0e322015-12-17 00:04:18 +0000725 if (Config->Shared || (S && !S->isTls()))
George Rimar77d1cb12015-11-24 09:00:06 +0000726 return false;
George Rimar25411f252015-12-04 11:20:13 +0000727 return Type == R_X86_64_TLSGD || Type == R_X86_64_TLSLD ||
728 Type == R_X86_64_DTPOFF32 ||
George Rimar6713cf82015-11-25 21:46:05 +0000729 (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true));
730}
731
732// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
733// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
734// how LD can be optimized to LE:
735// leaq bar@tlsld(%rip), %rdi
736// callq __tls_get_addr@PLT
737// leaq bar@dtpoff(%rax), %rcx
738// Is converted to:
739// .word 0x6666
740// .byte 0x66
741// mov %fs:0,%rax
742// leaq bar@tpoff(%rax), %rcx
743void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd,
744 uint64_t P, uint64_t SA) const {
745 const uint8_t Inst[] = {
746 0x66, 0x66, //.word 0x6666
747 0x66, //.byte 0x66
748 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax
749 };
750 memcpy(Loc - 3, Inst, sizeof(Inst));
751}
752
753// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
754// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
755// how GD can be optimized to LE:
756// .byte 0x66
757// leaq x@tlsgd(%rip), %rdi
758// .word 0x6666
759// rex64
760// call __tls_get_addr@plt
761// Is converted to:
762// mov %fs:0x0,%rax
763// lea x@tpoff,%rax
764void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd,
765 uint64_t P, uint64_t SA) const {
766 const uint8_t Inst[] = {
767 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
768 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax
769 };
770 memcpy(Loc - 4, Inst, sizeof(Inst));
771 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA);
George Rimar77d1cb12015-11-24 09:00:06 +0000772}
773
George Rimar25411f252015-12-04 11:20:13 +0000774// "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5
775// x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows
776// how GD can be optimized to IE:
777// .byte 0x66
778// leaq x@tlsgd(%rip), %rdi
779// .word 0x6666
780// rex64
781// call __tls_get_addr@plt
782// Is converted to:
783// mov %fs:0x0,%rax
784// addq x@tpoff,%rax
785void X86_64TargetInfo::relocateTlsGdToIe(uint8_t *Loc, uint8_t *BufEnd,
786 uint64_t P, uint64_t SA) const {
787 const uint8_t Inst[] = {
788 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax
789 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax
790 };
791 memcpy(Loc - 4, Inst, sizeof(Inst));
792 relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF64, P + 12, SA);
793}
794
George Rimar77d1cb12015-11-24 09:00:06 +0000795// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
George Rimarc55b4e22015-12-07 16:54:56 +0000796// R_X86_64_TPOFF32 so that it does not use GOT.
George Rimar77d1cb12015-11-24 09:00:06 +0000797// This function does that. Read "ELF Handling For Thread-Local Storage,
798// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
799// by Ulrich Drepper for details.
George Rimar6713cf82015-11-25 21:46:05 +0000800void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd,
801 uint64_t P, uint64_t SA) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000802 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
803 // used in MOVQ or ADDQ instructions only.
804 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
805 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
806 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
807 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
808 uint8_t *Prefix = Loc - 3;
809 uint8_t *Inst = Loc - 2;
810 uint8_t *RegSlot = Loc - 1;
811 uint8_t Reg = Loc[-1] >> 3;
812 bool IsMov = *Inst == 0x8b;
813 bool RspAdd = !IsMov && Reg == 4;
814 // r12 and rsp registers requires special handling.
815 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
816 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
817 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
818 // The same true for rsp. So we convert to addq for them, saving 1 byte that
819 // we dont have.
820 if (RspAdd)
821 *Inst = 0x81;
822 else
823 *Inst = IsMov ? 0xc7 : 0x8d;
824 if (*Prefix == 0x4c)
825 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
826 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
827 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
828}
829
George Rimar6713cf82015-11-25 21:46:05 +0000830// This function applies a TLS relocation with an optimization as described
831// in the Ulrich's document. As a result of rewriting instructions at the
832// relocation target, relocations immediately follow the TLS relocation (which
833// would be applied to rewritten instructions) may have to be skipped.
834// This function returns a number of relocations that need to be skipped.
Rui Ueyamac516ae12016-01-29 02:33:45 +0000835unsigned X86_64TargetInfo::relaxTls(uint8_t *Loc, uint8_t *BufEnd,
836 uint32_t Type, uint64_t P, uint64_t SA,
837 const SymbolBody *S) const {
George Rimar6713cf82015-11-25 21:46:05 +0000838 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +0000839 case R_X86_64_DTPOFF32:
840 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
841 return 0;
George Rimar6713cf82015-11-25 21:46:05 +0000842 case R_X86_64_GOTTPOFF:
843 relocateTlsIeToLe(Loc, BufEnd, P, SA);
844 return 0;
George Rimar25411f252015-12-04 11:20:13 +0000845 case R_X86_64_TLSGD: {
George Rimar237b2182016-01-22 18:02:28 +0000846 if (canBePreempted(S, true))
George Rimar25411f252015-12-04 11:20:13 +0000847 relocateTlsGdToIe(Loc, BufEnd, P, SA);
848 else
849 relocateTlsGdToLe(Loc, BufEnd, P, SA);
George Rimar6713cf82015-11-25 21:46:05 +0000850 // The next relocation should be against __tls_get_addr, so skip it
851 return 1;
George Rimar25411f252015-12-04 11:20:13 +0000852 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000853 case R_X86_64_TLSLD:
854 relocateTlsLdToLe(Loc, BufEnd, P, SA);
855 // The next relocation should be against __tls_get_addr, so skip it
856 return 1;
George Rimar6713cf82015-11-25 21:46:05 +0000857 }
858 llvm_unreachable("Unknown TLS optimization");
859}
860
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000861void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +0000862 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +0000863 uint8_t *PairedLoc) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000864 switch (Type) {
Rui Ueyama3835b492015-10-23 16:13:27 +0000865 case R_X86_64_32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000866 checkUInt<32>(SA, Type);
867 write32le(Loc, SA);
868 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000869 case R_X86_64_32S:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000870 checkInt<32>(SA, Type);
Rui Ueyama66072272015-10-15 19:52:27 +0000871 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000872 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000873 case R_X86_64_64:
Rui Ueyamad41cb952016-02-10 22:00:21 +0000874 case R_X86_64_DTPOFF64:
Igor Kudrinb4a09272015-12-01 08:41:20 +0000875 write64le(Loc, SA);
876 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000877 case R_X86_64_DTPOFF32:
878 write32le(Loc, SA);
879 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +0000880 case R_X86_64_GOTPCREL:
881 case R_X86_64_PC32:
882 case R_X86_64_PLT32:
883 case R_X86_64_TLSGD:
884 case R_X86_64_TLSLD:
885 write32le(Loc, SA - P);
886 break;
George Rimar48651482015-12-11 08:59:37 +0000887 case R_X86_64_SIZE32:
888 write32le(Loc, ZA);
889 break;
890 case R_X86_64_SIZE64:
891 write64le(Loc, ZA);
892 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000893 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000894 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +0000895 checkInt<32>(Val, Type);
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000896 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000897 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000898 }
Igor Kudrinb4a09272015-12-01 08:41:20 +0000899 case R_X86_64_TPOFF64:
900 write32le(Loc, SA - P);
901 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000902 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000903 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000904 }
905}
906
Hal Finkel3c8cc672015-10-12 20:56:18 +0000907// Relocation masks following the #lo(value), #hi(value), #ha(value),
908// #higher(value), #highera(value), #highest(value), and #highesta(value)
909// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
910// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000911static uint16_t applyPPCLo(uint64_t V) { return V; }
912static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
913static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
914static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
915static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000916static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000917static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
918
Davide Italiano8c3444362016-01-11 19:45:33 +0000919PPCTargetInfo::PPCTargetInfo() {}
Davide Italiano8c3444362016-01-11 19:45:33 +0000920bool PPCTargetInfo::isRelRelative(uint32_t Type) const { return false; }
921
922void PPCTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
923 uint64_t P, uint64_t SA, uint64_t ZA,
924 uint8_t *PairedLoc) const {
925 switch (Type) {
926 case R_PPC_ADDR16_HA:
927 write16be(Loc, applyPPCHa(SA));
928 break;
929 case R_PPC_ADDR16_LO:
930 write16be(Loc, applyPPCLo(SA));
931 break;
932 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +0000933 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano8c3444362016-01-11 19:45:33 +0000934 }
935}
936
Rafael Espindolac4010882015-09-22 20:54:08 +0000937PPC64TargetInfo::PPC64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +0000938 GotRel = R_PPC64_GLOB_DAT;
939 RelativeRel = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000940 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000941
942 // We need 64K pages (at least under glibc/Linux, the loader won't
943 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000944 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000945
946 // The PPC64 ELF ABI v1 spec, says:
947 //
948 // It is normally desirable to put segments with different characteristics
949 // in separate 256 Mbyte portions of the address space, to give the
950 // operating system full paging flexibility in the 64-bit address space.
951 //
952 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
953 // use 0x10000000 as the starting address.
954 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000955}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000956
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000957uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000958 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
959 // order. The TOC starts where the first of these sections starts.
960
961 // FIXME: This obviously does not do the right thing when there is no .got
962 // section, but there is a .toc or .tocbss section.
963 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
964 if (!TocVA)
965 TocVA = Out<ELF64BE>::Plt->getVA();
966
967 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
968 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
969 // code (crt1.o) assumes that you can get from the TOC base to the
970 // start of the .toc section with only a single (signed) 16-bit relocation.
971 return TocVA + 0x8000;
972}
973
Rui Ueyama9398f862016-01-29 04:15:02 +0000974void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
975 uint64_t PltEntryAddr, int32_t Index,
976 unsigned RelOff) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000977 uint64_t Off = GotEntryAddr - getPPC64TocBase();
978
979 // FIXME: What we should do, in theory, is get the offset of the function
980 // descriptor in the .opd section, and use that as the offset from %r2 (the
981 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
982 // be a pointer to the function descriptor in the .opd section. Using
983 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
984
Hal Finkelfa92f682015-10-13 21:47:34 +0000985 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000986 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
987 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
988 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
989 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
990 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
991 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
992 write32be(Buf + 28, 0x4e800420); // bctr
993}
994
Rafael Espindolaa0a65f92016-02-09 15:11:01 +0000995bool PPC64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
Rui Ueyamac516ae12016-01-29 02:33:45 +0000996 if (needsPlt(Type, S))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000997 return true;
998
999 switch (Type) {
1000 default: return false;
1001 case R_PPC64_GOT16:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001002 case R_PPC64_GOT16_DS:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001003 case R_PPC64_GOT16_HA:
1004 case R_PPC64_GOT16_HI:
1005 case R_PPC64_GOT16_LO:
Hal Finkel3c8cc672015-10-12 20:56:18 +00001006 case R_PPC64_GOT16_LO_DS:
1007 return true;
1008 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001009}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001010
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001011bool PPC64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001012 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +00001013 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001014}
Hal Finkel3c8cc672015-10-12 20:56:18 +00001015
Hal Finkelbe0823d2015-10-12 20:58:52 +00001016bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
1017 switch (Type) {
1018 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +00001019 return true;
Hal Finkel00918622015-10-16 19:01:50 +00001020 case R_PPC64_ADDR64:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001021 case R_PPC64_TOC:
Hal Finkel00918622015-10-16 19:01:50 +00001022 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +00001023 }
1024}
1025
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001026void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
George Rimar48651482015-12-11 08:59:37 +00001027 uint64_t P, uint64_t SA, uint64_t ZA,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001028 uint8_t *PairedLoc) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +00001029 uint64_t TB = getPPC64TocBase();
1030
Hal Finkel3c8cc672015-10-12 20:56:18 +00001031 // For a TOC-relative relocation, adjust the addend and proceed in terms of
1032 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001033 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +00001034 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
1035 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001036 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
1037 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
Rafael Espindola826941a2015-10-15 18:19:39 +00001038 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
1039 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001040 default: break;
1041 }
1042
Hal Finkel3c8cc672015-10-12 20:56:18 +00001043 switch (Type) {
Igor Kudrinb4a09272015-12-01 08:41:20 +00001044 case R_PPC64_ADDR14: {
1045 checkAlignment<4>(SA, Type);
1046 // Preserve the AA/LK bits in the branch instruction
1047 uint8_t AALK = Loc[3];
1048 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
1049 break;
1050 }
Hal Finkel3c8cc672015-10-12 20:56:18 +00001051 case R_PPC64_ADDR16:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001052 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001053 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001054 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001055 case R_PPC64_ADDR16_DS:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001056 checkInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001057 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001058 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001059 case R_PPC64_ADDR16_HA:
1060 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001061 break;
1062 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001063 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001064 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001065 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001066 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001067 break;
1068 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001069 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001070 break;
1071 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001072 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001073 break;
1074 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001075 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001076 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001077 case R_PPC64_ADDR16_LO:
1078 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001079 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001080 case R_PPC64_ADDR16_LO_DS:
1081 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +00001082 break;
1083 case R_PPC64_ADDR32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001084 checkInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001085 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001086 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001087 case R_PPC64_ADDR64:
1088 write64be(Loc, SA);
1089 break;
1090 case R_PPC64_REL16_HA:
1091 write16be(Loc, applyPPCHa(SA - P));
1092 break;
1093 case R_PPC64_REL16_HI:
1094 write16be(Loc, applyPPCHi(SA - P));
1095 break;
1096 case R_PPC64_REL16_LO:
1097 write16be(Loc, applyPPCLo(SA - P));
1098 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +00001099 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +00001100 // If we have an undefined weak symbol, we might get here with a symbol
1101 // address of zero. That could overflow, but the code must be unreachable,
1102 // so don't bother doing anything at all.
1103 if (!SA)
1104 break;
1105
Hal Finkeldaedc122015-10-12 23:16:53 +00001106 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
1107 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001108 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001109
1110 if (!InPlt && Out<ELF64BE>::Opd) {
1111 // If this is a local call, and we currently have the address of a
1112 // function-descriptor, get the underlying code address instead.
1113 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
1114 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001115 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +00001116
1117 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +00001118 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +00001119 }
1120
Hal Finkel3c8cc672015-10-12 20:56:18 +00001121 uint32_t Mask = 0x03FFFFFC;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001122 checkInt<24>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001123 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +00001124
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001125 uint32_t Nop = 0x60000000;
1126 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
1127 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +00001128 break;
1129 }
1130 case R_PPC64_REL32:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001131 checkInt<32>(SA - P, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001132 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001133 break;
1134 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001135 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +00001136 break;
Hal Finkel6f97c2b2015-10-16 21:55:40 +00001137 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001138 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001139 break;
1140 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001141 fatal("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +00001142 }
1143}
Rafael Espindola1d6063e2015-09-22 21:24:52 +00001144
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001145AArch64TargetInfo::AArch64TargetInfo() {
Rui Ueyama724d6252016-01-29 01:49:32 +00001146 CopyRel = R_AARCH64_COPY;
1147 IRelativeRel = R_AARCH64_IRELATIVE;
1148 GotRel = R_AARCH64_GLOB_DAT;
1149 PltRel = R_AARCH64_JUMP_SLOT;
1150 TlsGotRel = R_AARCH64_TLS_TPREL64;
1151 UseLazyBinding = true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001152 PltEntrySize = 16;
Rui Ueyama62515452016-01-29 03:00:32 +00001153 PltZeroSize = 32;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001154}
George Rimar648a2c32015-10-20 08:54:27 +00001155
Rui Ueyamac516ae12016-01-29 02:33:45 +00001156unsigned AArch64TargetInfo::getDynRel(unsigned Type) const {
Igor Kudrincfe47f52015-12-05 06:20:24 +00001157 if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64)
1158 return Type;
1159 StringRef S = getELFRelocationTypeName(EM_AARCH64, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001160 error("Relocation " + S + " cannot be used when making a shared object; "
Igor Kudrincfe47f52015-12-05 06:20:24 +00001161 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001162 // Keep it going with a dummy value so that we can find more reloc errors.
1163 return R_AARCH64_ABS32;
Igor Kudrincfe47f52015-12-05 06:20:24 +00001164}
1165
Rui Ueyamac516ae12016-01-29 02:33:45 +00001166void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001167 write64le(Buf, Out<ELF64LE>::Plt->getVA());
1168}
1169
Rui Ueyama900e2d22016-01-29 03:51:49 +00001170void AArch64TargetInfo::writePltZero(uint8_t *Buf) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001171 const uint8_t PltData[] = {
1172 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
1173 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
1174 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
1175 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
1176 0x20, 0x02, 0x1f, 0xd6, // br x17
1177 0x1f, 0x20, 0x03, 0xd5, // nop
1178 0x1f, 0x20, 0x03, 0xd5, // nop
1179 0x1f, 0x20, 0x03, 0xd5 // nop
1180 };
1181 memcpy(Buf, PltData, sizeof(PltData));
1182
Rui Ueyama900e2d22016-01-29 03:51:49 +00001183 uint64_t Got = Out<ELF64LE>::GotPlt->getVA();
1184 uint64_t Plt = Out<ELF64LE>::Plt->getVA();
1185 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, Plt + 4, Got + 16);
1186 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, Plt + 8,
1187 Got + 16);
1188 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, Plt + 12,
1189 Got + 16);
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001190}
1191
Rui Ueyama9398f862016-01-29 04:15:02 +00001192void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1193 uint64_t PltEntryAddr, int32_t Index,
1194 unsigned RelOff) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001195 const uint8_t Inst[] = {
1196 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
1197 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
1198 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
1199 0x20, 0x02, 0x1f, 0xd6 // br x17
1200 };
1201 memcpy(Buf, Inst, sizeof(Inst));
1202
1203 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
1204 GotEntryAddr);
1205 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
1206 GotEntryAddr);
1207 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
1208 GotEntryAddr);
1209}
1210
Rui Ueyama724d6252016-01-29 01:49:32 +00001211unsigned AArch64TargetInfo::getTlsGotRel(unsigned Type) const {
George Rimar3d737e42016-01-13 13:04:46 +00001212 if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1213 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC)
1214 return Type;
Rui Ueyama724d6252016-01-29 01:49:32 +00001215 return TlsGotRel;
George Rimar3d737e42016-01-13 13:04:46 +00001216}
1217
Rui Ueyamac516ae12016-01-29 02:33:45 +00001218bool AArch64TargetInfo::isTlsDynRel(unsigned Type, const SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001219 return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 ||
1220 Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC;
1221}
1222
Rui Ueyama02dfd492015-12-17 01:18:40 +00001223bool AArch64TargetInfo::needsCopyRel(uint32_t Type, const SymbolBody &S) const {
Igor Kudrin9606d192015-12-03 08:05:35 +00001224 if (Config->Shared)
1225 return false;
1226 switch (Type) {
1227 default:
1228 return false;
1229 case R_AARCH64_ABS16:
1230 case R_AARCH64_ABS32:
1231 case R_AARCH64_ABS64:
1232 case R_AARCH64_ADD_ABS_LO12_NC:
1233 case R_AARCH64_ADR_PREL_LO21:
1234 case R_AARCH64_ADR_PREL_PG_HI21:
1235 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001236 case R_AARCH64_LDST16_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001237 case R_AARCH64_LDST32_ABS_LO12_NC:
1238 case R_AARCH64_LDST64_ABS_LO12_NC:
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001239 case R_AARCH64_LDST128_ABS_LO12_NC:
Igor Kudrin9606d192015-12-03 08:05:35 +00001240 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
1241 return SS->Sym.getType() == STT_OBJECT;
1242 return false;
1243 }
1244}
1245
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001246bool AArch64TargetInfo::needsGot(uint32_t Type, SymbolBody &S) const {
George Rimar3d737e42016-01-13 13:04:46 +00001247 switch (Type) {
1248 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1249 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1250 case R_AARCH64_ADR_GOT_PAGE:
1251 case R_AARCH64_LD64_GOT_LO12_NC:
1252 return true;
1253 default:
Rui Ueyamac516ae12016-01-29 02:33:45 +00001254 return needsPlt(Type, S);
George Rimar3d737e42016-01-13 13:04:46 +00001255 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001256}
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001257
Rafael Espindolaa0a65f92016-02-09 15:11:01 +00001258bool AArch64TargetInfo::needsPlt(uint32_t Type, SymbolBody &S) const {
George Rimara4804352016-01-11 14:15:17 +00001259 if (isGnuIFunc<ELF64LE>(S))
1260 return true;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001261 switch (Type) {
1262 default:
1263 return false;
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001264 case R_AARCH64_CALL26:
George Rimar4102bfb2016-01-11 14:22:00 +00001265 case R_AARCH64_CONDBR19:
Igor Kudrinb4a09272015-12-01 08:41:20 +00001266 case R_AARCH64_JUMP26:
George Rimar1395dbd2016-01-11 14:27:05 +00001267 case R_AARCH64_TSTBR14:
Igor Kudrindb7de9f2015-11-17 18:01:30 +00001268 return canBePreempted(&S, true);
1269 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001270}
Davide Italiano1d750a62015-09-27 08:45:38 +00001271
Davide Italianoef4be6b2015-10-06 19:01:32 +00001272static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001273 uint32_t ImmLo = (Imm & 0x3) << 29;
1274 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
1275 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +00001276 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001277}
1278
Davide Italiano318ca222015-10-02 22:13:51 +00001279// Page(Expr) is the page address of the expression Expr, defined
1280// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +00001281// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +00001282static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +00001283 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001284}
1285
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001286void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001287 uint32_t Type, uint64_t P, uint64_t SA,
George Rimar48651482015-12-11 08:59:37 +00001288 uint64_t ZA, uint8_t *PairedLoc) const {
Davide Italiano1d750a62015-09-27 08:45:38 +00001289 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +00001290 case R_AARCH64_ABS16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001291 checkIntUInt<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001292 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001293 break;
1294 case R_AARCH64_ABS32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001295 checkIntUInt<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001296 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001297 break;
1298 case R_AARCH64_ABS64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001299 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +00001300 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +00001301 case R_AARCH64_ADD_ABS_LO12_NC:
Davide Italianoa7165742015-10-16 21:06:55 +00001302 // This relocation stores 12 bits and there's no instruction
1303 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001304 // of r_addend bitwise-or'ed Loc. This assumes that the addend
1305 // bits in Loc are zero.
1306 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +00001307 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001308 case R_AARCH64_ADR_GOT_PAGE: {
1309 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
1310 checkInt<33>(X, Type);
1311 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
1312 break;
1313 }
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001314 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001315 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001316 checkInt<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001317 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +00001318 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001319 }
George Rimar3d737e42016-01-13 13:04:46 +00001320 case R_AARCH64_ADR_PREL_PG_HI21:
1321 case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
Rafael Espindola826941a2015-10-15 18:19:39 +00001322 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001323 checkInt<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001324 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +00001325 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +00001326 }
Igor Kudrinb4a09272015-12-01 08:41:20 +00001327 case R_AARCH64_CALL26:
1328 case R_AARCH64_JUMP26: {
Igor Kudrinb34115b2015-11-13 03:26:59 +00001329 uint64_t X = SA - P;
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001330 checkInt<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +00001331 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
1332 break;
1333 }
George Rimar4102bfb2016-01-11 14:22:00 +00001334 case R_AARCH64_CONDBR19: {
1335 uint64_t X = SA - P;
1336 checkInt<21>(X, Type);
1337 or32le(Loc, (X & 0x1FFFFC) << 3);
1338 break;
1339 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001340 case R_AARCH64_LD64_GOT_LO12_NC:
George Rimar3d737e42016-01-13 13:04:46 +00001341 case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
Igor Kudrin9b7e7db2015-11-26 09:49:44 +00001342 checkAlignment<8>(SA, Type);
Igor Kudrin5d2bffd2015-11-24 06:48:31 +00001343 or32le(Loc, (SA & 0xFF8) << 7);
1344 break;
Davide Italiano0d4fbae2016-01-14 01:30:21 +00001345 case R_AARCH64_LDST128_ABS_LO12_NC:
1346 or32le(Loc, (SA & 0x0FF8) << 6);
1347 break;
Davide Italiano2dfc5fd2016-01-15 01:49:51 +00001348 case R_AARCH64_LDST16_ABS_LO12_NC:
1349 or32le(Loc, (SA & 0x0FFC) << 9);
1350 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +00001351 case R_AARCH64_LDST8_ABS_LO12_NC:
Davide Italianodc67f9b2015-11-20 21:35:38 +00001352 or32le(Loc, (SA & 0xFFF) << 10);
1353 break;
Igor Kudrinb4a09272015-12-01 08:41:20 +00001354 case R_AARCH64_LDST32_ABS_LO12_NC:
1355 or32le(Loc, (SA & 0xFFC) << 8);
1356 break;
1357 case R_AARCH64_LDST64_ABS_LO12_NC:
1358 or32le(Loc, (SA & 0xFF8) << 7);
1359 break;
Davide Italiano3300b792015-10-29 19:55:59 +00001360 case R_AARCH64_PREL16:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001361 checkIntUInt<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001362 write16le(Loc, SA - P);
1363 break;
1364 case R_AARCH64_PREL32:
Igor Kudrinfea8ed52015-11-26 10:05:24 +00001365 checkIntUInt<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +00001366 write32le(Loc, SA - P);
1367 break;
Davide Italianob12d6682015-10-28 16:14:18 +00001368 case R_AARCH64_PREL64:
Davide Italianob12d6682015-10-28 16:14:18 +00001369 write64le(Loc, SA - P);
1370 break;
George Rimar1395dbd2016-01-11 14:27:05 +00001371 case R_AARCH64_TSTBR14: {
1372 uint64_t X = SA - P;
1373 checkInt<16>(X, Type);
1374 or32le(Loc, (X & 0xFFFC) << 3);
1375 break;
1376 }
Davide Italiano1d750a62015-09-27 08:45:38 +00001377 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001378 fatal("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +00001379 }
1380}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001381
Rui Ueyama1300e6b2016-01-07 20:34:16 +00001382// Implementing relocations for AMDGPU is low priority since most
1383// programs don't use relocations now. Thus, this function is not
1384// actually called (relocateOne is called for each relocation).
1385// That's why the AMDGPU port works without implementing this function.
Tom Stellard80efb162016-01-07 03:59:08 +00001386void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
1387 uint64_t P, uint64_t SA, uint64_t ZA,
1388 uint8_t *PairedLoc) const {
1389 llvm_unreachable("not implemented");
1390}
1391
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001392template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001393 GotHeaderEntriesNum = 2;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001394 GotPltHeaderEntriesNum = 2;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001395 PageSize = 65536;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001396 PltEntrySize = 16;
1397 PltZeroSize = 32;
1398 UseLazyBinding = true;
Simon Atanasyaneae66c02016-02-10 10:08:39 +00001399 CopyRel = R_MIPS_COPY;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001400 PltRel = R_MIPS_JUMP_SLOT;
Rui Ueyama724d6252016-01-29 01:49:32 +00001401 RelativeRel = R_MIPS_REL32;
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001402}
1403
1404template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001405unsigned MipsTargetInfo<ELFT>::getDynRel(unsigned Type) const {
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001406 if (Type == R_MIPS_32 || Type == R_MIPS_64)
1407 return R_MIPS_REL32;
1408 StringRef S = getELFRelocationTypeName(EM_MIPS, Type);
Rui Ueyama21923992016-02-01 23:28:21 +00001409 error("Relocation " + S + " cannot be used when making a shared object; "
Simon Atanasyanca558ea2016-01-14 21:34:50 +00001410 "recompile with -fPIC.");
Rui Ueyama21923992016-02-01 23:28:21 +00001411 // Keep it going with a dummy value so that we can find more reloc errors.
1412 return R_MIPS_32;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001413}
1414
1415template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001416void MipsTargetInfo<ELFT>::writeGotHeader(uint8_t *Buf) const {
Rui Ueyama24e39522015-12-03 20:57:45 +00001417 typedef typename ELFFile<ELFT>::Elf_Off Elf_Off;
Rui Ueyama8364c622016-01-29 22:55:38 +00001418 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
1419
1420 // Set the MSB of the second GOT slot. This is not required by any
1421 // MIPS ABI documentation, though.
1422 //
1423 // There is a comment in glibc saying that "The MSB of got[1] of a
1424 // gnu object is set to identify gnu objects," and in GNU gold it
1425 // says "the second entry will be used by some runtime loaders".
1426 // But how this field is being used is unclear.
1427 //
1428 // We are not really willing to mimic other linkers behaviors
1429 // without understanding why they do that, but because all files
1430 // generated by GNU tools have this special GOT value, and because
1431 // we've been doing this for years, it is probably a safe bet to
1432 // keep doing this for now. We really need to revisit this to see
1433 // if we had to do this.
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001434 auto *P = reinterpret_cast<Elf_Off *>(Buf);
Rui Ueyama8364c622016-01-29 22:55:38 +00001435 P[1] = uintX_t(1) << (ELFT::Is64Bits ? 63 : 31);
Simon Atanasyan49829a12015-09-29 05:34:03 +00001436}
1437
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001438template <class ELFT>
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001439void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const {
1440 write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +00001441}
Simon Atanasyan49829a12015-09-29 05:34:03 +00001442
Simon Atanasyan35031192015-12-15 06:06:34 +00001443static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; }
Simon Atanasyan2cd670d2015-12-13 06:49:01 +00001444
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001445template <endianness E, uint8_t BSIZE, uint8_t SHIFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001446static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t P,
Simon Atanasyan62313912016-02-10 10:08:35 +00001447 uint64_t S) {
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001448 uint32_t Mask = 0xffffffff >> (32 - BSIZE);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001449 uint32_t Instr = read32<E>(Loc);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001450 int64_t A = SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT);
1451 if (SHIFT > 0)
Simon Atanasyan62313912016-02-10 10:08:35 +00001452 checkAlignment<(1 << SHIFT)>(S + A, Type);
1453 int64_t V = S + A - P;
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001454 checkInt<BSIZE + SHIFT>(V, Type);
1455 write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001456}
1457
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001458template <endianness E>
1459static void applyMipsHi16Reloc(uint8_t *Loc, uint64_t S, int64_t A) {
1460 uint32_t Instr = read32<E>(Loc);
1461 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + A));
1462}
1463
1464template <class ELFT>
1465void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const {
1466 const endianness E = ELFT::TargetEndianness;
1467 write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0])
1468 write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28)
1469 write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0])
1470 write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28
1471 write32<E>(Buf + 16, 0x03e07825); // move $15, $31
1472 write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2
1473 write32<E>(Buf + 24, 0x0320f809); // jalr $25
1474 write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2
1475 uint64_t Got = Out<ELFT>::GotPlt->getVA();
1476 uint64_t Plt = Out<ELFT>::Plt->getVA();
1477 applyMipsHi16Reloc<E>(Buf, Got, 0);
1478 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, Plt + 4, Got);
1479 relocateOne(Buf + 8, Buf + 12, R_MIPS_LO16, Plt + 8, Got);
1480}
1481
1482template <class ELFT>
1483void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr,
1484 uint64_t PltEntryAddr, int32_t Index,
1485 unsigned RelOff) const {
1486 const endianness E = ELFT::TargetEndianness;
1487 write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry)
1488 write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15)
1489 write32<E>(Buf + 8, 0x03200008); // jr $25
1490 write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry)
1491 applyMipsHi16Reloc<E>(Buf, GotEntryAddr, 0);
1492 relocateOne(Buf + 4, Buf + 8, R_MIPS_LO16, PltEntryAddr + 4, GotEntryAddr);
1493 relocateOne(Buf + 12, Buf + 16, R_MIPS_LO16, PltEntryAddr + 8, GotEntryAddr);
1494}
1495
1496template <class ELFT>
1497bool MipsTargetInfo<ELFT>::needsCopyRel(uint32_t Type,
1498 const SymbolBody &S) const {
1499 if (Config->Shared)
1500 return false;
1501 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1502 if (auto *SS = dyn_cast<SharedSymbol<ELFT>>(&S))
1503 return SS->Sym.getType() == STT_OBJECT;
1504 return false;
1505}
1506
1507template <class ELFT>
1508bool MipsTargetInfo<ELFT>::needsGot(uint32_t Type, SymbolBody &S) const {
1509 return needsPlt(Type, S) || Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
1510}
1511
1512template <class ELFT>
1513bool MipsTargetInfo<ELFT>::needsPlt(uint32_t Type, SymbolBody &S) const {
1514 if (needsCopyRel(Type, S))
1515 return false;
1516 if (Type == R_MIPS_26 && canBePreempted(&S, false))
1517 return true;
1518 if (Type == R_MIPS_HI16 || Type == R_MIPS_LO16 || isRelRelative(Type))
1519 return S.isShared();
1520 return false;
1521}
1522
Simon Atanasyan9c2d7882015-10-14 14:24:46 +00001523template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +00001524void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
Simon Atanasyan62313912016-02-10 10:08:35 +00001525 uint32_t Type, uint64_t P, uint64_t S,
George Rimar48651482015-12-11 08:59:37 +00001526 uint64_t ZA, uint8_t *PairedLoc) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +00001527 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001528 switch (Type) {
1529 case R_MIPS_32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001530 add32<E>(Loc, S);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001531 break;
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001532 case R_MIPS_26: {
1533 uint32_t Instr = read32<E>(Loc);
1534 // FIXME (simon): If the relocation target symbol is not a PLT entry
1535 // we should use another expression for calculation:
1536 // ((A << 2) | (P & 0xf0000000)) >> 2
1537 S += SignExtend64<28>((Instr & 0x3ffffff) << 2);
1538 write32<E>(Loc, (Instr & ~0x3ffffff) | (S >> 2));
1539 break;
1540 }
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001541 case R_MIPS_CALL16:
1542 case R_MIPS_GOT16: {
Simon Atanasyan62313912016-02-10 10:08:35 +00001543 int64_t V = S - getMipsGpAddr<ELFT>();
Rui Ueyama7ee3cf72015-12-03 20:59:51 +00001544 if (Type == R_MIPS_GOT16)
1545 checkInt<16>(V, Type);
1546 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
1547 break;
1548 }
Simon Atanasyan57830b62015-12-25 13:02:13 +00001549 case R_MIPS_GPREL16: {
1550 uint32_t Instr = read32<E>(Loc);
Simon Atanasyan62313912016-02-10 10:08:35 +00001551 int64_t V = S + SignExtend64<16>(Instr & 0xffff) - getMipsGpAddr<ELFT>();
Simon Atanasyan57830b62015-12-25 13:02:13 +00001552 checkInt<16>(V, Type);
1553 write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff));
1554 break;
1555 }
1556 case R_MIPS_GPREL32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001557 write32<E>(Loc, S + int32_t(read32<E>(Loc)) - getMipsGpAddr<ELFT>());
Simon Atanasyan57830b62015-12-25 13:02:13 +00001558 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001559 case R_MIPS_HI16: {
1560 uint32_t Instr = read32<E>(Loc);
1561 if (PairedLoc) {
1562 uint64_t AHL = ((Instr & 0xffff) << 16) +
Rui Ueyama24e39522015-12-03 20:57:45 +00001563 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001564 applyMipsHi16Reloc<E>(Loc, S, AHL);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001565 } else {
1566 warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16");
Simon Atanasyan2287dc32016-02-10 19:57:19 +00001567 applyMipsHi16Reloc<E>(Loc, S, 0);
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001568 }
1569 break;
1570 }
Simon Atanasyane4361852015-12-13 06:49:14 +00001571 case R_MIPS_JALR:
1572 // Ignore this optimization relocation for now
1573 break;
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001574 case R_MIPS_LO16: {
1575 uint32_t Instr = read32<E>(Loc);
Rui Ueyama24e39522015-12-03 20:57:45 +00001576 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001577 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL) & 0xffff));
Simon Atanasyan09b3e362015-12-01 21:24:45 +00001578 break;
1579 }
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001580 case R_MIPS_PC16:
Simon Atanasyan62313912016-02-10 10:08:35 +00001581 applyMipsPcReloc<E, 16, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001582 break;
1583 case R_MIPS_PC19_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001584 applyMipsPcReloc<E, 19, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001585 break;
1586 case R_MIPS_PC21_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001587 applyMipsPcReloc<E, 21, 2>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001588 break;
1589 case R_MIPS_PC26_S2:
Simon Atanasyan62313912016-02-10 10:08:35 +00001590 applyMipsPcReloc<E, 26, 2>(Loc, Type, P, S);
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001591 break;
1592 case R_MIPS_PC32:
Simon Atanasyan62313912016-02-10 10:08:35 +00001593 applyMipsPcReloc<E, 32, 0>(Loc, Type, P, S);
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001594 break;
1595 case R_MIPS_PCHI16: {
1596 uint32_t Instr = read32<E>(Loc);
1597 if (PairedLoc) {
1598 uint64_t AHL = ((Instr & 0xffff) << 16) +
1599 SignExtend64<16>(read32<E>(PairedLoc) & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001600 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S + AHL - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001601 } else {
1602 warning("Can't find matching R_MIPS_PCLO16 relocation for R_MIPS_PCHI16");
Simon Atanasyan62313912016-02-10 10:08:35 +00001603 write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(S - P));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001604 }
1605 break;
1606 }
1607 case R_MIPS_PCLO16: {
1608 uint32_t Instr = read32<E>(Loc);
1609 int64_t AHL = SignExtend64<16>(Instr & 0xffff);
Simon Atanasyan62313912016-02-10 10:08:35 +00001610 write32<E>(Loc, (Instr & 0xffff0000) | ((S + AHL - P) & 0xffff));
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001611 break;
1612 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001613 default:
Rui Ueyama64cfffd2016-01-28 18:40:06 +00001614 fatal("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +00001615 }
1616}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001617
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001618template <class ELFT>
Rui Ueyamac516ae12016-01-29 02:33:45 +00001619bool MipsTargetInfo<ELFT>::isHintRel(uint32_t Type) const {
Simon Atanasyan682aeea2016-01-14 20:42:09 +00001620 return Type == R_MIPS_JALR;
1621}
1622
1623template <class ELFT>
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001624bool MipsTargetInfo<ELFT>::isRelRelative(uint32_t Type) const {
1625 switch (Type) {
1626 default:
1627 return false;
1628 case R_MIPS_PC16:
1629 case R_MIPS_PC19_S2:
1630 case R_MIPS_PC21_S2:
1631 case R_MIPS_PC26_S2:
Simon Atanasyane364e2e2016-02-04 12:31:39 +00001632 case R_MIPS_PC32:
Simon Atanasyan0fc0acf2015-12-21 17:36:40 +00001633 case R_MIPS_PCHI16:
1634 case R_MIPS_PCLO16:
1635 return true;
1636 }
1637}
1638
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001639// _gp is a MIPS-specific ABI-defined symbol which points to
1640// a location that is relative to GOT. This function returns
1641// the value for the symbol.
Rui Ueyama24e39522015-12-03 20:57:45 +00001642template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() {
Rui Ueyama3f11c8c2015-12-24 08:41:12 +00001643 unsigned GPOffset = 0x7ff0;
1644 if (uint64_t V = Out<ELFT>::Got->getVA())
1645 return V + GPOffset;
1646 return 0;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001647}
1648
Rui Ueyama3c8d0492016-01-29 23:59:15 +00001649template bool isGnuIFunc<ELF32LE>(const SymbolBody &S);
1650template bool isGnuIFunc<ELF32BE>(const SymbolBody &S);
1651template bool isGnuIFunc<ELF64LE>(const SymbolBody &S);
1652template bool isGnuIFunc<ELF64BE>(const SymbolBody &S);
1653
Igor Kudrin15cd9ff2015-11-06 07:43:03 +00001654template uint32_t getMipsGpAddr<ELF32LE>();
1655template uint32_t getMipsGpAddr<ELF32BE>();
1656template uint64_t getMipsGpAddr<ELF64LE>();
1657template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +00001658}
1659}