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