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