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