Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 1 | //===- Target.cpp ---------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 9 | // |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 10 | // Machine-specific things, such as applying relocations, creation of |
| 11 | // GOT or PLT entries, etc., are handled in this file. |
| 12 | // |
| 13 | // Refer the ELF spec for the single letter varaibles, S, A or P, used |
| 14 | // in this file. SA is S+A. |
Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 17 | |
| 18 | #include "Target.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 19 | #include "Error.h" |
Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 20 | #include "OutputSections.h" |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 21 | #include "Symbols.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 22 | |
| 23 | #include "llvm/ADT/ArrayRef.h" |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 24 | #include "llvm/Object/ELF.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Endian.h" |
| 26 | #include "llvm/Support/ELF.h" |
| 27 | |
| 28 | using namespace llvm; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 29 | using namespace llvm::object; |
Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 30 | using namespace llvm::support::endian; |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 31 | using namespace llvm::ELF; |
| 32 | |
| 33 | namespace lld { |
| 34 | namespace elf2 { |
| 35 | |
| 36 | std::unique_ptr<TargetInfo> Target; |
| 37 | |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 38 | template <endianness E> static void add32(void *P, int32_t V) { |
| 39 | write32<E>(P, read32<E>(P) + V); |
| 40 | } |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 41 | |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 42 | static void add32le(uint8_t *P, int32_t V) { add32<support::little>(P, V); } |
| 43 | static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); } |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 44 | |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 45 | template <unsigned N> static void checkInt(int64_t V, uint32_t Type) { |
| 46 | if (isInt<N>(V)) |
| 47 | return; |
| 48 | StringRef S = getELFRelocationTypeName(Config->EMachine, Type); |
| 49 | error("Relocation " + S + " out of range"); |
| 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); |
| 56 | error("Relocation " + S + " out of range"); |
| 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); |
| 63 | error("Relocation " + S + " out of range"); |
| 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); |
| 70 | error("Improper alignment for relocation " + S); |
| 71 | } |
| 72 | |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 73 | namespace { |
| 74 | class X86TargetInfo final : public TargetInfo { |
| 75 | public: |
| 76 | X86TargetInfo(); |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 77 | void writeGotPltHeaderEntries(uint8_t *Buf) const override; |
George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 78 | unsigned getDynReloc(unsigned Type) const override; |
| 79 | bool isTlsDynReloc(unsigned Type) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 80 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 81 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 82 | uint64_t PltEntryAddr) const override; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 83 | void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, |
| 84 | uint64_t PltEntryAddr, int32_t Index, |
| 85 | unsigned RelOff) const override; |
George Rimar | 70e2508 | 2015-11-25 11:27:40 +0000 | [diff] [blame] | 86 | bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 87 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 88 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 89 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 90 | uint64_t SA, uint8_t *PairedLoc = nullptr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | class X86_64TargetInfo final : public TargetInfo { |
| 94 | public: |
| 95 | X86_64TargetInfo(); |
Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 96 | unsigned getPltRefReloc(unsigned Type) const override; |
George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 97 | bool isTlsDynReloc(unsigned Type) const override; |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 98 | void writeGotPltHeaderEntries(uint8_t *Buf) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 99 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 100 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 101 | uint64_t PltEntryAddr) const override; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 102 | void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, |
| 103 | uint64_t PltEntryAddr, int32_t Index, |
| 104 | unsigned RelOff) const override; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 105 | bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 106 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 107 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 108 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 109 | uint64_t SA, uint8_t *PairedLoc = nullptr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 110 | bool isRelRelative(uint32_t Type) const override; |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 111 | bool isTlsOptimized(unsigned Type, const SymbolBody *S) const override; |
| 112 | unsigned relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 113 | uint64_t P, uint64_t SA) const override; |
| 114 | |
| 115 | private: |
| 116 | void relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, |
| 117 | uint64_t SA) const; |
| 118 | void relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, |
| 119 | uint64_t SA) const; |
| 120 | void relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, uint64_t P, |
| 121 | uint64_t SA) const; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | class PPC64TargetInfo final : public TargetInfo { |
| 125 | public: |
| 126 | PPC64TargetInfo(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 127 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 128 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 129 | uint64_t PltEntryAddr) const override; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 130 | void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, |
| 131 | uint64_t PltEntryAddr, int32_t Index, |
| 132 | unsigned RelOff) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 133 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 134 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 135 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 136 | uint64_t SA, uint8_t *PairedLoc = nullptr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 137 | bool isRelRelative(uint32_t Type) const override; |
| 138 | }; |
| 139 | |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 140 | class AArch64TargetInfo final : public TargetInfo { |
| 141 | public: |
| 142 | AArch64TargetInfo(); |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 143 | unsigned getPltRefReloc(unsigned Type) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 144 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 145 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 146 | uint64_t PltEntryAddr) const override; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 147 | void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, |
| 148 | uint64_t PltEntryAddr, int32_t Index, |
| 149 | unsigned RelOff) const override; |
Igor Kudrin | 9606d19 | 2015-12-03 08:05:35 +0000 | [diff] [blame] | 150 | bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 151 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 152 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 153 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 154 | uint64_t SA, uint8_t *PairedLoc = nullptr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | template <class ELFT> class MipsTargetInfo final : public TargetInfo { |
| 158 | public: |
| 159 | MipsTargetInfo(); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 160 | void writeGotHeaderEntries(uint8_t *Buf) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 161 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 162 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 163 | uint64_t PltEntryAddr) const override; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 164 | void writePltEntry(uint8_t *Buf, uint64_t GotAddr, uint64_t GotEntryAddr, |
| 165 | uint64_t PltEntryAddr, int32_t Index, |
| 166 | unsigned RelOff) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 167 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 168 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 169 | void relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, uint64_t P, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 170 | uint64_t SA, uint8_t *PairedLoc = nullptr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 171 | }; |
| 172 | } // anonymous namespace |
| 173 | |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 174 | TargetInfo *createTarget() { |
| 175 | switch (Config->EMachine) { |
| 176 | case EM_386: |
| 177 | return new X86TargetInfo(); |
| 178 | case EM_AARCH64: |
| 179 | return new AArch64TargetInfo(); |
| 180 | case EM_MIPS: |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 181 | switch (Config->EKind) { |
| 182 | case ELF32LEKind: |
| 183 | return new MipsTargetInfo<ELF32LE>(); |
| 184 | case ELF32BEKind: |
| 185 | return new MipsTargetInfo<ELF32BE>(); |
| 186 | default: |
| 187 | error("Unsupported MIPS target"); |
| 188 | } |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 189 | case EM_PPC64: |
| 190 | return new PPC64TargetInfo(); |
| 191 | case EM_X86_64: |
| 192 | return new X86_64TargetInfo(); |
| 193 | } |
| 194 | error("Unknown target machine"); |
| 195 | } |
| 196 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 197 | TargetInfo::~TargetInfo() {} |
| 198 | |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 199 | bool TargetInfo::isTlsOptimized(unsigned Type, const SymbolBody *S) const { |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
Igor Kudrin | f6f4547 | 2015-11-10 08:39:27 +0000 | [diff] [blame] | 203 | uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; } |
| 204 | |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 205 | bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const { |
| 206 | return false; |
| 207 | } |
| 208 | |
Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 209 | unsigned TargetInfo::getPltRefReloc(unsigned Type) const { return PCRelReloc; } |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 210 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 211 | bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } |
| 212 | |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 213 | unsigned TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, |
| 214 | uint32_t Type, uint64_t P, |
| 215 | uint64_t SA) const { |
| 216 | return 0; |
| 217 | } |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 218 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 219 | void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {} |
| 220 | |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 221 | void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {} |
| 222 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 223 | X86TargetInfo::X86TargetInfo() { |
George Rimar | 70e2508 | 2015-11-25 11:27:40 +0000 | [diff] [blame] | 224 | CopyReloc = R_386_COPY; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 225 | PCRelReloc = R_386_PC32; |
| 226 | GotReloc = R_386_GLOB_DAT; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 227 | PltReloc = R_386_JUMP_SLOT; |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 228 | TlsGotReloc = R_386_TLS_TPOFF; |
| 229 | TlsGlobalDynamicReloc = R_386_TLS_GD; |
| 230 | TlsLocalDynamicReloc = R_386_TLS_LDM; |
| 231 | TlsModuleIndexReloc = R_386_TLS_DTPMOD32; |
| 232 | TlsOffsetReloc = R_386_TLS_DTPOFF32; |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 233 | LazyRelocations = true; |
| 234 | PltEntrySize = 16; |
| 235 | PltZeroEntrySize = 16; |
| 236 | } |
| 237 | |
| 238 | void X86TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const { |
| 239 | write32le(Buf, Out<ELF32LE>::Dynamic->getVA()); |
| 240 | } |
| 241 | |
| 242 | void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { |
| 243 | // Skip 6 bytes of "pushl (GOT+4)" |
| 244 | write32le(Buf, Plt + 6); |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 245 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 246 | |
George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 247 | unsigned X86TargetInfo::getDynReloc(unsigned Type) const { |
| 248 | if (Type == R_386_TLS_LE) |
| 249 | return R_386_TLS_TPOFF; |
| 250 | if (Type == R_386_TLS_LE_32) |
| 251 | return R_386_TLS_TPOFF32; |
| 252 | return Type; |
| 253 | } |
| 254 | |
| 255 | bool X86TargetInfo::isTlsDynReloc(unsigned Type) const { |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 256 | if (Type == R_386_TLS_LE || Type == R_386_TLS_LE_32 || |
| 257 | Type == R_386_TLS_GOTIE) |
George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 258 | return Config->Shared; |
| 259 | return false; |
| 260 | } |
| 261 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 262 | void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 263 | uint64_t PltEntryAddr) const { |
| 264 | // Executable files and shared object files have |
| 265 | // separate procedure linkage tables. |
| 266 | if (Config->Shared) { |
| 267 | const uint8_t V[] = { |
| 268 | 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx |
| 269 | 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx) |
| 270 | 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop |
| 271 | }; |
| 272 | memcpy(Buf, V, sizeof(V)); |
| 273 | return; |
| 274 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 275 | |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 276 | const uint8_t PltData[] = { |
| 277 | 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4) |
| 278 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8) |
| 279 | 0x90, 0x90, 0x90, 0x90 // nop;nop;nop;nop |
| 280 | }; |
| 281 | memcpy(Buf, PltData, sizeof(PltData)); |
| 282 | write32le(Buf + 2, GotEntryAddr + 4); // GOT+4 |
| 283 | write32le(Buf + 8, GotEntryAddr + 8); // GOT+8 |
| 284 | } |
| 285 | |
| 286 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, |
| 287 | uint64_t GotEntryAddr, uint64_t PltEntryAddr, |
| 288 | int32_t Index, unsigned RelOff) const { |
| 289 | const uint8_t Inst[] = { |
| 290 | 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx) |
| 291 | 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset |
| 292 | 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC |
| 293 | }; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 294 | memcpy(Buf, Inst, sizeof(Inst)); |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 295 | // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT |
| 296 | Buf[1] = Config->Shared ? 0xa3 : 0x25; |
| 297 | write32le(Buf + 2, Config->Shared ? (GotEntryAddr - GotAddr) : GotEntryAddr); |
| 298 | write32le(Buf + 7, RelOff); |
| 299 | write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 300 | } |
| 301 | |
George Rimar | 70e2508 | 2015-11-25 11:27:40 +0000 | [diff] [blame] | 302 | bool X86TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const { |
| 303 | if (Type == R_386_32 || Type == R_386_16 || Type == R_386_8) |
| 304 | if (auto *SS = dyn_cast<SharedSymbol<ELF32LE>>(&S)) |
| 305 | return SS->Sym.getType() == STT_OBJECT; |
| 306 | return false; |
| 307 | } |
| 308 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 309 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 310 | return Type == R_386_TLS_GOTIE || Type == R_386_GOT32 || |
| 311 | relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 314 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 730c278 | 2015-10-07 18:46:13 +0000 | [diff] [blame] | 315 | return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 318 | void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 319 | uint64_t P, uint64_t SA, |
| 320 | uint8_t *PairedLoc) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 321 | switch (Type) { |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 322 | case R_386_32: |
| 323 | add32le(Loc, SA); |
| 324 | break; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 325 | case R_386_GOT32: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 326 | add32le(Loc, SA - Out<ELF32LE>::Got->getVA()); |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 327 | break; |
George Rimar | 1393477 | 2015-11-25 20:20:31 +0000 | [diff] [blame] | 328 | case R_386_GOTPC: |
| 329 | add32le(Loc, SA + Out<ELF32LE>::Got->getVA() - P); |
| 330 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 331 | case R_386_PC32: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 332 | add32le(Loc, SA - P); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 333 | break; |
George Rimar | 9db204a | 2015-12-02 09:58:20 +0000 | [diff] [blame] | 334 | case R_386_TLS_GD: |
| 335 | case R_386_TLS_LDM: |
| 336 | case R_386_TLS_TPOFF: { |
| 337 | uint64_t V = SA - Out<ELF32LE>::Got->getVA() - |
| 338 | Out<ELF32LE>::Got->getNumEntries() * 4; |
| 339 | checkInt<32>(V, Type); |
| 340 | write32le(Loc, V); |
| 341 | break; |
| 342 | } |
| 343 | case R_386_TLS_LDO_32: |
| 344 | write32le(Loc, SA); |
| 345 | break; |
George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 346 | case R_386_TLS_LE: |
| 347 | write32le(Loc, SA - Out<ELF32LE>::TlsPhdr->p_memsz); |
| 348 | break; |
| 349 | case R_386_TLS_LE_32: |
| 350 | write32le(Loc, Out<ELF32LE>::TlsPhdr->p_memsz - SA); |
| 351 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 352 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 353 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 357 | X86_64TargetInfo::X86_64TargetInfo() { |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 358 | CopyReloc = R_X86_64_COPY; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 359 | PCRelReloc = R_X86_64_PC32; |
| 360 | GotReloc = R_X86_64_GLOB_DAT; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 361 | PltReloc = R_X86_64_JUMP_SLOT; |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 362 | RelativeReloc = R_X86_64_RELATIVE; |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 363 | TlsGotReloc = R_X86_64_TPOFF64; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 364 | TlsLocalDynamicReloc = R_X86_64_TLSLD; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 365 | TlsGlobalDynamicReloc = R_X86_64_TLSGD; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 366 | TlsModuleIndexReloc = R_X86_64_DTPMOD64; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 367 | TlsOffsetReloc = R_X86_64_DTPOFF64; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 368 | LazyRelocations = true; |
| 369 | PltEntrySize = 16; |
| 370 | PltZeroEntrySize = 16; |
| 371 | } |
| 372 | |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 373 | void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const { |
| 374 | write64le(Buf, Out<ELF64LE>::Dynamic->getVA()); |
| 375 | } |
| 376 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 377 | void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { |
| 378 | // Skip 6 bytes of "jmpq *got(%rip)" |
| 379 | write32le(Buf, Plt + 6); |
| 380 | } |
| 381 | |
| 382 | void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 383 | uint64_t PltEntryAddr) const { |
| 384 | const uint8_t PltData[] = { |
| 385 | 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip) |
| 386 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip) |
| 387 | 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax) |
| 388 | }; |
| 389 | memcpy(Buf, PltData, sizeof(PltData)); |
| 390 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8 |
| 391 | write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16 |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 392 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 393 | |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 394 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, |
| 395 | uint64_t GotEntryAddr, |
| 396 | uint64_t PltEntryAddr, int32_t Index, |
| 397 | unsigned RelOff) const { |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 398 | const uint8_t Inst[] = { |
| 399 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) |
| 400 | 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> |
| 401 | 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0] |
| 402 | }; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 403 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 404 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 405 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6); |
| 406 | write32le(Buf + 7, Index); |
| 407 | write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 408 | } |
| 409 | |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 410 | bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type, |
| 411 | const SymbolBody &S) const { |
| 412 | if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 || |
| 413 | Type == R_X86_64_64) |
| 414 | if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) |
| 415 | return SS->Sym.getType() == STT_OBJECT; |
| 416 | return false; |
| 417 | } |
| 418 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 419 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 420 | if (Type == R_X86_64_GOTTPOFF) |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 421 | return !isTlsOptimized(Type, &S); |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 422 | return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL || |
| 423 | relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 424 | } |
| 425 | |
George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 426 | bool X86_64TargetInfo::isTlsDynReloc(unsigned Type) const { |
| 427 | return Type == R_X86_64_GOTTPOFF; |
| 428 | } |
| 429 | |
Igor Kudrin | e7ad093 | 2015-11-17 17:47:53 +0000 | [diff] [blame] | 430 | unsigned X86_64TargetInfo::getPltRefReloc(unsigned Type) const { |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 431 | if (Type == R_X86_64_PLT32) |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 432 | return R_X86_64_PC32; |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 433 | return Type; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 436 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 437 | if (relocNeedsCopy(Type, S)) |
| 438 | return false; |
| 439 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 440 | switch (Type) { |
| 441 | default: |
| 442 | return false; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 443 | case R_X86_64_32: |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 444 | case R_X86_64_64: |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 445 | case R_X86_64_PC32: |
| 446 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 447 | // 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] | 448 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 449 | // 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] | 450 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 451 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 452 | // libc.so. |
| 453 | // |
| 454 | // The general idea on how to handle such cases is to create a PLT entry |
| 455 | // and use that as the function value. |
| 456 | // |
| 457 | // For the static linking part, we just return true and everything else |
| 458 | // will use the the PLT entry as the address. |
| 459 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 460 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 461 | // still works. We need the help of the dynamic linker for that. We |
| 462 | // let it know that we have a direct reference to a so symbol by creating |
| 463 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 464 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 465 | // This is true even for got entries, so pointer equality is maintained. |
| 466 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 467 | // real function is a dedicated got entry used by the plt. That is |
| 468 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 469 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 470 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 471 | case R_X86_64_PLT32: |
George Rimar | 8911d85 | 2015-10-16 23:52:24 +0000 | [diff] [blame] | 472 | return canBePreempted(&S, true); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 475 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 476 | bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { |
| 477 | switch (Type) { |
| 478 | default: |
| 479 | return false; |
Michael J. Spencer | a5d9d1f | 2015-11-11 01:27:58 +0000 | [diff] [blame] | 480 | case R_X86_64_DTPOFF32: |
Michael J. Spencer | ac2307b | 2015-11-11 01:28:11 +0000 | [diff] [blame] | 481 | case R_X86_64_DTPOFF64: |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 482 | case R_X86_64_PC8: |
| 483 | case R_X86_64_PC16: |
| 484 | case R_X86_64_PC32: |
| 485 | case R_X86_64_PC64: |
| 486 | case R_X86_64_PLT32: |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 487 | return true; |
| 488 | } |
| 489 | } |
| 490 | |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 491 | bool X86_64TargetInfo::isTlsOptimized(unsigned Type, |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 492 | const SymbolBody *S) const { |
| 493 | if (Config->Shared || (S && !S->isTLS())) |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 494 | return false; |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 495 | return Type == R_X86_64_TLSLD || Type == R_X86_64_DTPOFF32 || |
| 496 | (Type == R_X86_64_TLSGD && !canBePreempted(S, true)) || |
| 497 | (Type == R_X86_64_GOTTPOFF && !canBePreempted(S, true)); |
| 498 | } |
| 499 | |
| 500 | // "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5 |
| 501 | // x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows |
| 502 | // how LD can be optimized to LE: |
| 503 | // leaq bar@tlsld(%rip), %rdi |
| 504 | // callq __tls_get_addr@PLT |
| 505 | // leaq bar@dtpoff(%rax), %rcx |
| 506 | // Is converted to: |
| 507 | // .word 0x6666 |
| 508 | // .byte 0x66 |
| 509 | // mov %fs:0,%rax |
| 510 | // leaq bar@tpoff(%rax), %rcx |
| 511 | void X86_64TargetInfo::relocateTlsLdToLe(uint8_t *Loc, uint8_t *BufEnd, |
| 512 | uint64_t P, uint64_t SA) const { |
| 513 | const uint8_t Inst[] = { |
| 514 | 0x66, 0x66, //.word 0x6666 |
| 515 | 0x66, //.byte 0x66 |
| 516 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax |
| 517 | }; |
| 518 | memcpy(Loc - 3, Inst, sizeof(Inst)); |
| 519 | } |
| 520 | |
| 521 | // "Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5 |
| 522 | // x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows |
| 523 | // how GD can be optimized to LE: |
| 524 | // .byte 0x66 |
| 525 | // leaq x@tlsgd(%rip), %rdi |
| 526 | // .word 0x6666 |
| 527 | // rex64 |
| 528 | // call __tls_get_addr@plt |
| 529 | // Is converted to: |
| 530 | // mov %fs:0x0,%rax |
| 531 | // lea x@tpoff,%rax |
| 532 | void X86_64TargetInfo::relocateTlsGdToLe(uint8_t *Loc, uint8_t *BufEnd, |
| 533 | uint64_t P, uint64_t SA) const { |
| 534 | const uint8_t Inst[] = { |
| 535 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax |
| 536 | 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax |
| 537 | }; |
| 538 | memcpy(Loc - 4, Inst, sizeof(Inst)); |
| 539 | relocateOne(Loc + 8, BufEnd, R_X86_64_TPOFF32, P, SA); |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to |
| 543 | // R_X86_64_TPOFF32 so that R_X86_64_TPOFF32 so that it does not use GOT. |
| 544 | // This function does that. Read "ELF Handling For Thread-Local Storage, |
| 545 | // 5.5 x86-x64 linker optimizations" (http://www.akkadia.org/drepper/tls.pdf) |
| 546 | // by Ulrich Drepper for details. |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 547 | void X86_64TargetInfo::relocateTlsIeToLe(uint8_t *Loc, uint8_t *BufEnd, |
| 548 | uint64_t P, uint64_t SA) const { |
George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 549 | // Ulrich's document section 6.5 says that @gottpoff(%rip) must be |
| 550 | // used in MOVQ or ADDQ instructions only. |
| 551 | // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG". |
| 552 | // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG" |
| 553 | // (if the register is not RSP/R12) or "ADDQ $foo, %RSP". |
| 554 | // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48. |
| 555 | uint8_t *Prefix = Loc - 3; |
| 556 | uint8_t *Inst = Loc - 2; |
| 557 | uint8_t *RegSlot = Loc - 1; |
| 558 | uint8_t Reg = Loc[-1] >> 3; |
| 559 | bool IsMov = *Inst == 0x8b; |
| 560 | bool RspAdd = !IsMov && Reg == 4; |
| 561 | // r12 and rsp registers requires special handling. |
| 562 | // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11 |
| 563 | // result out is 7 bytes: 4d 8d 9b XX XX XX XX, |
| 564 | // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX. |
| 565 | // The same true for rsp. So we convert to addq for them, saving 1 byte that |
| 566 | // we dont have. |
| 567 | if (RspAdd) |
| 568 | *Inst = 0x81; |
| 569 | else |
| 570 | *Inst = IsMov ? 0xc7 : 0x8d; |
| 571 | if (*Prefix == 0x4c) |
| 572 | *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d; |
| 573 | *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3)); |
| 574 | relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA); |
| 575 | } |
| 576 | |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 577 | // This function applies a TLS relocation with an optimization as described |
| 578 | // in the Ulrich's document. As a result of rewriting instructions at the |
| 579 | // relocation target, relocations immediately follow the TLS relocation (which |
| 580 | // would be applied to rewritten instructions) may have to be skipped. |
| 581 | // This function returns a number of relocations that need to be skipped. |
| 582 | unsigned X86_64TargetInfo::relocateTlsOptimize(uint8_t *Loc, uint8_t *BufEnd, |
| 583 | uint32_t Type, uint64_t P, |
| 584 | uint64_t SA) const { |
| 585 | switch (Type) { |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 586 | case R_X86_64_DTPOFF32: |
| 587 | relocateOne(Loc, BufEnd, R_X86_64_TPOFF32, P, SA); |
| 588 | return 0; |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 589 | case R_X86_64_GOTTPOFF: |
| 590 | relocateTlsIeToLe(Loc, BufEnd, P, SA); |
| 591 | return 0; |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 592 | case R_X86_64_TLSGD: |
| 593 | relocateTlsGdToLe(Loc, BufEnd, P, SA); |
| 594 | // The next relocation should be against __tls_get_addr, so skip it |
| 595 | return 1; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 596 | case R_X86_64_TLSLD: |
| 597 | relocateTlsLdToLe(Loc, BufEnd, P, SA); |
| 598 | // The next relocation should be against __tls_get_addr, so skip it |
| 599 | return 1; |
George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 600 | } |
| 601 | llvm_unreachable("Unknown TLS optimization"); |
| 602 | } |
| 603 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 604 | void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 605 | uint64_t P, uint64_t SA, |
| 606 | uint8_t *PairedLoc) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 607 | switch (Type) { |
Rui Ueyama | 3835b49 | 2015-10-23 16:13:27 +0000 | [diff] [blame] | 608 | case R_X86_64_32: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 609 | checkUInt<32>(SA, Type); |
| 610 | write32le(Loc, SA); |
| 611 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 612 | case R_X86_64_32S: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 613 | checkInt<32>(SA, Type); |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 614 | write32le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 615 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 616 | case R_X86_64_64: |
| 617 | write64le(Loc, SA); |
| 618 | break; |
Michael J. Spencer | a5d9d1f | 2015-11-11 01:27:58 +0000 | [diff] [blame] | 619 | case R_X86_64_DTPOFF32: |
| 620 | write32le(Loc, SA); |
| 621 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 622 | case R_X86_64_DTPOFF64: |
| 623 | write64le(Loc, SA); |
| 624 | break; |
| 625 | case R_X86_64_GOTPCREL: |
| 626 | case R_X86_64_PC32: |
| 627 | case R_X86_64_PLT32: |
| 628 | case R_X86_64_TLSGD: |
| 629 | case R_X86_64_TLSLD: |
| 630 | write32le(Loc, SA - P); |
| 631 | break; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 632 | case R_X86_64_TPOFF32: { |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 633 | uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz; |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 634 | checkInt<32>(Val, Type); |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 635 | write32le(Loc, Val); |
Michael J. Spencer | d77f0d2 | 2015-11-03 22:39:09 +0000 | [diff] [blame] | 636 | break; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 637 | } |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 638 | case R_X86_64_TPOFF64: |
| 639 | write32le(Loc, SA - P); |
| 640 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 641 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 642 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 646 | // Relocation masks following the #lo(value), #hi(value), #ha(value), |
| 647 | // #higher(value), #highera(value), #highest(value), and #highesta(value) |
| 648 | // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi |
| 649 | // document. |
Rui Ueyama | c44e5a1 | 2015-10-23 16:54:58 +0000 | [diff] [blame] | 650 | static uint16_t applyPPCLo(uint64_t V) { return V; } |
| 651 | static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } |
| 652 | static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } |
| 653 | static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } |
| 654 | static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 655 | static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 656 | static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } |
| 657 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 658 | PPC64TargetInfo::PPC64TargetInfo() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 659 | PCRelReloc = R_PPC64_REL24; |
| 660 | GotReloc = R_PPC64_GLOB_DAT; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 661 | RelativeReloc = R_PPC64_RELATIVE; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 662 | PltEntrySize = 32; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 663 | |
| 664 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 665 | // set different permissions on a finer granularity than that). |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 666 | PageSize = 65536; |
Hal Finkel | 736c741 | 2015-10-15 07:49:07 +0000 | [diff] [blame] | 667 | |
| 668 | // The PPC64 ELF ABI v1 spec, says: |
| 669 | // |
| 670 | // It is normally desirable to put segments with different characteristics |
| 671 | // in separate 256 Mbyte portions of the address space, to give the |
| 672 | // operating system full paging flexibility in the 64-bit address space. |
| 673 | // |
| 674 | // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers |
| 675 | // use 0x10000000 as the starting address. |
| 676 | VAStart = 0x10000000; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 677 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 678 | |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 679 | uint64_t getPPC64TocBase() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 680 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 681 | // order. The TOC starts where the first of these sections starts. |
| 682 | |
| 683 | // FIXME: This obviously does not do the right thing when there is no .got |
| 684 | // section, but there is a .toc or .tocbss section. |
| 685 | uint64_t TocVA = Out<ELF64BE>::Got->getVA(); |
| 686 | if (!TocVA) |
| 687 | TocVA = Out<ELF64BE>::Plt->getVA(); |
| 688 | |
| 689 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 690 | // thus permitting a full 64 Kbytes segment. Note that the glibc startup |
| 691 | // code (crt1.o) assumes that you can get from the TOC base to the |
| 692 | // start of the .toc section with only a single (signed) 16-bit relocation. |
| 693 | return TocVA + 0x8000; |
| 694 | } |
| 695 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 696 | void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 697 | void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 698 | uint64_t PltEntryAddr) const {} |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 699 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, |
| 700 | uint64_t GotEntryAddr, |
| 701 | uint64_t PltEntryAddr, int32_t Index, |
| 702 | unsigned RelOff) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 703 | uint64_t Off = GotEntryAddr - getPPC64TocBase(); |
| 704 | |
| 705 | // FIXME: What we should do, in theory, is get the offset of the function |
| 706 | // descriptor in the .opd section, and use that as the offset from %r2 (the |
| 707 | // TOC-base pointer). Instead, we have the GOT-entry offset, and that will |
| 708 | // be a pointer to the function descriptor in the .opd section. Using |
| 709 | // this scheme is simpler, but requires an extra indirection per PLT dispatch. |
| 710 | |
Hal Finkel | fa92f68 | 2015-10-13 21:47:34 +0000 | [diff] [blame] | 711 | write32be(Buf, 0xf8410028); // std %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 712 | write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha |
| 713 | write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) |
| 714 | write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) |
| 715 | write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 |
| 716 | write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) |
| 717 | write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) |
| 718 | write32be(Buf + 28, 0x4e800420); // bctr |
| 719 | } |
| 720 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 721 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 722 | if (relocNeedsPlt(Type, S)) |
| 723 | return true; |
| 724 | |
| 725 | switch (Type) { |
| 726 | default: return false; |
| 727 | case R_PPC64_GOT16: |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 728 | case R_PPC64_GOT16_DS: |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 729 | case R_PPC64_GOT16_HA: |
| 730 | case R_PPC64_GOT16_HI: |
| 731 | case R_PPC64_GOT16_LO: |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 732 | case R_PPC64_GOT16_LO_DS: |
| 733 | return true; |
| 734 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 735 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 736 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 737 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 738 | // These are function calls that need to be redirected through a PLT stub. |
Hal Finkel | 8228198 | 2015-10-17 00:48:20 +0000 | [diff] [blame] | 739 | return Type == R_PPC64_REL24 && canBePreempted(&S, false); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 740 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 741 | |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 742 | bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { |
| 743 | switch (Type) { |
| 744 | default: |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 745 | return true; |
Hal Finkel | 0091862 | 2015-10-16 19:01:50 +0000 | [diff] [blame] | 746 | case R_PPC64_ADDR64: |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 747 | case R_PPC64_TOC: |
Hal Finkel | 0091862 | 2015-10-16 19:01:50 +0000 | [diff] [blame] | 748 | return false; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 752 | void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 753 | uint64_t P, uint64_t SA, |
| 754 | uint8_t *PairedLoc) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 755 | uint64_t TB = getPPC64TocBase(); |
| 756 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 757 | // For a TOC-relative relocation, adjust the addend and proceed in terms of |
| 758 | // the corresponding ADDR16 relocation type. |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 759 | switch (Type) { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 760 | case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break; |
| 761 | 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] | 762 | case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; SA -= TB; break; |
| 763 | 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] | 764 | case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break; |
| 765 | 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] | 766 | default: break; |
| 767 | } |
| 768 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 769 | switch (Type) { |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 770 | case R_PPC64_ADDR14: { |
| 771 | checkAlignment<4>(SA, Type); |
| 772 | // Preserve the AA/LK bits in the branch instruction |
| 773 | uint8_t AALK = Loc[3]; |
| 774 | write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc)); |
| 775 | break; |
| 776 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 777 | case R_PPC64_ADDR16: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 778 | checkInt<16>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 779 | write16be(Loc, SA); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 780 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 781 | case R_PPC64_ADDR16_DS: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 782 | checkInt<16>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 783 | write16be(Loc, (read16be(Loc) & 3) | (SA & ~3)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 784 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 785 | case R_PPC64_ADDR16_HA: |
| 786 | write16be(Loc, applyPPCHa(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 787 | break; |
| 788 | case R_PPC64_ADDR16_HI: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 789 | write16be(Loc, applyPPCHi(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 790 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 791 | case R_PPC64_ADDR16_HIGHER: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 792 | write16be(Loc, applyPPCHigher(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 793 | break; |
| 794 | case R_PPC64_ADDR16_HIGHERA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 795 | write16be(Loc, applyPPCHighera(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 796 | break; |
| 797 | case R_PPC64_ADDR16_HIGHEST: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 798 | write16be(Loc, applyPPCHighest(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 799 | break; |
| 800 | case R_PPC64_ADDR16_HIGHESTA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 801 | write16be(Loc, applyPPCHighesta(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 802 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 803 | case R_PPC64_ADDR16_LO: |
| 804 | write16be(Loc, applyPPCLo(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 805 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 806 | case R_PPC64_ADDR16_LO_DS: |
| 807 | write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 808 | break; |
| 809 | case R_PPC64_ADDR32: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 810 | checkInt<32>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 811 | write32be(Loc, SA); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 812 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 813 | case R_PPC64_ADDR64: |
| 814 | write64be(Loc, SA); |
| 815 | break; |
| 816 | case R_PPC64_REL16_HA: |
| 817 | write16be(Loc, applyPPCHa(SA - P)); |
| 818 | break; |
| 819 | case R_PPC64_REL16_HI: |
| 820 | write16be(Loc, applyPPCHi(SA - P)); |
| 821 | break; |
| 822 | case R_PPC64_REL16_LO: |
| 823 | write16be(Loc, applyPPCLo(SA - P)); |
| 824 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 825 | case R_PPC64_REL24: { |
Hal Finkel | 8228198 | 2015-10-17 00:48:20 +0000 | [diff] [blame] | 826 | // If we have an undefined weak symbol, we might get here with a symbol |
| 827 | // address of zero. That could overflow, but the code must be unreachable, |
| 828 | // so don't bother doing anything at all. |
| 829 | if (!SA) |
| 830 | break; |
| 831 | |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 832 | uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); |
| 833 | uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 834 | bool InPlt = PltStart <= SA && SA < PltEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 835 | |
| 836 | if (!InPlt && Out<ELF64BE>::Opd) { |
| 837 | // If this is a local call, and we currently have the address of a |
| 838 | // function-descriptor, get the underlying code address instead. |
| 839 | uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); |
| 840 | uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 841 | bool InOpd = OpdStart <= SA && SA < OpdEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 842 | |
| 843 | if (InOpd) |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 844 | SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]); |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 847 | uint32_t Mask = 0x03FFFFFC; |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 848 | checkInt<24>(SA - P, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 849 | write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask)); |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 850 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 851 | uint32_t Nop = 0x60000000; |
| 852 | if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop) |
| 853 | write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 854 | break; |
| 855 | } |
| 856 | case R_PPC64_REL32: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 857 | checkInt<32>(SA - P, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 858 | write32be(Loc, SA - P); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 859 | break; |
| 860 | case R_PPC64_REL64: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 861 | write64be(Loc, SA - P); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 862 | break; |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 863 | case R_PPC64_TOC: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 864 | write64be(Loc, SA); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 865 | break; |
| 866 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 867 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 870 | |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 871 | AArch64TargetInfo::AArch64TargetInfo() { |
Igor Kudrin | 9606d19 | 2015-12-03 08:05:35 +0000 | [diff] [blame] | 872 | CopyReloc = R_AARCH64_COPY; |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 873 | GotReloc = R_AARCH64_GLOB_DAT; |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 874 | PltReloc = R_AARCH64_JUMP_SLOT; |
| 875 | LazyRelocations = true; |
| 876 | PltEntrySize = 16; |
| 877 | PltZeroEntrySize = 32; |
| 878 | } |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 879 | |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 880 | unsigned AArch64TargetInfo::getPltRefReloc(unsigned Type) const { return Type; } |
| 881 | |
| 882 | void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { |
| 883 | write64le(Buf, Out<ELF64LE>::Plt->getVA()); |
| 884 | } |
| 885 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 886 | void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 887 | uint64_t PltEntryAddr) const { |
| 888 | const uint8_t PltData[] = { |
| 889 | 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! |
| 890 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) |
| 891 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] |
| 892 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) |
| 893 | 0x20, 0x02, 0x1f, 0xd6, // br x17 |
| 894 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 895 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 896 | 0x1f, 0x20, 0x03, 0xd5 // nop |
| 897 | }; |
| 898 | memcpy(Buf, PltData, sizeof(PltData)); |
| 899 | |
| 900 | relocateOne(Buf + 4, Buf + 8, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr + 4, |
| 901 | GotEntryAddr + 16); |
| 902 | relocateOne(Buf + 8, Buf + 12, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 8, |
| 903 | GotEntryAddr + 16); |
| 904 | relocateOne(Buf + 12, Buf + 16, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 12, |
| 905 | GotEntryAddr + 16); |
| 906 | } |
| 907 | |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 908 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotAddr, |
| 909 | uint64_t GotEntryAddr, |
| 910 | uint64_t PltEntryAddr, int32_t Index, |
| 911 | unsigned RelOff) const { |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 912 | const uint8_t Inst[] = { |
| 913 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) |
| 914 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] |
| 915 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) |
| 916 | 0x20, 0x02, 0x1f, 0xd6 // br x17 |
| 917 | }; |
| 918 | memcpy(Buf, Inst, sizeof(Inst)); |
| 919 | |
| 920 | relocateOne(Buf, Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, PltEntryAddr, |
| 921 | GotEntryAddr); |
| 922 | relocateOne(Buf + 4, Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, PltEntryAddr + 4, |
| 923 | GotEntryAddr); |
| 924 | relocateOne(Buf + 8, Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, PltEntryAddr + 8, |
| 925 | GotEntryAddr); |
| 926 | } |
| 927 | |
Igor Kudrin | 9606d19 | 2015-12-03 08:05:35 +0000 | [diff] [blame] | 928 | bool AArch64TargetInfo::relocNeedsCopy(uint32_t Type, |
| 929 | const SymbolBody &S) const { |
| 930 | if (Config->Shared) |
| 931 | return false; |
| 932 | switch (Type) { |
| 933 | default: |
| 934 | return false; |
| 935 | case R_AARCH64_ABS16: |
| 936 | case R_AARCH64_ABS32: |
| 937 | case R_AARCH64_ABS64: |
| 938 | case R_AARCH64_ADD_ABS_LO12_NC: |
| 939 | case R_AARCH64_ADR_PREL_LO21: |
| 940 | case R_AARCH64_ADR_PREL_PG_HI21: |
| 941 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| 942 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| 943 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| 944 | if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) |
| 945 | return SS->Sym.getType() == STT_OBJECT; |
| 946 | return false; |
| 947 | } |
| 948 | } |
| 949 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 950 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 951 | const SymbolBody &S) const { |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 952 | return Type == R_AARCH64_ADR_GOT_PAGE || Type == R_AARCH64_LD64_GOT_LO12_NC || |
| 953 | relocNeedsPlt(Type, S); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 954 | } |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 955 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 956 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 957 | const SymbolBody &S) const { |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 958 | switch (Type) { |
| 959 | default: |
| 960 | return false; |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 961 | case R_AARCH64_CALL26: |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 962 | case R_AARCH64_JUMP26: |
Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 963 | return canBePreempted(&S, true); |
| 964 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 965 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 966 | |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 967 | static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 968 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 969 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 970 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 971 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 974 | // Page(Expr) is the page address of the expression Expr, defined |
| 975 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
Davide Italiano | d9b5be4 | 2015-10-02 22:17:09 +0000 | [diff] [blame] | 976 | // supported by the platform has a different value.) |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 977 | static uint64_t getAArch64Page(uint64_t Expr) { |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 978 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 981 | void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 982 | uint32_t Type, uint64_t P, uint64_t SA, |
| 983 | uint8_t *PairedLoc) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 984 | switch (Type) { |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 985 | case R_AARCH64_ABS16: |
Igor Kudrin | fea8ed5 | 2015-11-26 10:05:24 +0000 | [diff] [blame] | 986 | checkIntUInt<16>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 987 | write16le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 988 | break; |
| 989 | case R_AARCH64_ABS32: |
Igor Kudrin | fea8ed5 | 2015-11-26 10:05:24 +0000 | [diff] [blame] | 990 | checkIntUInt<32>(SA, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 991 | write32le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 992 | break; |
| 993 | case R_AARCH64_ABS64: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 994 | write64le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 995 | break; |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 996 | case R_AARCH64_ADD_ABS_LO12_NC: |
Davide Italiano | a716574 | 2015-10-16 21:06:55 +0000 | [diff] [blame] | 997 | // This relocation stores 12 bits and there's no instruction |
| 998 | // 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] | 999 | // of r_addend bitwise-or'ed Loc. This assumes that the addend |
| 1000 | // bits in Loc are zero. |
| 1001 | or32le(Loc, (SA & 0xFFF) << 10); |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 1002 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1003 | case R_AARCH64_ADR_GOT_PAGE: { |
| 1004 | uint64_t X = getAArch64Page(SA) - getAArch64Page(P); |
| 1005 | checkInt<33>(X, Type); |
| 1006 | updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] |
| 1007 | break; |
| 1008 | } |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 1009 | case R_AARCH64_ADR_PREL_LO21: { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 1010 | uint64_t X = SA - P; |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 1011 | checkInt<21>(X, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 1012 | updateAArch64Adr(Loc, X & 0x1FFFFF); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1013 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 1014 | } |
| 1015 | case R_AARCH64_ADR_PREL_PG_HI21: { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 1016 | uint64_t X = getAArch64Page(SA) - getAArch64Page(P); |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 1017 | checkInt<33>(X, Type); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 1018 | updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 1019 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 1020 | } |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1021 | case R_AARCH64_CALL26: |
| 1022 | case R_AARCH64_JUMP26: { |
Igor Kudrin | b34115b | 2015-11-13 03:26:59 +0000 | [diff] [blame] | 1023 | uint64_t X = SA - P; |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 1024 | checkInt<28>(X, Type); |
Igor Kudrin | b34115b | 2015-11-13 03:26:59 +0000 | [diff] [blame] | 1025 | or32le(Loc, (X & 0x0FFFFFFC) >> 2); |
| 1026 | break; |
| 1027 | } |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 1028 | case R_AARCH64_LD64_GOT_LO12_NC: |
Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 1029 | checkAlignment<8>(SA, Type); |
Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 1030 | or32le(Loc, (SA & 0xFF8) << 7); |
| 1031 | break; |
Davide Italiano | dc67f9b | 2015-11-20 21:35:38 +0000 | [diff] [blame] | 1032 | case R_AARCH64_LDST8_ABS_LO12_NC: |
Davide Italiano | dc67f9b | 2015-11-20 21:35:38 +0000 | [diff] [blame] | 1033 | or32le(Loc, (SA & 0xFFF) << 10); |
| 1034 | break; |
Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1035 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| 1036 | or32le(Loc, (SA & 0xFFC) << 8); |
| 1037 | break; |
| 1038 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| 1039 | or32le(Loc, (SA & 0xFF8) << 7); |
| 1040 | break; |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 1041 | case R_AARCH64_PREL16: |
Igor Kudrin | fea8ed5 | 2015-11-26 10:05:24 +0000 | [diff] [blame] | 1042 | checkIntUInt<16>(SA - P, Type); |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 1043 | write16le(Loc, SA - P); |
| 1044 | break; |
| 1045 | case R_AARCH64_PREL32: |
Igor Kudrin | fea8ed5 | 2015-11-26 10:05:24 +0000 | [diff] [blame] | 1046 | checkIntUInt<32>(SA - P, Type); |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 1047 | write32le(Loc, SA - P); |
| 1048 | break; |
Davide Italiano | b12d668 | 2015-10-28 16:14:18 +0000 | [diff] [blame] | 1049 | case R_AARCH64_PREL64: |
Davide Italiano | b12d668 | 2015-10-28 16:14:18 +0000 | [diff] [blame] | 1050 | write64le(Loc, SA - P); |
| 1051 | break; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1052 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 1053 | error("unrecognized reloc " + Twine(Type)); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1056 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 1057 | template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 1058 | PageSize = 65536; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 1059 | GotHeaderEntriesNum = 2; |
| 1060 | } |
| 1061 | |
| 1062 | template <class ELFT> |
| 1063 | void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const { |
Rui Ueyama | 24e3952 | 2015-12-03 20:57:45 +0000 | [diff] [blame] | 1064 | typedef typename ELFFile<ELFT>::Elf_Off Elf_Off; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 1065 | auto *P = reinterpret_cast<Elf_Off *>(Buf); |
| 1066 | // Module pointer |
| 1067 | P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000; |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 1070 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 1071 | void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 1072 | template <class ELFT> |
| 1073 | void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 1074 | uint64_t PltEntryAddr) const {} |
| 1075 | template <class ELFT> |
George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 1076 | void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotAddr, |
| 1077 | uint64_t GotEntryAddr, |
| 1078 | uint64_t PltEntryAddr, int32_t Index, |
| 1079 | unsigned RelOff) const {} |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1080 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 1081 | template <class ELFT> |
| 1082 | bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, |
| 1083 | const SymbolBody &S) const { |
Simon Atanasyan | 96306bb | 2015-11-25 20:58:52 +0000 | [diff] [blame] | 1084 | return Type == R_MIPS_GOT16 || Type == R_MIPS_CALL16; |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 1085 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1086 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 1087 | template <class ELFT> |
| 1088 | bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type, |
| 1089 | const SymbolBody &S) const { |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 1090 | return false; |
| 1091 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1092 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 1093 | template <class ELFT> |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 1094 | void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd, |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 1095 | uint32_t Type, uint64_t P, uint64_t SA, |
| 1096 | uint8_t *PairedLoc) const { |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 1097 | const endianness E = ELFT::TargetEndianness; |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1098 | switch (Type) { |
| 1099 | case R_MIPS_32: |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 1100 | add32<E>(Loc, SA); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1101 | break; |
Rui Ueyama | 7ee3cf7 | 2015-12-03 20:59:51 +0000 | [diff] [blame] | 1102 | case R_MIPS_CALL16: |
| 1103 | case R_MIPS_GOT16: { |
| 1104 | int64_t V = SA - getMipsGpAddr<ELFT>(); |
| 1105 | if (Type == R_MIPS_GOT16) |
| 1106 | checkInt<16>(V, Type); |
| 1107 | write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff)); |
| 1108 | break; |
| 1109 | } |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 1110 | case R_MIPS_HI16: { |
| 1111 | uint32_t Instr = read32<E>(Loc); |
| 1112 | if (PairedLoc) { |
| 1113 | uint64_t AHL = ((Instr & 0xffff) << 16) + |
Rui Ueyama | 24e3952 | 2015-12-03 20:57:45 +0000 | [diff] [blame] | 1114 | SignExtend64<16>(read32<E>(PairedLoc) & 0xffff); |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 1115 | write32<E>(Loc, (Instr & 0xffff0000) | (((SA + AHL) >> 16) & 0xffff)); |
| 1116 | } else { |
| 1117 | warning("Can't find matching R_MIPS_LO16 relocation for R_MIPS_HI16"); |
| 1118 | write32<E>(Loc, (Instr & 0xffff0000) | ((SA >> 16) & 0xffff)); |
| 1119 | } |
| 1120 | break; |
| 1121 | } |
| 1122 | case R_MIPS_LO16: { |
| 1123 | uint32_t Instr = read32<E>(Loc); |
Rui Ueyama | 24e3952 | 2015-12-03 20:57:45 +0000 | [diff] [blame] | 1124 | int64_t AHL = SignExtend64<16>(Instr & 0xffff); |
Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 1125 | write32<E>(Loc, (Instr & 0xffff0000) | ((SA + AHL) & 0xffff)); |
| 1126 | break; |
| 1127 | } |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1128 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 1129 | error("unrecognized reloc " + Twine(Type)); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1130 | } |
| 1131 | } |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 1132 | |
Rui Ueyama | 24e3952 | 2015-12-03 20:57:45 +0000 | [diff] [blame] | 1133 | template <class ELFT> typename ELFFile<ELFT>::uintX_t getMipsGpAddr() { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 1134 | const unsigned GPOffset = 0x7ff0; |
| 1135 | return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0; |
| 1136 | } |
| 1137 | |
| 1138 | template uint32_t getMipsGpAddr<ELF32LE>(); |
| 1139 | template uint32_t getMipsGpAddr<ELF32BE>(); |
| 1140 | template uint64_t getMipsGpAddr<ELF64LE>(); |
| 1141 | template uint64_t getMipsGpAddr<ELF64BE>(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 1142 | } |
| 1143 | } |