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