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