blob: de6d3c3ed90fea4a6666edcf7f62f64caf2a1863 [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 Espindola01205f72015-09-22 18:19:46 +000012
13#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000014#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000015#include "llvm/Support/Endian.h"
16#include "llvm/Support/ELF.h"
17
18using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000019using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000020using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000021using namespace llvm::ELF;
22
23namespace lld {
24namespace elf2 {
25
26std::unique_ptr<TargetInfo> Target;
27
28TargetInfo::~TargetInfo() {}
29
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000030bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
31
Rafael Espindola7f074422015-09-22 21:35:51 +000032X86TargetInfo::X86TargetInfo() {
33 PCRelReloc = R_386_PC32;
34 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000035 GotRefReloc = R_386_GOT32;
Rafael Espindola7f074422015-09-22 21:35:51 +000036}
Rafael Espindola01205f72015-09-22 18:19:46 +000037
38void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
39 uint64_t PltEntryAddr) const {
40 ArrayRef<uint8_t> Jmp = {0xff, 0x25}; // jmpl *val
41 memcpy(Buf, Jmp.data(), Jmp.size());
42 Buf += Jmp.size();
43
44 assert(isUInt<32>(GotEntryAddr));
Rafael Espindola0872ea32015-09-24 14:16:02 +000045 write32le(Buf, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +000046 Buf += 4;
47
48 ArrayRef<uint8_t> Nops = {0x90, 0x90};
49 memcpy(Buf, Nops.data(), Nops.size());
50}
51
52bool X86TargetInfo::relocNeedsGot(uint32_t Type) const {
53 if (relocNeedsPlt(Type))
54 return true;
55 switch (Type) {
56 default:
57 return false;
58 case R_386_GOT32:
59 return true;
60 }
61}
62
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000063bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
64 return Type == R_386_GOTPC;
65}
66
Rafael Espindola01205f72015-09-22 18:19:46 +000067bool X86TargetInfo::relocNeedsPlt(uint32_t Type) const {
68 switch (Type) {
69 default:
70 return false;
71 case R_386_PLT32:
72 return true;
73 }
74}
75
Rafael Espindola0872ea32015-09-24 14:16:02 +000076static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
77
Rafael Espindolac4010882015-09-22 20:54:08 +000078void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +000079 uint64_t BaseAddr, uint64_t SymVA,
80 uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000081 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
82 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
83
84 uint32_t Offset = Rel.r_offset;
85 uint8_t *Location = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000086 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000087 case R_386_GOT32:
88 add32le(Location, SymVA - GotVA);
89 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000090 case R_386_PC32:
Rafael Espindola0872ea32015-09-24 14:16:02 +000091 add32le(Location, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000092 break;
93 case R_386_32:
Rafael Espindola0872ea32015-09-24 14:16:02 +000094 add32le(Location, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +000095 break;
96 default:
97 error(Twine("unrecognized reloc ") + Twine(Type));
98 break;
99 }
100}
101
Rafael Espindola7f074422015-09-22 21:35:51 +0000102X86_64TargetInfo::X86_64TargetInfo() {
103 PCRelReloc = R_X86_64_PC32;
104 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000105 GotRefReloc = R_X86_64_PC32;
Rafael Espindola7f074422015-09-22 21:35:51 +0000106}
Rafael Espindola01205f72015-09-22 18:19:46 +0000107
108void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
109 uint64_t PltEntryAddr) const {
110 ArrayRef<uint8_t> Jmp = {0xff, 0x25}; // jmpq *val(%rip)
111 memcpy(Buf, Jmp.data(), Jmp.size());
112 Buf += Jmp.size();
113
114 uintptr_t NextPC = PltEntryAddr + 6;
Rafael Espindola8c21fad2015-09-22 20:06:19 +0000115 intptr_t Delta = GotEntryAddr - NextPC;
Rafael Espindola01205f72015-09-22 18:19:46 +0000116 assert(isInt<32>(Delta));
Rafael Espindola0872ea32015-09-24 14:16:02 +0000117 write32le(Buf, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000118 Buf += 4;
119
120 ArrayRef<uint8_t> Nops = {0x90, 0x90};
121 memcpy(Buf, Nops.data(), Nops.size());
122}
123
124bool X86_64TargetInfo::relocNeedsGot(uint32_t Type) const {
125 if (relocNeedsPlt(Type))
126 return true;
127 switch (Type) {
128 default:
129 return false;
130 case R_X86_64_GOTPCREL:
131 return true;
132 }
133}
134
135bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type) const {
136 switch (Type) {
137 default:
138 return false;
139 case R_X86_64_PLT32:
140 return true;
141 }
142}
Rafael Espindolac4010882015-09-22 20:54:08 +0000143
144void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
145 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000146 uint64_t SymVA, uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000147 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
148 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
149
150 uint64_t Offset = Rel.r_offset;
151 uint8_t *Location = Buf + Offset;
152 switch (Type) {
153 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000154 case R_X86_64_GOTPCREL:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000155 write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000156 break;
157 case R_X86_64_64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000158 write64le(Location, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000159 break;
160 case R_X86_64_32: {
161 case R_X86_64_32S:
162 uint64_t VA = SymVA + Rel.r_addend;
163 if (Type == R_X86_64_32 && !isUInt<32>(VA))
164 error("R_X86_64_32 out of range");
165 else if (!isInt<32>(VA))
166 error("R_X86_64_32S out of range");
167
Rafael Espindola0872ea32015-09-24 14:16:02 +0000168 write32le(Location, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000169 break;
170 }
171 default:
172 error(Twine("unrecognized reloc ") + Twine(Type));
173 break;
174 }
175}
176
177PPC64TargetInfo::PPC64TargetInfo() {
178 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000179 // GotReloc = FIXME
Rafael Espindolac4010882015-09-22 20:54:08 +0000180}
181void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
182 uint64_t PltEntryAddr) const {}
183bool PPC64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
184bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
185void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000186 uint64_t BaseAddr, uint64_t SymVA,
187 uint64_t GotVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000188 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
189 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
190
191 uint64_t Offset = Rel.r_offset;
192 uint8_t *Location = Buf + Offset;
193 switch (Type) {
194 case R_PPC64_ADDR64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000195 write64be(Location, SymVA + Rel.r_addend);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000196 break;
197 case R_PPC64_TOC:
198 // We don't create a TOC yet.
199 break;
200 default:
201 error(Twine("unrecognized reloc ") + Twine(Type));
202 break;
203 }
204}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000205
206PPCTargetInfo::PPCTargetInfo() {
207 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000208 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000209}
210void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
211 uint64_t PltEntryAddr) const {}
212bool PPCTargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
213bool PPCTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
214void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000215 uint64_t BaseAddr, uint64_t SymVA,
216 uint64_t GotVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000217
218ARMTargetInfo::ARMTargetInfo() {
219 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000220 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000221}
222void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
223 uint64_t PltEntryAddr) const {}
224bool ARMTargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
225bool ARMTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
226void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000227 uint64_t BaseAddr, uint64_t SymVA,
228 uint64_t GotVA) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000229
230AArch64TargetInfo::AArch64TargetInfo() {
231 // PCRelReloc = FIXME
232 // GotReloc = FIXME
233}
234void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
235 uint64_t PltEntryAddr) const {}
236bool AArch64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
237bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
Davide Italiano1d750a62015-09-27 08:45:38 +0000238
239static void handle_ADR_PREL_LO21(uint8_t *Location, uint64_t S, int64_t A,
240 uint64_t P) {
241 uint64_t X = S + A - P;
242 if (!isInt<21>(X))
243 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
244 uint32_t Imm = X & 0x1FFFFF;
245 uint32_t ImmLo = (Imm & 0x3) << 29;
246 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
247 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
248 write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi);
249}
250
Davide Italianocde93362015-09-26 00:32:04 +0000251void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
252 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000253 uint64_t SymVA, uint64_t GotVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000254 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
255 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
256
257 uint8_t *Location = Buf + Rel.r_offset;
258 uint64_t S = SymVA;
259 int64_t A = Rel.r_addend;
260 uint64_t P = BaseAddr + Rel.r_offset;
261 switch (Type) {
262 case R_AARCH64_ADR_PREL_LO21:
263 handle_ADR_PREL_LO21(Location, S, A, P);
264 break;
265 default:
266 error(Twine("unrecognized reloc ") + Twine(Type));
267 break;
268 }
269}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000270
271MipsTargetInfo::MipsTargetInfo() {
272 // PCRelReloc = FIXME
273 // GotReloc = FIXME
274 DefaultEntry = "__start";
275}
276
277void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
278 uint64_t PltEntryAddr) const {}
279
280bool MipsTargetInfo::relocNeedsGot(uint32_t Type) const { return false; }
281
282bool MipsTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; }
283
284void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000285 uint64_t BaseAddr, uint64_t SymVA,
286 uint64_t GotVA) const {}
Rafael Espindola01205f72015-09-22 18:19:46 +0000287}
288}