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 | //===----------------------------------------------------------------------===// |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 9 | // |
| 10 | // Machine-specific stuff, such as applying relocations, creation of |
| 11 | // GOT or PLT entries, etc., are is handled in this file. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 14 | |
| 15 | #include "Target.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 16 | #include "Error.h" |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 17 | #include "OutputSections.h" |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 18 | #include "Symbols.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 19 | |
| 20 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 21 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Endian.h" |
| 23 | #include "llvm/Support/ELF.h" |
| 24 | |
| 25 | using namespace llvm; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 26 | using namespace llvm::object; |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 27 | using namespace llvm::support::endian; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 28 | using namespace llvm::ELF; |
| 29 | |
| 30 | namespace lld { |
| 31 | namespace elf2 { |
| 32 | |
| 33 | std::unique_ptr<TargetInfo> Target; |
| 34 | |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 35 | static void add32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) + V); } |
| 36 | static void add32be(uint8_t *L, int32_t V) { write32be(L, read32be(L) + V); } |
| 37 | static void or32le(uint8_t *L, int32_t V) { write32le(L, read32le(L) | V); } |
| 38 | |
| 39 | template <bool IsLE> static void add32(uint8_t *L, int32_t V); |
| 40 | template <> void add32<true>(uint8_t *L, int32_t V) { add32le(L, V); } |
| 41 | template <> void add32<false>(uint8_t *L, int32_t V) { add32be(L, V); } |
| 42 | |
| 43 | namespace { |
| 44 | class X86TargetInfo final : public TargetInfo { |
| 45 | public: |
| 46 | X86TargetInfo(); |
| 47 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 48 | uint64_t PltEntryAddr) const override; |
| 49 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 50 | bool relocPointsToGot(uint32_t Type) const override; |
| 51 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
| 52 | void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 53 | uint32_t Type, uint64_t BaseAddr, |
| 54 | uint64_t SymVA) const override; |
| 55 | }; |
| 56 | |
| 57 | class X86_64TargetInfo final : public TargetInfo { |
| 58 | public: |
| 59 | X86_64TargetInfo(); |
| 60 | unsigned getPLTRefReloc(unsigned Type) const override; |
| 61 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 62 | uint64_t PltEntryAddr) const override; |
| 63 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 64 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
| 65 | void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 66 | uint32_t Type, uint64_t BaseAddr, |
| 67 | uint64_t SymVA) const override; |
| 68 | bool isRelRelative(uint32_t Type) const override; |
| 69 | }; |
| 70 | |
| 71 | class PPC64TargetInfo final : public TargetInfo { |
| 72 | public: |
| 73 | PPC64TargetInfo(); |
| 74 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 75 | uint64_t PltEntryAddr) const override; |
| 76 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 77 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
| 78 | void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 79 | uint32_t Type, uint64_t BaseAddr, |
| 80 | uint64_t SymVA) const override; |
| 81 | bool isRelRelative(uint32_t Type) const override; |
| 82 | }; |
| 83 | |
| 84 | class PPCTargetInfo final : public TargetInfo { |
| 85 | public: |
| 86 | PPCTargetInfo(); |
| 87 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 88 | uint64_t PltEntryAddr) const override; |
| 89 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 90 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
| 91 | void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 92 | uint32_t Type, uint64_t BaseAddr, |
| 93 | uint64_t SymVA) const override; |
| 94 | }; |
| 95 | |
| 96 | class AArch64TargetInfo final : public TargetInfo { |
| 97 | public: |
| 98 | AArch64TargetInfo(); |
| 99 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 100 | uint64_t PltEntryAddr) const override; |
| 101 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 102 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
| 103 | void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 104 | uint32_t Type, uint64_t BaseAddr, |
| 105 | uint64_t SymVA) const override; |
| 106 | }; |
| 107 | |
| 108 | template <class ELFT> class MipsTargetInfo final : public TargetInfo { |
| 109 | public: |
| 110 | MipsTargetInfo(); |
| 111 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 112 | uint64_t PltEntryAddr) const override; |
| 113 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 114 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
| 115 | void relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 116 | uint32_t Type, uint64_t BaseAddr, |
| 117 | uint64_t SymVA) const override; |
| 118 | }; |
| 119 | } // anonymous namespace |
| 120 | |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 121 | TargetInfo *createTarget() { |
| 122 | switch (Config->EMachine) { |
| 123 | case EM_386: |
| 124 | return new X86TargetInfo(); |
| 125 | case EM_AARCH64: |
| 126 | return new AArch64TargetInfo(); |
| 127 | case EM_MIPS: |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 128 | switch (Config->EKind) { |
| 129 | case ELF32LEKind: |
| 130 | return new MipsTargetInfo<ELF32LE>(); |
| 131 | case ELF32BEKind: |
| 132 | return new MipsTargetInfo<ELF32BE>(); |
| 133 | default: |
| 134 | error("Unsupported MIPS target"); |
| 135 | } |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 136 | case EM_PPC: |
| 137 | return new PPCTargetInfo(); |
| 138 | case EM_PPC64: |
| 139 | return new PPC64TargetInfo(); |
| 140 | case EM_X86_64: |
| 141 | return new X86_64TargetInfo(); |
| 142 | } |
| 143 | error("Unknown target machine"); |
| 144 | } |
| 145 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 146 | TargetInfo::~TargetInfo() {} |
| 147 | |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 148 | unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; } |
| 149 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 150 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 151 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 152 | bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } |
| 153 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 154 | X86TargetInfo::X86TargetInfo() { |
| 155 | PCRelReloc = R_386_PC32; |
| 156 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 157 | GotRefReloc = R_386_GOT32; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 158 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 159 | |
| 160 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 161 | uint64_t PltEntryAddr) const { |
| 162 | // jmpl *val; nop; nop |
| 163 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 164 | memcpy(Buf, Inst, sizeof(Inst)); |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 165 | assert(isUInt<32>(GotEntryAddr)); |
| 166 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 169 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 170 | return Type == R_386_GOT32 || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 173 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 174 | return Type == R_386_GOTPC; |
| 175 | } |
| 176 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 177 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 730c278 | 2015-10-07 18:46:13 +0000 | [diff] [blame] | 178 | return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 181 | void X86TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, const void *RelP, |
| 182 | uint32_t Type, uint64_t BaseAddr, |
| 183 | uint64_t SymVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 184 | typedef ELFFile<ELF32LE>::Elf_Rel Elf_Rel; |
| 185 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 186 | |
| 187 | uint32_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 188 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 189 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 190 | case R_386_GOT32: |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 191 | add32le(Loc, SymVA - Out<ELF32LE>::Got->getVA()); |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 192 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 193 | case R_386_PC32: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 194 | add32le(Loc, SymVA - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 195 | break; |
| 196 | case R_386_32: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 197 | add32le(Loc, SymVA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 198 | break; |
| 199 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 200 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 204 | X86_64TargetInfo::X86_64TargetInfo() { |
| 205 | PCRelReloc = R_X86_64_PC32; |
| 206 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 207 | GotRefReloc = R_X86_64_PC32; |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 208 | RelativeReloc = R_X86_64_RELATIVE; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 209 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 210 | |
| 211 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 212 | uint64_t PltEntryAddr) const { |
| 213 | // jmpq *val(%rip); nop; nop |
| 214 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 215 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 216 | |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 217 | uint64_t NextPC = PltEntryAddr + 6; |
| 218 | int64_t Delta = GotEntryAddr - NextPC; |
| 219 | assert(isInt<32>(Delta)); |
| 220 | write32le(Buf + 2, Delta); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 223 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 224 | return Type == R_X86_64_GOTPCREL || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 227 | unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const { |
| 228 | switch (Type) { |
| 229 | case R_X86_64_32: |
| 230 | return R_X86_64_32; |
| 231 | case R_X86_64_PC32: |
| 232 | case R_X86_64_PLT32: |
| 233 | return R_X86_64_PC32; |
| 234 | } |
| 235 | llvm_unreachable("Unexpected relocation"); |
| 236 | } |
| 237 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 238 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 239 | switch (Type) { |
| 240 | default: |
| 241 | return false; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 242 | case R_X86_64_32: |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 243 | case R_X86_64_PC32: |
| 244 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 245 | // 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] | 246 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 247 | // 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] | 248 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 249 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 250 | // libc.so. |
| 251 | // |
| 252 | // The general idea on how to handle such cases is to create a PLT entry |
| 253 | // and use that as the function value. |
| 254 | // |
| 255 | // For the static linking part, we just return true and everything else |
| 256 | // will use the the PLT entry as the address. |
| 257 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 258 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 259 | // still works. We need the help of the dynamic linker for that. We |
| 260 | // let it know that we have a direct reference to a so symbol by creating |
| 261 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 262 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 263 | // This is true even for got entries, so pointer equality is maintained. |
| 264 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 265 | // real function is a dedicated got entry used by the plt. That is |
| 266 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 267 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 268 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 269 | case R_X86_64_PLT32: |
| 270 | return true; |
| 271 | } |
| 272 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 273 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 274 | bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { |
| 275 | switch (Type) { |
| 276 | default: |
| 277 | return false; |
| 278 | case R_X86_64_PC64: |
| 279 | case R_X86_64_PC32: |
| 280 | case R_X86_64_PC16: |
| 281 | case R_X86_64_PC8: |
| 282 | return true; |
| 283 | } |
| 284 | } |
| 285 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 286 | void X86_64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 287 | const void *RelP, uint32_t Type, |
| 288 | uint64_t BaseAddr, uint64_t SymVA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 289 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 290 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 291 | |
| 292 | uint64_t Offset = Rel.r_offset; |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 293 | uint8_t *Loc = Buf + Offset; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 294 | switch (Type) { |
| 295 | case R_X86_64_PC32: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 296 | case R_X86_64_GOTPCREL: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 297 | write32le(Loc, SymVA + Rel.r_addend - (BaseAddr + Offset)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 298 | break; |
| 299 | case R_X86_64_64: |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 300 | write64le(Loc, SymVA + Rel.r_addend); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 301 | break; |
| 302 | case R_X86_64_32: { |
| 303 | case R_X86_64_32S: |
| 304 | uint64_t VA = SymVA + Rel.r_addend; |
| 305 | if (Type == R_X86_64_32 && !isUInt<32>(VA)) |
| 306 | error("R_X86_64_32 out of range"); |
| 307 | else if (!isInt<32>(VA)) |
| 308 | error("R_X86_64_32S out of range"); |
| 309 | |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 310 | write32le(Loc, VA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 311 | break; |
| 312 | } |
| 313 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 314 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 318 | // Relocation masks following the #lo(value), #hi(value), #ha(value), |
| 319 | // #higher(value), #highera(value), #highest(value), and #highesta(value) |
| 320 | // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi |
| 321 | // document. |
| 322 | |
| 323 | static uint16_t applyPPCLo(uint64_t V) { return V & 0xffff; } |
| 324 | |
| 325 | static uint16_t applyPPCHi(uint64_t V) { return (V >> 16) & 0xffff; } |
| 326 | |
| 327 | static uint16_t applyPPCHa(uint64_t V) { return ((V + 0x8000) >> 16) & 0xffff; } |
| 328 | |
| 329 | static uint16_t applyPPCHigher(uint64_t V) { return (V >> 32) & 0xffff; } |
| 330 | |
| 331 | static uint16_t applyPPCHighera(uint64_t V) { |
| 332 | return ((V + 0x8000) >> 32) & 0xffff; |
| 333 | } |
| 334 | |
| 335 | static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } |
| 336 | |
| 337 | static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } |
| 338 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 339 | PPC64TargetInfo::PPC64TargetInfo() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 340 | PCRelReloc = R_PPC64_REL24; |
| 341 | GotReloc = R_PPC64_GLOB_DAT; |
| 342 | GotRefReloc = R_PPC64_REL64; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 343 | RelativeReloc = R_PPC64_RELATIVE; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 344 | PltEntrySize = 32; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 345 | |
| 346 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 347 | // set different permissions on a finer granularity than that). |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 348 | PageSize = 65536; |
Hal Finkel | 736c741 | 2015-10-15 07:49:07 +0000 | [diff] [blame] | 349 | |
| 350 | // The PPC64 ELF ABI v1 spec, says: |
| 351 | // |
| 352 | // It is normally desirable to put segments with different characteristics |
| 353 | // in separate 256 Mbyte portions of the address space, to give the |
| 354 | // operating system full paging flexibility in the 64-bit address space. |
| 355 | // |
| 356 | // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers |
| 357 | // use 0x10000000 as the starting address. |
| 358 | VAStart = 0x10000000; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 359 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 360 | |
| 361 | static uint64_t getPPC64TocBase() { |
| 362 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 363 | // order. The TOC starts where the first of these sections starts. |
| 364 | |
| 365 | // FIXME: This obviously does not do the right thing when there is no .got |
| 366 | // section, but there is a .toc or .tocbss section. |
| 367 | uint64_t TocVA = Out<ELF64BE>::Got->getVA(); |
| 368 | if (!TocVA) |
| 369 | TocVA = Out<ELF64BE>::Plt->getVA(); |
| 370 | |
| 371 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 372 | // thus permitting a full 64 Kbytes segment. Note that the glibc startup |
| 373 | // code (crt1.o) assumes that you can get from the TOC base to the |
| 374 | // start of the .toc section with only a single (signed) 16-bit relocation. |
| 375 | return TocVA + 0x8000; |
| 376 | } |
| 377 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 378 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 379 | uint64_t PltEntryAddr) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 380 | uint64_t Off = GotEntryAddr - getPPC64TocBase(); |
| 381 | |
| 382 | // FIXME: What we should do, in theory, is get the offset of the function |
| 383 | // descriptor in the .opd section, and use that as the offset from %r2 (the |
| 384 | // TOC-base pointer). Instead, we have the GOT-entry offset, and that will |
| 385 | // be a pointer to the function descriptor in the .opd section. Using |
| 386 | // this scheme is simpler, but requires an extra indirection per PLT dispatch. |
| 387 | |
Hal Finkel | fa92f68 | 2015-10-13 21:47:34 +0000 | [diff] [blame] | 388 | write32be(Buf, 0xf8410028); // std %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 389 | write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha |
| 390 | write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) |
| 391 | write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) |
| 392 | write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 |
| 393 | write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) |
| 394 | write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) |
| 395 | write32be(Buf + 28, 0x4e800420); // bctr |
| 396 | } |
| 397 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 398 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 399 | if (relocNeedsPlt(Type, S)) |
| 400 | return true; |
| 401 | |
| 402 | switch (Type) { |
| 403 | default: return false; |
| 404 | case R_PPC64_GOT16: |
| 405 | case R_PPC64_GOT16_LO: |
| 406 | case R_PPC64_GOT16_HI: |
| 407 | case R_PPC64_GOT16_HA: |
| 408 | case R_PPC64_GOT16_DS: |
| 409 | case R_PPC64_GOT16_LO_DS: |
| 410 | return true; |
| 411 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 412 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 413 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 414 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 415 | if (Type != R_PPC64_REL24) |
| 416 | return false; |
| 417 | |
| 418 | // These are function calls that need to be redirected through a PLT stub. |
Hal Finkel | ae3f05b | 2015-10-15 06:44:19 +0000 | [diff] [blame] | 419 | return S.isShared(); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 420 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 421 | |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 422 | bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { |
| 423 | switch (Type) { |
| 424 | default: |
| 425 | return false; |
| 426 | case R_PPC64_REL24: |
| 427 | case R_PPC64_REL14: |
| 428 | case R_PPC64_REL14_BRTAKEN: |
| 429 | case R_PPC64_REL14_BRNTAKEN: |
| 430 | case R_PPC64_REL32: |
| 431 | case R_PPC64_REL64: |
| 432 | return true; |
| 433 | } |
| 434 | } |
| 435 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 436 | void PPC64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 437 | const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 438 | uint64_t BaseAddr, uint64_t SymVA) const { |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 439 | typedef ELFFile<ELF64BE>::Elf_Rela Elf_Rela; |
| 440 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 441 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 442 | uint8_t *L = Buf + Rel.r_offset; |
| 443 | uint64_t S = SymVA; |
| 444 | int64_t A = Rel.r_addend; |
| 445 | uint64_t P = BaseAddr + Rel.r_offset; |
| 446 | uint64_t TB = getPPC64TocBase(); |
| 447 | |
| 448 | if (Type == R_PPC64_TOC) { |
| 449 | write64be(L, TB); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | // For a TOC-relative relocation, adjust the addend and proceed in terms of |
| 454 | // the corresponding ADDR16 relocation type. |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 455 | switch (Type) { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 456 | case R_PPC64_TOC16: Type = R_PPC64_ADDR16; A -= TB; break; |
| 457 | case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; A -= TB; break; |
| 458 | case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; A -= TB; break; |
| 459 | case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; A -= TB; break; |
| 460 | case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; A -= TB; break; |
| 461 | case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; A -= TB; break; |
| 462 | default: break; |
| 463 | } |
| 464 | |
| 465 | uint64_t R = S + A; |
| 466 | |
| 467 | switch (Type) { |
| 468 | case R_PPC64_ADDR16: |
| 469 | write16be(L, applyPPCLo(R)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 470 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 471 | case R_PPC64_ADDR16_DS: |
| 472 | if (!isInt<16>(R)) |
| 473 | error("Relocation R_PPC64_ADDR16_DS overflow"); |
| 474 | write16be(L, (read16be(L) & 3) | (R & ~3)); |
| 475 | break; |
| 476 | case R_PPC64_ADDR16_LO: |
| 477 | write16be(L, applyPPCLo(R)); |
| 478 | break; |
| 479 | case R_PPC64_ADDR16_LO_DS: |
| 480 | write16be(L, (read16be(L) & 3) | (applyPPCLo(R) & ~3)); |
| 481 | break; |
| 482 | case R_PPC64_ADDR16_HI: |
| 483 | write16be(L, applyPPCHi(R)); |
| 484 | break; |
| 485 | case R_PPC64_ADDR16_HA: |
| 486 | write16be(L, applyPPCHa(R)); |
| 487 | break; |
| 488 | case R_PPC64_ADDR16_HIGHER: |
| 489 | write16be(L, applyPPCHigher(R)); |
| 490 | break; |
| 491 | case R_PPC64_ADDR16_HIGHERA: |
| 492 | write16be(L, applyPPCHighera(R)); |
| 493 | break; |
| 494 | case R_PPC64_ADDR16_HIGHEST: |
| 495 | write16be(L, applyPPCHighest(R)); |
| 496 | break; |
| 497 | case R_PPC64_ADDR16_HIGHESTA: |
| 498 | write16be(L, applyPPCHighesta(R)); |
| 499 | break; |
| 500 | case R_PPC64_ADDR14: { |
| 501 | if ((R & 3) != 0) |
| 502 | error("Improper alignment for relocation R_PPC64_ADDR14"); |
| 503 | |
| 504 | // Preserve the AA/LK bits in the branch instruction |
| 505 | uint8_t AALK = L[3]; |
| 506 | write16be(L + 2, (AALK & 3) | (R & 0xfffc)); |
| 507 | break; |
| 508 | } |
| 509 | case R_PPC64_REL16_LO: |
| 510 | write16be(L, applyPPCLo(R - P)); |
| 511 | break; |
| 512 | case R_PPC64_REL16_HI: |
| 513 | write16be(L, applyPPCHi(R - P)); |
| 514 | break; |
| 515 | case R_PPC64_REL16_HA: |
| 516 | write16be(L, applyPPCHa(R - P)); |
| 517 | break; |
| 518 | case R_PPC64_ADDR32: |
| 519 | if (!isInt<32>(R)) |
| 520 | error("Relocation R_PPC64_ADDR32 overflow"); |
| 521 | write32be(L, R); |
| 522 | break; |
| 523 | case R_PPC64_REL24: { |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 524 | uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); |
| 525 | uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); |
Hal Finkel | 4e961dd | 2015-10-13 22:54:24 +0000 | [diff] [blame] | 526 | bool InPlt = PltStart <= R && R < PltEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 527 | |
| 528 | if (!InPlt && Out<ELF64BE>::Opd) { |
| 529 | // If this is a local call, and we currently have the address of a |
| 530 | // function-descriptor, get the underlying code address instead. |
| 531 | uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); |
| 532 | uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); |
Hal Finkel | 4e961dd | 2015-10-13 22:54:24 +0000 | [diff] [blame] | 533 | bool InOpd = OpdStart <= R && R < OpdEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 534 | |
| 535 | if (InOpd) |
Hal Finkel | 4e961dd | 2015-10-13 22:54:24 +0000 | [diff] [blame] | 536 | R = read64be(&Out<ELF64BE>::OpdBuf[R - OpdStart]); |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 539 | uint32_t Mask = 0x03FFFFFC; |
| 540 | if (!isInt<24>(R - P)) |
| 541 | error("Relocation R_PPC64_REL24 overflow"); |
| 542 | write32be(L, (read32be(L) & ~Mask) | ((R - P) & Mask)); |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 543 | |
Hal Finkel | 515ed44 | 2015-10-13 20:31:33 +0000 | [diff] [blame] | 544 | if (InPlt && L + 8 <= BufEnd && |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 545 | read32be(L + 4) == 0x60000000 /* nop */) |
| 546 | write32be(L + 4, 0xe8410028); // ld %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | case R_PPC64_REL32: |
| 550 | if (!isInt<32>(R - P)) |
| 551 | error("Relocation R_PPC64_REL32 overflow"); |
| 552 | write32be(L, R - P); |
| 553 | break; |
| 554 | case R_PPC64_REL64: |
| 555 | write64be(L, R - P); |
| 556 | break; |
| 557 | case R_PPC64_ADDR64: |
| 558 | write64be(L, R); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 559 | break; |
| 560 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 561 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 564 | |
| 565 | PPCTargetInfo::PPCTargetInfo() { |
| 566 | // PCRelReloc = FIXME |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 567 | // GotReloc = FIXME |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 568 | PageSize = 65536; |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 569 | } |
| 570 | void PPCTargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 571 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 572 | bool PPCTargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
| 573 | return false; |
| 574 | } |
| 575 | bool PPCTargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
| 576 | return false; |
| 577 | } |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 578 | void PPCTargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 579 | const void *RelP, uint32_t Type, |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 580 | uint64_t BaseAddr, uint64_t SymVA) const {} |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 581 | |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 582 | AArch64TargetInfo::AArch64TargetInfo() { |
| 583 | // PCRelReloc = FIXME |
| 584 | // GotReloc = FIXME |
| 585 | } |
| 586 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 587 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 588 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 589 | const SymbolBody &S) const { |
| 590 | return false; |
| 591 | } |
| 592 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 593 | const SymbolBody &S) const { |
| 594 | return false; |
| 595 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 596 | |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 597 | static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 598 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 599 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 600 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 601 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 604 | // Page(Expr) is the page address of the expression Expr, defined |
| 605 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
Davide Italiano | d9b5be4 | 2015-10-02 22:17:09 +0000 | [diff] [blame] | 606 | // supported by the platform has a different value.) |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 607 | static uint64_t getAArch64Page(uint64_t Expr) { |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 608 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 611 | void AArch64TargetInfo::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 612 | const void *RelP, uint32_t Type, |
| 613 | uint64_t BaseAddr, uint64_t SymVA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 614 | typedef ELFFile<ELF64LE>::Elf_Rela Elf_Rela; |
| 615 | auto &Rel = *reinterpret_cast<const Elf_Rela *>(RelP); |
| 616 | |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 617 | uint8_t *L = Buf + Rel.r_offset; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 618 | uint64_t S = SymVA; |
| 619 | int64_t A = Rel.r_addend; |
| 620 | uint64_t P = BaseAddr + Rel.r_offset; |
| 621 | switch (Type) { |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 622 | case R_AARCH64_ABS16: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 623 | if (!isInt<16>(S + A)) |
| 624 | error("Relocation R_AARCH64_ABS16 out of range"); |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 625 | write16le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 626 | break; |
| 627 | case R_AARCH64_ABS32: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 628 | if (!isInt<32>(S + A)) |
| 629 | error("Relocation R_AARCH64_ABS32 out of range"); |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 630 | write32le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 631 | break; |
| 632 | case R_AARCH64_ABS64: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 633 | // No overflow check needed. |
Davide Italiano | 06d8432 | 2015-10-07 22:10:02 +0000 | [diff] [blame] | 634 | write64le(L, S + A); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 635 | break; |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 636 | case R_AARCH64_ADD_ABS_LO12_NC: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 637 | // No overflow check needed. |
| 638 | or32le(L, ((S + A) & 0xFFF) << 10); |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 639 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 640 | case R_AARCH64_ADR_PREL_LO21: { |
| 641 | uint64_t X = S + A - P; |
| 642 | if (!isInt<21>(X)) |
| 643 | error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); |
| 644 | updateAArch64Adr(L, X & 0x1FFFFF); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 645 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 646 | } |
| 647 | case R_AARCH64_ADR_PREL_PG_HI21: { |
| 648 | uint64_t X = getAArch64Page(S + A) - getAArch64Page(P); |
| 649 | if (!isInt<33>(X)) |
| 650 | error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); |
| 651 | updateAArch64Adr(L, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 652 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 653 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 654 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 655 | error("unrecognized reloc " + Twine(Type)); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 658 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 659 | template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 660 | // PCRelReloc = FIXME |
| 661 | // GotReloc = FIXME |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 662 | PageSize = 65536; |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 665 | template <class ELFT> |
| 666 | void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 667 | uint64_t PltEntryAddr) const {} |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 668 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 669 | template <class ELFT> |
| 670 | bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, |
| 671 | const SymbolBody &S) const { |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 672 | return false; |
| 673 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 674 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 675 | template <class ELFT> |
| 676 | bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type, |
| 677 | const SymbolBody &S) const { |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 678 | return false; |
| 679 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 680 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 681 | template <class ELFT> |
| 682 | void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Buf, uint8_t *BufEnd, |
| 683 | const void *RelP, uint32_t Type, |
| 684 | uint64_t BaseAddr, |
| 685 | uint64_t SymVA) const { |
| 686 | const bool IsLE = ELFT::TargetEndianness == support::little; |
| 687 | typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel; |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 688 | auto &Rel = *reinterpret_cast<const Elf_Rel *>(RelP); |
| 689 | |
| 690 | switch (Type) { |
| 691 | case R_MIPS_32: |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 692 | add32<IsLE>(Buf + Rel.r_offset, SymVA); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 693 | break; |
| 694 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 695 | error("unrecognized reloc " + Twine(Type)); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 698 | } |
| 699 | } |