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