blob: 5e3de699af676dfacb65916894a16a2b2ba921f2 [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
45namespace {
46class X86TargetInfo final : public TargetInfo {
47public:
48 X86TargetInfo();
George Rimard23970f2015-11-25 20:41:53 +000049 unsigned getDynReloc(unsigned Type) const override;
50 bool isTlsDynReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +000051 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
52 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
53 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000054 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000055 uint64_t PltEntryAddr, int32_t Index) const override;
George Rimar70e25082015-11-25 11:27:40 +000056 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000057 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000058 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000059 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000060 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000061};
62
63class X86_64TargetInfo final : public TargetInfo {
64public:
65 X86_64TargetInfo();
Igor Kudrine7ad0932015-11-17 17:47:53 +000066 unsigned getPltRefReloc(unsigned Type) const override;
George Rimard23970f2015-11-25 20:41:53 +000067 bool isTlsDynReloc(unsigned Type) const override;
Igor Kudrin351b41d2015-11-16 17:44:08 +000068 void writeGotPltHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +000069 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
70 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
71 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000072 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000073 uint64_t PltEntryAddr, int32_t Index) const override;
George Rimarbc590fe2015-10-28 16:48:58 +000074 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000075 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
76 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000077 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000078 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000079 bool isRelRelative(uint32_t Type) const override;
George Rimar77d1cb12015-11-24 09:00:06 +000080 bool isTlsOptimized(unsigned Type, const SymbolBody &S) const override;
81 void relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
82 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000083};
84
85class PPC64TargetInfo final : public TargetInfo {
86public:
87 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000088 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
89 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
90 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000091 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000092 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000093 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
94 bool relocNeedsPlt(uint32_t Type, const 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,
Rui Ueyama66072272015-10-15 19:52:27 +000096 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000097 bool isRelRelative(uint32_t Type) const override;
98};
99
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000100class AArch64TargetInfo final : public TargetInfo {
101public:
102 AArch64TargetInfo();
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000103 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000104 unsigned getPltRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000105 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
106 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
107 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000108 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000109 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000110 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
111 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000112 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000113 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000114};
115
116template <class ELFT> class MipsTargetInfo final : public TargetInfo {
117public:
118 MipsTargetInfo();
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000119 unsigned getGotRefReloc(unsigned Type) const override;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000120 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000121 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
122 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
123 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000124 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000125 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000126 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
127 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000128 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000129 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000130};
131} // anonymous namespace
132
Rui Ueyama91004392015-10-13 16:08:15 +0000133TargetInfo *createTarget() {
134 switch (Config->EMachine) {
135 case EM_386:
136 return new X86TargetInfo();
137 case EM_AARCH64:
138 return new AArch64TargetInfo();
139 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000140 switch (Config->EKind) {
141 case ELF32LEKind:
142 return new MipsTargetInfo<ELF32LE>();
143 case ELF32BEKind:
144 return new MipsTargetInfo<ELF32BE>();
145 default:
146 error("Unsupported MIPS target");
147 }
Rui Ueyama91004392015-10-13 16:08:15 +0000148 case EM_PPC64:
149 return new PPC64TargetInfo();
150 case EM_X86_64:
151 return new X86_64TargetInfo();
152 }
153 error("Unknown target machine");
154}
155
Rafael Espindola01205f72015-09-22 18:19:46 +0000156TargetInfo::~TargetInfo() {}
157
George Rimar77d1cb12015-11-24 09:00:06 +0000158bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody &S) const {
159 return false;
160}
161
Igor Kudrinf6f45472015-11-10 08:39:27 +0000162uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
163
George Rimarbc590fe2015-10-28 16:48:58 +0000164bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
165 return false;
166}
167
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000168unsigned TargetInfo::getGotRefReloc(unsigned Type) const { return GotRefReloc; }
169
Igor Kudrine7ad0932015-11-17 17:47:53 +0000170unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; }
Rafael Espindola227556e2015-10-14 16:15:46 +0000171
Rafael Espindolaae244002015-10-05 19:30:12 +0000172bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
173
George Rimar77d1cb12015-11-24 09:00:06 +0000174void TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P,
175 uint64_t SA) const {}
176
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000177void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
178
Igor Kudrin351b41d2015-11-16 17:44:08 +0000179void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {}
180
Rafael Espindola7f074422015-09-22 21:35:51 +0000181X86TargetInfo::X86TargetInfo() {
George Rimar70e25082015-11-25 11:27:40 +0000182 CopyReloc = R_386_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000183 PCRelReloc = R_386_PC32;
184 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000185 GotRefReloc = R_386_GOT32;
George Rimar648a2c32015-10-20 08:54:27 +0000186 PltReloc = R_386_JUMP_SLOT;
Rafael Espindola7f074422015-09-22 21:35:51 +0000187}
Rafael Espindola01205f72015-09-22 18:19:46 +0000188
George Rimard23970f2015-11-25 20:41:53 +0000189unsigned X86TargetInfo::getDynReloc(unsigned Type) const {
190 if (Type == R_386_TLS_LE)
191 return R_386_TLS_TPOFF;
192 if (Type == R_386_TLS_LE_32)
193 return R_386_TLS_TPOFF32;
194 return Type;
195}
196
197bool X86TargetInfo::isTlsDynReloc(unsigned Type) const {
198 if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32)
199 return Config->Shared;
200 return false;
201}
202
George Rimar648a2c32015-10-20 08:54:27 +0000203void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
204void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
205 uint64_t PltEntryAddr) const {}
206
Rafael Espindola01205f72015-09-22 18:19:46 +0000207void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000208 uint64_t PltEntryAddr, int32_t Index) const {
Rui Ueyamac58656c2015-10-13 16:59:30 +0000209 // jmpl *val; nop; nop
210 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000211 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +0000212 assert(isUInt<32>(GotEntryAddr));
213 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +0000214}
215
George Rimar70e25082015-11-25 11:27:40 +0000216bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
217 if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8)
218 if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S))
219 return SS->Sym.getType() == STT_OBJECT;
220 return false;
221}
222
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000223bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000224 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000225}
226
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000227bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000228 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000229}
230
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000231void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
232 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000233 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000234 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000235 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000236 break;
George Rimar13934772015-11-25 20:20:31 +0000237 case R_386_GOTPC:
238 add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P);
239 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000240 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000241 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000242 break;
243 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000244 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000245 break;
George Rimard23970f2015-11-25 20:41:53 +0000246 case R_386_TLS_LE:
247 write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz);
248 break;
249 case R_386_TLS_LE_32:
250 write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA);
251 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000252 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000253 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000254 }
255}
256
Rafael Espindola7f074422015-09-22 21:35:51 +0000257X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000258 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000259 PCRelReloc = R_X86_64_PC32;
260 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000261 GotRefReloc = R_X86_64_PC32;
George Rimar648a2c32015-10-20 08:54:27 +0000262 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000263 RelativeReloc = R_X86_64_RELATIVE;
George Rimar687138c2015-11-13 16:28:53 +0000264 TlsGotReloc = R_X86_64_TPOFF64;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000265 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000266 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000267 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000268 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000269 LazyRelocations = true;
270 PltEntrySize = 16;
271 PltZeroEntrySize = 16;
272}
273
Igor Kudrin351b41d2015-11-16 17:44:08 +0000274void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {
275 write64le(Buf, Out<ELF64LE>::Dynamic->getVA());
276}
277
George Rimar648a2c32015-10-20 08:54:27 +0000278void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
279 // Skip 6 bytes of "jmpq *got(%rip)"
280 write32le(Buf, Plt + 6);
281}
282
283void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
284 uint64_t PltEntryAddr) const {
285 const uint8_t PltData[] = {
286 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
287 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
288 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
289 };
290 memcpy(Buf, PltData, sizeof(PltData));
291 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
292 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000293}
Rafael Espindola01205f72015-09-22 18:19:46 +0000294
295void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000296 uint64_t PltEntryAddr,
297 int32_t Index) const {
298 const uint8_t Inst[] = {
299 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
300 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
301 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
302 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000303 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000304
George Rimar648a2c32015-10-20 08:54:27 +0000305 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
306 write32le(Buf + 7, Index);
307 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000308}
309
George Rimarbc590fe2015-10-28 16:48:58 +0000310bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
311 const SymbolBody &S) const {
312 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
313 Type == R_X86_64_64)
314 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
315 return SS->Sym.getType() == STT_OBJECT;
316 return false;
317}
318
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000319bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
George Rimar77d1cb12015-11-24 09:00:06 +0000320 if (Type == R_X86_64_GOTTPOFF)
321 return !isTlsOptimized(Type, S);
George Rimar687138c2015-11-13 16:28:53 +0000322 return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL ||
323 relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000324}
325
George Rimard23970f2015-11-25 20:41:53 +0000326bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const {
327 return Type == R_X86_64_GOTTPOFF;
328}
329
Igor Kudrine7ad0932015-11-17 17:47:53 +0000330unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000331 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000332 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000333 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000334}
335
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000336bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000337 if (relocNeedsCopy(Type, S))
338 return false;
339
Rafael Espindola01205f72015-09-22 18:19:46 +0000340 switch (Type) {
341 default:
342 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000343 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000344 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000345 case R_X86_64_PC32:
346 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000347 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000348 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000349 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000350 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000351 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
352 // libc.so.
353 //
354 // The general idea on how to handle such cases is to create a PLT entry
355 // and use that as the function value.
356 //
357 // For the static linking part, we just return true and everything else
358 // will use the the PLT entry as the address.
359 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000360 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000361 // still works. We need the help of the dynamic linker for that. We
362 // let it know that we have a direct reference to a so symbol by creating
363 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000364 // dynamic linker resolves the symbol to the value of the symbol we created.
365 // This is true even for got entries, so pointer equality is maintained.
366 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000367 // real function is a dedicated got entry used by the plt. That is
368 // identified by special relocation types (R_X86_64_JUMP_SLOT,
369 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000370 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000371 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000372 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000373 }
374}
Rafael Espindolac4010882015-09-22 20:54:08 +0000375
Rafael Espindolaae244002015-10-05 19:30:12 +0000376bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
377 switch (Type) {
378 default:
379 return false;
380 case R_X86_64_PC64:
381 case R_X86_64_PC32:
382 case R_X86_64_PC16:
383 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000384 case R_X86_64_PLT32:
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000385 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000386 case R_X86_64_DTPOFF64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000387 return true;
388 }
389}
390
George Rimar77d1cb12015-11-24 09:00:06 +0000391bool X86_64TargetInfo::isTlsOptimized(unsigned Type,
392 const SymbolBody &S) const {
393 if (Config->Shared || !S.isTLS())
394 return false;
395 return Type == R_X86_64_GOTTPOFF && !canBePreempted(&S, true);
396}
397
398// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
399// R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT.
400// This function does that. Read "ELF Handling For Thread-Local Storage,
401// 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf)
402// by Ulrich Drepper for details.
403void X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd,
404 uint64_t P, uint64_t SA) const {
405 // Ulrich's document section 6.5 says that @gottpoff(%rip) must be
406 // used in MOVQ or ADDQ instructions only.
407 // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG".
408 // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG"
409 // (if the register is not RSP/R12) or "ADDQ $foo, %RSP".
410 // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48.
411 uint8_t *Prefix = Loc - 3;
412 uint8_t *Inst = Loc - 2;
413 uint8_t *RegSlot = Loc - 1;
414 uint8_t Reg = Loc[-1] >> 3;
415 bool IsMov = *Inst == 0x8b;
416 bool RspAdd = !IsMov && Reg == 4;
417 // r12 and rsp registers requires special handling.
418 // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11
419 // result out is 7 bytes: 4d 8d 9b XX XX XX XX,
420 // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX.
421 // The same true for rsp. So we convert to addq for them, saving 1 byte that
422 // we dont have.
423 if (RspAdd)
424 *Inst = 0x81;
425 else
426 *Inst = IsMov ? 0xc7 : 0x8d;
427 if (*Prefix == 0x4c)
428 *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d;
429 *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3));
430 relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA);
431}
432
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000433void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
434 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000435 switch (Type) {
436 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000437 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000438 case R_X86_64_PLT32:
Michael J. Spencer1e225612015-11-11 01:00:24 +0000439 case R_X86_64_TLSLD:
Michael J. Spencer627ae702015-11-13 00:28:34 +0000440 case R_X86_64_TLSGD:
George Rimar687138c2015-11-13 16:28:53 +0000441 case R_X86_64_TPOFF64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000442 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000443 break;
444 case R_X86_64_64:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000445 case R_X86_64_DTPOFF64:
Rui Ueyama66072272015-10-15 19:52:27 +0000446 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000447 break;
Rui Ueyama3835b492015-10-23 16:13:27 +0000448 case R_X86_64_32:
Rafael Espindolac4010882015-09-22 20:54:08 +0000449 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000450 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000451 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000452 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000453 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000454 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000455 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000456 case R_X86_64_DTPOFF32:
457 write32le(Loc, SA);
458 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000459 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000460 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000461 if (!isInt<32>(Val))
462 error("R_X86_64_TPOFF32 out of range");
463 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000464 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000465 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000466 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000467 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000468 }
469}
470
Hal Finkel3c8cc672015-10-12 20:56:18 +0000471// Relocation masks following the #lo(value), #hi(value), #ha(value),
472// #higher(value), #highera(value), #highest(value), and #highesta(value)
473// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
474// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000475static uint16_t applyPPCLo(uint64_t V) { return V; }
476static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
477static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
478static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
479static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000480static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000481static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
482
Rafael Espindolac4010882015-09-22 20:54:08 +0000483PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000484 PCRelReloc = R_PPC64_REL24;
485 GotReloc = R_PPC64_GLOB_DAT;
486 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000487 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000488 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000489
490 // We need 64K pages (at least under glibc/Linux, the loader won't
491 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000492 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000493
494 // The PPC64 ELF ABI v1 spec, says:
495 //
496 // It is normally desirable to put segments with different characteristics
497 // in separate 256 Mbyte portions of the address space, to give the
498 // operating system full paging flexibility in the 64-bit address space.
499 //
500 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
501 // use 0x10000000 as the starting address.
502 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000503}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000504
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000505uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000506 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
507 // order. The TOC starts where the first of these sections starts.
508
509 // FIXME: This obviously does not do the right thing when there is no .got
510 // section, but there is a .toc or .tocbss section.
511 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
512 if (!TocVA)
513 TocVA = Out<ELF64BE>::Plt->getVA();
514
515 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
516 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
517 // code (crt1.o) assumes that you can get from the TOC base to the
518 // start of the .toc section with only a single (signed) 16-bit relocation.
519 return TocVA + 0x8000;
520}
521
George Rimar648a2c32015-10-20 08:54:27 +0000522void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
523void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
524 uint64_t PltEntryAddr) const {}
Rafael Espindolac4010882015-09-22 20:54:08 +0000525void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000526 uint64_t PltEntryAddr, int32_t Index) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000527 uint64_t Off = GotEntryAddr - getPPC64TocBase();
528
529 // FIXME: What we should do, in theory, is get the offset of the function
530 // descriptor in the .opd section, and use that as the offset from %r2 (the
531 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
532 // be a pointer to the function descriptor in the .opd section. Using
533 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
534
Hal Finkelfa92f682015-10-13 21:47:34 +0000535 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000536 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
537 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
538 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
539 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
540 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
541 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
542 write32be(Buf + 28, 0x4e800420); // bctr
543}
544
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000545bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000546 if (relocNeedsPlt(Type, S))
547 return true;
548
549 switch (Type) {
550 default: return false;
551 case R_PPC64_GOT16:
552 case R_PPC64_GOT16_LO:
553 case R_PPC64_GOT16_HI:
554 case R_PPC64_GOT16_HA:
555 case R_PPC64_GOT16_DS:
556 case R_PPC64_GOT16_LO_DS:
557 return true;
558 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000559}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000560
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000561bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000562 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000563 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000564}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000565
Hal Finkelbe0823d2015-10-12 20:58:52 +0000566bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
567 switch (Type) {
568 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000569 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000570 case R_PPC64_TOC:
571 case R_PPC64_ADDR64:
572 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000573 }
574}
575
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000576void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
577 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000578 uint64_t TB = getPPC64TocBase();
579
Hal Finkel3c8cc672015-10-12 20:56:18 +0000580 // For a TOC-relative relocation, adjust the addend and proceed in terms of
581 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000582 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000583 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
584 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
585 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
586 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
587 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
588 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000589 default: break;
590 }
591
Hal Finkel3c8cc672015-10-12 20:56:18 +0000592 switch (Type) {
593 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000594 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000595 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000596 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000597 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000598 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000599 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000600 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000601 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000602 break;
603 case R_PPC64_ADDR16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000604 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000605 break;
606 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000607 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000608 break;
609 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000610 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000611 break;
612 case R_PPC64_ADDR16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000613 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000614 break;
615 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000616 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000617 break;
618 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000619 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000620 break;
621 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000622 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000623 break;
624 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000625 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000626 break;
627 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000628 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000629 error("Improper alignment for relocation R_PPC64_ADDR14");
630
631 // Preserve the AA/LK bits in the branch instruction
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000632 uint8_t AALK = Loc[3];
633 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000634 break;
635 }
636 case R_PPC64_REL16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000637 write16be(Loc, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000638 break;
639 case R_PPC64_REL16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000640 write16be(Loc, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000641 break;
642 case R_PPC64_REL16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000643 write16be(Loc, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000644 break;
645 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000646 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000647 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000648 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000649 break;
650 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000651 // If we have an undefined weak symbol, we might get here with a symbol
652 // address of zero. That could overflow, but the code must be unreachable,
653 // so don't bother doing anything at all.
654 if (!SA)
655 break;
656
Hal Finkeldaedc122015-10-12 23:16:53 +0000657 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
658 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000659 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000660
661 if (!InPlt && Out<ELF64BE>::Opd) {
662 // If this is a local call, and we currently have the address of a
663 // function-descriptor, get the underlying code address instead.
664 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
665 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000666 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000667
668 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000669 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000670 }
671
Hal Finkel3c8cc672015-10-12 20:56:18 +0000672 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000673 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000674 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000675 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000676
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000677 uint32_t Nop = 0x60000000;
678 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
679 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000680 break;
681 }
682 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000683 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000684 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000685 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000686 break;
687 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000688 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000689 break;
690 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000691 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000692 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000693 break;
694 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000695 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000696 }
697}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000698
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000699AArch64TargetInfo::AArch64TargetInfo() {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000700 GotReloc = R_AARCH64_GLOB_DAT;
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000701 PltReloc = R_AARCH64_JUMP_SLOT;
702 LazyRelocations = true;
703 PltEntrySize = 16;
704 PltZeroEntrySize = 32;
705}
George Rimar648a2c32015-10-20 08:54:27 +0000706
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000707unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; }
708
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000709unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; }
710
711void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
712 write64le(Buf, Out<ELF64LE>::Plt->getVA());
713}
714
George Rimar648a2c32015-10-20 08:54:27 +0000715void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000716 uint64_t PltEntryAddr) const {
717 const uint8_t PltData[] = {
718 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]!
719 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2]))
720 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))]
721 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2]))
722 0x20, 0x02, 0x1f, 0xd6, // br x17
723 0x1f, 0x20, 0x03, 0xd5, // nop
724 0x1f, 0x20, 0x03, 0xd5, // nop
725 0x1f, 0x20, 0x03, 0xd5 // nop
726 };
727 memcpy(Buf, PltData, sizeof(PltData));
728
729 relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4,
730 GotEntryAddr + 16);
731 relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8,
732 GotEntryAddr + 16);
733 relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12,
734 GotEntryAddr + 16);
735}
736
Davide Italianocde93362015-09-26 00:32:04 +0000737void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000738 uint64_t PltEntryAddr,
739 int32_t Index) const {
740 const uint8_t Inst[] = {
741 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n]))
742 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))]
743 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n]))
744 0x20, 0x02, 0x1f, 0xd6 // br x17
745 };
746 memcpy(Buf, Inst, sizeof(Inst));
747
748 relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr,
749 GotEntryAddr);
750 relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4,
751 GotEntryAddr);
752 relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8,
753 GotEntryAddr);
754}
755
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000756bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
757 const SymbolBody &S) const {
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000758 return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC ||
759 relocNeedsPlt(Type, S);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000760}
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000761
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000762bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
763 const SymbolBody &S) const {
Igor Kudrindb7de9f2015-11-17 18:01:30 +0000764 switch (Type) {
765 default:
766 return false;
767 case R_AARCH64_JUMP26:
768 case R_AARCH64_CALL26:
769 return canBePreempted(&S, true);
770 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000771}
Davide Italiano1d750a62015-09-27 08:45:38 +0000772
Davide Italianoef4be6b2015-10-06 19:01:32 +0000773static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000774 uint32_t ImmLo = (Imm & 0x3) << 29;
775 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
776 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000777 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000778}
779
Davide Italiano318ca222015-10-02 22:13:51 +0000780// Page(Expr) is the page address of the expression Expr, defined
781// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000782// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000783static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000784 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000785}
786
Igor Kudrinee252de2015-11-23 17:16:09 +0000787template <unsigned N>
788static void checkAArch64OutOfRange(int64_t X, uint32_t Type) {
789 if (!isInt<N>(X))
790 error("Relocation " + getELFRelocationTypeName(EM_AARCH64, Type) +
791 " out of range");
792}
793
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000794void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
795 uint32_t Type, uint64_t P,
796 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000797 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000798 case R_AARCH64_ABS16:
Igor Kudrinee252de2015-11-23 17:16:09 +0000799 checkAArch64OutOfRange<16>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000800 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000801 break;
802 case R_AARCH64_ABS32:
Igor Kudrinee252de2015-11-23 17:16:09 +0000803 checkAArch64OutOfRange<32>(SA, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000804 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000805 break;
806 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000807 // No overflow check needed.
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000808 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000809 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000810 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000811 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000812 // This relocation stores 12 bits and there's no instruction
813 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000814 // of r_addend bitwise-or'ed Loc. This assumes that the addend
815 // bits in Loc are zero.
816 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000817 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000818 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000819 uint64_t X = SA - P;
Igor Kudrinee252de2015-11-23 17:16:09 +0000820 checkAArch64OutOfRange<21>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000821 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000822 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000823 }
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000824 case R_AARCH64_ADR_GOT_PAGE:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000825 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000826 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Igor Kudrinee252de2015-11-23 17:16:09 +0000827 checkAArch64OutOfRange<33>(X, Type);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000828 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000829 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000830 }
Igor Kudrinb34115b2015-11-13 03:26:59 +0000831 case R_AARCH64_JUMP26:
832 case R_AARCH64_CALL26: {
833 uint64_t X = SA - P;
Igor Kudrinee252de2015-11-23 17:16:09 +0000834 checkAArch64OutOfRange<28>(X, Type);
Igor Kudrinb34115b2015-11-13 03:26:59 +0000835 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
836 break;
837 }
Davide Italianodc67f9b2015-11-20 21:35:38 +0000838 case R_AARCH64_LDST32_ABS_LO12_NC:
839 // No overflow check needed.
840 or32le(Loc, (SA & 0xFFC) << 8);
841 break;
Igor Kudrin5d2bffd2015-11-24 06:48:31 +0000842 case R_AARCH64_LD64_GOT_LO12_NC:
843 if (SA & 0x7)
844 error("Relocation R_AARCH64_LD64_GOT_LO12_NC not aligned");
845 // No overflow check needed.
846 or32le(Loc, (SA & 0xFF8) << 7);
847 break;
Davide Italianobbcc7f62015-11-08 04:45:26 +0000848 case R_AARCH64_LDST64_ABS_LO12_NC:
849 // No overflow check needed.
850 or32le(Loc, (SA & 0xFF8) << 7);
851 break;
Davide Italianodc67f9b2015-11-20 21:35:38 +0000852 case R_AARCH64_LDST8_ABS_LO12_NC:
853 // No overflow check needed.
854 or32le(Loc, (SA & 0xFFF) << 10);
855 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000856 case R_AARCH64_PREL16:
Igor Kudrinee252de2015-11-23 17:16:09 +0000857 checkAArch64OutOfRange<16>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000858 write16le(Loc, SA - P);
859 break;
860 case R_AARCH64_PREL32:
Igor Kudrinee252de2015-11-23 17:16:09 +0000861 checkAArch64OutOfRange<32>(SA - P, Type);
Davide Italiano3300b792015-10-29 19:55:59 +0000862 write32le(Loc, SA - P);
863 break;
Davide Italianob12d6682015-10-28 16:14:18 +0000864 case R_AARCH64_PREL64:
865 // No overflow check needed.
866 write64le(Loc, SA - P);
867 break;
Davide Italiano1d750a62015-09-27 08:45:38 +0000868 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000869 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000870 }
871}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000872
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000873template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +0000874 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000875 GotRefReloc = R_MIPS_GOT16;
876 GotHeaderEntriesNum = 2;
877}
878
879template <class ELFT>
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000880unsigned MipsTargetInfo<ELFT>::getGotRefReloc(unsigned Type) const {
881 return Type;
882}
883
884template <class ELFT>
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000885void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
886 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
887 auto *P = reinterpret_cast<Elf_Off *>(Buf);
888 // Module pointer
889 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000890}
891
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000892template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000893void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
894template <class ELFT>
895void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
896 uint64_t PltEntryAddr) const {}
897template <class ELFT>
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000898void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000899 uint64_t PltEntryAddr, int32_t Index) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000900
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000901template <class ELFT>
902bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
903 const SymbolBody &S) const {
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000904 return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000905}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000906
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000907template <class ELFT>
908bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
909 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000910 return false;
911}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000912
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000913template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000914void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
915 uint32_t Type, uint64_t P,
916 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000917 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000918 switch (Type) {
919 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000920 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000921 break;
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000922 case R_MIPS_CALL16:
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000923 case R_MIPS_GOT16: {
924 int64_t V = SA - getMipsGpAddr<ELFT>();
Simon Atanasyan96306bb2015-11-25 20:58:52 +0000925 if (Type == R_MIPS_GOT16 && !isInt<16>(V))
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000926 error("Relocation R_MIPS_GOT16 out of range");
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000927 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000928 break;
929 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000930 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000931 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000932 }
933}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000934
935template <class ELFT>
936typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
937 const unsigned GPOffset = 0x7ff0;
938 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
939}
940
941template uint32_t getMipsGpAddr<ELF32LE>();
942template uint32_t getMipsGpAddr<ELF32BE>();
943template uint64_t getMipsGpAddr<ELF64LE>();
944template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +0000945}
946}