blob: bb07c777c099312b7b0af818f9e5bbd5fee9ea3c [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
Rui Ueyamaefc23de2015-10-14 21:30:32 +000087class AArch64TargetInfo final : public TargetInfo {
88public:
89 AArch64TargetInfo();
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
99template <class ELFT> class MipsTargetInfo final : public TargetInfo {
100public:
101 MipsTargetInfo();
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} // anonymous namespace
111
Rui Ueyama91004392015-10-13 16:08:15 +0000112TargetInfo *createTarget() {
113 switch (Config->EMachine) {
114 case EM_386:
115 return new X86TargetInfo();
116 case EM_AARCH64:
117 return new AArch64TargetInfo();
118 case EM_MIPS:
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000119 switch (Config->EKind) {
120 case ELF32LEKind:
121 return new MipsTargetInfo<ELF32LE>();
122 case ELF32BEKind:
123 return new MipsTargetInfo<ELF32BE>();
124 default:
125 error("Unsupported MIPS target");
126 }
Rui Ueyama91004392015-10-13 16:08:15 +0000127 case EM_PPC64:
128 return new PPC64TargetInfo();
129 case EM_X86_64:
130 return new X86_64TargetInfo();
131 }
132 error("Unknown target machine");
133}
134
Rafael Espindola01205f72015-09-22 18:19:46 +0000135TargetInfo::~TargetInfo() {}
136
Rafael Espindola227556e2015-10-14 16:15:46 +0000137unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; }
138
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000139bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
140
Rafael Espindolaae244002015-10-05 19:30:12 +0000141bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
142
Rafael Espindola7f074422015-09-22 21:35:51 +0000143X86TargetInfo::X86TargetInfo() {
144 PCRelReloc = R_386_PC32;
145 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000146 GotRefReloc = R_386_GOT32;
Rafael Espindola7f074422015-09-22 21:35:51 +0000147}
Rafael Espindola01205f72015-09-22 18:19:46 +0000148
149void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000150 uint64_t PltEntryAddr) const {
151 // jmpl *val; nop; nop
152 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000153 memcpy(Buf, Inst, sizeof(Inst));
Rui Ueyamac58656c2015-10-13 16:59:30 +0000154 assert(isUInt<32>(GotEntryAddr));
155 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +0000156}
157
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000158bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000159 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000160}
161
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +0000162bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
163 return Type == R_386_GOTPC;
164}
165
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000166bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +0000167 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +0000168}
169
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000170void X86TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP,
171 uint32_t Type, uint64_t BaseAddr,
Rui Ueyama66072272015-10-15 19:52:27 +0000172 uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000173 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
174 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
175
176 uint32_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000177 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000178 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000179 case R_386_GOT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000180 add32le(Loc, SA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000181 break;
Rafael Espindolac4010882015-09-22 20:54:08 +0000182 case R_386_PC32:
Rui Ueyama66072272015-10-15 19:52:27 +0000183 add32le(Loc, SA - BaseAddr - Offset);
Rafael Espindolac4010882015-09-22 20:54:08 +0000184 break;
185 case R_386_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000186 add32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000187 break;
188 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000189 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000190 }
191}
192
Rafael Espindola7f074422015-09-22 21:35:51 +0000193X86_64TargetInfo::X86_64TargetInfo() {
194 PCRelReloc = R_X86_64_PC32;
195 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000196 GotRefReloc = R_X86_64_PC32;
Rafael Espindolaae244002015-10-05 19:30:12 +0000197 RelativeReloc = R_X86_64_RELATIVE;
Rafael Espindola7f074422015-09-22 21:35:51 +0000198}
Rafael Espindola01205f72015-09-22 18:19:46 +0000199
200void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000201 uint64_t PltEntryAddr) const {
202 // jmpq *val(%rip); nop; nop
203 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
Rui Ueyama1500a902015-09-29 23:00:47 +0000204 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000205
Rui Ueyamac58656c2015-10-13 16:59:30 +0000206 uint64_t NextPC = PltEntryAddr + 6;
207 int64_t Delta = GotEntryAddr - NextPC;
208 assert(isInt<32>(Delta));
209 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000210}
211
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000212bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000213 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000214}
215
Rafael Espindola227556e2015-10-14 16:15:46 +0000216unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const {
217 switch (Type) {
218 case R_X86_64_32:
219 return R_X86_64_32;
220 case R_X86_64_PC32:
221 case R_X86_64_PLT32:
222 return R_X86_64_PC32;
223 }
224 llvm_unreachable("Unexpected relocation");
225}
226
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000227bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000228 switch (Type) {
229 default:
230 return false;
Rafael Espindola227556e2015-10-14 16:15:46 +0000231 case R_X86_64_32:
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000232 case R_X86_64_PC32:
233 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000234 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000235 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000236 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000237 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000238 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
239 // libc.so.
240 //
241 // The general idea on how to handle such cases is to create a PLT entry
242 // and use that as the function value.
243 //
244 // For the static linking part, we just return true and everything else
245 // will use the the PLT entry as the address.
246 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000247 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000248 // still works. We need the help of the dynamic linker for that. We
249 // let it know that we have a direct reference to a so symbol by creating
250 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000251 // dynamic linker resolves the symbol to the value of the symbol we created.
252 // This is true even for got entries, so pointer equality is maintained.
253 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000254 // real function is a dedicated got entry used by the plt. That is
255 // identified by special relocation types (R_X86_64_JUMP_SLOT,
256 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000257 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000258 case R_X86_64_PLT32:
George Rimar8911d852015-10-16 23:52:24 +0000259 return canBePreempted(&S, true);
Rafael Espindola01205f72015-09-22 18:19:46 +0000260 }
261}
Rafael Espindolac4010882015-09-22 20:54:08 +0000262
Rafael Espindolaae244002015-10-05 19:30:12 +0000263bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
264 switch (Type) {
265 default:
266 return false;
267 case R_X86_64_PC64:
268 case R_X86_64_PC32:
269 case R_X86_64_PC16:
270 case R_X86_64_PC8:
Rafael Espindola69535df2015-10-19 05:20:01 +0000271 case R_X86_64_PLT32:
Rafael Espindolaae244002015-10-05 19:30:12 +0000272 return true;
273 }
274}
275
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000276void X86_64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
277 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000278 uint64_t BaseAddr, uint64_t SA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000279 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
280 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
281
282 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000283 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000284 switch (Type) {
285 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000286 case R_X86_64_GOTPCREL:
Rafael Espindola5045e442015-10-18 03:13:46 +0000287 case R_X86_64_PLT32:
Rui Ueyama66072272015-10-15 19:52:27 +0000288 write32le(Loc, SA - BaseAddr - Offset);
Rafael Espindolac4010882015-09-22 20:54:08 +0000289 break;
290 case R_X86_64_64:
Rui Ueyama66072272015-10-15 19:52:27 +0000291 write64le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000292 break;
293 case R_X86_64_32: {
294 case R_X86_64_32S:
Rui Ueyama66072272015-10-15 19:52:27 +0000295 if (Type == R_X86_64_32 && !isUInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000296 error("R_X86_64_32 out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000297 else if (!isInt<32>(SA))
Rafael Espindolac4010882015-09-22 20:54:08 +0000298 error("R_X86_64_32S out of range");
Rui Ueyama66072272015-10-15 19:52:27 +0000299 write32le(Loc, SA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000300 break;
301 }
302 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000303 error("unrecognized reloc " + Twine(Type));
Rafael Espindolac4010882015-09-22 20:54:08 +0000304 }
305}
306
Hal Finkel3c8cc672015-10-12 20:56:18 +0000307// Relocation masks following the #lo(value), #hi(value), #ha(value),
308// #higher(value), #highera(value), #highest(value), and #highesta(value)
309// macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
310// document.
311
312static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; }
313
314static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; }
315
316static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; }
317
318static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; }
319
320static uint16_t applyPPCHighera(uint64_t V) {
321 return ((V + 0x8000) >> 32) & 0xffff;
322}
323
324static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
325
326static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
327
Rafael Espindolac4010882015-09-22 20:54:08 +0000328PPC64TargetInfo::PPC64TargetInfo() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000329 PCRelReloc = R_PPC64_REL24;
330 GotReloc = R_PPC64_GLOB_DAT;
331 GotRefReloc = R_PPC64_REL64;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000332 RelativeReloc = R_PPC64_RELATIVE;
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000333 PltEntrySize = 32;
Hal Finkelc848b322015-10-12 19:34:29 +0000334
335 // We need 64K pages (at least under glibc/Linux, the loader won't
336 // set different permissions on a finer granularity than that).
Hal Finkele3c26262015-10-08 22:23:54 +0000337 PageSize = 65536;
Hal Finkel736c7412015-10-15 07:49:07 +0000338
339 // The PPC64 ELF ABI v1 spec, says:
340 //
341 // It is normally desirable to put segments with different characteristics
342 // in separate 256 Mbyte portions of the address space, to give the
343 // operating system full paging flexibility in the 64-bit address space.
344 //
345 // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers
346 // use 0x10000000 as the starting address.
347 VAStart = 0x10000000;
Rafael Espindolac4010882015-09-22 20:54:08 +0000348}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000349
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000350uint64_t getPPC64TocBase() {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000351 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
352 // order. The TOC starts where the first of these sections starts.
353
354 // FIXME: This obviously does not do the right thing when there is no .got
355 // section, but there is a .toc or .tocbss section.
356 uint64_t TocVA = Out<ELF64BE>::Got->getVA();
357 if (!TocVA)
358 TocVA = Out<ELF64BE>::Plt->getVA();
359
360 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
361 // thus permitting a full 64 Kbytes segment. Note that the glibc startup
362 // code (crt1.o) assumes that you can get from the TOC base to the
363 // start of the .toc section with only a single (signed) 16-bit relocation.
364 return TocVA + 0x8000;
365}
366
Rafael Espindolac4010882015-09-22 20:54:08 +0000367void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000368 uint64_t PltEntryAddr) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000369 uint64_t Off = GotEntryAddr - getPPC64TocBase();
370
371 // FIXME: What we should do, in theory, is get the offset of the function
372 // descriptor in the .opd section, and use that as the offset from %r2 (the
373 // TOC-base pointer). Instead, we have the GOT-entry offset, and that will
374 // be a pointer to the function descriptor in the .opd section. Using
375 // this scheme is simpler, but requires an extra indirection per PLT dispatch.
376
Hal Finkelfa92f682015-10-13 21:47:34 +0000377 write32be(Buf, 0xf8410028); // std %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000378 write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha
379 write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11)
380 write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12)
381 write32be(Buf + 16, 0x7d6903a6); // mtctr %r11
382 write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12)
383 write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12)
384 write32be(Buf + 28, 0x4e800420); // bctr
385}
386
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000387bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000388 if (relocNeedsPlt(Type, S))
389 return true;
390
391 switch (Type) {
392 default: return false;
393 case R_PPC64_GOT16:
394 case R_PPC64_GOT16_LO:
395 case R_PPC64_GOT16_HI:
396 case R_PPC64_GOT16_HA:
397 case R_PPC64_GOT16_DS:
398 case R_PPC64_GOT16_LO_DS:
399 return true;
400 }
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000401}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000402
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000403bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Hal Finkel3c8cc672015-10-12 20:56:18 +0000404 // These are function calls that need to be redirected through a PLT stub.
Hal Finkel82281982015-10-17 00:48:20 +0000405 return Type == R_PPC64_REL24 && canBePreempted(&S, false);
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000406}
Hal Finkel3c8cc672015-10-12 20:56:18 +0000407
Hal Finkelbe0823d2015-10-12 20:58:52 +0000408bool PPC64TargetInfo::isRelRelative(uint32_t Type) const {
409 switch (Type) {
410 default:
Hal Finkelbe0823d2015-10-12 20:58:52 +0000411 return true;
Hal Finkel00918622015-10-16 19:01:50 +0000412 case R_PPC64_TOC:
413 case R_PPC64_ADDR64:
414 return false;
Hal Finkelbe0823d2015-10-12 20:58:52 +0000415 }
416}
417
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000418void PPC64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
419 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000420 uint64_t BaseAddr, uint64_t SA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000421 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
422 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
423
Hal Finkel3c8cc672015-10-12 20:56:18 +0000424 uint8_t *L = Buf + Rel.r_offset;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000425 uint64_t P = BaseAddr + Rel.r_offset;
426 uint64_t TB = getPPC64TocBase();
427
Hal Finkel3c8cc672015-10-12 20:56:18 +0000428 // For a TOC-relative relocation, adjust the addend and proceed in terms of
429 // the corresponding ADDR16 relocation type.
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000430 switch (Type) {
Rafael Espindola826941a2015-10-15 18:19:39 +0000431 case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break;
432 case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break;
433 case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break;
434 case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break;
435 case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break;
436 case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000437 default: break;
438 }
439
Hal Finkel3c8cc672015-10-12 20:56:18 +0000440 switch (Type) {
441 case R_PPC64_ADDR16:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000442 if (!isInt<16>(SA))
Hal Finkel33e17a72015-10-15 16:17:30 +0000443 error("Relocation R_PPC64_ADDR16 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000444 write16be(L, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000445 break;
Hal Finkel3c8cc672015-10-12 20:56:18 +0000446 case R_PPC64_ADDR16_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000447 if (!isInt<16>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000448 error("Relocation R_PPC64_ADDR16_DS overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000449 write16be(L, (read16be(L) & 3) | (SA & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000450 break;
451 case R_PPC64_ADDR16_LO:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000452 write16be(L, applyPPCLo(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000453 break;
454 case R_PPC64_ADDR16_LO_DS:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000455 write16be(L, (read16be(L) & 3) | (applyPPCLo(SA) & ~3));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000456 break;
457 case R_PPC64_ADDR16_HI:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000458 write16be(L, applyPPCHi(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000459 break;
460 case R_PPC64_ADDR16_HA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000461 write16be(L, applyPPCHa(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000462 break;
463 case R_PPC64_ADDR16_HIGHER:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000464 write16be(L, applyPPCHigher(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000465 break;
466 case R_PPC64_ADDR16_HIGHERA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000467 write16be(L, applyPPCHighera(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000468 break;
469 case R_PPC64_ADDR16_HIGHEST:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000470 write16be(L, applyPPCHighest(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000471 break;
472 case R_PPC64_ADDR16_HIGHESTA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000473 write16be(L, applyPPCHighesta(SA));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000474 break;
475 case R_PPC64_ADDR14: {
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000476 if ((SA & 3) != 0)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000477 error("Improper alignment for relocation R_PPC64_ADDR14");
478
479 // Preserve the AA/LK bits in the branch instruction
480 uint8_t AALK = L[3];
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000481 write16be(L + 2, (AALK & 3) | (SA & 0xfffc));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000482 break;
483 }
484 case R_PPC64_REL16_LO:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000485 write16be(L, applyPPCLo(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000486 break;
487 case R_PPC64_REL16_HI:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000488 write16be(L, applyPPCHi(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000489 break;
490 case R_PPC64_REL16_HA:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000491 write16be(L, applyPPCHa(SA - P));
Hal Finkel3c8cc672015-10-12 20:56:18 +0000492 break;
493 case R_PPC64_ADDR32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000494 if (!isInt<32>(SA))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000495 error("Relocation R_PPC64_ADDR32 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000496 write32be(L, SA);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000497 break;
498 case R_PPC64_REL24: {
Hal Finkel82281982015-10-17 00:48:20 +0000499 // If we have an undefined weak symbol, we might get here with a symbol
500 // address of zero. That could overflow, but the code must be unreachable,
501 // so don't bother doing anything at all.
502 if (!SA)
503 break;
504
Hal Finkeldaedc122015-10-12 23:16:53 +0000505 uint64_t PltStart = Out<ELF64BE>::Plt->getVA();
506 uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000507 bool InPlt = PltStart <= SA && SA < PltEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000508
509 if (!InPlt && Out<ELF64BE>::Opd) {
510 // If this is a local call, and we currently have the address of a
511 // function-descriptor, get the underlying code address instead.
512 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA();
513 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize();
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000514 bool InOpd = OpdStart <= SA && SA < OpdEnd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000515
516 if (InOpd)
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000517 SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]);
Hal Finkeldaedc122015-10-12 23:16:53 +0000518 }
519
Hal Finkel3c8cc672015-10-12 20:56:18 +0000520 uint32_t Mask = 0x03FFFFFC;
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000521 if (!isInt<24>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000522 error("Relocation R_PPC64_REL24 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000523 write32be(L, (read32be(L) & ~Mask) | ((SA - P) & Mask));
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000524
Hal Finkel515ed442015-10-13 20:31:33 +0000525 if (InPlt && L + 8 <= BufEnd &&
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000526 read32be(L + 4) == 0x60000000 /* nop */)
527 write32be(L + 4, 0xe8410028); // ld %r2, 40(%r1)
Hal Finkel3c8cc672015-10-12 20:56:18 +0000528 break;
529 }
530 case R_PPC64_REL32:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000531 if (!isInt<32>(SA - P))
Hal Finkel3c8cc672015-10-12 20:56:18 +0000532 error("Relocation R_PPC64_REL32 overflow");
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000533 write32be(L, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000534 break;
535 case R_PPC64_REL64:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000536 write64be(L, SA - P);
Hal Finkel3c8cc672015-10-12 20:56:18 +0000537 break;
538 case R_PPC64_ADDR64:
Hal Finkel6f97c2b2015-10-16 21:55:40 +0000539 case R_PPC64_TOC:
Rui Ueyama9e82fa22015-10-15 19:39:36 +0000540 write64be(L, SA);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000541 break;
542 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000543 error("unrecognized reloc " + Twine(Type));
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000544 }
545}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000546
Davide Italianocde93362015-09-26 00:32:04 +0000547AArch64TargetInfo::AArch64TargetInfo() {
548 // PCRelReloc = FIXME
549 // GotReloc = FIXME
550}
551void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
Rui Ueyamac58656c2015-10-13 16:59:30 +0000552 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000553bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
554 const SymbolBody &S) const {
555 return false;
556}
557bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
558 const SymbolBody &S) const {
559 return false;
560}
Davide Italiano1d750a62015-09-27 08:45:38 +0000561
Davide Italianoef4be6b2015-10-06 19:01:32 +0000562static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000563 uint32_t ImmLo = (Imm & 0x3) << 29;
564 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
565 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000566 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000567}
568
Davide Italiano318ca222015-10-02 22:13:51 +0000569// Page(Expr) is the page address of the expression Expr, defined
570// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000571// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000572static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000573 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000574}
575
Hal Finkel87bbd5f2015-10-12 21:19:18 +0000576void AArch64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
577 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000578 uint64_t BaseAddr, uint64_t SA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000579 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
580 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
581
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000582 uint8_t *L = Buf + Rel.r_offset;
Davide Italiano1d750a62015-09-27 08:45:38 +0000583 uint64_t P = BaseAddr + Rel.r_offset;
584 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000585 case R_AARCH64_ABS16:
Rafael Espindola826941a2015-10-15 18:19:39 +0000586 if (!isInt<16>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000587 error("Relocation R_AARCH64_ABS16 out of range");
Rafael Espindola826941a2015-10-15 18:19:39 +0000588 write16le(L, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000589 break;
590 case R_AARCH64_ABS32:
Rafael Espindola826941a2015-10-15 18:19:39 +0000591 if (!isInt<32>(SA))
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000592 error("Relocation R_AARCH64_ABS32 out of range");
Rafael Espindola826941a2015-10-15 18:19:39 +0000593 write32le(L, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000594 break;
595 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000596 // No overflow check needed.
Rafael Espindola826941a2015-10-15 18:19:39 +0000597 write64le(L, SA);
Davide Italianodf88f962015-10-04 00:59:16 +0000598 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000599 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000600 // No overflow check needed.
Davide Italianoa7165742015-10-16 21:06:55 +0000601 // This relocation stores 12 bits and there's no instruction
602 // to do it. Instead, we do a 32 bits store of the value
603 // of r_addend bitwise-or'ed L. This assumes that the addend
604 // bits in L are zero.
Rafael Espindola826941a2015-10-15 18:19:39 +0000605 or32le(L, (SA & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000606 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000607 case R_AARCH64_ADR_PREL_LO21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000608 uint64_t X = SA - P;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000609 if (!isInt<21>(X))
610 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
611 updateAArch64Adr(L, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000612 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000613 }
614 case R_AARCH64_ADR_PREL_PG_HI21: {
Rafael Espindola826941a2015-10-15 18:19:39 +0000615 uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000616 if (!isInt<33>(X))
617 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
618 updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000619 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000620 }
Davide Italiano1d750a62015-09-27 08:45:38 +0000621 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000622 error("unrecognized reloc " + Twine(Type));
Davide Italiano1d750a62015-09-27 08:45:38 +0000623 }
624}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000625
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000626template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
Simon Atanasyan49829a12015-09-29 05:34:03 +0000627 // PCRelReloc = FIXME
628 // GotReloc = FIXME
Hal Finkele3c26262015-10-08 22:23:54 +0000629 PageSize = 65536;
Simon Atanasyan49829a12015-09-29 05:34:03 +0000630}
631
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000632template <class ELFT>
633void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
634 uint64_t PltEntryAddr) const {}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000635
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000636template <class ELFT>
637bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type,
638 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000639 return false;
640}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000641
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000642template <class ELFT>
643bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type,
644 const SymbolBody &S) const {
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000645 return false;
646}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000647
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000648template <class ELFT>
649void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Buf, uint8_t *BufEnd,
650 const void *RelP, uint32_t Type,
Rui Ueyama66072272015-10-15 19:52:27 +0000651 uint64_t BaseAddr, uint64_t SA) const {
Simon Atanasyan9c2d7882015-10-14 14:24:46 +0000652 const bool IsLE = ELFT::TargetEndianness == support::little;
653 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000654 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
655
656 switch (Type) {
657 case R_MIPS_32:
Rui Ueyama66072272015-10-15 19:52:27 +0000658 add32<IsLE>(Buf + Rel.r_offset, SA);
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000659 break;
660 default:
Rui Ueyama1c42afc2015-10-12 15:49:06 +0000661 error("unrecognized reloc " + Twine(Type));
Simon Atanasyan3b732ac2015-10-12 15:10:02 +0000662 }
663}
Rafael Espindola01205f72015-09-22 18:19:46 +0000664}
665}