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" |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 12 | #include "OutputSections.h" |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 13 | #include "Symbols.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 16 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Endian.h" |
| 18 | #include "llvm/Support/ELF.h" |
| 19 | |
| 20 | using namespace llvm; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 21 | using namespace llvm::object; |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 22 | using namespace llvm::support::endian; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 23 | using namespace llvm::ELF; |
| 24 | |
| 25 | namespace lld { |
| 26 | namespace elf2 { |
| 27 | |
| 28 | std::unique_ptr<TargetInfo> Target; |
| 29 | |
| 30 | TargetInfo::~TargetInfo() {} |
| 31 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 32 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 33 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 34 | bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } |
| 35 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 36 | X86TargetInfo::X86TargetInfo() { |
| 37 | PCRelReloc = R_386_PC32; |
| 38 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 39 | GotRefReloc = R_386_GOT32; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 40 | VAStart = 0x10000; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 41 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 42 | |
| 43 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 44 | uint64_t PltEntryAddr) const { |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 45 | // jmpl *val; nop; nop |
| 46 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
| 47 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 48 | assert(isUInt<32>(GotEntryAddr)); |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 49 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 52 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 53 | return Type == R_386_GOT32 || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 56 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 57 | return Type == R_386_GOTPC; |
| 58 | } |
| 59 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 60 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 730c278 | 2015-10-07 18:46:13 +0000 | [diff] [blame] | 61 | return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 64 | static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); } |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 65 | static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); } |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 66 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 67 | void X86TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 68 | uint64_t BaseAddr, uint64_t SymVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 69 | typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel; |
| 70 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 71 | |
| 72 | uint32_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 73 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 74 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 75 | case R_386_GOT32: |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 76 | add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA()); |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 77 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 78 | case R_386_PC32: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 79 | add32le(Loc, SymVA - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 80 | break; |
| 81 | case R_386_32: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 82 | add32le(Loc, SymVA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 83 | break; |
| 84 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 85 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 89 | X86_64TargetInfo::X86_64TargetInfo() { |
| 90 | PCRelReloc = R_X86_64_PC32; |
| 91 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 92 | GotRefReloc = R_X86_64_PC32; |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 93 | RelativeReloc = R_X86_64_RELATIVE; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 94 | |
| 95 | // On freebsd x86_64 the first page cannot be mmaped. |
| 96 | // On linux that is controled by vm.mmap_min_addr. At least on some x86_64 |
| 97 | // installs that is 65536, so the first 15 pages cannot be used. |
| 98 | // Given that, the smallest value that can be used in here is 0x10000. |
| 99 | // If using 2MB pages, the smallest page aligned address that works is |
| 100 | // 0x200000, but it looks like every OS uses 4k pages for executables. |
| 101 | VAStart = 0x10000; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 102 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 103 | |
| 104 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 105 | uint64_t PltEntryAddr) const { |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 106 | // jmpq *val(%rip); nop; nop |
| 107 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
| 108 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 109 | |
Rui Ueyama | e3fbc89 | 2015-09-29 23:25:21 +0000 | [diff] [blame] | 110 | uint64_t NextPC = PltEntryAddr + 6; |
| 111 | int64_t Delta = GotEntryAddr - NextPC; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 112 | assert(isInt<32>(Delta)); |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 113 | write32le(Buf + 2, Delta); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 116 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 117 | return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 120 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 121 | switch (Type) { |
| 122 | default: |
| 123 | return false; |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 124 | case R_X86_64_PC32: |
| 125 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 126 | // 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] | 127 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 128 | // 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] | 129 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 130 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 131 | // libc.so. |
| 132 | // |
| 133 | // The general idea on how to handle such cases is to create a PLT entry |
| 134 | // and use that as the function value. |
| 135 | // |
| 136 | // For the static linking part, we just return true and everything else |
| 137 | // will use the the PLT entry as the address. |
| 138 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 139 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 140 | // still works. We need the help of the dynamic linker for that. We |
| 141 | // let it know that we have a direct reference to a so symbol by creating |
| 142 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 143 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 144 | // This is true even for got entries, so pointer equality is maintained. |
| 145 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 146 | // real function is a dedicated got entry used by the plt. That is |
| 147 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 148 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 149 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 150 | case R_X86_64_PLT32: |
| 151 | return true; |
| 152 | } |
| 153 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 155 | bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { |
| 156 | switch (Type) { |
| 157 | default: |
| 158 | return false; |
| 159 | case R_X86_64_PC64: |
| 160 | case R_X86_64_PC32: |
| 161 | case R_X86_64_PC16: |
| 162 | case R_X86_64_PC8: |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 167 | void X86_64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 168 | uint32_t Type, uint64_t BaseAddr, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 169 | uint64_t SymVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 170 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 171 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 172 | |
| 173 | uint64_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 174 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 175 | switch (Type) { |
| 176 | case R_X86_64_PC32: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 177 | case R_X86_64_GOTPCREL: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 178 | write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 179 | break; |
| 180 | case R_X86_64_64: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 181 | write64le(Loc, SymVA + Rel.r_addend); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 182 | break; |
| 183 | case R_X86_64_32: { |
| 184 | case R_X86_64_32S: |
| 185 | uint64_t VA = SymVA + Rel.r_addend; |
| 186 | if (Type == R_X86_64_32 && !isUInt<32>(VA)) |
| 187 | error("R_X86_64_32 out of range"); |
| 188 | else if (!isInt<32>(VA)) |
| 189 | error("R_X86_64_32S out of range"); |
| 190 | |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 191 | write32le(Loc, VA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 192 | break; |
| 193 | } |
| 194 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 195 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | PPC64TargetInfo::PPC64TargetInfo() { |
| 200 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 201 | // GotReloc = FIXME |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 202 | PltEntrySize = 32; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame^] | 203 | |
| 204 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 205 | // set different permissions on a finer granularity than that). |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 206 | PageSize = 65536; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame^] | 207 | |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 208 | VAStart = 0x10000000; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 209 | } |
| 210 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 211 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 212 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 213 | return false; |
| 214 | } |
| 215 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 216 | return false; |
| 217 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 218 | void PPC64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 219 | uint64_t BaseAddr, uint64_t SymVA) const { |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 220 | typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela; |
| 221 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 222 | |
| 223 | uint64_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 224 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 225 | switch (Type) { |
| 226 | case R_PPC64_ADDR64: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 227 | write64be(Loc, SymVA + Rel.r_addend); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 228 | break; |
| 229 | case R_PPC64_TOC: |
| 230 | // We don't create a TOC yet. |
| 231 | break; |
| 232 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 233 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 236 | |
| 237 | PPCTargetInfo::PPCTargetInfo() { |
| 238 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 239 | // GotReloc = FIXME |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 240 | PageSize = 65536; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 241 | VAStart = 0x10000000; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 242 | } |
| 243 | void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 244 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 245 | bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 246 | return false; |
| 247 | } |
| 248 | bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 249 | return false; |
| 250 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 251 | void PPCTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 252 | uint64_t BaseAddr, uint64_t SymVA) const {} |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 253 | |
| 254 | ARMTargetInfo::ARMTargetInfo() { |
| 255 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 256 | // GotReloc = FIXME |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 257 | VAStart = 0x8000; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 258 | } |
| 259 | void ARMTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 260 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 261 | bool ARMTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 262 | return false; |
| 263 | } |
| 264 | bool ARMTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 265 | return false; |
| 266 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 267 | void ARMTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 268 | uint64_t BaseAddr, uint64_t SymVA) const {} |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 269 | |
| 270 | AArch64TargetInfo::AArch64TargetInfo() { |
| 271 | // PCRelReloc = FIXME |
| 272 | // GotReloc = FIXME |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 273 | VAStart = 0x400000; |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 274 | } |
| 275 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 276 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 277 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 278 | const SymbolBody &S) const { |
| 279 | return false; |
| 280 | } |
| 281 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 282 | const SymbolBody &S) const { |
| 283 | return false; |
| 284 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 285 | |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 286 | static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 287 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 288 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 289 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 290 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 293 | // Page(Expr) is the page address of the expression Expr, defined |
| 294 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
Davide Italiano | d9b5be4 | 2015-10-02 22:17:09 +0000 | [diff] [blame] | 295 | // supported by the platform has a different value.) |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 296 | static uint64_t getAArch64Page(uint64_t Expr) { |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 297 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 300 | void AArch64TargetInfo::relocateOne(uint8_t *Buf, const void *RelP, |
| 301 | uint32_t Type, uint64_t BaseAddr, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 302 | uint64_t SymVA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 303 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 304 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 305 | |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 306 | uint8_t *L = Buf + Rel.r_offset; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 307 | uint64_t S = SymVA; |
| 308 | int64_t A = Rel.r_addend; |
| 309 | uint64_t P = BaseAddr + Rel.r_offset; |
| 310 | switch (Type) { |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 311 | case R_AARCH64_ABS16: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 312 | if (!isInt<16>(S + A)) |
| 313 | error("Relocation R_AARCH64_ABS16 out of range"); |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 314 | write16le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 315 | break; |
| 316 | case R_AARCH64_ABS32: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 317 | if (!isInt<32>(S + A)) |
| 318 | error("Relocation R_AARCH64_ABS32 out of range"); |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 319 | write32le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 320 | break; |
| 321 | case R_AARCH64_ABS64: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 322 | // No overflow check needed. |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 323 | write64le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 324 | break; |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 325 | case R_AARCH64_ADD_ABS_LO12_NC: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 326 | // No overflow check needed. |
| 327 | or32le(L, ((S + A) & 0xFFF) << 10); |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 328 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 329 | case R_AARCH64_ADR_PREL_LO21: { |
| 330 | uint64_t X = S + A - P; |
| 331 | if (!isInt<21>(X)) |
| 332 | error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); |
| 333 | updateAArch64Adr(L, X & 0x1FFFFF); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 334 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 335 | } |
| 336 | case R_AARCH64_ADR_PREL_PG_HI21: { |
| 337 | uint64_t X = getAArch64Page(S + A) - getAArch64Page(P); |
| 338 | if (!isInt<33>(X)) |
| 339 | error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); |
| 340 | updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 341 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 342 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 343 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 344 | error("unrecognized reloc " + Twine(Type)); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 347 | |
| 348 | MipsTargetInfo::MipsTargetInfo() { |
| 349 | // PCRelReloc = FIXME |
| 350 | // GotReloc = FIXME |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 351 | PageSize = 65536; |
Hal Finkel | 4729064 | 2015-10-08 21:25:04 +0000 | [diff] [blame] | 352 | VAStart = 0x400000; |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void MipsTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 356 | uint64_t PltEntryAddr) const {} |
| 357 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 358 | bool MipsTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 359 | return false; |
| 360 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 361 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 362 | bool MipsTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 363 | return false; |
| 364 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 365 | |
| 366 | void MipsTargetInfo::relocateOne(uint8_t *Buf, const void *RelP, uint32_t Type, |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 367 | uint64_t BaseAddr, uint64_t SymVA) const { |
| 368 | typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel; |
| 369 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 370 | |
| 371 | switch (Type) { |
| 372 | case R_MIPS_32: |
| 373 | add32le(Buf + Rel.r_offset, SymVA); |
| 374 | break; |
| 375 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 376 | error("unrecognized reloc " + Twine(Type)); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 379 | } |
| 380 | } |