Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 1 | //===- 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 Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 12 | |
| 13 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 14 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Endian.h" |
| 16 | #include "llvm/Support/ELF.h" |
| 17 | |
| 18 | using namespace llvm; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 19 | using namespace llvm::object; |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 20 | using namespace llvm::support::endian; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 21 | using namespace llvm::ELF; |
| 22 | |
| 23 | namespace lld { |
| 24 | namespace elf2 { |
| 25 | |
| 26 | std::unique_ptr<TargetInfo> Target; |
| 27 | |
| 28 | TargetInfo::~TargetInfo() {} |
| 29 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 30 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 31 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 32 | X86TargetInfo::X86TargetInfo() { |
| 33 | PCRelReloc = R_386_PC32; |
| 34 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 35 | GotRefReloc = R_386_GOT32; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 36 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 37 | |
| 38 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 39 | uint64_t PltEntryAddr) const { |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame^] | 40 | // jmpl *val; nop; nop |
| 41 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
| 42 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 43 | assert(isUInt<32>(GotEntryAddr)); |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame^] | 44 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool X86TargetInfo::relocNeedsGot(uint32_t Type) const { |
| 48 | if (relocNeedsPlt(Type)) |
| 49 | return true; |
| 50 | switch (Type) { |
| 51 | default: |
| 52 | return false; |
| 53 | case R_386_GOT32: |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 58 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 59 | return Type == R_386_GOTPC; |
| 60 | } |
| 61 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 62 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type) const { |
| 63 | switch (Type) { |
| 64 | default: |
| 65 | return false; |
| 66 | case R_386_PLT32: |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 71 | static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } |
| 72 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 73 | void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 74 | uint64_t BaseAddr, uint64_t SymVA, |
| 75 | uint64_t GotVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 76 | typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel; |
| 77 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 78 | |
| 79 | uint32_t Offset = Rel.r_offset; |
| 80 | uint8_t *Location = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 81 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 82 | case R_386_GOT32: |
| 83 | add32le(Location, SymVA - GotVA); |
| 84 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 85 | case R_386_PC32: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 86 | add32le(Location, SymVA - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 87 | break; |
| 88 | case R_386_32: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 89 | add32le(Location, SymVA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 90 | break; |
| 91 | default: |
| 92 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 97 | X86_64TargetInfo::X86_64TargetInfo() { |
| 98 | PCRelReloc = R_X86_64_PC32; |
| 99 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 100 | GotRefReloc = R_X86_64_PC32; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 101 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 102 | |
| 103 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 104 | uint64_t PltEntryAddr) const { |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame^] | 105 | // jmpq *val(%rip); nop; nop |
| 106 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
| 107 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 108 | |
| 109 | uintptr_t NextPC = PltEntryAddr + 6; |
Rafael Espindola | 8c21fad | 2015-09-22 20:06:19 +0000 | [diff] [blame] | 110 | intptr_t Delta = GotEntryAddr - NextPC; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 111 | assert(isInt<32>(Delta)); |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame^] | 112 | write32le(Buf + 2, Delta); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type) const { |
| 116 | if (relocNeedsPlt(Type)) |
| 117 | return true; |
| 118 | switch (Type) { |
| 119 | default: |
| 120 | return false; |
| 121 | case R_X86_64_GOTPCREL: |
| 122 | return true; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type) const { |
| 127 | switch (Type) { |
| 128 | default: |
| 129 | return false; |
| 130 | case R_X86_64_PLT32: |
| 131 | return true; |
| 132 | } |
| 133 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 134 | |
| 135 | void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 136 | uint32_t Type, uint64_t BaseAddr, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 137 | uint64_t SymVA, uint64_t GotVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 138 | 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 Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 145 | case R_X86_64_GOTPCREL: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 146 | write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 147 | break; |
| 148 | case R_X86_64_64: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 149 | write64le(Location, SymVA + Rel.r_addend); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 150 | 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 Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 159 | write32le(Location, VA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 160 | break; |
| 161 | } |
| 162 | default: |
| 163 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | PPC64TargetInfo::PPC64TargetInfo() { |
| 169 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 170 | // GotReloc = FIXME |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 171 | } |
| 172 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 173 | uint64_t PltEntryAddr) const {} |
| 174 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 175 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 176 | void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 177 | uint64_t BaseAddr, uint64_t SymVA, |
| 178 | uint64_t GotVA) const { |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 179 | typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela; |
| 180 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 181 | |
| 182 | uint64_t Offset = Rel.r_offset; |
| 183 | uint8_t *Location = Buf + Offset; |
| 184 | switch (Type) { |
| 185 | case R_PPC64_ADDR64: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 186 | write64be(Location, SymVA + Rel.r_addend); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 187 | break; |
| 188 | case R_PPC64_TOC: |
| 189 | // We don't create a TOC yet. |
| 190 | break; |
| 191 | default: |
| 192 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 193 | break; |
| 194 | } |
| 195 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 196 | |
| 197 | PPCTargetInfo::PPCTargetInfo() { |
| 198 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 199 | // GotReloc = FIXME |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 200 | } |
| 201 | void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 202 | uint64_t PltEntryAddr) const {} |
| 203 | bool PPCTargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 204 | bool PPCTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 205 | void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 206 | uint64_t BaseAddr, uint64_t SymVA, |
| 207 | uint64_t GotVA) const {} |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 208 | |
| 209 | ARMTargetInfo::ARMTargetInfo() { |
| 210 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 211 | // GotReloc = FIXME |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 212 | } |
| 213 | void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 214 | uint64_t PltEntryAddr) const {} |
| 215 | bool ARMTargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 216 | bool ARMTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 217 | void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 218 | uint64_t BaseAddr, uint64_t SymVA, |
| 219 | uint64_t GotVA) const {} |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 220 | |
| 221 | AArch64TargetInfo::AArch64TargetInfo() { |
| 222 | // PCRelReloc = FIXME |
| 223 | // GotReloc = FIXME |
| 224 | } |
| 225 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 226 | uint64_t PltEntryAddr) const {} |
| 227 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 228 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 229 | |
| 230 | static void handle_ADR_PREL_LO21(uint8_t *Location, uint64_t S, int64_t A, |
| 231 | uint64_t P) { |
| 232 | uint64_t X = S + A - P; |
| 233 | if (!isInt<21>(X)) |
| 234 | error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); |
| 235 | uint32_t Imm = X & 0x1FFFFF; |
| 236 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 237 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 238 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
| 239 | write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi); |
| 240 | } |
| 241 | |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 242 | void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 243 | uint32_t Type, uint64_t BaseAddr, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 244 | uint64_t SymVA, uint64_t GotVA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 245 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 246 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 247 | |
| 248 | uint8_t *Location = Buf + Rel.r_offset; |
| 249 | uint64_t S = SymVA; |
| 250 | int64_t A = Rel.r_addend; |
| 251 | uint64_t P = BaseAddr + Rel.r_offset; |
| 252 | switch (Type) { |
| 253 | case R_AARCH64_ADR_PREL_LO21: |
| 254 | handle_ADR_PREL_LO21(Location, S, A, P); |
| 255 | break; |
| 256 | default: |
| 257 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 258 | break; |
| 259 | } |
| 260 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 261 | |
| 262 | MipsTargetInfo::MipsTargetInfo() { |
| 263 | // PCRelReloc = FIXME |
| 264 | // GotReloc = FIXME |
| 265 | DefaultEntry = "__start"; |
| 266 | } |
| 267 | |
| 268 | void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 269 | uint64_t PltEntryAddr) const {} |
| 270 | |
| 271 | bool MipsTargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 272 | |
| 273 | bool MipsTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 274 | |
| 275 | void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 276 | uint64_t BaseAddr, uint64_t SymVA, |
| 277 | uint64_t GotVA) const {} |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 278 | } |
| 279 | } |