blob: 1f1b344cb2deeaf46aa5772ac1de2629f690a791 [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
Rui Ueyamaefc23de2015-10-14 21:30:32 +000038static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); }
39static void add32be(uint8_t *L, int32_t V) { write32be(L, read32be(L) + V); }
40static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); }
41
42template <bool IsLE> static void add32(uint8_t *L, int32_t V);
43template <> void add32<true>(uint8_t *L, int32_t V) { add32le(L, V); }
44template <> void add32<false>(uint8_t *L, int32_t V) { add32be(L, V); }
45
46namespace {
47class X86TargetInfo final : public TargetInfo {
48public:
49 X86TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000050 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
51 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
52 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000053 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000054 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000055 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
56 bool relocPointsToGot(uint32_t Type) const override;
57 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000058 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000059 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000060};
61
62class X86_64TargetInfo final : public TargetInfo {
63public:
64 X86_64TargetInfo();
65 unsigned getPLTRefReloc(unsigned Type) const override;
George Rimar648a2c32015-10-20 08:54:27 +000066 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
67 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
68 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000069 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000070 uint64_t PltEntryAddr, int32_t Index) const override;
George Rimarbc590fe2015-10-28 16:48:58 +000071 bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000072 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
73 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000074 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000075 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000076 bool isRelRelative(uint32_t Type) const override;
77};
78
79class PPC64TargetInfo final : public TargetInfo {
80public:
81 PPC64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000082 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
83 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
84 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000085 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +000086 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000087 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
88 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000089 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +000090 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000091 bool isRelRelative(uint32_t Type) const override;
92};
93
Rui Ueyamaefc23de2015-10-14 21:30:32 +000094class AArch64TargetInfo final : public TargetInfo {
95public:
96 AArch64TargetInfo();
George Rimar648a2c32015-10-20 08:54:27 +000097 void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override;
98 void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
99 uint64_t PltEntryAddr) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000100 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000101 uint64_t PltEntryAddr, int32_t Index) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000102 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
103 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000104 void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P,
Rui Ueyama66072272015-10-15 19:52:27 +0000105 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000106};
107
108template <class ELFT> class MipsTargetInfo final : public TargetInfo {
109public:
110 MipsTargetInfo();
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
George Rimarbc590fe2015-10-28 16:48:58 +0000148bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const {
149 return false;
150}
151
Rafael Espindola227556e2015-10-14 16:15:46 +0000152unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; }
153
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000154bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
155
Rafael Espindolaae244002015-10-05 19:30:12 +0000156bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
157
Rafael Espindola7f074422015-09-22 21:35:51 +0000158X86TargetInfo::X86TargetInfo() {
159 PCRelReloc = R_386_PC32;
160 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000161 GotRefReloc = R_386_GOT32;
George Rimar648a2c32015-10-20 08:54:27 +0000162 PltReloc = R_386_JUMP_SLOT;
Rafael Espindola7f074422015-09-22 21:35:51 +0000163}
Rafael Espindola01205f72015-09-22 18:19:46 +0000164
George Rimar648a2c32015-10-20 08:54:27 +0000165void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
166void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
167 uint64_t PltEntryAddr) const {}
168
Rafael Espindola01205f72015-09-22 18:19:46 +0000169void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000170 uint64_t PltEntryAddr, int32_t Index) const {
Rui Ueyamac58656c2015-10-13 16:59:30 +0000171 // jmpl *val; nop; nop
172 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000173 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +0000174 assert(isUInt<32>(GotEntryAddr));
175 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +0000176}
177
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000178bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000179 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000180}
181
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000182bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
183 return Type == R_386_GOTPC;
184}
185
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000186bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000187 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000188}
189
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000190void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
191 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000192 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000193 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000194 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000195 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000196 case R_386_PC32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000197 add32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000198 break;
199 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000200 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000201 break;
202 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000203 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000204 }
205}
206
Rafael Espindola7f074422015-09-22 21:35:51 +0000207X86_64TargetInfo::X86_64TargetInfo() {
George Rimarbc590fe2015-10-28 16:48:58 +0000208 CopyReloc = R_X86_64_COPY;
Rafael Espindola7f074422015-09-22 21:35:51 +0000209 PCRelReloc = R_X86_64_PC32;
210 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000211 GotRefReloc = R_X86_64_PC32;
George Rimar648a2c32015-10-20 08:54:27 +0000212 PltReloc = R_X86_64_JUMP_SLOT;
Rafael Espindolaae244002015-10-05 19:30:12 +0000213 RelativeReloc = R_X86_64_RELATIVE;
George Rimar648a2c32015-10-20 08:54:27 +0000214 LazyRelocations = true;
215 PltEntrySize = 16;
216 PltZeroEntrySize = 16;
217}
218
219void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {
220 // Skip 6 bytes of "jmpq *got(%rip)"
221 write32le(Buf, Plt + 6);
222}
223
224void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
225 uint64_t PltEntryAddr) const {
226 const uint8_t PltData[] = {
227 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip)
228 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip)
229 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax)
230 };
231 memcpy(Buf, PltData, sizeof(PltData));
232 write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8
233 write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16
Rafael Espindola7f074422015-09-22 21:35:51 +0000234}
Rafael Espindola01205f72015-09-22 18:19:46 +0000235
236void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000237 uint64_t PltEntryAddr,
238 int32_t Index) const {
239 const uint8_t Inst[] = {
240 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip)
241 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index>
242 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0]
243 };
Rui Ueyama1500a902015-09-29 23:00:47 +0000244 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000245
George Rimar648a2c32015-10-20 08:54:27 +0000246 write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6);
247 write32le(Buf + 7, Index);
248 write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16);
Rafael Espindola01205f72015-09-22 18:19:46 +0000249}
250
George Rimarbc590fe2015-10-28 16:48:58 +0000251bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type,
252 const SymbolBody &S) const {
253 if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 ||
254 Type == R_X86_64_64)
255 if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S))
256 return SS->Sym.getType() == STT_OBJECT;
257 return false;
258}
259
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000260bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000261 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000262}
263
Rafael Espindola227556e2015-10-14 16:15:46 +0000264unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const {
265 switch (Type) {
266 case R_X86_64_32:
267 return R_X86_64_32;
268 case R_X86_64_PC32:
269 case R_X86_64_PLT32:
270 return R_X86_64_PC32;
271 }
272 llvm_unreachable("Unexpected relocation");
273}
274
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000275bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimarbc590fe2015-10-28 16:48:58 +0000276 if (relocNeedsCopy(Type, S))
277 return false;
278
Rafael Espindola01205f72015-09-22 18:19:46 +0000279 switch (Type) {
280 default:
281 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000282 case R_X86_64_32:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000283 case R_X86_64_PC32:
284 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000285 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000286 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000287 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000288 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000289 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
290 // libc.so.
291 //
292 // The general idea on how to handle such cases is to create a PLT entry
293 // and use that as the function value.
294 //
295 // For the static linking part, we just return true and everything else
296 // will use the the PLT entry as the address.
297 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000298 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000299 // still works. We need the help of the dynamic linker for that. We
300 // let it know that we have a direct reference to a so symbol by creating
301 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000302 // dynamic linker resolves the symbol to the value of the symbol we created.
303 // This is true even for got entries, so pointer equality is maintained.
304 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000305 // real function is a dedicated got entry used by the plt. That is
306 // identified by special relocation types (R_X86_64_JUMP_SLOT,
307 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000308 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000309 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000310 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000311 }
312}
Rafael Espindolac4010882015-09-22 20:54:08 +0000313
Rafael Espindolaae244002015-10-05 19:30:12 +0000314bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
315 switch (Type) {
316 default:
317 return false;
318 case R_X86_64_PC64:
319 case R_X86_64_PC32:
320 case R_X86_64_PC16:
321 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000322 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000323 return true;
324 }
325}
326
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000327void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
328 uint64_t P, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000329 switch (Type) {
330 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000331 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000332 case R_X86_64_PLT32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000333 write32le(Loc, SA - P);
Rafael Espindolac4010882015-09-22 20:54:08 +0000334 break;
335 case R_X86_64_64:
Rui Ueyama66072272015-10-15 19:52:27 +0000336 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000337 break;
Rui Ueyama3835b492015-10-23 16:13:27 +0000338 case R_X86_64_32:
Rafael Espindolac4010882015-09-22 20:54:08 +0000339 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000340 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000341 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000342 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000343 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000344 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000345 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000346 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000347 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000348 }
349}
350
Hal Finkel3c8cc672015-10-12 20:56:18 +0000351// Relocation masks following the #lo(value), #hi(value), #ha(value),
352// #higher(value), #highera(value), #highest(value), and #highesta(value)
353// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
354// document.
Rui Ueyamac44e5a12015-10-23 16:54:58 +0000355static uint16_t applyPPCLo(uint64_t V) { return V; }
356static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
357static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
358static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
359static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000360static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
Hal Finkel3c8cc672015-10-12 20:56:18 +0000361static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
362
Rafael Espindolac4010882015-09-22 20:54:08 +0000363PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000364 PCRelReloc = R_PPC64_REL24;
365 GotReloc = R_PPC64_GLOB_DAT;
366 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000367 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000368 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000369
370 // We need 64K pages (at least under glibc/Linux, the loader won't
371 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000372 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000373
374 // The PPC64 ELF ABI v1 spec, says:
375 //
376 // It is normally desirable to put segments with different characteristics
377 // in separate 256 Mbyte portions of the address space, to give the
378 // operating system full paging flexibility in the 64-bit address space.
379 //
380 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
381 // use 0x10000000 as the starting address.
382 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000383}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000384
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000385uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000386 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
387 // order. The TOC starts where the first of these sections starts.
388
389 // FIXME: This obviously does not do the right thing when there is no .got
390 // section, but there is a .toc or .tocbss section.
391 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
392 if (!TocVA)
393 TocVA = Out<ELF64BE>::Plt->getVA();
394
395 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
396 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
397 // code (crt1.o) assumes that you can get from the TOC base to the
398 // start of the .toc section with only a single (signed) 16-bit relocation.
399 return TocVA + 0x8000;
400}
401
George Rimar648a2c32015-10-20 08:54:27 +0000402void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
403void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
404 uint64_t PltEntryAddr) const {}
Rafael Espindolac4010882015-09-22 20:54:08 +0000405void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000406 uint64_t PltEntryAddr, int32_t Index) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000407 uint64_t Off = GotEntryAddr - getPPC64TocBase();
408
409 // FIXME: What we should do, in theory, is get the offset of the function
410 // descriptor in the .opd section, and use that as the offset from %r2 (the
411 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
412 // be a pointer to the function descriptor in the .opd section. Using
413 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
414
Hal Finkelfa92f682015-10-13 21:47:34 +0000415 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000416 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
417 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
418 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
419 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
420 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
421 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
422 write32be(Buf + 28, 0x4e800420); // bctr
423}
424
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000425bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000426 if (relocNeedsPlt(Type, S))
427 return true;
428
429 switch (Type) {
430 default: return false;
431 case R_PPC64_GOT16:
432 case R_PPC64_GOT16_LO:
433 case R_PPC64_GOT16_HI:
434 case R_PPC64_GOT16_HA:
435 case R_PPC64_GOT16_DS:
436 case R_PPC64_GOT16_LO_DS:
437 return true;
438 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000439}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000440
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000441bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000442 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000443 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000444}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000445
Hal Finkelbe0823d2015-10-12 20:58:52 +0000446bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
447 switch (Type) {
448 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000449 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000450 case R_PPC64_TOC:
451 case R_PPC64_ADDR64:
452 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000453 }
454}
455
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000456void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type,
457 uint64_t P, uint64_t SA) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000458 uint64_t TB = getPPC64TocBase();
459
Hal Finkel3c8cc672015-10-12 20:56:18 +0000460 // For a TOC-relative relocation, adjust the addend and proceed in terms of
461 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000462 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000463 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
464 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
465 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
466 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
467 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
468 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000469 default: break;
470 }
471
Hal Finkel3c8cc672015-10-12 20:56:18 +0000472 switch (Type) {
473 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000474 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000475 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000476 write16be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000477 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000478 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000479 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000480 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000481 write16be(Loc, (read16be(Loc) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000482 break;
483 case R_PPC64_ADDR16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000484 write16be(Loc, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000485 break;
486 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000487 write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000488 break;
489 case R_PPC64_ADDR16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000490 write16be(Loc, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000491 break;
492 case R_PPC64_ADDR16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000493 write16be(Loc, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000494 break;
495 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000496 write16be(Loc, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000497 break;
498 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000499 write16be(Loc, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000500 break;
501 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000502 write16be(Loc, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000503 break;
504 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000505 write16be(Loc, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000506 break;
507 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000508 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000509 error("Improper alignment for relocation R_PPC64_ADDR14");
510
511 // Preserve the AA/LK bits in the branch instruction
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000512 uint8_t AALK = Loc[3];
513 write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000514 break;
515 }
516 case R_PPC64_REL16_LO:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000517 write16be(Loc, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000518 break;
519 case R_PPC64_REL16_HI:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000520 write16be(Loc, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000521 break;
522 case R_PPC64_REL16_HA:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000523 write16be(Loc, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000524 break;
525 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000526 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000527 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000528 write32be(Loc, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000529 break;
530 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000531 // If we have an undefined weak symbol, we might get here with a symbol
532 // address of zero. That could overflow, but the code must be unreachable,
533 // so don't bother doing anything at all.
534 if (!SA)
535 break;
536
Hal Finkeldaedc122015-10-12 23:16:53 +0000537 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
538 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000539 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000540
541 if (!InPlt && Out<ELF64BE>::Opd) {
542 // If this is a local call, and we currently have the address of a
543 // function-descriptor, get the underlying code address instead.
544 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
545 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000546 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000547
548 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000549 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000550 }
551
Hal Finkel3c8cc672015-10-12 20:56:18 +0000552 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000553 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000554 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000555 write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000556
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000557 uint32_t Nop = 0x60000000;
558 if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop)
559 write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000560 break;
561 }
562 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000563 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000564 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000565 write32be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000566 break;
567 case R_PPC64_REL64:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000568 write64be(Loc, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000569 break;
570 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000571 case R_PPC64_TOC:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000572 write64be(Loc, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000573 break;
574 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000575 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000576 }
577}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000578
Rui Ueyama9cfc8e02015-10-21 21:13:35 +0000579AArch64TargetInfo::AArch64TargetInfo() {}
George Rimar648a2c32015-10-20 08:54:27 +0000580
581void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
582void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
583 uint64_t PltEntryAddr) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000584void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000585 uint64_t PltEntryAddr, int32_t Index) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000586bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
587 const SymbolBody &S) const {
588 return false;
589}
590bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
591 const SymbolBody &S) const {
592 return false;
593}
Davide Italiano1d750a62015-09-27 08:45:38 +0000594
Davide Italianoef4be6b2015-10-06 19:01:32 +0000595static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000596 uint32_t ImmLo = (Imm & 0x3) << 29;
597 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
598 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000599 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000600}
601
Davide Italiano318ca222015-10-02 22:13:51 +0000602// Page(Expr) is the page address of the expression Expr, defined
603// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000604// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000605static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000606 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000607}
608
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000609void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
610 uint32_t Type, uint64_t P,
611 uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000612 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000613 case R_AARCH64_ABS16:
Rafael Espindola826941a2015-10-15 18:19:39 +0000614 if (!isInt<16>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000615 error("Relocation R_AARCH64_ABS16 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000616 write16le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000617 break;
618 case R_AARCH64_ABS32:
Rafael Espindola826941a2015-10-15 18:19:39 +0000619 if (!isInt<32>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000620 error("Relocation R_AARCH64_ABS32 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000621 write32le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000622 break;
623 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000624 // No overflow check needed.
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000625 write64le(Loc, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000626 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000627 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000628 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000629 // This relocation stores 12 bits and there's no instruction
630 // to do it. Instead, we do a 32 bits store of the value
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000631 // of r_addend bitwise-or'ed Loc. This assumes that the addend
632 // bits in Loc are zero.
633 or32le(Loc, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000634 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000635 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000636 uint64_t X = SA - P;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000637 if (!isInt<21>(X))
638 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000639 updateAArch64Adr(Loc, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000640 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000641 }
642 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000643 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000644 if (!isInt<33>(X))
645 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000646 updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000647 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000648 }
Davide Italianob12d6682015-10-28 16:14:18 +0000649 case R_AARCH64_PREL64:
650 // No overflow check needed.
651 write64le(Loc, SA - P);
652 break;
Davide Italiano1d750a62015-09-27 08:45:38 +0000653 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000654 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000655 }
656}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000657
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000658template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Hal Finkele3c26262015-10-08 22:23:54 +0000659 PageSize = 65536;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000660}
661
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000662template <class ELFT>
George Rimar648a2c32015-10-20 08:54:27 +0000663void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {}
664template <class ELFT>
665void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr,
666 uint64_t PltEntryAddr) const {}
667template <class ELFT>
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000668void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
George Rimar648a2c32015-10-20 08:54:27 +0000669 uint64_t PltEntryAddr, int32_t Index) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000670
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000671template <class ELFT>
672bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
673 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000674 return false;
675}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000676
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000677template <class ELFT>
678bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
679 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000680 return false;
681}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000682
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000683template <class ELFT>
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000684void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
685 uint32_t Type, uint64_t P,
686 uint64_t SA) const {
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000687 const bool IsLE = ELFT::TargetEndianness == support::little;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000688 switch (Type) {
689 case R_MIPS_32:
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000690 add32<IsLE>(Loc, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000691 break;
692 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000693 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000694 }
695}
Rafael Espindola01205f72015-09-22 18:19:46 +0000696}
697}