blob: cc033fcf9aa4bc4995e1768b48237497e0e38c7b [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 Rimar648a2c32015-10-20 08:54:27 +000049 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
50 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
51 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000052 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000053 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000054 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
55 bool relocPointsToGot(uint32_t Type) const override;
56 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000057 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000058 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000059};
60
61class X86_64TargetInfo final : public TargetInfo {
62public:
63 X86_64TargetInfo();
64 unsigned getPLTRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +000065 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
66 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
67 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000068 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000069 uint64_t PltEntryAddr, int32_t Index) const override;
George Rimarbc590fe2015-10-28 16:48:58 +000070 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000071 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
72 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000073 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000074 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000075 bool isRelRelative(uint32_t Type) const override;
76};
77
78class PPC64TargetInfo final : public TargetInfo {
79public:
80 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000081 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
82 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
83 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000084 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000085 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000086 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
87 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000088 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000089 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000090 bool isRelRelative(uint32_t Type) const override;
91};
92
Rui Ueyamaefc23de2015-10-14 21:30:32 +000093class AArch64TargetInfo final : public TargetInfo {
94public:
95 AArch64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000096 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
97 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
98 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000099 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000100 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000101 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
102 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000103 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000104 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000105};
106
107template <class ELFT> class MipsTargetInfo final : public TargetInfo {
108public:
109 MipsTargetInfo();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000110 void writeGotHeaderEntries(uint8_t *Buf) const override;
George Rimar648a2c32015-10-20 08:54:27 +0000111 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
112 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
113 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000114 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000115 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000116 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
117 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000118 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000119 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000120};
121} // anonymous namespace
122
Rui Ueyama91004392015-10-13 16:08:15 +0000123TargetInfo *createTarget() {
124 switch (Config->EMachine) {
125 case EM_386:
126 return new X86TargetInfo();
127 case EM_AARCH64:
128 return new AArch64TargetInfo();
129 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000130 switch (Config->EKind) {
131 case ELF32LEKind:
132 return new MipsTargetInfo<ELF32LE>();
133 case ELF32BEKind:
134 return new MipsTargetInfo<ELF32BE>();
135 default:
136 error("Unsupported MIPS target");
137 }
Rui Ueyama91004392015-10-13 16:08:15 +0000138 case EM_PPC64:
139 return new PPC64TargetInfo();
140 case EM_X86_64:
141 return new X86_64TargetInfo();
142 }
143 error("Unknown target machine");
144}
145
Rafael Espindola01205f72015-09-22 18:19:46 +0000146TargetInfo::~TargetInfo() {}
147
Igor Kudrinf6f45472015-11-10 08:39:27 +0000148uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; }
149
George Rimarbc590fe2015-10-28 16:48:58 +0000150bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
151 return false;
152}
153
Rafael Espindola227556e2015-10-14 16:15:46 +0000154unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; }
155
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000156bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
157
Rafael Espindolaae244002015-10-05 19:30:12 +0000158bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
159
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000160void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {}
161
Rafael Espindola7f074422015-09-22 21:35:51 +0000162X86TargetInfo::X86TargetInfo() {
163 PCRelReloc = R_386_PC32;
164 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000165 GotRefReloc = R_386_GOT32;
George Rimar648a2c32015-10-20 08:54:27 +0000166 PltReloc = R_386_JUMP_SLOT;
Rafael Espindola7f074422015-09-22 21:35:51 +0000167}
Rafael Espindola01205f72015-09-22 18:19:46 +0000168
George Rimar648a2c32015-10-20 08:54:27 +0000169void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
170void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
171 uint64_t PltEntryAddr) const {}
172
Rafael Espindola01205f72015-09-22 18:19:46 +0000173void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000174 uint64_t PltEntryAddr, int32_t Index) const {
Rui Ueyamac58656c2015-10-13 16:59:30 +0000175 // jmpl *val; nop; nop
176 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000177 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +0000178 assert(isUInt<32>(GotEntryAddr));
179 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +0000180}
181
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000182bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000183 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000184}
185
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000186bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
187 return Type == R_386_GOTPC;
188}
189
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000190bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000191 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000192}
193
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000194void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
195 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000196 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000197 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000198 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000199 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000200 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000201 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000202 break;
203 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000204 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000205 break;
206 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000207 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000208 }
209}
210
Rafael Espindola7f074422015-09-22 21:35:51 +0000211X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000212 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000213 PCRelReloc = R_X86_64_PC32;
214 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000215 GotRefReloc = R_X86_64_PC32;
George Rimar648a2c32015-10-20 08:54:27 +0000216 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000217 RelativeReloc = R_X86_64_RELATIVE;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000218 TlsLocalDynamicReloc = R_X86_64_TLSLD;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000219 TlsGlobalDynamicReloc = R_X86_64_TLSGD;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000220 TlsModuleIndexReloc = R_X86_64_DTPMOD64;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000221 TlsOffsetReloc = R_X86_64_DTPOFF64;
George Rimar648a2c32015-10-20 08:54:27 +0000222 LazyRelocations = true;
223 PltEntrySize = 16;
224 PltZeroEntrySize = 16;
225}
226
227void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
228 // Skip 6 bytes of "jmpq *got(%rip)"
229 write32le(Buf, Plt + 6);
230}
231
232void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
233 uint64_t PltEntryAddr) const {
234 const uint8_t PltData[] = {
235 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
236 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
237 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
238 };
239 memcpy(Buf, PltData, sizeof(PltData));
240 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
241 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000242}
Rafael Espindola01205f72015-09-22 18:19:46 +0000243
244void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000245 uint64_t PltEntryAddr,
246 int32_t Index) const {
247 const uint8_t Inst[] = {
248 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
249 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
250 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
251 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000252 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000253
George Rimar648a2c32015-10-20 08:54:27 +0000254 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
255 write32le(Buf + 7, Index);
256 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000257}
258
George Rimarbc590fe2015-10-28 16:48:58 +0000259bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
260 const SymbolBody &S) const {
261 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
262 Type == R_X86_64_64)
263 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
264 return SS->Sym.getType() == STT_OBJECT;
265 return false;
266}
267
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000268bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000269 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000270}
271
Rafael Espindola227556e2015-10-14 16:15:46 +0000272unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const {
George Rimar52687212015-10-28 18:33:08 +0000273 if (Type == R_X86_64_PLT32)
Rafael Espindola227556e2015-10-14 16:15:46 +0000274 return R_X86_64_PC32;
George Rimar52687212015-10-28 18:33:08 +0000275 return Type;
Rafael Espindola227556e2015-10-14 16:15:46 +0000276}
277
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000278bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000279 if (relocNeedsCopy(Type, S))
280 return false;
281
Rafael Espindola01205f72015-09-22 18:19:46 +0000282 switch (Type) {
283 default:
284 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000285 case R_X86_64_32:
George Rimar52687212015-10-28 18:33:08 +0000286 case R_X86_64_64:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000287 case R_X86_64_PC32:
288 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000289 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000290 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000291 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000292 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000293 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
294 // libc.so.
295 //
296 // The general idea on how to handle such cases is to create a PLT entry
297 // and use that as the function value.
298 //
299 // For the static linking part, we just return true and everything else
300 // will use the the PLT entry as the address.
301 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000302 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000303 // still works. We need the help of the dynamic linker for that. We
304 // let it know that we have a direct reference to a so symbol by creating
305 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000306 // dynamic linker resolves the symbol to the value of the symbol we created.
307 // This is true even for got entries, so pointer equality is maintained.
308 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000309 // real function is a dedicated got entry used by the plt. That is
310 // identified by special relocation types (R_X86_64_JUMP_SLOT,
311 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000312 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000313 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000314 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000315 }
316}
Rafael Espindolac4010882015-09-22 20:54:08 +0000317
Rafael Espindolaae244002015-10-05 19:30:12 +0000318bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
319 switch (Type) {
320 default:
321 return false;
322 case R_X86_64_PC64:
323 case R_X86_64_PC32:
324 case R_X86_64_PC16:
325 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000326 case R_X86_64_PLT32:
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000327 case R_X86_64_DTPOFF32:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000328 case R_X86_64_DTPOFF64:
Rafael Espindolaae244002015-10-05 19:30:12 +0000329 return true;
330 }
331}
332
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000333void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
334 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000335 switch (Type) {
336 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000337 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000338 case R_X86_64_PLT32:
Michael J. Spencer1e225612015-11-11 01:00:24 +0000339 case R_X86_64_TLSLD:
Michael J. Spencer627ae702015-11-13 00:28:34 +0000340 case R_X86_64_TLSGD:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000341 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000342 break;
343 case R_X86_64_64:
Michael J. Spencerac2307b2015-11-11 01:28:11 +0000344 case R_X86_64_DTPOFF64:
Rui Ueyama66072272015-10-15 19:52:27 +0000345 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000346 break;
Rui Ueyama3835b492015-10-23 16:13:27 +0000347 case R_X86_64_32:
Rafael Espindolac4010882015-09-22 20:54:08 +0000348 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000349 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000350 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000351 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000352 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000353 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000354 break;
Michael J. Spencera5d9d1f2015-11-11 01:27:58 +0000355 case R_X86_64_DTPOFF32:
356 write32le(Loc, SA);
357 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000358 case R_X86_64_TPOFF32: {
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000359 uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000360 if (!isInt<32>(Val))
361 error("R_X86_64_TPOFF32 out of range");
362 write32le(Loc, Val);
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000363 break;
Rafael Espindolaac1c0f82015-11-05 15:22:26 +0000364 }
Rafael Espindolac4010882015-09-22 20:54:08 +0000365 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000366 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000367 }
368}
369
Hal Finkel3c8cc672015-10-12 20:56:18 +0000370// Relocation masks following the #lo(value), #hi(value), #ha(value),
371// #higher(value), #highera(value), #highest(value), and #highesta(value)
372// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
373// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000374static uint16_t applyPPCLo(uint64_t V) { return V; }
375static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
376static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
377static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
378static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000379static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000380static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
381
Rafael Espindolac4010882015-09-22 20:54:08 +0000382PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000383 PCRelReloc = R_PPC64_REL24;
384 GotReloc = R_PPC64_GLOB_DAT;
385 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000386 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000387 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000388
389 // We need 64K pages (at least under glibc/Linux, the loader won't
390 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000391 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000392
393 // The PPC64 ELF ABI v1 spec, says:
394 //
395 // It is normally desirable to put segments with different characteristics
396 // in separate 256 Mbyte portions of the address space, to give the
397 // operating system full paging flexibility in the 64-bit address space.
398 //
399 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
400 // use 0x10000000 as the starting address.
401 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000402}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000403
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000404uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000405 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
406 // order. The TOC starts where the first of these sections starts.
407
408 // FIXME: This obviously does not do the right thing when there is no .got
409 // section, but there is a .toc or .tocbss section.
410 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
411 if (!TocVA)
412 TocVA = Out<ELF64BE>::Plt->getVA();
413
414 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
415 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
416 // code (crt1.o) assumes that you can get from the TOC base to the
417 // start of the .toc section with only a single (signed) 16-bit relocation.
418 return TocVA + 0x8000;
419}
420
George Rimar648a2c32015-10-20 08:54:27 +0000421void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
422void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
423 uint64_t PltEntryAddr) const {}
Rafael Espindolac4010882015-09-22 20:54:08 +0000424void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000425 uint64_t PltEntryAddr, int32_t Index) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000426 uint64_t Off = GotEntryAddr - getPPC64TocBase();
427
428 // FIXME: What we should do, in theory, is get the offset of the function
429 // descriptor in the .opd section, and use that as the offset from %r2 (the
430 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
431 // be a pointer to the function descriptor in the .opd section. Using
432 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
433
Hal Finkelfa92f682015-10-13 21:47:34 +0000434 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000435 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
436 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
437 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
438 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
439 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
440 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
441 write32be(Buf + 28, 0x4e800420); // bctr
442}
443
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000444bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000445 if (relocNeedsPlt(Type, S))
446 return true;
447
448 switch (Type) {
449 default: return false;
450 case R_PPC64_GOT16:
451 case R_PPC64_GOT16_LO:
452 case R_PPC64_GOT16_HI:
453 case R_PPC64_GOT16_HA:
454 case R_PPC64_GOT16_DS:
455 case R_PPC64_GOT16_LO_DS:
456 return true;
457 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000458}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000459
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000460bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000461 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000462 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000463}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000464
Hal Finkelbe0823d2015-10-12 20:58:52 +0000465bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
466 switch (Type) {
467 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000468 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000469 case R_PPC64_TOC:
470 case R_PPC64_ADDR64:
471 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000472 }
473}
474
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000475void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
476 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000477 uint64_t TB = getPPC64TocBase();
478
Hal Finkel3c8cc672015-10-12 20:56:18 +0000479 // For a TOC-relative relocation, adjust the addend and proceed in terms of
480 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000481 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000482 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
483 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
484 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
485 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
486 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
487 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000488 default: break;
489 }
490
Hal Finkel3c8cc672015-10-12 20:56:18 +0000491 switch (Type) {
492 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000493 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000494 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000495 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000496 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000497 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000498 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000499 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000500 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000501 break;
502 case R_PPC64_ADDR16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000503 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000504 break;
505 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000506 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000507 break;
508 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000509 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000510 break;
511 case R_PPC64_ADDR16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000512 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000513 break;
514 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000515 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000516 break;
517 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000518 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000519 break;
520 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000521 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000522 break;
523 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000524 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000525 break;
526 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000527 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000528 error("Improper alignment for relocation R_PPC64_ADDR14");
529
530 // Preserve the AA/LK bits in the branch instruction
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000531 uint8_t AALK = Loc[3];
532 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000533 break;
534 }
535 case R_PPC64_REL16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000536 write16be(Loc, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000537 break;
538 case R_PPC64_REL16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000539 write16be(Loc, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000540 break;
541 case R_PPC64_REL16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000542 write16be(Loc, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000543 break;
544 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000545 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000546 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000547 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000548 break;
549 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000550 // If we have an undefined weak symbol, we might get here with a symbol
551 // address of zero. That could overflow, but the code must be unreachable,
552 // so don't bother doing anything at all.
553 if (!SA)
554 break;
555
Hal Finkeldaedc122015-10-12 23:16:53 +0000556 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
557 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000558 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000559
560 if (!InPlt && Out<ELF64BE>::Opd) {
561 // If this is a local call, and we currently have the address of a
562 // function-descriptor, get the underlying code address instead.
563 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
564 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000565 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000566
567 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000568 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000569 }
570
Hal Finkel3c8cc672015-10-12 20:56:18 +0000571 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000572 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000573 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000574 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000575
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000576 uint32_t Nop = 0x60000000;
577 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
578 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000579 break;
580 }
581 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000582 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000583 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000584 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000585 break;
586 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000587 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000588 break;
589 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000590 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000591 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000592 break;
593 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000594 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000595 }
596}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000597
Rui Ueyama9cfc8e02015-10-21 21:13:35 +0000598AArch64TargetInfo::AArch64TargetInfo() {}
George Rimar648a2c32015-10-20 08:54:27 +0000599
600void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
601void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
602 uint64_t PltEntryAddr) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000603void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000604 uint64_t PltEntryAddr, int32_t Index) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000605bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
606 const SymbolBody &S) const {
607 return false;
608}
609bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
610 const SymbolBody &S) const {
611 return false;
612}
Davide Italiano1d750a62015-09-27 08:45:38 +0000613
Davide Italianoef4be6b2015-10-06 19:01:32 +0000614static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000615 uint32_t ImmLo = (Imm & 0x3) << 29;
616 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
617 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000618 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000619}
620
Davide Italiano318ca222015-10-02 22:13:51 +0000621// Page(Expr) is the page address of the expression Expr, defined
622// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000623// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000624static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000625 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000626}
627
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000628void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
629 uint32_t Type, uint64_t P,
630 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000631 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000632 case R_AARCH64_ABS16:
Rafael Espindola826941a2015-10-15 18:19:39 +0000633 if (!isInt<16>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000634 error("Relocation R_AARCH64_ABS16 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000635 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000636 break;
637 case R_AARCH64_ABS32:
Rafael Espindola826941a2015-10-15 18:19:39 +0000638 if (!isInt<32>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000639 error("Relocation R_AARCH64_ABS32 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000640 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000641 break;
642 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000643 // No overflow check needed.
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000644 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000645 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000646 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000647 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000648 // This relocation stores 12 bits and there's no instruction
649 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000650 // of r_addend bitwise-or'ed Loc. This assumes that the addend
651 // bits in Loc are zero.
652 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000653 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000654 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000655 uint64_t X = SA - P;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000656 if (!isInt<21>(X))
657 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000658 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000659 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000660 }
661 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000662 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000663 if (!isInt<33>(X))
664 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000665 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000666 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000667 }
Igor Kudrinb34115b2015-11-13 03:26:59 +0000668 case R_AARCH64_JUMP26:
669 case R_AARCH64_CALL26: {
670 uint64_t X = SA - P;
671 if (!isInt<28>(X)) {
672 if (Type == R_AARCH64_JUMP26)
673 error("Relocation R_AARCH64_JUMP26 out of range");
674 error("Relocation R_AARCH64_CALL26 out of range");
675 }
676 or32le(Loc, (X & 0x0FFFFFFC) >> 2);
677 break;
678 }
Davide Italianobbcc7f62015-11-08 04:45:26 +0000679 case R_AARCH64_LDST64_ABS_LO12_NC:
680 // No overflow check needed.
681 or32le(Loc, (SA & 0xFF8) << 7);
682 break;
Davide Italiano3300b792015-10-29 19:55:59 +0000683 case R_AARCH64_PREL16:
684 if (!isInt<16>(SA))
685 error("Relocation R_AARCH64_PREL16 out of range");
686 write16le(Loc, SA - P);
687 break;
688 case R_AARCH64_PREL32:
689 if (!isInt<32>(SA))
690 error("Relocation R_AARCH64_PREL32 out of range");
691 write32le(Loc, SA - P);
692 break;
Davide Italianob12d6682015-10-28 16:14:18 +0000693 case R_AARCH64_PREL64:
694 // No overflow check needed.
695 write64le(Loc, SA - P);
696 break;
Davide Italiano1d750a62015-09-27 08:45:38 +0000697 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000698 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000699 }
700}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000701
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000702template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +0000703 PageSize = 65536;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000704 GotRefReloc = R_MIPS_GOT16;
705 GotHeaderEntriesNum = 2;
706}
707
708template <class ELFT>
709void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const {
710 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
711 auto *P = reinterpret_cast<Elf_Off *>(Buf);
712 // Module pointer
713 P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000714}
715
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000716template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000717void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
718template <class ELFT>
719void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
720 uint64_t PltEntryAddr) const {}
721template <class ELFT>
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000722void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000723 uint64_t PltEntryAddr, int32_t Index) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000724
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000725template <class ELFT>
726bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
727 const SymbolBody &S) const {
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000728 return Type == R_MIPS_GOT16;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000729}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000730
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000731template <class ELFT>
732bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
733 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000734 return false;
735}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000736
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000737template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000738void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
739 uint32_t Type, uint64_t P,
740 uint64_t SA) const {
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000741 const endianness E = ELFT::TargetEndianness;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000742 switch (Type) {
743 case R_MIPS_32:
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000744 add32<E>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000745 break;
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000746 case R_MIPS_GOT16: {
747 int64_t V = SA - getMipsGpAddr<ELFT>();
748 if (!isInt<16>(V))
749 error("Relocation R_MIPS_GOT16 out of range");
Rafael Espindolae7e57b22015-11-09 21:43:00 +0000750 write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff));
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000751 break;
752 }
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000753 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000754 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000755 }
756}
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000757
758template <class ELFT>
759typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() {
760 const unsigned GPOffset = 0x7ff0;
761 return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0;
762}
763
764template uint32_t getMipsGpAddr<ELF32LE>();
765template uint32_t getMipsGpAddr<ELF32BE>();
766template uint64_t getMipsGpAddr<ELF64LE>();
767template uint64_t getMipsGpAddr<ELF64BE>();
Rafael Espindola01205f72015-09-22 18:19:46 +0000768}
769}