blob: 778bff19499fd6560ebc53f31260fb0360c662dc [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"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000012#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000013
14#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000015#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000016#include "llvm/Support/Endian.h"
17#include "llvm/Support/ELF.h"
18
19using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000020using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000021using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000022using namespace llvm::ELF;
23
24namespace lld {
25namespace elf2 {
26
27std::unique_ptr<TargetInfo> Target;
28
29TargetInfo::~TargetInfo() {}
30
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000031bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
32
Rafael Espindola7f074422015-09-22 21:35:51 +000033X86TargetInfo::X86TargetInfo() {
34 PCRelReloc = R_386_PC32;
35 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000036 GotRefReloc = R_386_GOT32;
Rafael Espindola7f074422015-09-22 21:35:51 +000037}
Rafael Espindola01205f72015-09-22 18:19:46 +000038
39void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
40 uint64_t PltEntryAddr) const {
Rui Ueyama1500a902015-09-29 23:00:47 +000041 // jmpl *val; nop; nop
42 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
43 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +000044 assert(isUInt<32>(GotEntryAddr));
Rui Ueyama1500a902015-09-29 23:00:47 +000045 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +000046}
47
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000048bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
49 if (relocNeedsPlt(Type, S))
Rafael Espindola01205f72015-09-22 18:19:46 +000050 return true;
51 switch (Type) {
52 default:
53 return false;
54 case R_386_GOT32:
55 return true;
56 }
57}
58
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000059bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
60 return Type == R_386_GOTPC;
61}
62
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000063bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +000064 switch (Type) {
65 default:
66 return false;
67 case R_386_PLT32:
68 return true;
69 }
70}
71
Rafael Espindola0872ea32015-09-24 14:16:02 +000072static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
73
Rafael Espindolac4010882015-09-22 20:54:08 +000074void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +000075 uint64_t BaseAddr, uint64_t SymVA,
76 uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000077 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
78 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
79
80 uint32_t Offset = Rel.r_offset;
81 uint8_t *Location = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000082 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000083 case R_386_GOT32:
84 add32le(Location, SymVA - GotVA);
85 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000086 case R_386_PC32:
Rafael Espindola0872ea32015-09-24 14:16:02 +000087 add32le(Location, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000088 break;
89 case R_386_32:
Rafael Espindola0872ea32015-09-24 14:16:02 +000090 add32le(Location, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +000091 break;
92 default:
93 error(Twine("unrecognized reloc ") + Twine(Type));
94 break;
95 }
96}
97
Rafael Espindola7f074422015-09-22 21:35:51 +000098X86_64TargetInfo::X86_64TargetInfo() {
99 PCRelReloc = R_X86_64_PC32;
100 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000101 GotRefReloc = R_X86_64_PC32;
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
110 uintptr_t NextPC = PltEntryAddr + 6;
Rafael Espindola8c21fad2015-09-22 20:06:19 +0000111 intptr_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 {
117 if (relocNeedsPlt(Type, S))
Rafael Espindola01205f72015-09-22 18:19:46 +0000118 return true;
119 switch (Type) {
120 default:
121 return false;
122 case R_X86_64_GOTPCREL:
123 return true;
124 }
125}
126
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000127bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000128 switch (Type) {
129 default:
130 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000131 case R_X86_64_PC32:
132 // This relocation is defined to have a value of (S + A - P).
133 // The problems start when a non PIC program calls a function is a shared
134 // library.
135 // In an idea world, we could just report an error saying the relocation
136 // can overflow at runtime.
137 // In the real world, crt1.o has a R_X86_64_PC32 pointing to libc.so.
138 // The general idea is to create a PLT entry and use that as the function
139 // value, which is why we return true in here.
140 // The remaining (unimplemented) problem is making sure pointer equality
141 // still works. For that, we need the help of the dynamic linker. We
142 // let it know that we have a direct reference to a symbol by creating
143 // an undefined symbol with a non zero st_value. Seeing that the
144 // dynamic linker resolves the symbol to the value of the symbol we created.
145 // This is true even for got entries, so pointer equality is maintained.
146 // To avoid an infinite loop, the only entry that points to the
147 // real function is a dedicated got entry used by the plt.
148 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000149 case R_X86_64_PLT32:
150 return true;
151 }
152}
Rafael Espindolac4010882015-09-22 20:54:08 +0000153
154void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
155 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000156 uint64_t SymVA, uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000157 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
158 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
159
160 uint64_t Offset = Rel.r_offset;
161 uint8_t *Location = Buf + Offset;
162 switch (Type) {
163 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000164 case R_X86_64_GOTPCREL:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000165 write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000166 break;
167 case R_X86_64_64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000168 write64le(Location, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000169 break;
170 case R_X86_64_32: {
171 case R_X86_64_32S:
172 uint64_t VA = SymVA + Rel.r_addend;
173 if (Type == R_X86_64_32 && !isUInt<32>(VA))
174 error("R_X86_64_32 out of range");
175 else if (!isInt<32>(VA))
176 error("R_X86_64_32S out of range");
177
Rafael Espindola0872ea32015-09-24 14:16:02 +0000178 write32le(Location, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000179 break;
180 }
181 default:
182 error(Twine("unrecognized reloc ") + Twine(Type));
183 break;
184 }
185}
186
187PPC64TargetInfo::PPC64TargetInfo() {
188 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000189 // GotReloc = FIXME
Rafael Espindolac4010882015-09-22 20:54:08 +0000190}
191void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
192 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000193bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
194 return false;
195}
196bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
197 return false;
198}
Rafael Espindolac4010882015-09-22 20:54:08 +0000199void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000200 uint64_t BaseAddr, uint64_t SymVA,
201 uint64_t GotVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000202 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
203 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
204
205 uint64_t Offset = Rel.r_offset;
206 uint8_t *Location = Buf + Offset;
207 switch (Type) {
208 case R_PPC64_ADDR64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000209 write64be(Location, SymVA + Rel.r_addend);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000210 break;
211 case R_PPC64_TOC:
212 // We don't create a TOC yet.
213 break;
214 default:
215 error(Twine("unrecognized reloc ") + Twine(Type));
216 break;
217 }
218}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000219
220PPCTargetInfo::PPCTargetInfo() {
221 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000222 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000223}
224void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
225 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000226bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
227 return false;
228}
229bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
230 return false;
231}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000232void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000233 uint64_t BaseAddr, uint64_t SymVA,
234 uint64_t GotVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000235
236ARMTargetInfo::ARMTargetInfo() {
237 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000238 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000239}
240void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
241 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000242bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
243 return false;
244}
245bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
246 return false;
247}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000248void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000249 uint64_t BaseAddr, uint64_t SymVA,
250 uint64_t GotVA) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000251
252AArch64TargetInfo::AArch64TargetInfo() {
253 // PCRelReloc = FIXME
254 // GotReloc = FIXME
255}
256void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
257 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000258bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
259 const SymbolBody &S) const {
260 return false;
261}
262bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
263 const SymbolBody &S) const {
264 return false;
265}
Davide Italiano1d750a62015-09-27 08:45:38 +0000266
267static void handle_ADR_PREL_LO21(uint8_t *Location, uint64_t S, int64_t A,
268 uint64_t P) {
269 uint64_t X = S + A - P;
270 if (!isInt<21>(X))
271 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
272 uint32_t Imm = X & 0x1FFFFF;
273 uint32_t ImmLo = (Imm & 0x3) << 29;
274 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
275 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
276 write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi);
277}
278
Davide Italianocde93362015-09-26 00:32:04 +0000279void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
280 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000281 uint64_t SymVA, uint64_t GotVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000282 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
283 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
284
285 uint8_t *Location = Buf + Rel.r_offset;
286 uint64_t S = SymVA;
287 int64_t A = Rel.r_addend;
288 uint64_t P = BaseAddr + Rel.r_offset;
289 switch (Type) {
290 case R_AARCH64_ADR_PREL_LO21:
291 handle_ADR_PREL_LO21(Location, S, A, P);
292 break;
293 default:
294 error(Twine("unrecognized reloc ") + Twine(Type));
295 break;
296 }
297}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000298
299MipsTargetInfo::MipsTargetInfo() {
300 // PCRelReloc = FIXME
301 // GotReloc = FIXME
302 DefaultEntry = "__start";
303}
304
305void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
306 uint64_t PltEntryAddr) const {}
307
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000308bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
309 return false;
310}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000311
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000312bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
313 return false;
314}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000315
316void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000317 uint64_t BaseAddr, uint64_t SymVA,
318 uint64_t GotVA) const {}
Rafael Espindola01205f72015-09-22 18:19:46 +0000319}
320}