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