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