blob: df7b34cf8498f0da068ab3b06a5122637b528061 [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
30TargetInfo::~TargetInfo() {}
31
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000032bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
33
Rafael Espindolaae244002015-10-05 19:30:12 +000034bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
35
Rafael Espindola7f074422015-09-22 21:35:51 +000036X86TargetInfo::X86TargetInfo() {
37 PCRelReloc = R_386_PC32;
38 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000039 GotRefReloc = R_386_GOT32;
Hal Finkel47290642015-10-08 21:25:04 +000040 VAStart = 0x10000;
Rafael Espindola7f074422015-09-22 21:35:51 +000041}
Rafael Espindola01205f72015-09-22 18:19:46 +000042
43void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
44 uint64_t PltEntryAddr) const {
Rui Ueyama1500a902015-09-29 23:00:47 +000045 // jmpl *val; nop; nop
46 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
47 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +000048 assert(isUInt<32>(GotEntryAddr));
Rui Ueyama1500a902015-09-29 23:00:47 +000049 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +000050}
51
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000052bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +000053 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +000054}
55
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000056bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
57 return Type == R_386_GOTPC;
58}
59
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000060bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +000061 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +000062}
63
Rui Ueyama87bc41b2015-10-06 18:54:43 +000064static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); }
Rui Ueyama87bc41b2015-10-06 18:54:43 +000065static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); }
Rafael Espindola0872ea32015-09-24 14:16:02 +000066
Rafael Espindolac4010882015-09-22 20:54:08 +000067void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +000068 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000069 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
70 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
71
72 uint32_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +000073 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000074 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000075 case R_386_GOT32:
Rui Ueyamaaf21d922015-10-08 20:06:07 +000076 add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +000077 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000078 case R_386_PC32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +000079 add32le(Loc, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000080 break;
81 case R_386_32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +000082 add32le(Loc, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +000083 break;
84 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +000085 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +000086 }
87}
88
Rafael Espindola7f074422015-09-22 21:35:51 +000089X86_64TargetInfo::X86_64TargetInfo() {
90 PCRelReloc = R_X86_64_PC32;
91 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000092 GotRefReloc = R_X86_64_PC32;
Rafael Espindolaae244002015-10-05 19:30:12 +000093 RelativeReloc = R_X86_64_RELATIVE;
Hal Finkel47290642015-10-08 21:25:04 +000094
95 // On freebsd x86_64 the first page cannot be mmaped.
96 // On linux that is controled by vm.mmap_min_addr. At least on some x86_64
97 // installs that is 65536, so the first 15 pages cannot be used.
98 // Given that, the smallest value that can be used in here is 0x10000.
99 // If using 2MB pages, the smallest page aligned address that works is
100 // 0x200000, but it looks like every OS uses 4k pages for executables.
101 VAStart = 0x10000;
Rafael Espindola7f074422015-09-22 21:35:51 +0000102}
Rafael Espindola01205f72015-09-22 18:19:46 +0000103
104void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
105 uint64_t PltEntryAddr) const {
Rui Ueyama1500a902015-09-29 23:00:47 +0000106 // jmpq *val(%rip); nop; nop
107 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
108 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000109
Rui Ueyamae3fbc892015-09-29 23:25:21 +0000110 uint64_t NextPC = PltEntryAddr + 6;
111 int64_t Delta = GotEntryAddr - NextPC;
Rafael Espindola01205f72015-09-22 18:19:46 +0000112 assert(isInt<32>(Delta));
Rui Ueyama1500a902015-09-29 23:00:47 +0000113 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000114}
115
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000116bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000117 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000118}
119
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000120bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000121 switch (Type) {
122 default:
123 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000124 case R_X86_64_PC32:
125 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000126 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000127 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000128 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000129 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000130 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
131 // libc.so.
132 //
133 // The general idea on how to handle such cases is to create a PLT entry
134 // and use that as the function value.
135 //
136 // For the static linking part, we just return true and everything else
137 // will use the the PLT entry as the address.
138 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000139 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000140 // still works. We need the help of the dynamic linker for that. We
141 // let it know that we have a direct reference to a so symbol by creating
142 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000143 // dynamic linker resolves the symbol to the value of the symbol we created.
144 // This is true even for got entries, so pointer equality is maintained.
145 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000146 // real function is a dedicated got entry used by the plt. That is
147 // identified by special relocation types (R_X86_64_JUMP_SLOT,
148 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000149 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000150 case R_X86_64_PLT32:
151 return true;
152 }
153}
Rafael Espindolac4010882015-09-22 20:54:08 +0000154
Rafael Espindolaae244002015-10-05 19:30:12 +0000155bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
156 switch (Type) {
157 default:
158 return false;
159 case R_X86_64_PC64:
160 case R_X86_64_PC32:
161 case R_X86_64_PC16:
162 case R_X86_64_PC8:
163 return true;
164 }
165}
166
Rafael Espindolac4010882015-09-22 20:54:08 +0000167void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
168 uint32_t Type, uint64_t BaseAddr,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000169 uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000170 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
171 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
172
173 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000174 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000175 switch (Type) {
176 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000177 case R_X86_64_GOTPCREL:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000178 write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000179 break;
180 case R_X86_64_64:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000181 write64le(Loc, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000182 break;
183 case R_X86_64_32: {
184 case R_X86_64_32S:
185 uint64_t VA = SymVA + Rel.r_addend;
186 if (Type == R_X86_64_32 && !isUInt<32>(VA))
187 error("R_X86_64_32 out of range");
188 else if (!isInt<32>(VA))
189 error("R_X86_64_32S out of range");
190
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000191 write32le(Loc, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000192 break;
193 }
194 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000195 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000196 }
197}
198
Hal Finkel3c8cc672015-10-12 20:56:18 +0000199// Relocation masks following the #lo(value), #hi(value), #ha(value),
200// #higher(value), #highera(value), #highest(value), and #highesta(value)
201// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
202// document.
203
204static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; }
205
206static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; }
207
208static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; }
209
210static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; }
211
212static uint16_t applyPPCHighera(uint64_t V) {
213 return ((V + 0x8000) >> 32) & 0xffff;
214}
215
216static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
217
218static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
219
Rafael Espindolac4010882015-09-22 20:54:08 +0000220PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000221 PCRelReloc = R_PPC64_REL24;
222 GotReloc = R_PPC64_GLOB_DAT;
223 GotRefReloc = R_PPC64_REL64;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000224 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000225
226 // We need 64K pages (at least under glibc/Linux, the loader won't
227 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000228 PageSize = 65536;
Hal Finkelc848b322015-10-12 19:34:29 +0000229
Hal Finkel47290642015-10-08 21:25:04 +0000230 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000231}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000232
233static uint64_t getPPC64TocBase() {
234 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
235 // order. The TOC starts where the first of these sections starts.
236
237 // FIXME: This obviously does not do the right thing when there is no .got
238 // section, but there is a .toc or .tocbss section.
239 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
240 if (!TocVA)
241 TocVA = Out<ELF64BE>::Plt->getVA();
242
243 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
244 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
245 // code (crt1.o) assumes that you can get from the TOC base to the
246 // start of the .toc section with only a single (signed) 16-bit relocation.
247 return TocVA + 0x8000;
248}
249
Rafael Espindolac4010882015-09-22 20:54:08 +0000250void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Hal Finkel3c8cc672015-10-12 20:56:18 +0000251 uint64_t PltEntryAddr) const {
252 uint64_t Off = GotEntryAddr - getPPC64TocBase();
253
254 // FIXME: What we should do, in theory, is get the offset of the function
255 // descriptor in the .opd section, and use that as the offset from %r2 (the
256 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
257 // be a pointer to the function descriptor in the .opd section. Using
258 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
259
260 write32be(Buf, 0xf8410000); // std %r2, 40(%r1)
261 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
262 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
263 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
264 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
265 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
266 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
267 write32be(Buf + 28, 0x4e800420); // bctr
268}
269
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000270bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000271 if (relocNeedsPlt(Type, S))
272 return true;
273
274 switch (Type) {
275 default: return false;
276 case R_PPC64_GOT16:
277 case R_PPC64_GOT16_LO:
278 case R_PPC64_GOT16_HI:
279 case R_PPC64_GOT16_HA:
280 case R_PPC64_GOT16_DS:
281 case R_PPC64_GOT16_LO_DS:
282 return true;
283 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000284}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000285
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000286bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000287 if (Type != R_PPC64_REL24)
288 return false;
289
290 // These are function calls that need to be redirected through a PLT stub.
291 return S.isShared() || (S.isUndefined() && S.isWeak());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000292}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000293
Rafael Espindolac4010882015-09-22 20:54:08 +0000294void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000295 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000296 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
297 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
298
Hal Finkel3c8cc672015-10-12 20:56:18 +0000299 uint8_t *L = Buf + Rel.r_offset;
300 uint64_t S = SymVA;
301 int64_t A = Rel.r_addend;
302 uint64_t P = BaseAddr + Rel.r_offset;
303 uint64_t TB = getPPC64TocBase();
304
305 if (Type == R_PPC64_TOC) {
306 write64be(L, TB);
307 return;
308 }
309
310 // For a TOC-relative relocation, adjust the addend and proceed in terms of
311 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000312 switch (Type) {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000313 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; A -= TB; break;
314 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; A -= TB; break;
315 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; A -= TB; break;
316 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; A -= TB; break;
317 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; A -= TB; break;
318 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; A -= TB; break;
319 default: break;
320 }
321
322 uint64_t R = S + A;
323
324 switch (Type) {
325 case R_PPC64_ADDR16:
326 write16be(L, applyPPCLo(R));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000327 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000328 case R_PPC64_ADDR16_DS:
329 if (!isInt<16>(R))
330 error("Relocation R_PPC64_ADDR16_DS overflow");
331 write16be(L, (read16be(L) & 3) | (R & ~3));
332 break;
333 case R_PPC64_ADDR16_LO:
334 write16be(L, applyPPCLo(R));
335 break;
336 case R_PPC64_ADDR16_LO_DS:
337 write16be(L, (read16be(L) & 3) | (applyPPCLo(R) & ~3));
338 break;
339 case R_PPC64_ADDR16_HI:
340 write16be(L, applyPPCHi(R));
341 break;
342 case R_PPC64_ADDR16_HA:
343 write16be(L, applyPPCHa(R));
344 break;
345 case R_PPC64_ADDR16_HIGHER:
346 write16be(L, applyPPCHigher(R));
347 break;
348 case R_PPC64_ADDR16_HIGHERA:
349 write16be(L, applyPPCHighera(R));
350 break;
351 case R_PPC64_ADDR16_HIGHEST:
352 write16be(L, applyPPCHighest(R));
353 break;
354 case R_PPC64_ADDR16_HIGHESTA:
355 write16be(L, applyPPCHighesta(R));
356 break;
357 case R_PPC64_ADDR14: {
358 if ((R & 3) != 0)
359 error("Improper alignment for relocation R_PPC64_ADDR14");
360
361 // Preserve the AA/LK bits in the branch instruction
362 uint8_t AALK = L[3];
363 write16be(L + 2, (AALK & 3) | (R & 0xfffc));
364 break;
365 }
366 case R_PPC64_REL16_LO:
367 write16be(L, applyPPCLo(R - P));
368 break;
369 case R_PPC64_REL16_HI:
370 write16be(L, applyPPCHi(R - P));
371 break;
372 case R_PPC64_REL16_HA:
373 write16be(L, applyPPCHa(R - P));
374 break;
375 case R_PPC64_ADDR32:
376 if (!isInt<32>(R))
377 error("Relocation R_PPC64_ADDR32 overflow");
378 write32be(L, R);
379 break;
380 case R_PPC64_REL24: {
381 uint32_t Mask = 0x03FFFFFC;
382 if (!isInt<24>(R - P))
383 error("Relocation R_PPC64_REL24 overflow");
384 write32be(L, (read32be(L) & ~Mask) | ((R - P) & Mask));
385 break;
386 }
387 case R_PPC64_REL32:
388 if (!isInt<32>(R - P))
389 error("Relocation R_PPC64_REL32 overflow");
390 write32be(L, R - P);
391 break;
392 case R_PPC64_REL64:
393 write64be(L, R - P);
394 break;
395 case R_PPC64_ADDR64:
396 write64be(L, R);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000397 break;
398 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000399 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000400 }
401}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000402
403PPCTargetInfo::PPCTargetInfo() {
404 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000405 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000406 PageSize = 65536;
Hal Finkel47290642015-10-08 21:25:04 +0000407 VAStart = 0x10000000;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000408}
409void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
410 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000411bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
412 return false;
413}
414bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
415 return false;
416}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000417void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000418 uint64_t BaseAddr, uint64_t SymVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000419
420ARMTargetInfo::ARMTargetInfo() {
421 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000422 // GotReloc = FIXME
Hal Finkel47290642015-10-08 21:25:04 +0000423 VAStart = 0x8000;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000424}
425void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
426 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000427bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
428 return false;
429}
430bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
431 return false;
432}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000433void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000434 uint64_t BaseAddr, uint64_t SymVA) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000435
436AArch64TargetInfo::AArch64TargetInfo() {
437 // PCRelReloc = FIXME
438 // GotReloc = FIXME
Hal Finkel47290642015-10-08 21:25:04 +0000439 VAStart = 0x400000;
Davide Italianocde93362015-09-26 00:32:04 +0000440}
441void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
442 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000443bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
444 const SymbolBody &S) const {
445 return false;
446}
447bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
448 const SymbolBody &S) const {
449 return false;
450}
Davide Italiano1d750a62015-09-27 08:45:38 +0000451
Davide Italianoef4be6b2015-10-06 19:01:32 +0000452static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000453 uint32_t ImmLo = (Imm & 0x3) << 29;
454 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
455 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000456 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000457}
458
Davide Italiano318ca222015-10-02 22:13:51 +0000459// Page(Expr) is the page address of the expression Expr, defined
460// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000461// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000462static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000463 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000464}
465
Davide Italianocde93362015-09-26 00:32:04 +0000466void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
467 uint32_t Type, uint64_t BaseAddr,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000468 uint64_t SymVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000469 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
470 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
471
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000472 uint8_t *L = Buf + Rel.r_offset;
Davide Italiano1d750a62015-09-27 08:45:38 +0000473 uint64_t S = SymVA;
474 int64_t A = Rel.r_addend;
475 uint64_t P = BaseAddr + Rel.r_offset;
476 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000477 case R_AARCH64_ABS16:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000478 if (!isInt<16>(S + A))
479 error("Relocation R_AARCH64_ABS16 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000480 write16le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000481 break;
482 case R_AARCH64_ABS32:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000483 if (!isInt<32>(S + A))
484 error("Relocation R_AARCH64_ABS32 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000485 write32le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000486 break;
487 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000488 // No overflow check needed.
Davide Italiano06d84322015-10-07 22:10:02 +0000489 write64le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000490 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000491 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000492 // No overflow check needed.
493 or32le(L, ((S + A) & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000494 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000495 case R_AARCH64_ADR_PREL_LO21: {
496 uint64_t X = S + A - P;
497 if (!isInt<21>(X))
498 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
499 updateAArch64Adr(L, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000500 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000501 }
502 case R_AARCH64_ADR_PREL_PG_HI21: {
503 uint64_t X = getAArch64Page(S + A) - getAArch64Page(P);
504 if (!isInt<33>(X))
505 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
506 updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000507 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000508 }
Davide Italiano1d750a62015-09-27 08:45:38 +0000509 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000510 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000511 }
512}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000513
514MipsTargetInfo::MipsTargetInfo() {
515 // PCRelReloc = FIXME
516 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000517 PageSize = 65536;
Hal Finkel47290642015-10-08 21:25:04 +0000518 VAStart = 0x400000;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000519}
520
521void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
522 uint64_t PltEntryAddr) const {}
523
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000524bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
525 return false;
526}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000527
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000528bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
529 return false;
530}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000531
532void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000533 uint64_t BaseAddr, uint64_t SymVA) const {
534 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
535 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
536
537 switch (Type) {
538 case R_MIPS_32:
539 add32le(Buf + Rel.r_offset, SymVA);
540 break;
541 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000542 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000543 }
544}
Rafael Espindola01205f72015-09-22 18:19:46 +0000545}
546}