blob: 76fae6fcd4d8afcdf56794ba39f3e882ba389473 [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();
50 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
51 uint64_t PltEntryAddr) const override;
52 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
53 bool relocPointsToGot(uint32_t Type) const override;
54 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
55 void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
56 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +000057 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000058};
59
60class X86_64TargetInfo final : public TargetInfo {
61public:
62 X86_64TargetInfo();
63 unsigned getPLTRefReloc(unsigned Type) const override;
64 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
65 uint64_t PltEntryAddr) const override;
66 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
67 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
68 void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
69 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +000070 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000071 bool isRelRelative(uint32_t Type) const override;
72};
73
74class PPC64TargetInfo final : public TargetInfo {
75public:
76 PPC64TargetInfo();
77 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
78 uint64_t PltEntryAddr) const override;
79 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
80 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
81 void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
82 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +000083 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000084 bool isRelRelative(uint32_t Type) const override;
85};
86
87class PPCTargetInfo final : public TargetInfo {
88public:
89 PPCTargetInfo();
90 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
91 uint64_t PltEntryAddr) const override;
92 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
93 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
94 void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
95 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +000096 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +000097};
98
99class AArch64TargetInfo final : public TargetInfo {
100public:
101 AArch64TargetInfo();
102 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
103 uint64_t PltEntryAddr) const override;
104 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
105 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
106 void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
107 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +0000108 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000109};
110
111template <class ELFT> class MipsTargetInfo final : public TargetInfo {
112public:
113 MipsTargetInfo();
114 void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
115 uint64_t PltEntryAddr) const override;
116 bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override;
117 bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override;
118 void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
119 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +0000120 uint64_t SA) const override;
Rui Ueyamaefc23de2015-10-14 21:30:32 +0000121};
122} // anonymous namespace
123
Rui Ueyama91004392015-10-13 16:08:15 +0000124TargetInfo *createTarget() {
125 switch (Config->EMachine) {
126 case EM_386:
127 return new X86TargetInfo();
128 case EM_AARCH64:
129 return new AArch64TargetInfo();
130 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000131 switch (Config->EKind) {
132 case ELF32LEKind:
133 return new MipsTargetInfo<ELF32LE>();
134 case ELF32BEKind:
135 return new MipsTargetInfo<ELF32BE>();
136 default:
137 error("Unsupported MIPS target");
138 }
Rui Ueyama91004392015-10-13 16:08:15 +0000139 case EM_PPC:
140 return new PPCTargetInfo();
141 case EM_PPC64:
142 return new PPC64TargetInfo();
143 case EM_X86_64:
144 return new X86_64TargetInfo();
145 }
146 error("Unknown target machine");
147}
148
Rafael Espindola01205f72015-09-22 18:19:46 +0000149TargetInfo::~TargetInfo() {}
150
Rafael Espindola227556e2015-10-14 16:15:46 +0000151unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; }
152
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000153bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
154
Rafael Espindolaae244002015-10-05 19:30:12 +0000155bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
156
Rafael Espindola7f074422015-09-22 21:35:51 +0000157X86TargetInfo::X86TargetInfo() {
158 PCRelReloc = R_386_PC32;
159 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000160 GotRefReloc = R_386_GOT32;
Rafael Espindola7f074422015-09-22 21:35:51 +0000161}
Rafael Espindola01205f72015-09-22 18:19:46 +0000162
163void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000164 uint64_t PltEntryAddr) const {
165 // jmpl *val; nop; nop
166 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000167 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +0000168 assert(isUInt<32>(GotEntryAddr));
169 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +0000170}
171
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000172bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000173 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000174}
175
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000176bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
177 return Type == R_386_GOTPC;
178}
179
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000180bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000181 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000182}
183
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000184void X86TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
185 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +0000186 uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000187 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
188 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
189
190 uint32_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000191 uint8_t *Loc = Buf + Offset;
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 Ueyama66072272015-10-15 19:52:27 +0000197 add32le(Loc, SA - BaseAddr - Offset);
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() {
208 PCRelReloc = R_X86_64_PC32;
209 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000210 GotRefReloc = R_X86_64_PC32;
Rafael Espindolaae244002015-10-05 19:30:12 +0000211 RelativeReloc = R_X86_64_RELATIVE;
Rafael Espindola7f074422015-09-22 21:35:51 +0000212}
Rafael Espindola01205f72015-09-22 18:19:46 +0000213
214void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000215 uint64_t PltEntryAddr) const {
216 // jmpq *val(%rip); nop; nop
217 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000218 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000219
Rui Ueyamac58656c2015-10-13 16:59:30 +0000220 uint64_t NextPC = PltEntryAddr + 6;
221 int64_t Delta = GotEntryAddr - NextPC;
222 assert(isInt<32>(Delta));
223 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000224}
225
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000226bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000227 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000228}
229
Rafael Espindola227556e2015-10-14 16:15:46 +0000230unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const {
231 switch (Type) {
232 case R_X86_64_32:
233 return R_X86_64_32;
234 case R_X86_64_PC32:
235 case R_X86_64_PLT32:
236 return R_X86_64_PC32;
237 }
238 llvm_unreachable("Unexpected relocation");
239}
240
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000241bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000242 switch (Type) {
243 default:
244 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000245 case R_X86_64_32:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000246 case R_X86_64_PC32:
247 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000248 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000249 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000250 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000251 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000252 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
253 // libc.so.
254 //
255 // The general idea on how to handle such cases is to create a PLT entry
256 // and use that as the function value.
257 //
258 // For the static linking part, we just return true and everything else
259 // will use the the PLT entry as the address.
260 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000261 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000262 // still works. We need the help of the dynamic linker for that. We
263 // let it know that we have a direct reference to a so symbol by creating
264 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000265 // dynamic linker resolves the symbol to the value of the symbol we created.
266 // This is true even for got entries, so pointer equality is maintained.
267 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000268 // real function is a dedicated got entry used by the plt. That is
269 // identified by special relocation types (R_X86_64_JUMP_SLOT,
270 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000271 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000272 case R_X86_64_PLT32:
273 return true;
274 }
275}
Rafael Espindolac4010882015-09-22 20:54:08 +0000276
Rafael Espindolaae244002015-10-05 19:30:12 +0000277bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
278 switch (Type) {
279 default:
280 return false;
281 case R_X86_64_PC64:
282 case R_X86_64_PC32:
283 case R_X86_64_PC16:
284 case R_X86_64_PC8:
285 return true;
286 }
287}
288
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000289void X86_64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
290 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000291 uint64_t BaseAddr, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000292 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
293 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
294
295 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000296 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000297 switch (Type) {
298 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000299 case R_X86_64_GOTPCREL:
Rui Ueyama66072272015-10-15 19:52:27 +0000300 write32le(Loc, SA - BaseAddr - Offset);
Rafael Espindolac4010882015-09-22 20:54:08 +0000301 break;
302 case R_X86_64_64:
Rui Ueyama66072272015-10-15 19:52:27 +0000303 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000304 break;
305 case R_X86_64_32: {
306 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000307 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000308 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000309 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000310 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000311 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000312 break;
313 }
314 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000315 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000316 }
317}
318
Hal Finkel3c8cc672015-10-12 20:56:18 +0000319// Relocation masks following the #lo(value), #hi(value), #ha(value),
320// #higher(value), #highera(value), #highest(value), and #highesta(value)
321// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
322// document.
323
324static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; }
325
326static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; }
327
328static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; }
329
330static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; }
331
332static uint16_t applyPPCHighera(uint64_t V) {
333 return ((V + 0x8000) >> 32) & 0xffff;
334}
335
336static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
337
338static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
339
Rafael Espindolac4010882015-09-22 20:54:08 +0000340PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000341 PCRelReloc = R_PPC64_REL24;
342 GotReloc = R_PPC64_GLOB_DAT;
343 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000344 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000345 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000346
347 // We need 64K pages (at least under glibc/Linux, the loader won't
348 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000349 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000350
351 // The PPC64 ELF ABI v1 spec, says:
352 //
353 // It is normally desirable to put segments with different characteristics
354 // in separate 256 Mbyte portions of the address space, to give the
355 // operating system full paging flexibility in the 64-bit address space.
356 //
357 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
358 // use 0x10000000 as the starting address.
359 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000360}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000361
362static uint64_t getPPC64TocBase() {
363 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
364 // order. The TOC starts where the first of these sections starts.
365
366 // FIXME: This obviously does not do the right thing when there is no .got
367 // section, but there is a .toc or .tocbss section.
368 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
369 if (!TocVA)
370 TocVA = Out<ELF64BE>::Plt->getVA();
371
372 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
373 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
374 // code (crt1.o) assumes that you can get from the TOC base to the
375 // start of the .toc section with only a single (signed) 16-bit relocation.
376 return TocVA + 0x8000;
377}
378
Rafael Espindolac4010882015-09-22 20:54:08 +0000379void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000380 uint64_t PltEntryAddr) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000381 uint64_t Off = GotEntryAddr - getPPC64TocBase();
382
383 // FIXME: What we should do, in theory, is get the offset of the function
384 // descriptor in the .opd section, and use that as the offset from %r2 (the
385 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
386 // be a pointer to the function descriptor in the .opd section. Using
387 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
388
Hal Finkelfa92f682015-10-13 21:47:34 +0000389 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000390 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
391 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
392 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
393 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
394 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
395 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
396 write32be(Buf + 28, 0x4e800420); // bctr
397}
398
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000399bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000400 if (relocNeedsPlt(Type, S))
401 return true;
402
403 switch (Type) {
404 default: return false;
405 case R_PPC64_GOT16:
406 case R_PPC64_GOT16_LO:
407 case R_PPC64_GOT16_HI:
408 case R_PPC64_GOT16_HA:
409 case R_PPC64_GOT16_DS:
410 case R_PPC64_GOT16_LO_DS:
411 return true;
412 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000413}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000414
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000415bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000416 if (Type != R_PPC64_REL24)
417 return false;
418
419 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel000561c2015-10-15 16:12:35 +0000420 return S.isShared() || (S.isUndefined() && S.isWeak());
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000421}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000422
Hal Finkelbe0823d2015-10-12 20:58:52 +0000423bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
424 switch (Type) {
425 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000426 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000427 case R_PPC64_TOC:
428 case R_PPC64_ADDR64:
429 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000430 }
431}
432
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000433void PPC64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
434 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000435 uint64_t BaseAddr, uint64_t SA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000436 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
437 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
438
Hal Finkel3c8cc672015-10-12 20:56:18 +0000439 uint8_t *L = Buf + Rel.r_offset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000440 uint64_t P = BaseAddr + Rel.r_offset;
441 uint64_t TB = getPPC64TocBase();
442
443 if (Type == R_PPC64_TOC) {
444 write64be(L, TB);
445 return;
446 }
447
448 // For a TOC-relative relocation, adjust the addend and proceed in terms of
449 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000450 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000451 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
452 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
453 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
454 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
455 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
456 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000457 default: break;
458 }
459
Hal Finkel3c8cc672015-10-12 20:56:18 +0000460 switch (Type) {
461 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000462 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000463 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000464 write16be(L, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000465 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000466 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000467 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000468 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000469 write16be(L, (read16be(L) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000470 break;
471 case R_PPC64_ADDR16_LO:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000472 write16be(L, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000473 break;
474 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000475 write16be(L, (read16be(L) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000476 break;
477 case R_PPC64_ADDR16_HI:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000478 write16be(L, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000479 break;
480 case R_PPC64_ADDR16_HA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000481 write16be(L, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000482 break;
483 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000484 write16be(L, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000485 break;
486 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000487 write16be(L, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000488 break;
489 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000490 write16be(L, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000491 break;
492 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000493 write16be(L, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000494 break;
495 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000496 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000497 error("Improper alignment for relocation R_PPC64_ADDR14");
498
499 // Preserve the AA/LK bits in the branch instruction
500 uint8_t AALK = L[3];
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000501 write16be(L + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000502 break;
503 }
504 case R_PPC64_REL16_LO:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000505 write16be(L, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000506 break;
507 case R_PPC64_REL16_HI:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000508 write16be(L, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000509 break;
510 case R_PPC64_REL16_HA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000511 write16be(L, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000512 break;
513 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000514 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000515 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000516 write32be(L, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000517 break;
518 case R_PPC64_REL24: {
Hal Finkeldaedc122015-10-12 23:16:53 +0000519 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
520 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000521 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000522
523 if (!InPlt && Out<ELF64BE>::Opd) {
524 // If this is a local call, and we currently have the address of a
525 // function-descriptor, get the underlying code address instead.
526 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
527 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000528 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000529
530 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000531 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000532 }
533
Hal Finkel3c8cc672015-10-12 20:56:18 +0000534 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000535 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000536 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000537 write32be(L, (read32be(L) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000538
Hal Finkel515ed442015-10-13 20:31:33 +0000539 if (InPlt && L + 8 <= BufEnd &&
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000540 read32be(L + 4) == 0x60000000 /* nop */)
541 write32be(L + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000542 break;
543 }
544 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000545 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000546 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000547 write32be(L, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000548 break;
549 case R_PPC64_REL64:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000550 write64be(L, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000551 break;
552 case R_PPC64_ADDR64:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000553 write64be(L, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000554 break;
555 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000556 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000557 }
558}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000559
560PPCTargetInfo::PPCTargetInfo() {
561 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000562 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000563 PageSize = 65536;
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000564}
565void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000566 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000567bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
568 return false;
569}
570bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
571 return false;
572}
Rui Ueyama66072272015-10-15 19:52:27 +0000573void PPCTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
574 uint32_t Type, uint64_t BaseAddr,
575 uint64_t SA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000576
Davide Italianocde93362015-09-26 00:32:04 +0000577AArch64TargetInfo::AArch64TargetInfo() {
578 // PCRelReloc = FIXME
579 // GotReloc = FIXME
580}
581void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000582 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000583bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
584 const SymbolBody &S) const {
585 return false;
586}
587bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
588 const SymbolBody &S) const {
589 return false;
590}
Davide Italiano1d750a62015-09-27 08:45:38 +0000591
Davide Italianoef4be6b2015-10-06 19:01:32 +0000592static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000593 uint32_t ImmLo = (Imm & 0x3) << 29;
594 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
595 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000596 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000597}
598
Davide Italiano318ca222015-10-02 22:13:51 +0000599// Page(Expr) is the page address of the expression Expr, defined
600// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000601// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000602static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000603 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000604}
605
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000606void AArch64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
607 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000608 uint64_t BaseAddr, uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000609 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
610 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
611
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000612 uint8_t *L = Buf + Rel.r_offset;
Davide Italiano1d750a62015-09-27 08:45:38 +0000613 uint64_t P = BaseAddr + Rel.r_offset;
614 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000615 case R_AARCH64_ABS16:
Rafael Espindola826941a2015-10-15 18:19:39 +0000616 if (!isInt<16>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000617 error("Relocation R_AARCH64_ABS16 out of range");
Rafael Espindola826941a2015-10-15 18:19:39 +0000618 write16le(L, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000619 break;
620 case R_AARCH64_ABS32:
Rafael Espindola826941a2015-10-15 18:19:39 +0000621 if (!isInt<32>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000622 error("Relocation R_AARCH64_ABS32 out of range");
Rafael Espindola826941a2015-10-15 18:19:39 +0000623 write32le(L, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000624 break;
625 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000626 // No overflow check needed.
Rafael Espindola826941a2015-10-15 18:19:39 +0000627 write64le(L, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000628 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000629 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000630 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000631 // This relocation stores 12 bits and there's no instruction
632 // to do it. Instead, we do a 32 bits store of the value
633 // of r_addend bitwise-or'ed L. This assumes that the addend
634 // bits in L are zero.
Rafael Espindola826941a2015-10-15 18:19:39 +0000635 or32le(L, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000636 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000637 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000638 uint64_t X = SA - P;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000639 if (!isInt<21>(X))
640 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
641 updateAArch64Adr(L, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000642 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000643 }
644 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000645 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000646 if (!isInt<33>(X))
647 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
648 updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000649 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000650 }
Davide Italiano1d750a62015-09-27 08:45:38 +0000651 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000652 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000653 }
654}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000655
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000656template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan49829a12015-09-29 05:34:03 +0000657 // PCRelReloc = FIXME
658 // GotReloc = FIXME
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>
663void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
664 uint64_t PltEntryAddr) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000665
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000666template <class ELFT>
667bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
668 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000669 return false;
670}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000671
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000672template <class ELFT>
673bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
674 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000675 return false;
676}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000677
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000678template <class ELFT>
679void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
680 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000681 uint64_t BaseAddr, uint64_t SA) const {
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000682 const bool IsLE = ELFT::TargetEndianness == support::little;
683 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000684 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
685
686 switch (Type) {
687 case R_MIPS_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000688 add32<IsLE>(Buf + Rel.r_offset, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000689 break;
690 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000691 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000692 }
693}
Rafael Espindola01205f72015-09-22 18:19:46 +0000694}
695}