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