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