blob: 7009b7f721a312dca659893a34424ebde3d45485 [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;
Hal Finkel47290642015-10-08 21:25:04 +000058 VAStart = 0x10000;
Rafael Espindola7f074422015-09-22 21:35:51 +000059}
Rafael Espindola01205f72015-09-22 18:19:46 +000060
61void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +000062 uint64_t PltEntryAddr) const {
63 // jmpl *val; nop; nop
64 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +000065 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +000066 assert(isUInt<32>(GotEntryAddr));
67 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +000068}
69
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000070bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +000071 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +000072}
73
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000074bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
75 return Type == R_386_GOTPC;
76}
77
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000078bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +000079 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +000080}
81
Rui Ueyama87bc41b2015-10-06 18:54:43 +000082static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); }
Rui Ueyama87bc41b2015-10-06 18:54:43 +000083static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); }
Rafael Espindola0872ea32015-09-24 14:16:02 +000084
Hal Finkel87bbd5f2015-10-12 21:19:18 +000085void X86TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
86 uint32_t Type, uint64_t BaseAddr,
87 uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000088 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
89 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
90
91 uint32_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +000092 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000093 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000094 case R_386_GOT32:
Rui Ueyamaaf21d922015-10-08 20:06:07 +000095 add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +000096 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000097 case R_386_PC32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +000098 add32le(Loc, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000099 break;
100 case R_386_32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000101 add32le(Loc, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000102 break;
103 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000104 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000105 }
106}
107
Rafael Espindola7f074422015-09-22 21:35:51 +0000108X86_64TargetInfo::X86_64TargetInfo() {
109 PCRelReloc = R_X86_64_PC32;
110 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000111 GotRefReloc = R_X86_64_PC32;
Rafael Espindolaae244002015-10-05 19:30:12 +0000112 RelativeReloc = R_X86_64_RELATIVE;
Hal Finkel47290642015-10-08 21:25:04 +0000113
114 // On freebsd x86_64 the first page cannot be mmaped.
115 // On linux that is controled by vm.mmap_min_addr. At least on some x86_64
116 // installs that is 65536, so the first 15 pages cannot be used.
117 // Given that, the smallest value that can be used in here is 0x10000.
118 // If using 2MB pages, the smallest page aligned address that works is
119 // 0x200000, but it looks like every OS uses 4k pages for executables.
120 VAStart = 0x10000;
Rafael Espindola7f074422015-09-22 21:35:51 +0000121}
Rafael Espindola01205f72015-09-22 18:19:46 +0000122
123void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000124 uint64_t PltEntryAddr) const {
125 // jmpq *val(%rip); nop; nop
126 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000127 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000128
Rui Ueyamac58656c2015-10-13 16:59:30 +0000129 uint64_t NextPC = PltEntryAddr + 6;
130 int64_t Delta = GotEntryAddr - NextPC;
131 assert(isInt<32>(Delta));
132 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000133}
134
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000135bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000136 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000137}
138
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000139bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000140 switch (Type) {
141 default:
142 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000143 case R_X86_64_PC32:
144 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000145 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000146 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000147 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000148 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000149 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
150 // libc.so.
151 //
152 // The general idea on how to handle such cases is to create a PLT entry
153 // and use that as the function value.
154 //
155 // For the static linking part, we just return true and everything else
156 // will use the the PLT entry as the address.
157 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000158 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000159 // still works. We need the help of the dynamic linker for that. We
160 // let it know that we have a direct reference to a so symbol by creating
161 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000162 // dynamic linker resolves the symbol to the value of the symbol we created.
163 // This is true even for got entries, so pointer equality is maintained.
164 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000165 // real function is a dedicated got entry used by the plt. That is
166 // identified by special relocation types (R_X86_64_JUMP_SLOT,
167 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000168 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000169 case R_X86_64_PLT32:
170 return true;
171 }
172}
Rafael Espindolac4010882015-09-22 20:54:08 +0000173
Rafael Espindolaae244002015-10-05 19:30:12 +0000174bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
175 switch (Type) {
176 default:
177 return false;
178 case R_X86_64_PC64:
179 case R_X86_64_PC32:
180 case R_X86_64_PC16:
181 case R_X86_64_PC8:
182 return true;
183 }
184}
185
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000186void X86_64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
187 const void *RelP, uint32_t Type,
188 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000189 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
190 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
191
192 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000193 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000194 switch (Type) {
195 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000196 case R_X86_64_GOTPCREL:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000197 write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000198 break;
199 case R_X86_64_64:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000200 write64le(Loc, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000201 break;
202 case R_X86_64_32: {
203 case R_X86_64_32S:
204 uint64_t VA = SymVA + Rel.r_addend;
205 if (Type == R_X86_64_32 && !isUInt<32>(VA))
206 error("R_X86_64_32 out of range");
207 else if (!isInt<32>(VA))
208 error("R_X86_64_32S out of range");
209
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000210 write32le(Loc, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000211 break;
212 }
213 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000214 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000215 }
216}
217
Hal Finkel3c8cc672015-10-12 20:56:18 +0000218// Relocation masks following the #lo(value), #hi(value), #ha(value),
219// #higher(value), #highera(value), #highest(value), and #highesta(value)
220// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
221// document.
222
223static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; }
224
225static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; }
226
227static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; }
228
229static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; }
230
231static uint16_t applyPPCHighera(uint64_t V) {
232 return ((V + 0x8000) >> 32) & 0xffff;
233}
234
235static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
236
237static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
238
Rafael Espindolac4010882015-09-22 20:54:08 +0000239PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000240 PCRelReloc = R_PPC64_REL24;
241 GotReloc = R_PPC64_GLOB_DAT;
242 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000243 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000244 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000245
246 // We need 64K pages (at least under glibc/Linux, the loader won't
247 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000248 PageSize = 65536;
Hal Finkelc848b322015-10-12 19:34:29 +0000249
Hal Finkel47290642015-10-08 21:25:04 +0000250 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000251}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000252
253static uint64_t getPPC64TocBase() {
254 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
255 // order. The TOC starts where the first of these sections starts.
256
257 // FIXME: This obviously does not do the right thing when there is no .got
258 // section, but there is a .toc or .tocbss section.
259 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
260 if (!TocVA)
261 TocVA = Out<ELF64BE>::Plt->getVA();
262
263 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
264 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
265 // code (crt1.o) assumes that you can get from the TOC base to the
266 // start of the .toc section with only a single (signed) 16-bit relocation.
267 return TocVA + 0x8000;
268}
269
Rafael Espindolac4010882015-09-22 20:54:08 +0000270void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000271 uint64_t PltEntryAddr) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000272 uint64_t Off = GotEntryAddr - getPPC64TocBase();
273
274 // FIXME: What we should do, in theory, is get the offset of the function
275 // descriptor in the .opd section, and use that as the offset from %r2 (the
276 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
277 // be a pointer to the function descriptor in the .opd section. Using
278 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
279
280 write32be(Buf, 0xf8410000); // std %r2, 40(%r1)
281 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
282 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
283 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
284 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
285 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
286 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
287 write32be(Buf + 28, 0x4e800420); // bctr
288}
289
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000290bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000291 if (relocNeedsPlt(Type, S))
292 return true;
293
294 switch (Type) {
295 default: return false;
296 case R_PPC64_GOT16:
297 case R_PPC64_GOT16_LO:
298 case R_PPC64_GOT16_HI:
299 case R_PPC64_GOT16_HA:
300 case R_PPC64_GOT16_DS:
301 case R_PPC64_GOT16_LO_DS:
302 return true;
303 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000304}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000305
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000306bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000307 if (Type != R_PPC64_REL24)
308 return false;
309
310 // These are function calls that need to be redirected through a PLT stub.
311 return S.isShared() || (S.isUndefined() && S.isWeak());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000312}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000313
Hal Finkelbe0823d2015-10-12 20:58:52 +0000314bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
315 switch (Type) {
316 default:
317 return false;
318 case R_PPC64_REL24:
319 case R_PPC64_REL14:
320 case R_PPC64_REL14_BRTAKEN:
321 case R_PPC64_REL14_BRNTAKEN:
322 case R_PPC64_REL32:
323 case R_PPC64_REL64:
324 return true;
325 }
326}
327
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000328void PPC64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
329 const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000330 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000331 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
332 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
333
Hal Finkel3c8cc672015-10-12 20:56:18 +0000334 uint8_t *L = Buf + Rel.r_offset;
335 uint64_t S = SymVA;
336 int64_t A = Rel.r_addend;
337 uint64_t P = BaseAddr + Rel.r_offset;
338 uint64_t TB = getPPC64TocBase();
339
340 if (Type == R_PPC64_TOC) {
341 write64be(L, TB);
342 return;
343 }
344
345 // For a TOC-relative relocation, adjust the addend and proceed in terms of
346 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000347 switch (Type) {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000348 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; A -= TB; break;
349 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; A -= TB; break;
350 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; A -= TB; break;
351 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; A -= TB; break;
352 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; A -= TB; break;
353 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; A -= TB; break;
354 default: break;
355 }
356
357 uint64_t R = S + A;
358
359 switch (Type) {
360 case R_PPC64_ADDR16:
361 write16be(L, applyPPCLo(R));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000362 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000363 case R_PPC64_ADDR16_DS:
364 if (!isInt<16>(R))
365 error("Relocation R_PPC64_ADDR16_DS overflow");
366 write16be(L, (read16be(L) & 3) | (R & ~3));
367 break;
368 case R_PPC64_ADDR16_LO:
369 write16be(L, applyPPCLo(R));
370 break;
371 case R_PPC64_ADDR16_LO_DS:
372 write16be(L, (read16be(L) & 3) | (applyPPCLo(R) & ~3));
373 break;
374 case R_PPC64_ADDR16_HI:
375 write16be(L, applyPPCHi(R));
376 break;
377 case R_PPC64_ADDR16_HA:
378 write16be(L, applyPPCHa(R));
379 break;
380 case R_PPC64_ADDR16_HIGHER:
381 write16be(L, applyPPCHigher(R));
382 break;
383 case R_PPC64_ADDR16_HIGHERA:
384 write16be(L, applyPPCHighera(R));
385 break;
386 case R_PPC64_ADDR16_HIGHEST:
387 write16be(L, applyPPCHighest(R));
388 break;
389 case R_PPC64_ADDR16_HIGHESTA:
390 write16be(L, applyPPCHighesta(R));
391 break;
392 case R_PPC64_ADDR14: {
393 if ((R & 3) != 0)
394 error("Improper alignment for relocation R_PPC64_ADDR14");
395
396 // Preserve the AA/LK bits in the branch instruction
397 uint8_t AALK = L[3];
398 write16be(L + 2, (AALK & 3) | (R & 0xfffc));
399 break;
400 }
401 case R_PPC64_REL16_LO:
402 write16be(L, applyPPCLo(R - P));
403 break;
404 case R_PPC64_REL16_HI:
405 write16be(L, applyPPCHi(R - P));
406 break;
407 case R_PPC64_REL16_HA:
408 write16be(L, applyPPCHa(R - P));
409 break;
410 case R_PPC64_ADDR32:
411 if (!isInt<32>(R))
412 error("Relocation R_PPC64_ADDR32 overflow");
413 write32be(L, R);
414 break;
415 case R_PPC64_REL24: {
Hal Finkeldaedc122015-10-12 23:16:53 +0000416 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
417 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
418 bool InPlt = PltStart <= S + A && S + A < PltEnd;
419
420 if (!InPlt && Out<ELF64BE>::Opd) {
421 // If this is a local call, and we currently have the address of a
422 // function-descriptor, get the underlying code address instead.
423 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
424 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
425 bool InOpd = OpdStart <= S + A && S + A < OpdEnd;
426
427 if (InOpd)
428 R = read64be(&Out<ELF64BE>::OpdBuf[S + A - OpdStart]);
429 }
430
Hal Finkel3c8cc672015-10-12 20:56:18 +0000431 uint32_t Mask = 0x03FFFFFC;
432 if (!isInt<24>(R - P))
433 error("Relocation R_PPC64_REL24 overflow");
434 write32be(L, (read32be(L) & ~Mask) | ((R - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000435
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000436 if (InPlt && L + 8 < BufEnd &&
437 read32be(L + 4) == 0x60000000 /* nop */)
438 write32be(L + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000439 break;
440 }
441 case R_PPC64_REL32:
442 if (!isInt<32>(R - P))
443 error("Relocation R_PPC64_REL32 overflow");
444 write32be(L, R - P);
445 break;
446 case R_PPC64_REL64:
447 write64be(L, R - P);
448 break;
449 case R_PPC64_ADDR64:
450 write64be(L, R);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000451 break;
452 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000453 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000454 }
455}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000456
457PPCTargetInfo::PPCTargetInfo() {
458 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000459 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000460 PageSize = 65536;
Hal Finkel47290642015-10-08 21:25:04 +0000461 VAStart = 0x10000000;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000462}
463void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000464 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000465bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
466 return false;
467}
468bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
469 return false;
470}
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000471void PPCTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
472 const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000473 uint64_t BaseAddr, uint64_t SymVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000474
Davide Italianocde93362015-09-26 00:32:04 +0000475AArch64TargetInfo::AArch64TargetInfo() {
476 // PCRelReloc = FIXME
477 // GotReloc = FIXME
Hal Finkel47290642015-10-08 21:25:04 +0000478 VAStart = 0x400000;
Davide Italianocde93362015-09-26 00:32:04 +0000479}
480void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000481 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000482bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
483 const SymbolBody &S) const {
484 return false;
485}
486bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
487 const SymbolBody &S) const {
488 return false;
489}
Davide Italiano1d750a62015-09-27 08:45:38 +0000490
Davide Italianoef4be6b2015-10-06 19:01:32 +0000491static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000492 uint32_t ImmLo = (Imm & 0x3) << 29;
493 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
494 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000495 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000496}
497
Davide Italiano318ca222015-10-02 22:13:51 +0000498// Page(Expr) is the page address of the expression Expr, defined
499// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000500// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000501static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000502 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000503}
504
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000505void AArch64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
506 const void *RelP, uint32_t Type,
507 uint64_t BaseAddr, uint64_t SymVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000508 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
509 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
510
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000511 uint8_t *L = Buf + Rel.r_offset;
Davide Italiano1d750a62015-09-27 08:45:38 +0000512 uint64_t S = SymVA;
513 int64_t A = Rel.r_addend;
514 uint64_t P = BaseAddr + Rel.r_offset;
515 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000516 case R_AARCH64_ABS16:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000517 if (!isInt<16>(S + A))
518 error("Relocation R_AARCH64_ABS16 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000519 write16le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000520 break;
521 case R_AARCH64_ABS32:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000522 if (!isInt<32>(S + A))
523 error("Relocation R_AARCH64_ABS32 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000524 write32le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000525 break;
526 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000527 // No overflow check needed.
Davide Italiano06d84322015-10-07 22:10:02 +0000528 write64le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000529 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000530 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000531 // No overflow check needed.
532 or32le(L, ((S + A) & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000533 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000534 case R_AARCH64_ADR_PREL_LO21: {
535 uint64_t X = S + A - P;
536 if (!isInt<21>(X))
537 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
538 updateAArch64Adr(L, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000539 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000540 }
541 case R_AARCH64_ADR_PREL_PG_HI21: {
542 uint64_t X = getAArch64Page(S + A) - getAArch64Page(P);
543 if (!isInt<33>(X))
544 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
545 updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000546 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000547 }
Davide Italiano1d750a62015-09-27 08:45:38 +0000548 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000549 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000550 }
551}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000552
553MipsTargetInfo::MipsTargetInfo() {
554 // PCRelReloc = FIXME
555 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000556 PageSize = 65536;
Hal Finkel47290642015-10-08 21:25:04 +0000557 VAStart = 0x400000;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000558}
559
560void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000561 uint64_t PltEntryAddr) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000562
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000563bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
564 return false;
565}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000566
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000567bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
568 return false;
569}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000570
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000571void MipsTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
572 const void *RelP, uint32_t Type,
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000573 uint64_t BaseAddr, uint64_t SymVA) const {
574 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
575 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
576
577 switch (Type) {
578 case R_MIPS_32:
579 add32le(Buf + Rel.r_offset, SymVA);
580 break;
581 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000582 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000583 }
584}
Rafael Espindola01205f72015-09-22 18:19:46 +0000585}
586}