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 | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 12 | #include "Symbols.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 13 | |
| 14 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 15 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Endian.h" |
| 17 | #include "llvm/Support/ELF.h" |
| 18 | |
| 19 | using namespace llvm; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 20 | using namespace llvm::object; |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 21 | using namespace llvm::support::endian; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 22 | using namespace llvm::ELF; |
| 23 | |
| 24 | namespace lld { |
| 25 | namespace elf2 { |
| 26 | |
| 27 | std::unique_ptr<TargetInfo> Target; |
| 28 | |
| 29 | TargetInfo::~TargetInfo() {} |
| 30 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 31 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 32 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 33 | X86TargetInfo::X86TargetInfo() { |
| 34 | PCRelReloc = R_386_PC32; |
| 35 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 36 | GotRefReloc = R_386_GOT32; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 37 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 38 | |
| 39 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 40 | uint64_t PltEntryAddr) const { |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 41 | // jmpl *val; nop; nop |
| 42 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
| 43 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 44 | assert(isUInt<32>(GotEntryAddr)); |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 45 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 48 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 49 | return Type == R_386_GOT32 || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 52 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 53 | return Type == R_386_GOTPC; |
| 54 | } |
| 55 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 56 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 57 | return Type == R_386_PLT32; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 60 | static void add32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); } |
| 61 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 62 | void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 63 | uint64_t BaseAddr, uint64_t SymVA, |
| 64 | uint64_t GotVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 65 | 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 Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 70 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 71 | case R_386_GOT32: |
| 72 | add32le(Location, SymVA - GotVA); |
| 73 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 74 | case R_386_PC32: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 75 | add32le(Location, SymVA - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 76 | break; |
| 77 | case R_386_32: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 78 | add32le(Location, SymVA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 86 | X86_64TargetInfo::X86_64TargetInfo() { |
| 87 | PCRelReloc = R_X86_64_PC32; |
| 88 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 89 | GotRefReloc = R_X86_64_PC32; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 90 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 91 | |
| 92 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 93 | uint64_t PltEntryAddr) const { |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 94 | // 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 Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 97 | |
Rui Ueyama | e3fbc89 | 2015-09-29 23:25:21 +0000 | [diff] [blame] | 98 | uint64_t NextPC = PltEntryAddr + 6; |
| 99 | int64_t Delta = GotEntryAddr - NextPC; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 100 | assert(isInt<32>(Delta)); |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 101 | write32le(Buf + 2, Delta); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 104 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 105 | return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 108 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 109 | switch (Type) { |
| 110 | default: |
| 111 | return false; |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 112 | case R_X86_64_PC32: |
| 113 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 114 | // The problems start when a non PIC program calls a function in a shared |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 115 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 116 | // In an ideal world, we could just report an error saying the relocation |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 117 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 118 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 119 | // libc.so. |
| 120 | // |
| 121 | // The general idea on how to handle such cases is to create a PLT entry |
| 122 | // and use that as the function value. |
| 123 | // |
| 124 | // For the static linking part, we just return true and everything else |
| 125 | // will use the the PLT entry as the address. |
| 126 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 127 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 128 | // still works. We need the help of the dynamic linker for that. We |
| 129 | // let it know that we have a direct reference to a so symbol by creating |
| 130 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 131 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 132 | // This is true even for got entries, so pointer equality is maintained. |
| 133 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 134 | // real function is a dedicated got entry used by the plt. That is |
| 135 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 136 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 137 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 138 | case R_X86_64_PLT32: |
| 139 | return true; |
| 140 | } |
| 141 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 142 | |
| 143 | void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 144 | uint32_t Type, uint64_t BaseAddr, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 145 | uint64_t SymVA, uint64_t GotVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 146 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 147 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 148 | |
| 149 | uint64_t Offset = Rel.r_offset; |
| 150 | uint8_t *Location = Buf + Offset; |
| 151 | switch (Type) { |
| 152 | case R_X86_64_PC32: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 153 | case R_X86_64_GOTPCREL: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 154 | write32le(Location, SymVA + Rel.r_addend - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 155 | break; |
| 156 | case R_X86_64_64: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 157 | write64le(Location, SymVA + Rel.r_addend); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 158 | break; |
| 159 | case R_X86_64_32: { |
| 160 | case R_X86_64_32S: |
| 161 | uint64_t VA = SymVA + Rel.r_addend; |
| 162 | if (Type == R_X86_64_32 && !isUInt<32>(VA)) |
| 163 | error("R_X86_64_32 out of range"); |
| 164 | else if (!isInt<32>(VA)) |
| 165 | error("R_X86_64_32S out of range"); |
| 166 | |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 167 | write32le(Location, VA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | default: |
| 171 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | PPC64TargetInfo::PPC64TargetInfo() { |
| 177 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 178 | // GotReloc = FIXME |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 179 | } |
| 180 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 181 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 182 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 183 | return false; |
| 184 | } |
| 185 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 186 | return false; |
| 187 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 188 | void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 189 | uint64_t BaseAddr, uint64_t SymVA, |
| 190 | uint64_t GotVA) const { |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 191 | typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela; |
| 192 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 193 | |
| 194 | uint64_t Offset = Rel.r_offset; |
| 195 | uint8_t *Location = Buf + Offset; |
| 196 | switch (Type) { |
| 197 | case R_PPC64_ADDR64: |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 198 | write64be(Location, SymVA + Rel.r_addend); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 199 | break; |
| 200 | case R_PPC64_TOC: |
| 201 | // We don't create a TOC yet. |
| 202 | break; |
| 203 | default: |
| 204 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 205 | break; |
| 206 | } |
| 207 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 208 | |
| 209 | PPCTargetInfo::PPCTargetInfo() { |
| 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 PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 214 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 215 | bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 216 | return false; |
| 217 | } |
| 218 | bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 219 | return false; |
| 220 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 221 | void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 222 | uint64_t BaseAddr, uint64_t SymVA, |
| 223 | uint64_t GotVA) const {} |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 224 | |
| 225 | ARMTargetInfo::ARMTargetInfo() { |
| 226 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 227 | // GotReloc = FIXME |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 228 | } |
| 229 | void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 230 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 231 | bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 232 | return false; |
| 233 | } |
| 234 | bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 235 | return false; |
| 236 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 237 | void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 238 | uint64_t BaseAddr, uint64_t SymVA, |
| 239 | uint64_t GotVA) const {} |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 240 | |
| 241 | AArch64TargetInfo::AArch64TargetInfo() { |
| 242 | // PCRelReloc = FIXME |
| 243 | // GotReloc = FIXME |
| 244 | } |
| 245 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 246 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 247 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 248 | const SymbolBody &S) const { |
| 249 | return false; |
| 250 | } |
| 251 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 252 | const SymbolBody &S) const { |
| 253 | return false; |
| 254 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 255 | |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 256 | static void AArch64UpdateAdr(uint8_t *Location, uint64_t Imm) { |
| 257 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 258 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 259 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
| 260 | write32le(Location, (read32le(Location) & ~Mask) | ImmLo | ImmHi); |
| 261 | } |
| 262 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame^] | 263 | // Page(Expr) is the page address of the expression Expr, defined |
| 264 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
| 265 | // supported by the platform has a differen value). |
| 266 | static uint64_t AArch64GetPage(uint64_t Expr) { |
| 267 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 270 | static void handle_ADR_PREL_LO21(uint8_t *Location, uint64_t S, int64_t A, |
| 271 | uint64_t P) { |
| 272 | uint64_t X = S + A - P; |
| 273 | if (!isInt<21>(X)) |
| 274 | error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 275 | AArch64UpdateAdr(Location, X & 0x1FFFFF); |
| 276 | } |
| 277 | |
| 278 | static void handle_ADR_PREL_PG_HI21(uint8_t *Location, uint64_t S, int64_t A, |
| 279 | uint64_t P) { |
| 280 | uint64_t X = AArch64GetPage(S + A) - AArch64GetPage(P); |
| 281 | if (!isInt<33>(X)) |
| 282 | error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); |
| 283 | AArch64UpdateAdr(Location, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 286 | void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 287 | uint32_t Type, uint64_t BaseAddr, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 288 | uint64_t SymVA, uint64_t GotVA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 289 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 290 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 291 | |
| 292 | uint8_t *Location = Buf + Rel.r_offset; |
| 293 | uint64_t S = SymVA; |
| 294 | int64_t A = Rel.r_addend; |
| 295 | uint64_t P = BaseAddr + Rel.r_offset; |
| 296 | switch (Type) { |
| 297 | case R_AARCH64_ADR_PREL_LO21: |
| 298 | handle_ADR_PREL_LO21(Location, S, A, P); |
| 299 | break; |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 300 | case R_AARCH64_ADR_PREL_PG_HI21: |
| 301 | handle_ADR_PREL_PG_HI21(Location, S, A, P); |
| 302 | break; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 303 | default: |
| 304 | error(Twine("unrecognized reloc ") + Twine(Type)); |
| 305 | break; |
| 306 | } |
| 307 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 308 | |
| 309 | MipsTargetInfo::MipsTargetInfo() { |
| 310 | // PCRelReloc = FIXME |
| 311 | // GotReloc = FIXME |
| 312 | DefaultEntry = "__start"; |
| 313 | } |
| 314 | |
| 315 | void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 316 | uint64_t PltEntryAddr) const {} |
| 317 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 318 | bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 319 | return false; |
| 320 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 321 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 322 | bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 323 | return false; |
| 324 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 325 | |
| 326 | void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 327 | uint64_t BaseAddr, uint64_t SymVA, |
| 328 | uint64_t GotVA) const {} |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 329 | } |
| 330 | } |