blob: 398bb099fac3b23bd0f1000c84e523f232501e2d [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"
Rui Ueyamaaf21d922015-10-08 20:06:07 +000012#include "OutputSections.h"
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000013#include "Symbols.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000014
15#include "llvm/ADT/ArrayRef.h"
Rafael Espindolac4010882015-09-22 20:54:08 +000016#include "llvm/Object/ELF.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000017#include "llvm/Support/Endian.h"
18#include "llvm/Support/ELF.h"
19
20using namespace llvm;
Rafael Espindolac4010882015-09-22 20:54:08 +000021using namespace llvm::object;
Rafael Espindola0872ea32015-09-24 14:16:02 +000022using namespace llvm::support::endian;
Rafael Espindola01205f72015-09-22 18:19:46 +000023using namespace llvm::ELF;
24
25namespace lld {
26namespace elf2 {
27
28std::unique_ptr<TargetInfo> Target;
29
30TargetInfo::~TargetInfo() {}
31
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000032bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; }
33
Rafael Espindolaae244002015-10-05 19:30:12 +000034bool TargetInfo::isRelRelative(uint32_t Type) const { return true; }
35
Rafael Espindola7f074422015-09-22 21:35:51 +000036X86TargetInfo::X86TargetInfo() {
37 PCRelReloc = R_386_PC32;
38 GotReloc = R_386_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000039 GotRefReloc = R_386_GOT32;
Rafael Espindola7f074422015-09-22 21:35:51 +000040}
Rafael Espindola01205f72015-09-22 18:19:46 +000041
42void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
43 uint64_t PltEntryAddr) const {
Rui Ueyama1500a902015-09-29 23:00:47 +000044 // jmpl *val; nop; nop
45 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
46 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +000047 assert(isUInt<32>(GotEntryAddr));
Rui Ueyama1500a902015-09-29 23:00:47 +000048 write32le(Buf + 2, GotEntryAddr);
Rafael Espindola01205f72015-09-22 18:19:46 +000049}
50
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000051bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +000052 return Type == R_386_GOT32 || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +000053}
54
Rafael Espindola6d7fcdb2015-09-29 13:36:32 +000055bool X86TargetInfo::relocPointsToGot(uint32_t Type) const {
56 return Type == R_386_GOTPC;
57}
58
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +000059bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
George Rimar730c2782015-10-07 18:46:13 +000060 return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared());
Rafael Espindola01205f72015-09-22 18:19:46 +000061}
62
Rui Ueyama87bc41b2015-10-06 18:54:43 +000063static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); }
Rui Ueyama87bc41b2015-10-06 18:54:43 +000064static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); }
Rafael Espindola0872ea32015-09-24 14:16:02 +000065
Rafael Espindolac4010882015-09-22 20:54:08 +000066void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +000067 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +000068 typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel;
69 auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP);
70
71 uint32_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +000072 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +000073 switch (Type) {
Rafael Espindola8acb95c2015-09-29 14:42:37 +000074 case R_386_GOT32:
Rui Ueyamaaf21d922015-10-08 20:06:07 +000075 add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA());
Rafael Espindola8acb95c2015-09-29 14:42:37 +000076 break;
Rafael Espindolac4010882015-09-22 20:54:08 +000077 case R_386_PC32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +000078 add32le(Loc, SymVA - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +000079 break;
80 case R_386_32:
Rui Ueyama87bc41b2015-10-06 18:54:43 +000081 add32le(Loc, SymVA);
Rafael Espindolac4010882015-09-22 20:54:08 +000082 break;
83 default:
84 error(Twine("unrecognized reloc ") + Twine(Type));
85 break;
86 }
87}
88
Rafael Espindola7f074422015-09-22 21:35:51 +000089X86_64TargetInfo::X86_64TargetInfo() {
90 PCRelReloc = R_X86_64_PC32;
91 GotReloc = R_X86_64_GLOB_DAT;
Rafael Espindola8acb95c2015-09-29 14:42:37 +000092 GotRefReloc = R_X86_64_PC32;
Rafael Espindolaae244002015-10-05 19:30:12 +000093 RelativeReloc = R_X86_64_RELATIVE;
Rafael Espindola7f074422015-09-22 21:35:51 +000094}
Rafael Espindola01205f72015-09-22 18:19:46 +000095
96void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
97 uint64_t PltEntryAddr) const {
Rui Ueyama1500a902015-09-29 23:00:47 +000098 // jmpq *val(%rip); nop; nop
99 const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90};
100 memcpy(Buf, Inst, sizeof(Inst));
Rafael Espindola01205f72015-09-22 18:19:46 +0000101
Rui Ueyamae3fbc892015-09-29 23:25:21 +0000102 uint64_t NextPC = PltEntryAddr + 6;
103 int64_t Delta = GotEntryAddr - NextPC;
Rafael Espindola01205f72015-09-22 18:19:46 +0000104 assert(isInt<32>(Delta));
Rui Ueyama1500a902015-09-29 23:00:47 +0000105 write32le(Buf + 2, Delta);
Rafael Espindola01205f72015-09-22 18:19:46 +0000106}
107
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000108bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
Rui Ueyama5ba3ac42015-09-30 01:40:08 +0000109 return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S);
Rafael Espindola01205f72015-09-22 18:19:46 +0000110}
111
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000112bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
Rafael Espindola01205f72015-09-22 18:19:46 +0000113 switch (Type) {
114 default:
115 return false;
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000116 case R_X86_64_PC32:
117 // This relocation is defined to have a value of (S + A - P).
Rafael Espindola3c412e12015-09-30 12:30:58 +0000118 // The problems start when a non PIC program calls a function in a shared
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000119 // library.
Rafael Espindola9a0db7c2015-09-29 23:23:53 +0000120 // In an ideal world, we could just report an error saying the relocation
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000121 // can overflow at runtime.
Rafael Espindola3c412e12015-09-30 12:30:58 +0000122 // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to
123 // libc.so.
124 //
125 // The general idea on how to handle such cases is to create a PLT entry
126 // and use that as the function value.
127 //
128 // For the static linking part, we just return true and everything else
129 // will use the the PLT entry as the address.
130 //
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000131 // The remaining (unimplemented) problem is making sure pointer equality
Rafael Espindola3c412e12015-09-30 12:30:58 +0000132 // still works. We need the help of the dynamic linker for that. We
133 // let it know that we have a direct reference to a so symbol by creating
134 // an undefined symbol with a non zero st_value. Seeing that, the
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000135 // dynamic linker resolves the symbol to the value of the symbol we created.
136 // This is true even for got entries, so pointer equality is maintained.
137 // To avoid an infinite loop, the only entry that points to the
Rafael Espindola3c412e12015-09-30 12:30:58 +0000138 // real function is a dedicated got entry used by the plt. That is
139 // identified by special relocation types (R_X86_64_JUMP_SLOT,
140 // R_386_JMP_SLOT, etc).
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000141 return S.isShared();
Rafael Espindola01205f72015-09-22 18:19:46 +0000142 case R_X86_64_PLT32:
143 return true;
144 }
145}
Rafael Espindolac4010882015-09-22 20:54:08 +0000146
Rafael Espindolaae244002015-10-05 19:30:12 +0000147bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {
148 switch (Type) {
149 default:
150 return false;
151 case R_X86_64_PC64:
152 case R_X86_64_PC32:
153 case R_X86_64_PC16:
154 case R_X86_64_PC8:
155 return true;
156 }
157}
158
Rafael Espindolac4010882015-09-22 20:54:08 +0000159void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP,
160 uint32_t Type, uint64_t BaseAddr,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000161 uint64_t SymVA) const {
Rafael Espindolac4010882015-09-22 20:54:08 +0000162 typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela;
163 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
164
165 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000166 uint8_t *Loc = Buf + Offset;
Rafael Espindolac4010882015-09-22 20:54:08 +0000167 switch (Type) {
168 case R_X86_64_PC32:
Rafael Espindolacdfecff2015-09-23 20:08:25 +0000169 case R_X86_64_GOTPCREL:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000170 write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset));
Rafael Espindolac4010882015-09-22 20:54:08 +0000171 break;
172 case R_X86_64_64:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000173 write64le(Loc, SymVA + Rel.r_addend);
Rafael Espindolac4010882015-09-22 20:54:08 +0000174 break;
175 case R_X86_64_32: {
176 case R_X86_64_32S:
177 uint64_t VA = SymVA + Rel.r_addend;
178 if (Type == R_X86_64_32 && !isUInt<32>(VA))
179 error("R_X86_64_32 out of range");
180 else if (!isInt<32>(VA))
181 error("R_X86_64_32S out of range");
182
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000183 write32le(Loc, VA);
Rafael Espindolac4010882015-09-22 20:54:08 +0000184 break;
185 }
186 default:
187 error(Twine("unrecognized reloc ") + Twine(Type));
188 break;
189 }
190}
191
192PPC64TargetInfo::PPC64TargetInfo() {
193 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000194 // GotReloc = FIXME
Rafael Espindolac4010882015-09-22 20:54:08 +0000195}
196void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
197 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000198bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
199 return false;
200}
201bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
202 return false;
203}
Rafael Espindolac4010882015-09-22 20:54:08 +0000204void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000205 uint64_t BaseAddr, uint64_t SymVA) const {
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000206 typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela;
207 auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP);
208
209 uint64_t Offset = Rel.r_offset;
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000210 uint8_t *Loc = Buf + Offset;
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000211 switch (Type) {
212 case R_PPC64_ADDR64:
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000213 write64be(Loc, SymVA + Rel.r_addend);
Rafael Espindola3efa4e92015-09-22 21:12:55 +0000214 break;
215 case R_PPC64_TOC:
216 // We don't create a TOC yet.
217 break;
218 default:
219 error(Twine("unrecognized reloc ") + Twine(Type));
220 break;
221 }
222}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000223
224PPCTargetInfo::PPCTargetInfo() {
225 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000226 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000227}
228void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
229 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000230bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
231 return false;
232}
233bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
234 return false;
235}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000236void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000237 uint64_t BaseAddr, uint64_t SymVA) const {}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000238
239ARMTargetInfo::ARMTargetInfo() {
240 // PCRelReloc = FIXME
Rafael Espindola7f074422015-09-22 21:35:51 +0000241 // GotReloc = FIXME
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000242}
243void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
244 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000245bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
246 return false;
247}
248bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
249 return false;
250}
Rafael Espindola1d6063e2015-09-22 21:24:52 +0000251void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000252 uint64_t BaseAddr, uint64_t SymVA) const {}
Davide Italianocde93362015-09-26 00:32:04 +0000253
254AArch64TargetInfo::AArch64TargetInfo() {
255 // PCRelReloc = FIXME
256 // GotReloc = FIXME
257}
258void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
259 uint64_t PltEntryAddr) const {}
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000260bool AArch64TargetInfo::relocNeedsGot(uint32_t Type,
261 const SymbolBody &S) const {
262 return false;
263}
264bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type,
265 const SymbolBody &S) const {
266 return false;
267}
Davide Italiano1d750a62015-09-27 08:45:38 +0000268
Davide Italianoef4be6b2015-10-06 19:01:32 +0000269static void updateAArch64Adr(uint8_t *L, uint64_t Imm) {
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000270 uint32_t ImmLo = (Imm & 0x3) << 29;
271 uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5;
272 uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5);
Rui Ueyama87bc41b2015-10-06 18:54:43 +0000273 write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi);
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000274}
275
Davide Italiano318ca222015-10-02 22:13:51 +0000276// Page(Expr) is the page address of the expression Expr, defined
277// as (Expr & ~0xFFF). (This applies even if the machine page size
Davide Italianod9b5be42015-10-02 22:17:09 +0000278// supported by the platform has a different value.)
Davide Italianoef4be6b2015-10-06 19:01:32 +0000279static uint64_t getAArch64Page(uint64_t Expr) {
Davide Italiano318ca222015-10-02 22:13:51 +0000280 return Expr & (~static_cast<uint64_t>(0xFFF));
Davide Italiano1f31a2c2015-10-02 22:00:42 +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,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000285 uint64_t SymVA) 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
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000289 uint8_t *L = Buf + Rel.r_offset;
Davide Italiano1d750a62015-09-27 08:45:38 +0000290 uint64_t S = SymVA;
291 int64_t A = Rel.r_addend;
292 uint64_t P = BaseAddr + Rel.r_offset;
293 switch (Type) {
Davide Italianodf88f962015-10-04 00:59:16 +0000294 case R_AARCH64_ABS16:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000295 if (!isInt<16>(S + A))
296 error("Relocation R_AARCH64_ABS16 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000297 write16le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000298 break;
299 case R_AARCH64_ABS32:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000300 if (!isInt<32>(S + A))
301 error("Relocation R_AARCH64_ABS32 out of range");
Davide Italiano06d84322015-10-07 22:10:02 +0000302 write32le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000303 break;
304 case R_AARCH64_ABS64:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000305 // No overflow check needed.
Davide Italiano06d84322015-10-07 22:10:02 +0000306 write64le(L, S + A);
Davide Italianodf88f962015-10-04 00:59:16 +0000307 break;
Davide Italiano0b6974b2015-10-03 19:56:07 +0000308 case R_AARCH64_ADD_ABS_LO12_NC:
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000309 // No overflow check needed.
310 or32le(L, ((S + A) & 0xFFF) << 10);
Davide Italiano0b6974b2015-10-03 19:56:07 +0000311 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000312 case R_AARCH64_ADR_PREL_LO21: {
313 uint64_t X = S + A - P;
314 if (!isInt<21>(X))
315 error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
316 updateAArch64Adr(L, X & 0x1FFFFF);
Davide Italiano1d750a62015-09-27 08:45:38 +0000317 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000318 }
319 case R_AARCH64_ADR_PREL_PG_HI21: {
320 uint64_t X = getAArch64Page(S + A) - getAArch64Page(P);
321 if (!isInt<33>(X))
322 error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
323 updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12]
Davide Italiano1f31a2c2015-10-02 22:00:42 +0000324 break;
Rui Ueyamaee8c53b2015-10-06 19:57:01 +0000325 }
Davide Italiano1d750a62015-09-27 08:45:38 +0000326 default:
327 error(Twine("unrecognized reloc ") + Twine(Type));
328 break;
329 }
330}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000331
332MipsTargetInfo::MipsTargetInfo() {
333 // PCRelReloc = FIXME
334 // GotReloc = FIXME
335 DefaultEntry = "__start";
336}
337
338void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr,
339 uint64_t PltEntryAddr) const {}
340
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000341bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const {
342 return false;
343}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000344
Rafael Espindola3ef3a4c2015-09-29 23:22:16 +0000345bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const {
346 return false;
347}
Simon Atanasyan49829a12015-09-29 05:34:03 +0000348
349void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type,
Rui Ueyamaaf21d922015-10-08 20:06:07 +0000350 uint64_t BaseAddr, uint64_t SymVA) const {}
Rafael Espindola01205f72015-09-22 18:19:46 +0000351}
352}