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