blob: 3e1f48e8534cedbcd238d9be087eb976a350a5d9 [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).
114 // The problems start when a non PIC program calls a function is a shared
115 // 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.
118 // In the real world, crt1.o has a R_X86_64_PC32 pointing to libc.so.
119 // The general idea is to create a PLT entry and use that as the function
120 // value, which is why we return true in here.
121 // The remaining (unimplemented) problem is making sure pointer equality
122 // still works. For that, we need the help of the dynamic linker. We
123 // let it know that we have a direct reference to a symbol by creating
124 // an undefined symbol with a non zero st_value. Seeing that the
125 // dynamic linker resolves the symbol to the value of the symbol we created.
126 // This is true even for got entries, so pointer equality is maintained.
127 // To avoid an infinite loop, the only entry that points to the
128 // real function is a dedicated got entry used by the plt.
129 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000130 case R_X86_64_PLT32:
131 return true;
132 }
133}
Rafael Espindolac4010882015-09-22 20:54:08 +0000134
135void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
136 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000137 uint64_t SymVA, uint64_t GotVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000138 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
139 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
140
141 uint64_t Offset = Rel.r_offset;
142 uint8_t *Location = Buf + Offset;
143 switch (Type) {
144 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000145 case R_X86_64_GOTPCREL:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000146 write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000147 break;
148 case R_X86_64_64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000149 write64le(Location, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000150 break;
151 case R_X86_64_32: {
152 case R_X86_64_32S:
153 uint64_t VA = SymVA + Rel.r_addend;
154 if (Type == R_X86_64_32 && !isUInt<32>(VA))
155 error("R_X86_64_32 out of range");
156 else if (!isInt<32>(VA))
157 error("R_X86_64_32S out of range");
158
Rafael Espindola0872ea32015-09-24 14:16:02 +0000159 write32le(Location, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000160 break;
161 }
162 default:
163 error(Twine("unrecognized reloc ") + Twine(Type));
164 break;
165 }
166}
167
168PPC64TargetInfo::PPC64TargetInfo() {
169 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000170 // GotReloc = FIXME
Rafael Espindolac4010882015-09-22 20:54:08 +0000171}
172void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
173 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000174bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
175 return false;
176}
177bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
178 return false;
179}
Rafael Espindolac4010882015-09-22 20:54:08 +0000180void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000181 uint64_t BaseAddr, uint64_t SymVA,
182 uint64_t GotVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000183 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
184 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
185
186 uint64_t Offset = Rel.r_offset;
187 uint8_t *Location = Buf + Offset;
188 switch (Type) {
189 case R_PPC64_ADDR64:
Rafael Espindola0872ea32015-09-24 14:16:02 +0000190 write64be(Location, SymVA + Rel.r_addend);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000191 break;
192 case R_PPC64_TOC:
193 // We don't create a TOC yet.
194 break;
195 default:
196 error(Twine("unrecognized reloc ") + Twine(Type));
197 break;
198 }
199}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000200
201PPCTargetInfo::PPCTargetInfo() {
202 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000203 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000204}
205void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
206 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000207bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
208 return false;
209}
210bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
211 return false;
212}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000213void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000214 uint64_t BaseAddr, uint64_t SymVA,
215 uint64_t GotVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000216
217ARMTargetInfo::ARMTargetInfo() {
218 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000219 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000220}
221void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
222 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000223bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
224 return false;
225}
226bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
227 return false;
228}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000229void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000230 uint64_t BaseAddr, uint64_t SymVA,
231 uint64_t GotVA) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000232
233AArch64TargetInfo::AArch64TargetInfo() {
234 // PCRelReloc = FIXME
235 // GotReloc = FIXME
236}
237void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
238 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000239bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
240 const SymbolBody &S) const {
241 return false;
242}
243bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
244 const SymbolBody &S) const {
245 return false;
246}
Davide Italiano1d750a62015-09-27 08:45:38 +0000247
248static void handle_ADR_PREL_LO21(uint8_t *Location, uint64_t S, int64_t A,
249 uint64_t P) {
250 uint64_t X = S + A - P;
251 if (!isInt<21>(X))
252 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
253 uint32_t Imm = X & 0x1FFFFF;
254 uint32_t ImmLo = (Imm & 0x3) << 29;
255 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
256 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
257 write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi);
258}
259
Davide Italianocde93362015-09-26 00:32:04 +0000260void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
261 uint32_t Type, uint64_t BaseAddr,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000262 uint64_t SymVA, uint64_t GotVA) const {
Davide Italiano1d750a62015-09-27 08:45:38 +0000263 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
264 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
265
266 uint8_t *Location = Buf + Rel.r_offset;
267 uint64_t S = SymVA;
268 int64_t A = Rel.r_addend;
269 uint64_t P = BaseAddr + Rel.r_offset;
270 switch (Type) {
271 case R_AARCH64_ADR_PREL_LO21:
272 handle_ADR_PREL_LO21(Location, S, A, P);
273 break;
274 default:
275 error(Twine("unrecognized reloc ") + Twine(Type));
276 break;
277 }
278}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000279
280MipsTargetInfo::MipsTargetInfo() {
281 // PCRelReloc = FIXME
282 // GotReloc = FIXME
283 DefaultEntry = "__start";
284}
285
286void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
287 uint64_t PltEntryAddr) const {}
288
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000289bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
290 return false;
291}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000292
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000293bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
294 return false;
295}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000296
297void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rafael Espindola8acb95c2015-09-29 14:42:37 +0000298 uint64_t BaseAddr, uint64_t SymVA,
299 uint64_t GotVA) const {}
Rafael Espindola01205f72015-09-22 18:19:46 +0000300}
301}