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 | |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 38 | template <endianness E> static void add32(void *P, int32_t V) { |
| 39 | write32<E>(P, read32<E>(P) + V); |
| 40 | } |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 41 | |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 42 | static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); } |
| 43 | static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); } |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | class X86TargetInfo final : public TargetInfo { |
| 47 | public: |
| 48 | X86TargetInfo(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 49 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 50 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 51 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 52 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 53 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 54 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 55 | bool relocPointsToGot(uint32_t Type) const override; |
| 56 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 57 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 58 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class X86_64TargetInfo final : public TargetInfo { |
| 62 | public: |
| 63 | X86_64TargetInfo(); |
Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 64 | unsigned getPltRefReloc(unsigned Type) const override; |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 65 | void writeGotPltHeaderEntries(uint8_t *Buf) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 66 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 67 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 68 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 69 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 70 | uint64_t PltEntryAddr, int32_t Index) const override; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 71 | bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 72 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 73 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 74 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 75 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 76 | bool isRelRelative(uint32_t Type) const override; |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 77 | bool isTlsOptimized(unsigned Type, const SymbolBody &S) const override; |
| 78 | void relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, |
| 79 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | class PPC64TargetInfo final : public TargetInfo { |
| 83 | public: |
| 84 | PPC64TargetInfo(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 85 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 86 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 87 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 88 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 89 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 90 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 91 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 92 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 93 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 94 | bool isRelRelative(uint32_t Type) const override; |
| 95 | }; |
| 96 | |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 97 | class AArch64TargetInfo final : public TargetInfo { |
| 98 | public: |
| 99 | AArch64TargetInfo(); |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 100 | unsigned getGotRefReloc(unsigned Type) const override; |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 101 | unsigned getPltRefReloc(unsigned Type) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 102 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 103 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 104 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 105 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 106 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 107 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 108 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 109 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 110 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | template <class ELFT> class MipsTargetInfo final : public TargetInfo { |
| 114 | public: |
| 115 | MipsTargetInfo(); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 116 | void writeGotHeaderEntries(uint8_t *Buf) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 117 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 118 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 119 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 120 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 121 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 122 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 123 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 124 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 125 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 126 | }; |
| 127 | } // anonymous namespace |
| 128 | |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 129 | TargetInfo *createTarget() { |
| 130 | switch (Config->EMachine) { |
| 131 | case EM_386: |
| 132 | return new X86TargetInfo(); |
| 133 | case EM_AARCH64: |
| 134 | return new AArch64TargetInfo(); |
| 135 | case EM_MIPS: |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 136 | switch (Config->EKind) { |
| 137 | case ELF32LEKind: |
| 138 | return new MipsTargetInfo<ELF32LE>(); |
| 139 | case ELF32BEKind: |
| 140 | return new MipsTargetInfo<ELF32BE>(); |
| 141 | default: |
| 142 | error("Unsupported MIPS target"); |
| 143 | } |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 144 | case EM_PPC64: |
| 145 | return new PPC64TargetInfo(); |
| 146 | case EM_X86_64: |
| 147 | return new X86_64TargetInfo(); |
| 148 | } |
| 149 | error("Unknown target machine"); |
| 150 | } |
| 151 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 152 | TargetInfo::~TargetInfo() {} |
| 153 | |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 154 | bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody &S) const { |
| 155 | return false; |
| 156 | } |
| 157 | |
Igor Kudrin | f6f4547 | 2015-11-10 08:39:27 +0000 | [diff] [blame] | 158 | uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; } |
| 159 | |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 160 | bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const { |
| 161 | return false; |
| 162 | } |
| 163 | |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 164 | unsigned TargetInfo::getGotRefReloc(unsigned Type) const { return GotRefReloc; } |
| 165 | |
Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 166 | unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; } |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 167 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 168 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 169 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 170 | bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } |
| 171 | |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 172 | void TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, |
| 173 | uint64_t SA) const {} |
| 174 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 175 | void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {} |
| 176 | |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 177 | void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {} |
| 178 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 179 | X86TargetInfo::X86TargetInfo() { |
| 180 | PCRelReloc = R_386_PC32; |
| 181 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 182 | GotRefReloc = R_386_GOT32; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 183 | PltReloc = R_386_JUMP_SLOT; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 184 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 185 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 186 | void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 187 | void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 188 | uint64_t PltEntryAddr) const {} |
| 189 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 190 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 191 | uint64_t PltEntryAddr, int32_t Index) const { |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 192 | // jmpl *val; nop; nop |
| 193 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 194 | memcpy(Buf, Inst, sizeof(Inst)); |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 195 | assert(isUInt<32>(GotEntryAddr)); |
| 196 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 199 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 200 | return Type == R_386_GOT32 || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 203 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 204 | return Type == R_386_GOTPC; |
| 205 | } |
| 206 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 207 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 730c278 | 2015-10-07 18:46:13 +0000 | [diff] [blame] | 208 | return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 211 | void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 212 | uint64_t P, uint64_t SA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 213 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 214 | case R_386_GOT32: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 215 | add32le(Loc, SA - Out<ELF32LE>::Got->getVA()); |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 216 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 217 | case R_386_PC32: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 218 | add32le(Loc, SA - P); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 219 | break; |
| 220 | case R_386_32: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 221 | add32le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 222 | break; |
| 223 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 224 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 228 | X86_64TargetInfo::X86_64TargetInfo() { |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 229 | CopyReloc = R_X86_64_COPY; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 230 | PCRelReloc = R_X86_64_PC32; |
| 231 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 232 | GotRefReloc = R_X86_64_PC32; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 233 | PltReloc = R_X86_64_JUMP_SLOT; |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 234 | RelativeReloc = R_X86_64_RELATIVE; |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 235 | TlsGotReloc = R_X86_64_TPOFF64; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 236 | TlsLocalDynamicReloc = R_X86_64_TLSLD; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 237 | TlsGlobalDynamicReloc = R_X86_64_TLSGD; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 238 | TlsModuleIndexReloc = R_X86_64_DTPMOD64; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 239 | TlsOffsetReloc = R_X86_64_DTPOFF64; |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 240 | TlsPcRelGotReloc = R_X86_64_GOTTPOFF; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 241 | LazyRelocations = true; |
| 242 | PltEntrySize = 16; |
| 243 | PltZeroEntrySize = 16; |
| 244 | } |
| 245 | |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 246 | void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const { |
| 247 | write64le(Buf, Out<ELF64LE>::Dynamic->getVA()); |
| 248 | } |
| 249 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 250 | void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { |
| 251 | // Skip 6 bytes of "jmpq *got(%rip)" |
| 252 | write32le(Buf, Plt + 6); |
| 253 | } |
| 254 | |
| 255 | void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 256 | uint64_t PltEntryAddr) const { |
| 257 | const uint8_t PltData[] = { |
| 258 | 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip) |
| 259 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip) |
| 260 | 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax) |
| 261 | }; |
| 262 | memcpy(Buf, PltData, sizeof(PltData)); |
| 263 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8 |
| 264 | write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16 |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 265 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 266 | |
| 267 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 268 | uint64_t PltEntryAddr, |
| 269 | int32_t Index) const { |
| 270 | const uint8_t Inst[] = { |
| 271 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) |
| 272 | 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> |
| 273 | 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0] |
| 274 | }; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 275 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 276 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 277 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6); |
| 278 | write32le(Buf + 7, Index); |
| 279 | write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 280 | } |
| 281 | |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 282 | bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type, |
| 283 | const SymbolBody &S) const { |
| 284 | if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 || |
| 285 | Type == R_X86_64_64) |
| 286 | if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) |
| 287 | return SS->Sym.getType() == STT_OBJECT; |
| 288 | return false; |
| 289 | } |
| 290 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 291 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 292 | if (Type == R_X86_64_GOTTPOFF) |
| 293 | return !isTlsOptimized(Type, S); |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 294 | return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL || |
| 295 | relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 298 | unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const { |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 299 | if (Type == R_X86_64_PLT32) |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 300 | return R_X86_64_PC32; |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 301 | return Type; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 304 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 305 | if (relocNeedsCopy(Type, S)) |
| 306 | return false; |
| 307 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 308 | switch (Type) { |
| 309 | default: |
| 310 | return false; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 311 | case R_X86_64_32: |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 312 | case R_X86_64_64: |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 313 | case R_X86_64_PC32: |
| 314 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 315 | // 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] | 316 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 317 | // 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] | 318 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 319 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 320 | // libc.so. |
| 321 | // |
| 322 | // The general idea on how to handle such cases is to create a PLT entry |
| 323 | // and use that as the function value. |
| 324 | // |
| 325 | // For the static linking part, we just return true and everything else |
| 326 | // will use the the PLT entry as the address. |
| 327 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 328 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 329 | // still works. We need the help of the dynamic linker for that. We |
| 330 | // let it know that we have a direct reference to a so symbol by creating |
| 331 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 332 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 333 | // This is true even for got entries, so pointer equality is maintained. |
| 334 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 335 | // real function is a dedicated got entry used by the plt. That is |
| 336 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 337 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 338 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 339 | case R_X86_64_PLT32: |
George Rimar | 8911d85 | 2015-10-16 23:52:24 +0000 | [diff] [blame] | 340 | return canBePreempted(&S, true); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 343 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 344 | bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { |
| 345 | switch (Type) { |
| 346 | default: |
| 347 | return false; |
| 348 | case R_X86_64_PC64: |
| 349 | case R_X86_64_PC32: |
| 350 | case R_X86_64_PC16: |
| 351 | case R_X86_64_PC8: |
Rafael Espindola | 69535df | 2015-10-19 05:20:01 +0000 | [diff] [blame] | 352 | case R_X86_64_PLT32: |
Michael J. Spencer | a5d9d1f | 2015-11-11 01:27:58 +0000 | [diff] [blame] | 353 | case R_X86_64_DTPOFF32: |
Michael J. Spencer | ac2307b | 2015-11-11 01:28:11 +0000 | [diff] [blame] | 354 | case R_X86_64_DTPOFF64: |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 355 | return true; |
| 356 | } |
| 357 | } |
| 358 | |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 359 | bool X86_64TargetInfo::isTlsOptimized(unsigned Type, |
| 360 | const SymbolBody &S) const { |
| 361 | if (Config->Shared || !S.isTLS()) |
| 362 | return false; |
| 363 | return Type == R_X86_64_GOTTPOFF && !canBePreempted(&S, true); |
| 364 | } |
| 365 | |
| 366 | // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to |
| 367 | // R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT. |
| 368 | // This function does that. Read "ELF Handling For Thread-Local Storage, |
| 369 | // 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf) |
| 370 | // by Ulrich Drepper for details. |
| 371 | void X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, |
| 372 | uint64_t P, uint64_t SA) const { |
| 373 | // Ulrich's document section 6.5 says that @gottpoff(%rip) must be |
| 374 | // used in MOVQ or ADDQ instructions only. |
| 375 | // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG". |
| 376 | // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG" |
| 377 | // (if the register is not RSP/R12) or "ADDQ $foo, %RSP". |
| 378 | // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48. |
| 379 | uint8_t *Prefix = Loc - 3; |
| 380 | uint8_t *Inst = Loc - 2; |
| 381 | uint8_t *RegSlot = Loc - 1; |
| 382 | uint8_t Reg = Loc[-1] >> 3; |
| 383 | bool IsMov = *Inst == 0x8b; |
| 384 | bool RspAdd = !IsMov && Reg == 4; |
| 385 | // r12 and rsp registers requires special handling. |
| 386 | // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11 |
| 387 | // result out is 7 bytes: 4d 8d 9b XX XX XX XX, |
| 388 | // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX. |
| 389 | // The same true for rsp. So we convert to addq for them, saving 1 byte that |
| 390 | // we dont have. |
| 391 | if (RspAdd) |
| 392 | *Inst = 0x81; |
| 393 | else |
| 394 | *Inst = IsMov ? 0xc7 : 0x8d; |
| 395 | if (*Prefix == 0x4c) |
| 396 | *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d; |
| 397 | *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3)); |
| 398 | relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA); |
| 399 | } |
| 400 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 401 | void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 402 | uint64_t P, uint64_t SA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 403 | switch (Type) { |
| 404 | case R_X86_64_PC32: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 405 | case R_X86_64_GOTPCREL: |
Rafael Espindola | 5045e44 | 2015-10-18 03:13:46 +0000 | [diff] [blame] | 406 | case R_X86_64_PLT32: |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 407 | case R_X86_64_TLSLD: |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 408 | case R_X86_64_TLSGD: |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 409 | case R_X86_64_TPOFF64: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 410 | write32le(Loc, SA - P); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 411 | break; |
| 412 | case R_X86_64_64: |
Michael J. Spencer | ac2307b | 2015-11-11 01:28:11 +0000 | [diff] [blame] | 413 | case R_X86_64_DTPOFF64: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 414 | write64le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 415 | break; |
Rui Ueyama | 3835b49 | 2015-10-23 16:13:27 +0000 | [diff] [blame] | 416 | case R_X86_64_32: |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 417 | case R_X86_64_32S: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 418 | if (Type == R_X86_64_32 && !isUInt<32>(SA)) |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 419 | error("R_X86_64_32 out of range"); |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 420 | else if (!isInt<32>(SA)) |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 421 | error("R_X86_64_32S out of range"); |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 422 | write32le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 423 | break; |
Michael J. Spencer | a5d9d1f | 2015-11-11 01:27:58 +0000 | [diff] [blame] | 424 | case R_X86_64_DTPOFF32: |
| 425 | write32le(Loc, SA); |
| 426 | break; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 427 | case R_X86_64_TPOFF32: { |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 428 | uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 429 | if (!isInt<32>(Val)) |
| 430 | error("R_X86_64_TPOFF32 out of range"); |
| 431 | write32le(Loc, Val); |
Michael J. Spencer | d77f0d2 | 2015-11-03 22:39:09 +0000 | [diff] [blame] | 432 | break; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 433 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 434 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 435 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 439 | // Relocation masks following the #lo(value), #hi(value), #ha(value), |
| 440 | // #higher(value), #highera(value), #highest(value), and #highesta(value) |
| 441 | // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi |
| 442 | // document. |
Rui Ueyama | c44e5a1 | 2015-10-23 16:54:58 +0000 | [diff] [blame] | 443 | static uint16_t applyPPCLo(uint64_t V) { return V; } |
| 444 | static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } |
| 445 | static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } |
| 446 | static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } |
| 447 | static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 448 | static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 449 | static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } |
| 450 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 451 | PPC64TargetInfo::PPC64TargetInfo() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 452 | PCRelReloc = R_PPC64_REL24; |
| 453 | GotReloc = R_PPC64_GLOB_DAT; |
| 454 | GotRefReloc = R_PPC64_REL64; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 455 | RelativeReloc = R_PPC64_RELATIVE; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 456 | PltEntrySize = 32; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 457 | |
| 458 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 459 | // set different permissions on a finer granularity than that). |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 460 | PageSize = 65536; |
Hal Finkel | 736c741 | 2015-10-15 07:49:07 +0000 | [diff] [blame] | 461 | |
| 462 | // The PPC64 ELF ABI v1 spec, says: |
| 463 | // |
| 464 | // It is normally desirable to put segments with different characteristics |
| 465 | // in separate 256 Mbyte portions of the address space, to give the |
| 466 | // operating system full paging flexibility in the 64-bit address space. |
| 467 | // |
| 468 | // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers |
| 469 | // use 0x10000000 as the starting address. |
| 470 | VAStart = 0x10000000; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 471 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 472 | |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 473 | uint64_t getPPC64TocBase() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 474 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 475 | // order. The TOC starts where the first of these sections starts. |
| 476 | |
| 477 | // FIXME: This obviously does not do the right thing when there is no .got |
| 478 | // section, but there is a .toc or .tocbss section. |
| 479 | uint64_t TocVA = Out<ELF64BE>::Got->getVA(); |
| 480 | if (!TocVA) |
| 481 | TocVA = Out<ELF64BE>::Plt->getVA(); |
| 482 | |
| 483 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 484 | // thus permitting a full 64 Kbytes segment. Note that the glibc startup |
| 485 | // code (crt1.o) assumes that you can get from the TOC base to the |
| 486 | // start of the .toc section with only a single (signed) 16-bit relocation. |
| 487 | return TocVA + 0x8000; |
| 488 | } |
| 489 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 490 | void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 491 | void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 492 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 493 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 494 | uint64_t PltEntryAddr, int32_t Index) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 495 | uint64_t Off = GotEntryAddr - getPPC64TocBase(); |
| 496 | |
| 497 | // FIXME: What we should do, in theory, is get the offset of the function |
| 498 | // descriptor in the .opd section, and use that as the offset from %r2 (the |
| 499 | // TOC-base pointer). Instead, we have the GOT-entry offset, and that will |
| 500 | // be a pointer to the function descriptor in the .opd section. Using |
| 501 | // this scheme is simpler, but requires an extra indirection per PLT dispatch. |
| 502 | |
Hal Finkel | fa92f68 | 2015-10-13 21:47:34 +0000 | [diff] [blame] | 503 | write32be(Buf, 0xf8410028); // std %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 504 | write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha |
| 505 | write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) |
| 506 | write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) |
| 507 | write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 |
| 508 | write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) |
| 509 | write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) |
| 510 | write32be(Buf + 28, 0x4e800420); // bctr |
| 511 | } |
| 512 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 513 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 514 | if (relocNeedsPlt(Type, S)) |
| 515 | return true; |
| 516 | |
| 517 | switch (Type) { |
| 518 | default: return false; |
| 519 | case R_PPC64_GOT16: |
| 520 | case R_PPC64_GOT16_LO: |
| 521 | case R_PPC64_GOT16_HI: |
| 522 | case R_PPC64_GOT16_HA: |
| 523 | case R_PPC64_GOT16_DS: |
| 524 | case R_PPC64_GOT16_LO_DS: |
| 525 | return true; |
| 526 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 527 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 528 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 529 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 530 | // These are function calls that need to be redirected through a PLT stub. |
Hal Finkel | 8228198 | 2015-10-17 00:48:20 +0000 | [diff] [blame] | 531 | return Type == R_PPC64_REL24 && canBePreempted(&S, false); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 532 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 533 | |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 534 | bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { |
| 535 | switch (Type) { |
| 536 | default: |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 537 | return true; |
Hal Finkel | 0091862 | 2015-10-16 19:01:50 +0000 | [diff] [blame] | 538 | case R_PPC64_TOC: |
| 539 | case R_PPC64_ADDR64: |
| 540 | return false; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 544 | void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 545 | uint64_t P, uint64_t SA) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 546 | uint64_t TB = getPPC64TocBase(); |
| 547 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 548 | // For a TOC-relative relocation, adjust the addend and proceed in terms of |
| 549 | // the corresponding ADDR16 relocation type. |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 550 | switch (Type) { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 551 | case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break; |
| 552 | case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break; |
| 553 | case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break; |
| 554 | case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break; |
| 555 | case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break; |
| 556 | 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] | 557 | default: break; |
| 558 | } |
| 559 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 560 | switch (Type) { |
| 561 | case R_PPC64_ADDR16: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 562 | if (!isInt<16>(SA)) |
Hal Finkel | 33e17a7 | 2015-10-15 16:17:30 +0000 | [diff] [blame] | 563 | error("Relocation R_PPC64_ADDR16 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 564 | write16be(Loc, SA); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 565 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 566 | case R_PPC64_ADDR16_DS: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 567 | if (!isInt<16>(SA)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 568 | error("Relocation R_PPC64_ADDR16_DS overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 569 | write16be(Loc, (read16be(Loc) & 3) | (SA & ~3)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 570 | break; |
| 571 | case R_PPC64_ADDR16_LO: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 572 | write16be(Loc, applyPPCLo(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 573 | break; |
| 574 | case R_PPC64_ADDR16_LO_DS: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 575 | write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 576 | break; |
| 577 | case R_PPC64_ADDR16_HI: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 578 | write16be(Loc, applyPPCHi(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 579 | break; |
| 580 | case R_PPC64_ADDR16_HA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 581 | write16be(Loc, applyPPCHa(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 582 | break; |
| 583 | case R_PPC64_ADDR16_HIGHER: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 584 | write16be(Loc, applyPPCHigher(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 585 | break; |
| 586 | case R_PPC64_ADDR16_HIGHERA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 587 | write16be(Loc, applyPPCHighera(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 588 | break; |
| 589 | case R_PPC64_ADDR16_HIGHEST: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 590 | write16be(Loc, applyPPCHighest(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 591 | break; |
| 592 | case R_PPC64_ADDR16_HIGHESTA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 593 | write16be(Loc, applyPPCHighesta(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 594 | break; |
| 595 | case R_PPC64_ADDR14: { |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 596 | if ((SA & 3) != 0) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 597 | error("Improper alignment for relocation R_PPC64_ADDR14"); |
| 598 | |
| 599 | // Preserve the AA/LK bits in the branch instruction |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 600 | uint8_t AALK = Loc[3]; |
| 601 | write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 602 | break; |
| 603 | } |
| 604 | case R_PPC64_REL16_LO: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 605 | write16be(Loc, applyPPCLo(SA - P)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 606 | break; |
| 607 | case R_PPC64_REL16_HI: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 608 | write16be(Loc, applyPPCHi(SA - P)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 609 | break; |
| 610 | case R_PPC64_REL16_HA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 611 | write16be(Loc, applyPPCHa(SA - P)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 612 | break; |
| 613 | case R_PPC64_ADDR32: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 614 | if (!isInt<32>(SA)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 615 | error("Relocation R_PPC64_ADDR32 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 616 | write32be(Loc, SA); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 617 | break; |
| 618 | case R_PPC64_REL24: { |
Hal Finkel | 8228198 | 2015-10-17 00:48:20 +0000 | [diff] [blame] | 619 | // If we have an undefined weak symbol, we might get here with a symbol |
| 620 | // address of zero. That could overflow, but the code must be unreachable, |
| 621 | // so don't bother doing anything at all. |
| 622 | if (!SA) |
| 623 | break; |
| 624 | |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 625 | uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); |
| 626 | uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 627 | bool InPlt = PltStart <= SA && SA < PltEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 628 | |
| 629 | if (!InPlt && Out<ELF64BE>::Opd) { |
| 630 | // If this is a local call, and we currently have the address of a |
| 631 | // function-descriptor, get the underlying code address instead. |
| 632 | uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); |
| 633 | uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 634 | bool InOpd = OpdStart <= SA && SA < OpdEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 635 | |
| 636 | if (InOpd) |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 637 | SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]); |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 640 | uint32_t Mask = 0x03FFFFFC; |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 641 | if (!isInt<24>(SA - P)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 642 | error("Relocation R_PPC64_REL24 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 643 | write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask)); |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 644 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 645 | uint32_t Nop = 0x60000000; |
| 646 | if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop) |
| 647 | write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | case R_PPC64_REL32: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 651 | if (!isInt<32>(SA - P)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 652 | error("Relocation R_PPC64_REL32 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 653 | write32be(Loc, SA - P); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 654 | break; |
| 655 | case R_PPC64_REL64: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 656 | write64be(Loc, SA - P); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 657 | break; |
| 658 | case R_PPC64_ADDR64: |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 659 | case R_PPC64_TOC: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 660 | write64be(Loc, SA); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 661 | break; |
| 662 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 663 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 666 | |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 667 | AArch64TargetInfo::AArch64TargetInfo() { |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 668 | GotReloc = R_AARCH64_GLOB_DAT; |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 669 | PltReloc = R_AARCH64_JUMP_SLOT; |
| 670 | LazyRelocations = true; |
| 671 | PltEntrySize = 16; |
| 672 | PltZeroEntrySize = 32; |
| 673 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 674 | |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 675 | unsigned AArch64TargetInfo::getGotRefReloc(unsigned Type) const { return Type; } |
| 676 | |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 677 | unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; } |
| 678 | |
| 679 | void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { |
| 680 | write64le(Buf, Out<ELF64LE>::Plt->getVA()); |
| 681 | } |
| 682 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 683 | void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 684 | uint64_t PltEntryAddr) const { |
| 685 | const uint8_t PltData[] = { |
| 686 | 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! |
| 687 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) |
| 688 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] |
| 689 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) |
| 690 | 0x20, 0x02, 0x1f, 0xd6, // br x17 |
| 691 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 692 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 693 | 0x1f, 0x20, 0x03, 0xd5 // nop |
| 694 | }; |
| 695 | memcpy(Buf, PltData, sizeof(PltData)); |
| 696 | |
| 697 | relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4, |
| 698 | GotEntryAddr + 16); |
| 699 | relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8, |
| 700 | GotEntryAddr + 16); |
| 701 | relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12, |
| 702 | GotEntryAddr + 16); |
| 703 | } |
| 704 | |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 705 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 706 | uint64_t PltEntryAddr, |
| 707 | int32_t Index) const { |
| 708 | const uint8_t Inst[] = { |
| 709 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) |
| 710 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] |
| 711 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) |
| 712 | 0x20, 0x02, 0x1f, 0xd6 // br x17 |
| 713 | }; |
| 714 | memcpy(Buf, Inst, sizeof(Inst)); |
| 715 | |
| 716 | relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr, |
| 717 | GotEntryAddr); |
| 718 | relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4, |
| 719 | GotEntryAddr); |
| 720 | relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8, |
| 721 | GotEntryAddr); |
| 722 | } |
| 723 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 724 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 725 | const SymbolBody &S) const { |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 726 | return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC || |
| 727 | relocNeedsPlt(Type, S); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 728 | } |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 729 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 730 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 731 | const SymbolBody &S) const { |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 732 | switch (Type) { |
| 733 | default: |
| 734 | return false; |
| 735 | case R_AARCH64_JUMP26: |
| 736 | case R_AARCH64_CALL26: |
| 737 | return canBePreempted(&S, true); |
| 738 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 739 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 740 | |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 741 | static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 742 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 743 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 744 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 745 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 748 | // Page(Expr) is the page address of the expression Expr, defined |
| 749 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
Davide Italiano | d9b5be4 | 2015-10-02 22:17:09 +0000 | [diff] [blame] | 750 | // supported by the platform has a different value.) |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 751 | static uint64_t getAArch64Page(uint64_t Expr) { |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 752 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 755 | template <unsigned N> |
| 756 | static void checkAArch64OutOfRange(int64_t X, uint32_t Type) { |
| 757 | if (!isInt<N>(X)) |
| 758 | error("Relocation " + getELFRelocationTypeName(EM_AARCH64, Type) + |
| 759 | " out of range"); |
| 760 | } |
| 761 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 762 | void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, |
| 763 | uint32_t Type, uint64_t P, |
| 764 | uint64_t SA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 765 | switch (Type) { |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 766 | case R_AARCH64_ABS16: |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 767 | checkAArch64OutOfRange<16>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 768 | write16le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 769 | break; |
| 770 | case R_AARCH64_ABS32: |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 771 | checkAArch64OutOfRange<32>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 772 | write32le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 773 | break; |
| 774 | case R_AARCH64_ABS64: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 775 | // No overflow check needed. |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 776 | write64le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 777 | break; |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 778 | case R_AARCH64_ADD_ABS_LO12_NC: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 779 | // No overflow check needed. |
Davide Italiano | a716574 | 2015-10-16 21:06:55 +0000 | [diff] [blame] | 780 | // This relocation stores 12 bits and there's no instruction |
| 781 | // to do it. Instead, we do a 32 bits store of the value |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 782 | // of r_addend bitwise-or'ed Loc. This assumes that the addend |
| 783 | // bits in Loc are zero. |
| 784 | or32le(Loc, (SA & 0xFFF) << 10); |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 785 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 786 | case R_AARCH64_ADR_PREL_LO21: { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 787 | uint64_t X = SA - P; |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 788 | checkAArch64OutOfRange<21>(X, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 789 | updateAArch64Adr(Loc, X & 0x1FFFFF); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 790 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 791 | } |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 792 | case R_AARCH64_ADR_GOT_PAGE: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 793 | case R_AARCH64_ADR_PREL_PG_HI21: { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 794 | uint64_t X = getAArch64Page(SA) - getAArch64Page(P); |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 795 | checkAArch64OutOfRange<33>(X, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 796 | updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 797 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 798 | } |
Igor Kudrin | b34115b | 2015-11-13 03:26:59 +0000 | [diff] [blame] | 799 | case R_AARCH64_JUMP26: |
| 800 | case R_AARCH64_CALL26: { |
| 801 | uint64_t X = SA - P; |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 802 | checkAArch64OutOfRange<28>(X, Type); |
Igor Kudrin | b34115b | 2015-11-13 03:26:59 +0000 | [diff] [blame] | 803 | or32le(Loc, (X & 0x0FFFFFFC) >> 2); |
| 804 | break; |
| 805 | } |
Davide Italiano | dc67f9b | 2015-11-20 21:35:38 +0000 | [diff] [blame] | 806 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| 807 | // No overflow check needed. |
| 808 | or32le(Loc, (SA & 0xFFC) << 8); |
| 809 | break; |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 810 | case R_AARCH64_LD64_GOT_LO12_NC: |
| 811 | if (SA & 0x7) |
| 812 | error("Relocation R_AARCH64_LD64_GOT_LO12_NC not aligned"); |
| 813 | // No overflow check needed. |
| 814 | or32le(Loc, (SA & 0xFF8) << 7); |
| 815 | break; |
Davide Italiano | bbcc7f6 | 2015-11-08 04:45:26 +0000 | [diff] [blame] | 816 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| 817 | // No overflow check needed. |
| 818 | or32le(Loc, (SA & 0xFF8) << 7); |
| 819 | break; |
Davide Italiano | dc67f9b | 2015-11-20 21:35:38 +0000 | [diff] [blame] | 820 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| 821 | // No overflow check needed. |
| 822 | or32le(Loc, (SA & 0xFFF) << 10); |
| 823 | break; |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 824 | case R_AARCH64_PREL16: |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 825 | checkAArch64OutOfRange<16>(SA - P, Type); |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 826 | write16le(Loc, SA - P); |
| 827 | break; |
| 828 | case R_AARCH64_PREL32: |
Igor Kudrin | ee252de | 2015-11-23 17:16:09 +0000 | [diff] [blame] | 829 | checkAArch64OutOfRange<32>(SA - P, Type); |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 830 | write32le(Loc, SA - P); |
| 831 | break; |
Davide Italiano | b12d668 | 2015-10-28 16:14:18 +0000 | [diff] [blame] | 832 | case R_AARCH64_PREL64: |
| 833 | // No overflow check needed. |
| 834 | write64le(Loc, SA - P); |
| 835 | break; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 836 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 837 | error("unrecognized reloc " + Twine(Type)); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 838 | } |
| 839 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 840 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 841 | template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 842 | PageSize = 65536; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 843 | GotRefReloc = R_MIPS_GOT16; |
| 844 | GotHeaderEntriesNum = 2; |
| 845 | } |
| 846 | |
| 847 | template <class ELFT> |
| 848 | void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const { |
| 849 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off; |
| 850 | auto *P = reinterpret_cast<Elf_Off *>(Buf); |
| 851 | // Module pointer |
| 852 | P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000; |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 855 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 856 | void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 857 | template <class ELFT> |
| 858 | void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 859 | uint64_t PltEntryAddr) const {} |
| 860 | template <class ELFT> |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 861 | void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 862 | uint64_t PltEntryAddr, int32_t Index) const {} |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 863 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 864 | template <class ELFT> |
| 865 | bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, |
| 866 | const SymbolBody &S) const { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 867 | return Type == R_MIPS_GOT16; |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 868 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 869 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 870 | template <class ELFT> |
| 871 | bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type, |
| 872 | const SymbolBody &S) const { |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 873 | return false; |
| 874 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 875 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 876 | template <class ELFT> |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 877 | void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd, |
| 878 | uint32_t Type, uint64_t P, |
| 879 | uint64_t SA) const { |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 880 | const endianness E = ELFT::TargetEndianness; |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 881 | switch (Type) { |
| 882 | case R_MIPS_32: |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 883 | add32<E>(Loc, SA); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 884 | break; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 885 | case R_MIPS_GOT16: { |
| 886 | int64_t V = SA - getMipsGpAddr<ELFT>(); |
| 887 | if (!isInt<16>(V)) |
| 888 | error("Relocation R_MIPS_GOT16 out of range"); |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 889 | write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff)); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 890 | break; |
| 891 | } |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 892 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 893 | error("unrecognized reloc " + Twine(Type)); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 896 | |
| 897 | template <class ELFT> |
| 898 | typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() { |
| 899 | const unsigned GPOffset = 0x7ff0; |
| 900 | return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0; |
| 901 | } |
| 902 | |
| 903 | template uint32_t getMipsGpAddr<ELF32LE>(); |
| 904 | template uint32_t getMipsGpAddr<ELF32BE>(); |
| 905 | template uint64_t getMipsGpAddr<ELF64LE>(); |
| 906 | template uint64_t getMipsGpAddr<ELF64BE>(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 907 | } |
| 908 | } |