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 { |
| 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 Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 45 | write32le(Buf, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 46 | Buf += 4; |
| 47 | |
| 48 | ArrayRef<uint8_t> Nops = {0x90, 0x90}; |
| 49 | memcpy(Buf, Nops.data(), Nops.size()); |
| 50 | } |
| 51 | |
| 52 | bool 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 Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 63 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 64 | return Type == R_386_GOTPC; |
| 65 | } |
| 66 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 67 | bool 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 Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 76 | static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } |
| 77 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 78 | void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 79 | uint64_t BaseAddr, uint64_t SymVA, |
| 80 | uint64_t GotVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 81 | 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 Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 86 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 87 | case R_386_GOT32: |
| 88 | add32le(Location, SymVA - GotVA); |
| 89 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 90 | case R_386_PC32: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 91 | add32le(Location, SymVA - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 92 | break; |
| 93 | case R_386_32: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 94 | add32le(Location, SymVA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 95 | break; |
| 96 | default: |
| 97 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 102 | X86_64TargetInfo::X86_64TargetInfo() { |
| 103 | PCRelReloc = R_X86_64_PC32; |
| 104 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 105 | GotRefReloc = R_X86_64_PC32; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 106 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 107 | |
| 108 | void 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 Espindola | 8c21fad | 2015-09-22 20:06:19 +0000 | [diff] [blame] | 115 | intptr_t Delta = GotEntryAddr - NextPC; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 116 | assert(isInt<32>(Delta)); |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 117 | write32le(Buf, Delta); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 118 | Buf += 4; |
| 119 | |
| 120 | ArrayRef<uint8_t> Nops = {0x90, 0x90}; |
| 121 | memcpy(Buf, Nops.data(), Nops.size()); |
| 122 | } |
| 123 | |
| 124 | bool 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 | |
| 135 | bool 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 Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 143 | |
| 144 | void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 145 | uint32_t Type, uint64_t BaseAddr, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 146 | uint64_t SymVA, uint64_t GotVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 147 | 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 Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 154 | case R_X86_64_GOTPCREL: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 155 | write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 156 | break; |
| 157 | case R_X86_64_64: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 158 | write64le(Location, SymVA + Rel.r_addend); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 159 | 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 Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 168 | write32le(Location, VA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | default: |
| 172 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | PPC64TargetInfo::PPC64TargetInfo() { |
| 178 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 179 | // GotReloc = FIXME |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 180 | } |
| 181 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 182 | uint64_t PltEntryAddr) const {} |
| 183 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 184 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 185 | void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 186 | uint64_t BaseAddr, uint64_t SymVA, |
| 187 | uint64_t GotVA) const { |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 188 | 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 Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 195 | write64be(Location, SymVA + Rel.r_addend); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 196 | 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 Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 205 | |
| 206 | PPCTargetInfo::PPCTargetInfo() { |
| 207 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 208 | // GotReloc = FIXME |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 209 | } |
| 210 | void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 211 | uint64_t PltEntryAddr) const {} |
| 212 | bool PPCTargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 213 | bool PPCTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 214 | void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 215 | uint64_t BaseAddr, uint64_t SymVA, |
| 216 | uint64_t GotVA) const {} |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 217 | |
| 218 | ARMTargetInfo::ARMTargetInfo() { |
| 219 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 220 | // GotReloc = FIXME |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 221 | } |
| 222 | void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 223 | uint64_t PltEntryAddr) const {} |
| 224 | bool ARMTargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 225 | bool ARMTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 226 | void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 227 | uint64_t BaseAddr, uint64_t SymVA, |
| 228 | uint64_t GotVA) const {} |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 229 | |
| 230 | AArch64TargetInfo::AArch64TargetInfo() { |
| 231 | // PCRelReloc = FIXME |
| 232 | // GotReloc = FIXME |
| 233 | } |
| 234 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 235 | uint64_t PltEntryAddr) const {} |
| 236 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 237 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 238 | |
| 239 | static 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 Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 251 | void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 252 | uint32_t Type, uint64_t BaseAddr, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 253 | uint64_t SymVA, uint64_t GotVA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 254 | 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 Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 270 | |
| 271 | MipsTargetInfo::MipsTargetInfo() { |
| 272 | // PCRelReloc = FIXME |
| 273 | // GotReloc = FIXME |
| 274 | DefaultEntry = "__start"; |
| 275 | } |
| 276 | |
| 277 | void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 278 | uint64_t PltEntryAddr) const {} |
| 279 | |
| 280 | bool MipsTargetInfo::relocNeedsGot(uint32_t Type) const { return false; } |
| 281 | |
| 282 | bool MipsTargetInfo::relocNeedsPlt(uint32_t Type) const { return false; } |
| 283 | |
| 284 | void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame^] | 285 | uint64_t BaseAddr, uint64_t SymVA, |
| 286 | uint64_t GotVA) const {} |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 287 | } |
| 288 | } |