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