blob: 0e08c5ae03db42fc06b1215d854feec917da61f6 [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//===----------------------------------------------------------------------===//
9
10#include "Target.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000011#include "Error.h"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000012#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000013#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000014
15#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000016#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000017#include "llvm/Support/Endian.h"
18#include "llvm/Support/ELF.h"
19
20using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000021using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000022using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000023using namespace llvm::ELF;
24
25namespace lld {
26namespace elf2 {
27
28std::unique_ptr<TargetInfo> Target;
29
Rui Ueyama91004392015-10-13 16:08:15 +000030TargetInfo *createTarget() {
31 switch (Config->EMachine) {
32 case EM_386:
33 return new X86TargetInfo();
34 case EM_AARCH64:
35 return new AArch64TargetInfo();
36 case EM_MIPS:
37 return new MipsTargetInfo();
38 case EM_PPC:
39 return new PPCTargetInfo();
40 case EM_PPC64:
41 return new PPC64TargetInfo();
42 case EM_X86_64:
43 return new X86_64TargetInfo();
44 }
45 error("Unknown target machine");
46}
47
Rafael Espindola01205f72015-09-22 18:19:46 +000048TargetInfo::~TargetInfo() {}
49
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000050bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
51
Rafael Espindolaae244002015-10-05 19:30:12 +000052bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
53
Rafael Espindola7f074422015-09-22 21:35:51 +000054X86TargetInfo::X86TargetInfo() {
55 PCRelReloc = R_386_PC32;
56 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000057 GotRefReloc = R_386_GOT32;
Rafael Espindola7f074422015-09-22 21:35:51 +000058}
Rafael Espindola01205f72015-09-22 18:19:46 +000059
60void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +000061 uint64_t PltEntryAddr) const {
62 // jmpl *val; nop; nop
63 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +000064 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +000065 assert(isUInt<32>(GotEntryAddr));
66 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +000067}
68
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000069bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +000070 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +000071}
72
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000073bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
74 return Type == R_386_GOTPC;
75}
76
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000077bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +000078 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +000079}
80
Rui Ueyama87bc41b2015-10-06 18:54:43 +000081static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); }
Rui Ueyama87bc41b2015-10-06 18:54:43 +000082static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); }
Rafael Espindola0872ea32015-09-24 14:16:02 +000083
Hal Finkel87bbd5f2015-10-12 21:19:18 +000084void X86TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
85 uint32_t Type, uint64_t BaseAddr,
86 uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000087 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
88 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
89
90 uint32_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +000091 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000092 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000093 case R_386_GOT32:
Rui Ueyamaaf21d922015-10-08 20:06:07 +000094 add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +000095 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000096 case R_386_PC32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +000097 add32le(Loc, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000098 break;
99 case R_386_32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000100 add32le(Loc, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000101 break;
102 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000103 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000104 }
105}
106
Rafael Espindola7f074422015-09-22 21:35:51 +0000107X86_64TargetInfo::X86_64TargetInfo() {
108 PCRelReloc = R_X86_64_PC32;
109 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000110 GotRefReloc = R_X86_64_PC32;
Rafael Espindolaae244002015-10-05 19:30:12 +0000111 RelativeReloc = R_X86_64_RELATIVE;
Rafael Espindola7f074422015-09-22 21:35:51 +0000112}
Rafael Espindola01205f72015-09-22 18:19:46 +0000113
114void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000115 uint64_t PltEntryAddr) const {
116 // jmpq *val(%rip); nop; nop
117 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000118 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000119
Rui Ueyamac58656c2015-10-13 16:59:30 +0000120 uint64_t NextPC = PltEntryAddr + 6;
121 int64_t Delta = GotEntryAddr - NextPC;
122 assert(isInt<32>(Delta));
123 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000124}
125
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000126bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000127 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000128}
129
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000130bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000131 switch (Type) {
132 default:
133 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000134 case R_X86_64_PC32:
135 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000136 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000137 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000138 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000139 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000140 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
141 // libc.so.
142 //
143 // The general idea on how to handle such cases is to create a PLT entry
144 // and use that as the function value.
145 //
146 // For the static linking part, we just return true and everything else
147 // will use the the PLT entry as the address.
148 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000149 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000150 // still works. We need the help of the dynamic linker for that. We
151 // let it know that we have a direct reference to a so symbol by creating
152 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000153 // dynamic linker resolves the symbol to the value of the symbol we created.
154 // This is true even for got entries, so pointer equality is maintained.
155 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000156 // real function is a dedicated got entry used by the plt. That is
157 // identified by special relocation types (R_X86_64_JUMP_SLOT,
158 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000159 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000160 case R_X86_64_PLT32:
161 return true;
162 }
163}
Rafael Espindolac4010882015-09-22 20:54:08 +0000164
Rafael Espindolaae244002015-10-05 19:30:12 +0000165bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
166 switch (Type) {
167 default:
168 return false;
169 case R_X86_64_PC64:
170 case R_X86_64_PC32:
171 case R_X86_64_PC16:
172 case R_X86_64_PC8:
173 return true;
174 }
175}
176
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000177void X86_64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
178 const void *RelP, uint32_t Type,
179 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000180 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
181 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
182
183 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000184 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000185 switch (Type) {
186 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000187 case R_X86_64_GOTPCREL:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000188 write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000189 break;
190 case R_X86_64_64:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000191 write64le(Loc, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000192 break;
193 case R_X86_64_32: {
194 case R_X86_64_32S:
195 uint64_t VA = SymVA + Rel.r_addend;
196 if (Type == R_X86_64_32 && !isUInt<32>(VA))
197 error("R_X86_64_32 out of range");
198 else if (!isInt<32>(VA))
199 error("R_X86_64_32S out of range");
200
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000201 write32le(Loc, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000202 break;
203 }
204 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000205 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000206 }
207}
208
Hal Finkel3c8cc672015-10-12 20:56:18 +0000209// Relocation masks following the #lo(value), #hi(value), #ha(value),
210// #higher(value), #highera(value), #highest(value), and #highesta(value)
211// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
212// document.
213
214static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; }
215
216static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; }
217
218static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; }
219
220static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; }
221
222static uint16_t applyPPCHighera(uint64_t V) {
223 return ((V + 0x8000) >> 32) & 0xffff;
224}
225
226static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
227
228static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
229
Rafael Espindolac4010882015-09-22 20:54:08 +0000230PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000231 PCRelReloc = R_PPC64_REL24;
232 GotReloc = R_PPC64_GLOB_DAT;
233 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000234 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000235 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000236
237 // We need 64K pages (at least under glibc/Linux, the loader won't
238 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000239 PageSize = 65536;
Rafael Espindolac4010882015-09-22 20:54:08 +0000240}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000241
242static uint64_t getPPC64TocBase() {
243 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
244 // order. The TOC starts where the first of these sections starts.
245
246 // FIXME: This obviously does not do the right thing when there is no .got
247 // section, but there is a .toc or .tocbss section.
248 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
249 if (!TocVA)
250 TocVA = Out<ELF64BE>::Plt->getVA();
251
252 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
253 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
254 // code (crt1.o) assumes that you can get from the TOC base to the
255 // start of the .toc section with only a single (signed) 16-bit relocation.
256 return TocVA + 0x8000;
257}
258
Rafael Espindolac4010882015-09-22 20:54:08 +0000259void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000260 uint64_t PltEntryAddr) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000261 uint64_t Off = GotEntryAddr - getPPC64TocBase();
262
263 // FIXME: What we should do, in theory, is get the offset of the function
264 // descriptor in the .opd section, and use that as the offset from %r2 (the
265 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
266 // be a pointer to the function descriptor in the .opd section. Using
267 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
268
269 write32be(Buf, 0xf8410000); // std %r2, 40(%r1)
270 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
271 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
272 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
273 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
274 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
275 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
276 write32be(Buf + 28, 0x4e800420); // bctr
277}
278
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000279bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000280 if (relocNeedsPlt(Type, S))
281 return true;
282
283 switch (Type) {
284 default: return false;
285 case R_PPC64_GOT16:
286 case R_PPC64_GOT16_LO:
287 case R_PPC64_GOT16_HI:
288 case R_PPC64_GOT16_HA:
289 case R_PPC64_GOT16_DS:
290 case R_PPC64_GOT16_LO_DS:
291 return true;
292 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000293}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000294
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000295bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000296 if (Type != R_PPC64_REL24)
297 return false;
298
299 // These are function calls that need to be redirected through a PLT stub.
300 return S.isShared() || (S.isUndefined() && S.isWeak());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000301}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000302
Hal Finkelbe0823d2015-10-12 20:58:52 +0000303bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
304 switch (Type) {
305 default:
306 return false;
307 case R_PPC64_REL24:
308 case R_PPC64_REL14:
309 case R_PPC64_REL14_BRTAKEN:
310 case R_PPC64_REL14_BRNTAKEN:
311 case R_PPC64_REL32:
312 case R_PPC64_REL64:
313 return true;
314 }
315}
316
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000317void PPC64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
318 const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000319 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000320 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
321 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
322
Hal Finkel3c8cc672015-10-12 20:56:18 +0000323 uint8_t *L = Buf + Rel.r_offset;
324 uint64_t S = SymVA;
325 int64_t A = Rel.r_addend;
326 uint64_t P = BaseAddr + Rel.r_offset;
327 uint64_t TB = getPPC64TocBase();
328
329 if (Type == R_PPC64_TOC) {
330 write64be(L, TB);
331 return;
332 }
333
334 // For a TOC-relative relocation, adjust the addend and proceed in terms of
335 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000336 switch (Type) {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000337 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; A -= TB; break;
338 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; A -= TB; break;
339 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; A -= TB; break;
340 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; A -= TB; break;
341 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; A -= TB; break;
342 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; A -= TB; break;
343 default: break;
344 }
345
346 uint64_t R = S + A;
347
348 switch (Type) {
349 case R_PPC64_ADDR16:
350 write16be(L, applyPPCLo(R));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000351 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000352 case R_PPC64_ADDR16_DS:
353 if (!isInt<16>(R))
354 error("Relocation R_PPC64_ADDR16_DS overflow");
355 write16be(L, (read16be(L) & 3) | (R & ~3));
356 break;
357 case R_PPC64_ADDR16_LO:
358 write16be(L, applyPPCLo(R));
359 break;
360 case R_PPC64_ADDR16_LO_DS:
361 write16be(L, (read16be(L) & 3) | (applyPPCLo(R) & ~3));
362 break;
363 case R_PPC64_ADDR16_HI:
364 write16be(L, applyPPCHi(R));
365 break;
366 case R_PPC64_ADDR16_HA:
367 write16be(L, applyPPCHa(R));
368 break;
369 case R_PPC64_ADDR16_HIGHER:
370 write16be(L, applyPPCHigher(R));
371 break;
372 case R_PPC64_ADDR16_HIGHERA:
373 write16be(L, applyPPCHighera(R));
374 break;
375 case R_PPC64_ADDR16_HIGHEST:
376 write16be(L, applyPPCHighest(R));
377 break;
378 case R_PPC64_ADDR16_HIGHESTA:
379 write16be(L, applyPPCHighesta(R));
380 break;
381 case R_PPC64_ADDR14: {
382 if ((R & 3) != 0)
383 error("Improper alignment for relocation R_PPC64_ADDR14");
384
385 // Preserve the AA/LK bits in the branch instruction
386 uint8_t AALK = L[3];
387 write16be(L + 2, (AALK & 3) | (R & 0xfffc));
388 break;
389 }
390 case R_PPC64_REL16_LO:
391 write16be(L, applyPPCLo(R - P));
392 break;
393 case R_PPC64_REL16_HI:
394 write16be(L, applyPPCHi(R - P));
395 break;
396 case R_PPC64_REL16_HA:
397 write16be(L, applyPPCHa(R - P));
398 break;
399 case R_PPC64_ADDR32:
400 if (!isInt<32>(R))
401 error("Relocation R_PPC64_ADDR32 overflow");
402 write32be(L, R);
403 break;
404 case R_PPC64_REL24: {
Hal Finkeldaedc122015-10-12 23:16:53 +0000405 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
406 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
407 bool InPlt = PltStart <= S + A && S + A < PltEnd;
408
409 if (!InPlt && Out<ELF64BE>::Opd) {
410 // If this is a local call, and we currently have the address of a
411 // function-descriptor, get the underlying code address instead.
412 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
413 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
414 bool InOpd = OpdStart <= S + A && S + A < OpdEnd;
415
416 if (InOpd)
417 R = read64be(&Out<ELF64BE>::OpdBuf[S + A - OpdStart]);
418 }
419
Hal Finkel3c8cc672015-10-12 20:56:18 +0000420 uint32_t Mask = 0x03FFFFFC;
421 if (!isInt<24>(R - P))
422 error("Relocation R_PPC64_REL24 overflow");
423 write32be(L, (read32be(L) & ~Mask) | ((R - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000424
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000425 if (InPlt && L + 8 < BufEnd &&
426 read32be(L + 4) == 0x60000000 /* nop */)
427 write32be(L + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000428 break;
429 }
430 case R_PPC64_REL32:
431 if (!isInt<32>(R - P))
432 error("Relocation R_PPC64_REL32 overflow");
433 write32be(L, R - P);
434 break;
435 case R_PPC64_REL64:
436 write64be(L, R - P);
437 break;
438 case R_PPC64_ADDR64:
439 write64be(L, R);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000440 break;
441 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000442 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000443 }
444}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000445
446PPCTargetInfo::PPCTargetInfo() {
447 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000448 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000449 PageSize = 65536;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000450}
451void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000452 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000453bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
454 return false;
455}
456bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
457 return false;
458}
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000459void PPCTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
460 const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000461 uint64_t BaseAddr, uint64_t SymVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000462
Davide Italianocde93362015-09-26 00:32:04 +0000463AArch64TargetInfo::AArch64TargetInfo() {
464 // PCRelReloc = FIXME
465 // GotReloc = FIXME
466}
467void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000468 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000469bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
470 const SymbolBody &S) const {
471 return false;
472}
473bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
474 const SymbolBody &S) const {
475 return false;
476}
Davide Italiano1d750a62015-09-27 08:45:38 +0000477
Davide Italianoef4be6b2015-10-06 19:01:32 +0000478static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000479 uint32_t ImmLo = (Imm & 0x3) << 29;
480 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
481 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000482 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000483}
484
Davide Italiano318ca222015-10-02 22:13:51 +0000485// Page(Expr) is the page address of the expression Expr, defined
486// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000487// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000488static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000489 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000490}
491
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000492void AArch64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
493 const void *RelP, uint32_t Type,
494 uint64_t BaseAddr, uint64_t SymVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000495 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
496 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
497
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000498 uint8_t *L = Buf + Rel.r_offset;
Davide Italiano1d750a62015-09-27 08:45:38 +0000499 uint64_t S = SymVA;
500 int64_t A = Rel.r_addend;
501 uint64_t P = BaseAddr + Rel.r_offset;
502 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000503 case R_AARCH64_ABS16:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000504 if (!isInt<16>(S + A))
505 error("Relocation R_AARCH64_ABS16 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000506 write16le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000507 break;
508 case R_AARCH64_ABS32:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000509 if (!isInt<32>(S + A))
510 error("Relocation R_AARCH64_ABS32 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000511 write32le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000512 break;
513 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000514 // No overflow check needed.
Davide Italiano06d84322015-10-07 22:10:02 +0000515 write64le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000516 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000517 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000518 // No overflow check needed.
519 or32le(L, ((S + A) & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000520 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000521 case R_AARCH64_ADR_PREL_LO21: {
522 uint64_t X = S + A - P;
523 if (!isInt<21>(X))
524 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
525 updateAArch64Adr(L, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000526 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000527 }
528 case R_AARCH64_ADR_PREL_PG_HI21: {
529 uint64_t X = getAArch64Page(S + A) - getAArch64Page(P);
530 if (!isInt<33>(X))
531 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
532 updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000533 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000534 }
Davide Italiano1d750a62015-09-27 08:45:38 +0000535 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000536 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000537 }
538}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000539
540MipsTargetInfo::MipsTargetInfo() {
541 // PCRelReloc = FIXME
542 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000543 PageSize = 65536;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000544}
545
546void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000547 uint64_t PltEntryAddr) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000548
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000549bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
550 return false;
551}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000552
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000553bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
554 return false;
555}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000556
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000557void MipsTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
558 const void *RelP, uint32_t Type,
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000559 uint64_t BaseAddr, uint64_t SymVA) const {
560 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
561 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
562
563 switch (Type) {
564 case R_MIPS_32:
565 add32le(Buf + Rel.r_offset, SymVA);
566 break;
567 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000568 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000569 }
570}
Rafael Espindola01205f72015-09-22 18:19:46 +0000571}
572}