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