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