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