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