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