blob: d59f1304a7309ee05ff2a65db5ea33ebce9f37f6 [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 {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +000049 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +000050}
51
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000052bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
53 return Type == R_386_GOTPC;
54}
55
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000056bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +000057 return Type == R_386_PLT32;
Rafael Espindola01205f72015-09-22 18:19:46 +000058}
59
Rafael Espindola0872ea32015-09-24 14:16:02 +000060static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
61
Rafael Espindolac4010882015-09-22 20:54:08 +000062void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +000063 uint64_t BaseAddr, uint64_t SymVA,
64 uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000065 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
66 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
67
68 uint32_t Offset = Rel.r_offset;
69 uint8_t *Location = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000070 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000071 case R_386_GOT32:
72 add32le(Location, SymVA - GotVA);
73 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000074 case R_386_PC32:
Rafael Espindola0872ea32015-09-24 14:16:02 +000075 add32le(Location, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000076 break;
77 case R_386_32:
Rafael Espindola0872ea32015-09-24 14:16:02 +000078 add32le(Location, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +000079 break;
80 default:
81 error(Twine("unrecognized reloc ") + Twine(Type));
82 break;
83 }
84}
85
Rafael Espindola7f074422015-09-22 21:35:51 +000086X86_64TargetInfo::X86_64TargetInfo() {
87 PCRelReloc = R_X86_64_PC32;
88 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000089 GotRefReloc = R_X86_64_PC32;
Rafael Espindola7f074422015-09-22 21:35:51 +000090}
Rafael Espindola01205f72015-09-22 18:19:46 +000091
92void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
93 uint64_t PltEntryAddr) const {
Rui Ueyama1500a902015-09-29 23:00:47 +000094 // jmpq *val(%rip); nop; nop
95 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
96 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +000097
Rui Ueyamae3fbc892015-09-29 23:25:21 +000098 uint64_t NextPC = PltEntryAddr + 6;
99 int64_t Delta = GotEntryAddr - NextPC;
Rafael Espindola01205f72015-09-22 18:19:46 +0000100 assert(isInt<32>(Delta));
Rui Ueyama1500a902015-09-29 23:00:47 +0000101 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000102}
103
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000104bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000105 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000106}
107
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000108bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000109 switch (Type) {
110 default:
111 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000112 case R_X86_64_PC32:
113 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000114 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000115 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000116 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000117 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000118 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
119 // libc.so.
120 //
121 // The general idea on how to handle such cases is to create a PLT entry
122 // and use that as the function value.
123 //
124 // For the static linking part, we just return true and everything else
125 // will use the the PLT entry as the address.
126 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000127 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000128 // still works. We need the help of the dynamic linker for that. We
129 // let it know that we have a direct reference to a so symbol by creating
130 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000131 // dynamic linker resolves the symbol to the value of the symbol we created.
132 // This is true even for got entries, so pointer equality is maintained.
133 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000134 // real function is a dedicated got entry used by the plt. That is
135 // identified by special relocation types (R_X86_64_JUMP_SLOT,
136 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000137 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000138 case R_X86_64_PLT32:
139 return true;
140 }
141}
Rafael Espindolac4010882015-09-22 20:54:08 +0000142
143void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
144 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000145 uint64_t SymVA, uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000146 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
147 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
148
149 uint64_t Offset = Rel.r_offset;
150 uint8_t *Location = Buf + Offset;
151 switch (Type) {
152 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000153 case R_X86_64_GOTPCREL:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000154 write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000155 break;
156 case R_X86_64_64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000157 write64le(Location, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000158 break;
159 case R_X86_64_32: {
160 case R_X86_64_32S:
161 uint64_t VA = SymVA + Rel.r_addend;
162 if (Type == R_X86_64_32 && !isUInt<32>(VA))
163 error("R_X86_64_32 out of range");
164 else if (!isInt<32>(VA))
165 error("R_X86_64_32S out of range");
166
Rafael Espindola0872ea32015-09-24 14:16:02 +0000167 write32le(Location, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000168 break;
169 }
170 default:
171 error(Twine("unrecognized reloc ") + Twine(Type));
172 break;
173 }
174}
175
176PPC64TargetInfo::PPC64TargetInfo() {
177 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000178 // GotReloc = FIXME
Rafael Espindolac4010882015-09-22 20:54:08 +0000179}
180void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
181 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000182bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
183 return false;
184}
185bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
186 return false;
187}
Rafael Espindolac4010882015-09-22 20:54:08 +0000188void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000189 uint64_t BaseAddr, uint64_t SymVA,
190 uint64_t GotVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000191 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
192 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
193
194 uint64_t Offset = Rel.r_offset;
195 uint8_t *Location = Buf + Offset;
196 switch (Type) {
197 case R_PPC64_ADDR64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000198 write64be(Location, SymVA + Rel.r_addend);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000199 break;
200 case R_PPC64_TOC:
201 // We don't create a TOC yet.
202 break;
203 default:
204 error(Twine("unrecognized reloc ") + Twine(Type));
205 break;
206 }
207}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000208
209PPCTargetInfo::PPCTargetInfo() {
210 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000211 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000212}
213void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
214 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000215bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
216 return false;
217}
218bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
219 return false;
220}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000221void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000222 uint64_t BaseAddr, uint64_t SymVA,
223 uint64_t GotVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000224
225ARMTargetInfo::ARMTargetInfo() {
226 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000227 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000228}
229void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
230 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000231bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
232 return false;
233}
234bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
235 return false;
236}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000237void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000238 uint64_t BaseAddr, uint64_t SymVA,
239 uint64_t GotVA) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000240
241AArch64TargetInfo::AArch64TargetInfo() {
242 // PCRelReloc = FIXME
243 // GotReloc = FIXME
244}
245void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
246 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000247bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
248 const SymbolBody &S) const {
249 return false;
250}
251bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
252 const SymbolBody &S) const {
253 return false;
254}
Davide Italiano1d750a62015-09-27 08:45:38 +0000255
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000256static void AArch64UpdateAdr(uint8_t *Location, uint64_t Imm) {
257 uint32_t ImmLo = (Imm & 0x3) << 29;
258 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
259 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
260 write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi);
261}
262
263static uint64_t AArch64GetPage(uint64_t Address) {
264 return (Address & (~static_cast<uint64_t>(0xFFF)));
265}
266
Davide Italiano1d750a62015-09-27 08:45:38 +0000267static 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");
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000272 AArch64UpdateAdr(Location, X & 0x1FFFFF);
273}
274
275static void handle_ADR_PREL_PG_HI21(uint8_t *Location, uint64_t S, int64_t A,
276 uint64_t P) {
277 uint64_t X = AArch64GetPage(S + A) - AArch64GetPage(P);
278 if (!isInt<33>(X))
279 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
280 AArch64UpdateAdr(Location, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1d750a62015-09-27 08:45:38 +0000281}
282
Davide Italianocde93362015-09-26 00:32:04 +0000283void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
284 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000285 uint64_t SymVA, uint64_t GotVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000286 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
287 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
288
289 uint8_t *Location = Buf + Rel.r_offset;
290 uint64_t S = SymVA;
291 int64_t A = Rel.r_addend;
292 uint64_t P = BaseAddr + Rel.r_offset;
293 switch (Type) {
294 case R_AARCH64_ADR_PREL_LO21:
295 handle_ADR_PREL_LO21(Location, S, A, P);
296 break;
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000297 case R_AARCH64_ADR_PREL_PG_HI21:
298 handle_ADR_PREL_PG_HI21(Location, S, A, P);
299 break;
Davide Italiano1d750a62015-09-27 08:45:38 +0000300 default:
301 error(Twine("unrecognized reloc ") + Twine(Type));
302 break;
303 }
304}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000305
306MipsTargetInfo::MipsTargetInfo() {
307 // PCRelReloc = FIXME
308 // GotReloc = FIXME
309 DefaultEntry = "__start";
310}
311
312void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
313 uint64_t PltEntryAddr) const {}
314
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000315bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
316 return false;
317}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000318
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000319bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
320 return false;
321}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000322
323void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000324 uint64_t BaseAddr, uint64_t SymVA,
325 uint64_t GotVA) const {}
Rafael Espindola01205f72015-09-22 18:19:46 +0000326}
327}