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