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 | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 49 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 50 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 51 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 52 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 53 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 54 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 55 | bool relocPointsToGot(uint32_t Type) const override; |
| 56 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 57 | 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] | 58 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class X86_64TargetInfo final : public TargetInfo { |
| 62 | public: |
| 63 | X86_64TargetInfo(); |
| 64 | unsigned getPLTRefReloc(unsigned Type) const override; |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 65 | void writeGotPltHeaderEntries(uint8_t *Buf) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 66 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 67 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 68 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 69 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 70 | uint64_t PltEntryAddr, int32_t Index) const override; |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 71 | bool relocNeedsCopy(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 72 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 73 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 74 | 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] | 75 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 76 | bool isRelRelative(uint32_t Type) const override; |
| 77 | }; |
| 78 | |
| 79 | class PPC64TargetInfo final : public TargetInfo { |
| 80 | public: |
| 81 | PPC64TargetInfo(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 82 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 83 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 84 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 85 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 86 | uint64_t PltEntryAddr, int32_t Index) 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; |
| 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 | bool isRelRelative(uint32_t Type) const override; |
| 92 | }; |
| 93 | |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 94 | class AArch64TargetInfo final : public TargetInfo { |
| 95 | public: |
| 96 | AArch64TargetInfo(); |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 97 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 98 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 99 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 100 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 101 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 102 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 103 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 104 | 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] | 105 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | template <class ELFT> class MipsTargetInfo final : public TargetInfo { |
| 109 | public: |
| 110 | MipsTargetInfo(); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 111 | void writeGotHeaderEntries(uint8_t *Buf) const override; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 112 | void writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const override; |
| 113 | void writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 114 | uint64_t PltEntryAddr) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 115 | void writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 116 | uint64_t PltEntryAddr, int32_t Index) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 117 | bool relocNeedsGot(uint32_t Type, const SymbolBody &S) const override; |
| 118 | bool relocNeedsPlt(uint32_t Type, const SymbolBody &S) const override; |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 119 | 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] | 120 | uint64_t SA) const override; |
Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 121 | }; |
| 122 | } // anonymous namespace |
| 123 | |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 124 | TargetInfo *createTarget() { |
| 125 | switch (Config->EMachine) { |
| 126 | case EM_386: |
| 127 | return new X86TargetInfo(); |
| 128 | case EM_AARCH64: |
| 129 | return new AArch64TargetInfo(); |
| 130 | case EM_MIPS: |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 131 | switch (Config->EKind) { |
| 132 | case ELF32LEKind: |
| 133 | return new MipsTargetInfo<ELF32LE>(); |
| 134 | case ELF32BEKind: |
| 135 | return new MipsTargetInfo<ELF32BE>(); |
| 136 | default: |
| 137 | error("Unsupported MIPS target"); |
| 138 | } |
Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 139 | case EM_PPC64: |
| 140 | return new PPC64TargetInfo(); |
| 141 | case EM_X86_64: |
| 142 | return new X86_64TargetInfo(); |
| 143 | } |
| 144 | error("Unknown target machine"); |
| 145 | } |
| 146 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 147 | TargetInfo::~TargetInfo() {} |
| 148 | |
Igor Kudrin | f6f4547 | 2015-11-10 08:39:27 +0000 | [diff] [blame] | 149 | uint64_t TargetInfo::getVAStart() const { return Config->Shared ? 0 : VAStart; } |
| 150 | |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 151 | bool TargetInfo::relocNeedsCopy(uint32_t Type, const SymbolBody &S) const { |
| 152 | return false; |
| 153 | } |
| 154 | |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 155 | unsigned TargetInfo::getPLTRefReloc(unsigned Type) const { return PCRelReloc; } |
| 156 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 157 | bool TargetInfo::relocPointsToGot(uint32_t Type) const { return false; } |
| 158 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 159 | bool TargetInfo::isRelRelative(uint32_t Type) const { return true; } |
| 160 | |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 161 | void TargetInfo::writeGotHeaderEntries(uint8_t *Buf) const {} |
| 162 | |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 163 | void TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const {} |
| 164 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 165 | X86TargetInfo::X86TargetInfo() { |
| 166 | PCRelReloc = R_386_PC32; |
| 167 | GotReloc = R_386_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 168 | GotRefReloc = R_386_GOT32; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 169 | PltReloc = R_386_JUMP_SLOT; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 170 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 171 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 172 | void X86TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 173 | void X86TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 174 | uint64_t PltEntryAddr) const {} |
| 175 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 176 | void X86TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 177 | uint64_t PltEntryAddr, int32_t Index) const { |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 178 | // jmpl *val; nop; nop |
| 179 | const uint8_t Inst[] = {0xff, 0x25, 0, 0, 0, 0, 0x90, 0x90}; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 180 | memcpy(Buf, Inst, sizeof(Inst)); |
Rui Ueyama | c58656c | 2015-10-13 16:59:30 +0000 | [diff] [blame] | 181 | assert(isUInt<32>(GotEntryAddr)); |
| 182 | write32le(Buf + 2, GotEntryAddr); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 185 | bool X86TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Rui Ueyama | 5ba3ac4 | 2015-09-30 01:40:08 +0000 | [diff] [blame] | 186 | return Type == R_386_GOT32 || relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Rafael Espindola | 6d7fcdb | 2015-09-29 13:36:32 +0000 | [diff] [blame] | 189 | bool X86TargetInfo::relocPointsToGot(uint32_t Type) const { |
| 190 | return Type == R_386_GOTPC; |
| 191 | } |
| 192 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 193 | bool X86TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 730c278 | 2015-10-07 18:46:13 +0000 | [diff] [blame] | 194 | return Type == R_386_PLT32 || (Type == R_386_PC32 && S.isShared()); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 197 | void X86TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 198 | uint64_t P, uint64_t SA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 199 | switch (Type) { |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 200 | case R_386_GOT32: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 201 | add32le(Loc, SA - Out<ELF32LE>::Got->getVA()); |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 202 | break; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 203 | case R_386_PC32: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 204 | add32le(Loc, SA - P); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 205 | break; |
| 206 | case R_386_32: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 207 | add32le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 208 | break; |
| 209 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 210 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 214 | X86_64TargetInfo::X86_64TargetInfo() { |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 215 | CopyReloc = R_X86_64_COPY; |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 216 | PCRelReloc = R_X86_64_PC32; |
| 217 | GotReloc = R_X86_64_GLOB_DAT; |
Rafael Espindola | 8acb95c | 2015-09-29 14:42:37 +0000 | [diff] [blame] | 218 | GotRefReloc = R_X86_64_PC32; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 219 | PltReloc = R_X86_64_JUMP_SLOT; |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 220 | RelativeReloc = R_X86_64_RELATIVE; |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 221 | TlsGotReloc = R_X86_64_TPOFF64; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 222 | TlsLocalDynamicReloc = R_X86_64_TLSLD; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 223 | TlsGlobalDynamicReloc = R_X86_64_TLSGD; |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 224 | TlsModuleIndexReloc = R_X86_64_DTPMOD64; |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 225 | TlsOffsetReloc = R_X86_64_DTPOFF64; |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 226 | TlsPcRelGotReloc = R_X86_64_GOTTPOFF; |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 227 | LazyRelocations = true; |
| 228 | PltEntrySize = 16; |
| 229 | PltZeroEntrySize = 16; |
| 230 | } |
| 231 | |
Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 232 | void X86_64TargetInfo::writeGotPltHeaderEntries(uint8_t *Buf) const { |
| 233 | write64le(Buf, Out<ELF64LE>::Dynamic->getVA()); |
| 234 | } |
| 235 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 236 | void X86_64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const { |
| 237 | // Skip 6 bytes of "jmpq *got(%rip)" |
| 238 | write32le(Buf, Plt + 6); |
| 239 | } |
| 240 | |
| 241 | void X86_64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 242 | uint64_t PltEntryAddr) const { |
| 243 | const uint8_t PltData[] = { |
| 244 | 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip) |
| 245 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip) |
| 246 | 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax) |
| 247 | }; |
| 248 | memcpy(Buf, PltData, sizeof(PltData)); |
| 249 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr + 2); // GOT+8 |
| 250 | write32le(Buf + 8, GotEntryAddr - PltEntryAddr + 4); // GOT+16 |
Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 251 | } |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 252 | |
| 253 | void X86_64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 254 | uint64_t PltEntryAddr, |
| 255 | int32_t Index) const { |
| 256 | const uint8_t Inst[] = { |
| 257 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) |
| 258 | 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> |
| 259 | 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0] |
| 260 | }; |
Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 261 | memcpy(Buf, Inst, sizeof(Inst)); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 262 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 263 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6); |
| 264 | write32le(Buf + 7, Index); |
| 265 | write32le(Buf + 12, -Index * PltEntrySize - PltZeroEntrySize - 16); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 266 | } |
| 267 | |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 268 | bool X86_64TargetInfo::relocNeedsCopy(uint32_t Type, |
| 269 | const SymbolBody &S) const { |
| 270 | if (Type == R_X86_64_32S || Type == R_X86_64_32 || Type == R_X86_64_PC32 || |
| 271 | Type == R_X86_64_64) |
| 272 | if (auto *SS = dyn_cast<SharedSymbol<ELF64LE>>(&S)) |
| 273 | return SS->Sym.getType() == STT_OBJECT; |
| 274 | return false; |
| 275 | } |
| 276 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 277 | bool X86_64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 278 | return Type == R_X86_64_GOTTPOFF || Type == R_X86_64_GOTPCREL || |
| 279 | relocNeedsPlt(Type, S); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 282 | unsigned X86_64TargetInfo::getPLTRefReloc(unsigned Type) const { |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 283 | if (Type == R_X86_64_PLT32) |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 284 | return R_X86_64_PC32; |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 285 | return Type; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 288 | bool X86_64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
George Rimar | bc590fe | 2015-10-28 16:48:58 +0000 | [diff] [blame] | 289 | if (relocNeedsCopy(Type, S)) |
| 290 | return false; |
| 291 | |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 292 | switch (Type) { |
| 293 | default: |
| 294 | return false; |
Rafael Espindola | 227556e | 2015-10-14 16:15:46 +0000 | [diff] [blame] | 295 | case R_X86_64_32: |
George Rimar | 5268721 | 2015-10-28 18:33:08 +0000 | [diff] [blame] | 296 | case R_X86_64_64: |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 297 | case R_X86_64_PC32: |
| 298 | // This relocation is defined to have a value of (S + A - P). |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 299 | // 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] | 300 | // library. |
Rafael Espindola | 9a0db7c | 2015-09-29 23:23:53 +0000 | [diff] [blame] | 301 | // 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] | 302 | // can overflow at runtime. |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 303 | // In the real world with glibc, crt1.o has a R_X86_64_PC32 pointing to |
| 304 | // libc.so. |
| 305 | // |
| 306 | // The general idea on how to handle such cases is to create a PLT entry |
| 307 | // and use that as the function value. |
| 308 | // |
| 309 | // For the static linking part, we just return true and everything else |
| 310 | // will use the the PLT entry as the address. |
| 311 | // |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 312 | // The remaining (unimplemented) problem is making sure pointer equality |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 313 | // still works. We need the help of the dynamic linker for that. We |
| 314 | // let it know that we have a direct reference to a so symbol by creating |
| 315 | // an undefined symbol with a non zero st_value. Seeing that, the |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 316 | // dynamic linker resolves the symbol to the value of the symbol we created. |
| 317 | // This is true even for got entries, so pointer equality is maintained. |
| 318 | // To avoid an infinite loop, the only entry that points to the |
Rafael Espindola | 3c412e1 | 2015-09-30 12:30:58 +0000 | [diff] [blame] | 319 | // real function is a dedicated got entry used by the plt. That is |
| 320 | // identified by special relocation types (R_X86_64_JUMP_SLOT, |
| 321 | // R_386_JMP_SLOT, etc). |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 322 | return S.isShared(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 323 | case R_X86_64_PLT32: |
George Rimar | 8911d85 | 2015-10-16 23:52:24 +0000 | [diff] [blame] | 324 | return canBePreempted(&S, true); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 327 | |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 328 | bool X86_64TargetInfo::isRelRelative(uint32_t Type) const { |
| 329 | switch (Type) { |
| 330 | default: |
| 331 | return false; |
| 332 | case R_X86_64_PC64: |
| 333 | case R_X86_64_PC32: |
| 334 | case R_X86_64_PC16: |
| 335 | case R_X86_64_PC8: |
Rafael Espindola | 69535df | 2015-10-19 05:20:01 +0000 | [diff] [blame] | 336 | case R_X86_64_PLT32: |
Michael J. Spencer | a5d9d1f | 2015-11-11 01:27:58 +0000 | [diff] [blame] | 337 | case R_X86_64_DTPOFF32: |
Michael J. Spencer | ac2307b | 2015-11-11 01:28:11 +0000 | [diff] [blame] | 338 | case R_X86_64_DTPOFF64: |
Rafael Espindola | ae24400 | 2015-10-05 19:30:12 +0000 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | } |
| 342 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 343 | void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 344 | uint64_t P, uint64_t SA) const { |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 345 | switch (Type) { |
| 346 | case R_X86_64_PC32: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 347 | case R_X86_64_GOTPCREL: |
Rafael Espindola | 5045e44 | 2015-10-18 03:13:46 +0000 | [diff] [blame] | 348 | case R_X86_64_PLT32: |
Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 349 | case R_X86_64_TLSLD: |
Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 350 | case R_X86_64_TLSGD: |
George Rimar | 687138c | 2015-11-13 16:28:53 +0000 | [diff] [blame] | 351 | case R_X86_64_TPOFF64: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 352 | write32le(Loc, SA - P); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 353 | break; |
| 354 | case R_X86_64_64: |
Michael J. Spencer | ac2307b | 2015-11-11 01:28:11 +0000 | [diff] [blame] | 355 | case R_X86_64_DTPOFF64: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 356 | write64le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 357 | break; |
Rui Ueyama | 3835b49 | 2015-10-23 16:13:27 +0000 | [diff] [blame] | 358 | case R_X86_64_32: |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 359 | case R_X86_64_32S: |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 360 | if (Type == R_X86_64_32 && !isUInt<32>(SA)) |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 361 | error("R_X86_64_32 out of range"); |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 362 | else if (!isInt<32>(SA)) |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 363 | error("R_X86_64_32S out of range"); |
Rui Ueyama | 6607227 | 2015-10-15 19:52:27 +0000 | [diff] [blame] | 364 | write32le(Loc, SA); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 365 | break; |
Michael J. Spencer | a5d9d1f | 2015-11-11 01:27:58 +0000 | [diff] [blame] | 366 | case R_X86_64_DTPOFF32: |
| 367 | write32le(Loc, SA); |
| 368 | break; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 369 | case R_X86_64_TPOFF32: { |
Rafael Espindola | ea7a1e90 | 2015-11-06 22:14:44 +0000 | [diff] [blame] | 370 | uint64_t Val = SA - Out<ELF64LE>::TlsPhdr->p_memsz; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 371 | if (!isInt<32>(Val)) |
| 372 | error("R_X86_64_TPOFF32 out of range"); |
| 373 | write32le(Loc, Val); |
Michael J. Spencer | d77f0d2 | 2015-11-03 22:39:09 +0000 | [diff] [blame] | 374 | break; |
Rafael Espindola | ac1c0f8 | 2015-11-05 15:22:26 +0000 | [diff] [blame] | 375 | } |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 376 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 377 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 381 | // Relocation masks following the #lo(value), #hi(value), #ha(value), |
| 382 | // #higher(value), #highera(value), #highest(value), and #highesta(value) |
| 383 | // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi |
| 384 | // document. |
Rui Ueyama | c44e5a1 | 2015-10-23 16:54:58 +0000 | [diff] [blame] | 385 | static uint16_t applyPPCLo(uint64_t V) { return V; } |
| 386 | static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } |
| 387 | static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } |
| 388 | static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } |
| 389 | static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 390 | static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 391 | static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } |
| 392 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 393 | PPC64TargetInfo::PPC64TargetInfo() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 394 | PCRelReloc = R_PPC64_REL24; |
| 395 | GotReloc = R_PPC64_GLOB_DAT; |
| 396 | GotRefReloc = R_PPC64_REL64; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 397 | RelativeReloc = R_PPC64_RELATIVE; |
Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 398 | PltEntrySize = 32; |
Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 399 | |
| 400 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 401 | // set different permissions on a finer granularity than that). |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 402 | PageSize = 65536; |
Hal Finkel | 736c741 | 2015-10-15 07:49:07 +0000 | [diff] [blame] | 403 | |
| 404 | // The PPC64 ELF ABI v1 spec, says: |
| 405 | // |
| 406 | // It is normally desirable to put segments with different characteristics |
| 407 | // in separate 256 Mbyte portions of the address space, to give the |
| 408 | // operating system full paging flexibility in the 64-bit address space. |
| 409 | // |
| 410 | // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers |
| 411 | // use 0x10000000 as the starting address. |
| 412 | VAStart = 0x10000000; |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 413 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 414 | |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 415 | uint64_t getPPC64TocBase() { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 416 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 417 | // order. The TOC starts where the first of these sections starts. |
| 418 | |
| 419 | // FIXME: This obviously does not do the right thing when there is no .got |
| 420 | // section, but there is a .toc or .tocbss section. |
| 421 | uint64_t TocVA = Out<ELF64BE>::Got->getVA(); |
| 422 | if (!TocVA) |
| 423 | TocVA = Out<ELF64BE>::Plt->getVA(); |
| 424 | |
| 425 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 426 | // thus permitting a full 64 Kbytes segment. Note that the glibc startup |
| 427 | // code (crt1.o) assumes that you can get from the TOC base to the |
| 428 | // start of the .toc section with only a single (signed) 16-bit relocation. |
| 429 | return TocVA + 0x8000; |
| 430 | } |
| 431 | |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 432 | void PPC64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 433 | void PPC64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 434 | uint64_t PltEntryAddr) const {} |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 435 | void PPC64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 436 | uint64_t PltEntryAddr, int32_t Index) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 437 | uint64_t Off = GotEntryAddr - getPPC64TocBase(); |
| 438 | |
| 439 | // FIXME: What we should do, in theory, is get the offset of the function |
| 440 | // descriptor in the .opd section, and use that as the offset from %r2 (the |
| 441 | // TOC-base pointer). Instead, we have the GOT-entry offset, and that will |
| 442 | // be a pointer to the function descriptor in the .opd section. Using |
| 443 | // this scheme is simpler, but requires an extra indirection per PLT dispatch. |
| 444 | |
Hal Finkel | fa92f68 | 2015-10-13 21:47:34 +0000 | [diff] [blame] | 445 | write32be(Buf, 0xf8410028); // std %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 446 | write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha |
| 447 | write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) |
| 448 | write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) |
| 449 | write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 |
| 450 | write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) |
| 451 | write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) |
| 452 | write32be(Buf + 28, 0x4e800420); // bctr |
| 453 | } |
| 454 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 455 | bool PPC64TargetInfo::relocNeedsGot(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 456 | if (relocNeedsPlt(Type, S)) |
| 457 | return true; |
| 458 | |
| 459 | switch (Type) { |
| 460 | default: return false; |
| 461 | case R_PPC64_GOT16: |
| 462 | case R_PPC64_GOT16_LO: |
| 463 | case R_PPC64_GOT16_HI: |
| 464 | case R_PPC64_GOT16_HA: |
| 465 | case R_PPC64_GOT16_DS: |
| 466 | case R_PPC64_GOT16_LO_DS: |
| 467 | return true; |
| 468 | } |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 469 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 470 | |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 471 | bool PPC64TargetInfo::relocNeedsPlt(uint32_t Type, const SymbolBody &S) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 472 | // 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] | 473 | return Type == R_PPC64_REL24 && canBePreempted(&S, false); |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 474 | } |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 475 | |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 476 | bool PPC64TargetInfo::isRelRelative(uint32_t Type) const { |
| 477 | switch (Type) { |
| 478 | default: |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 479 | return true; |
Hal Finkel | 0091862 | 2015-10-16 19:01:50 +0000 | [diff] [blame] | 480 | case R_PPC64_TOC: |
| 481 | case R_PPC64_ADDR64: |
| 482 | return false; |
Hal Finkel | be0823d | 2015-10-12 20:58:52 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 486 | void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, uint32_t Type, |
| 487 | uint64_t P, uint64_t SA) const { |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 488 | uint64_t TB = getPPC64TocBase(); |
| 489 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 490 | // For a TOC-relative relocation, adjust the addend and proceed in terms of |
| 491 | // the corresponding ADDR16 relocation type. |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 492 | switch (Type) { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 493 | case R_PPC64_TOC16: Type = R_PPC64_ADDR16; SA -= TB; break; |
| 494 | case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; SA -= TB; break; |
| 495 | case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; SA -= TB; break; |
| 496 | case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; SA -= TB; break; |
| 497 | case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; SA -= TB; break; |
| 498 | 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] | 499 | default: break; |
| 500 | } |
| 501 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 502 | switch (Type) { |
| 503 | case R_PPC64_ADDR16: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 504 | if (!isInt<16>(SA)) |
Hal Finkel | 33e17a7 | 2015-10-15 16:17:30 +0000 | [diff] [blame] | 505 | error("Relocation R_PPC64_ADDR16 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 506 | write16be(Loc, SA); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 507 | break; |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 508 | case R_PPC64_ADDR16_DS: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 509 | if (!isInt<16>(SA)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 510 | error("Relocation R_PPC64_ADDR16_DS overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 511 | write16be(Loc, (read16be(Loc) & 3) | (SA & ~3)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 512 | break; |
| 513 | case R_PPC64_ADDR16_LO: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 514 | write16be(Loc, applyPPCLo(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 515 | break; |
| 516 | case R_PPC64_ADDR16_LO_DS: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 517 | write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(SA) & ~3)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 518 | break; |
| 519 | case R_PPC64_ADDR16_HI: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 520 | write16be(Loc, applyPPCHi(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 521 | break; |
| 522 | case R_PPC64_ADDR16_HA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 523 | write16be(Loc, applyPPCHa(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 524 | break; |
| 525 | case R_PPC64_ADDR16_HIGHER: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 526 | write16be(Loc, applyPPCHigher(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 527 | break; |
| 528 | case R_PPC64_ADDR16_HIGHERA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 529 | write16be(Loc, applyPPCHighera(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 530 | break; |
| 531 | case R_PPC64_ADDR16_HIGHEST: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 532 | write16be(Loc, applyPPCHighest(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 533 | break; |
| 534 | case R_PPC64_ADDR16_HIGHESTA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 535 | write16be(Loc, applyPPCHighesta(SA)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 536 | break; |
| 537 | case R_PPC64_ADDR14: { |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 538 | if ((SA & 3) != 0) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 539 | error("Improper alignment for relocation R_PPC64_ADDR14"); |
| 540 | |
| 541 | // Preserve the AA/LK bits in the branch instruction |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 542 | uint8_t AALK = Loc[3]; |
| 543 | write16be(Loc + 2, (AALK & 3) | (SA & 0xfffc)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 544 | break; |
| 545 | } |
| 546 | case R_PPC64_REL16_LO: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 547 | write16be(Loc, applyPPCLo(SA - P)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 548 | break; |
| 549 | case R_PPC64_REL16_HI: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 550 | write16be(Loc, applyPPCHi(SA - P)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 551 | break; |
| 552 | case R_PPC64_REL16_HA: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 553 | write16be(Loc, applyPPCHa(SA - P)); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 554 | break; |
| 555 | case R_PPC64_ADDR32: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 556 | if (!isInt<32>(SA)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 557 | error("Relocation R_PPC64_ADDR32 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 558 | write32be(Loc, SA); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 559 | break; |
| 560 | case R_PPC64_REL24: { |
Hal Finkel | 8228198 | 2015-10-17 00:48:20 +0000 | [diff] [blame] | 561 | // If we have an undefined weak symbol, we might get here with a symbol |
| 562 | // address of zero. That could overflow, but the code must be unreachable, |
| 563 | // so don't bother doing anything at all. |
| 564 | if (!SA) |
| 565 | break; |
| 566 | |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 567 | uint64_t PltStart = Out<ELF64BE>::Plt->getVA(); |
| 568 | uint64_t PltEnd = PltStart + Out<ELF64BE>::Plt->getSize(); |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 569 | bool InPlt = PltStart <= SA && SA < PltEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 570 | |
| 571 | if (!InPlt && Out<ELF64BE>::Opd) { |
| 572 | // If this is a local call, and we currently have the address of a |
| 573 | // function-descriptor, get the underlying code address instead. |
| 574 | uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); |
| 575 | uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 576 | bool InOpd = OpdStart <= SA && SA < OpdEnd; |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 577 | |
| 578 | if (InOpd) |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 579 | SA = read64be(&Out<ELF64BE>::OpdBuf[SA - OpdStart]); |
Hal Finkel | daedc12 | 2015-10-12 23:16:53 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 582 | uint32_t Mask = 0x03FFFFFC; |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 583 | if (!isInt<24>(SA - P)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 584 | error("Relocation R_PPC64_REL24 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 585 | write32be(Loc, (read32be(Loc) & ~Mask) | ((SA - P) & Mask)); |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 586 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 587 | uint32_t Nop = 0x60000000; |
| 588 | if (InPlt && Loc + 8 <= BufEnd && read32be(Loc + 4) == Nop) |
| 589 | write32be(Loc + 4, 0xe8410028); // ld %r2, 40(%r1) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 590 | break; |
| 591 | } |
| 592 | case R_PPC64_REL32: |
Rui Ueyama | 9e82fa2 | 2015-10-15 19:39:36 +0000 | [diff] [blame] | 593 | if (!isInt<32>(SA - P)) |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 594 | error("Relocation R_PPC64_REL32 overflow"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 595 | write32be(Loc, SA - P); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 596 | break; |
| 597 | case R_PPC64_REL64: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 598 | write64be(Loc, SA - P); |
Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 599 | break; |
| 600 | case R_PPC64_ADDR64: |
Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 601 | case R_PPC64_TOC: |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 602 | write64be(Loc, SA); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 603 | break; |
| 604 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 605 | error("unrecognized reloc " + Twine(Type)); |
Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 608 | |
Rui Ueyama | 9cfc8e0 | 2015-10-21 21:13:35 +0000 | [diff] [blame] | 609 | AArch64TargetInfo::AArch64TargetInfo() {} |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 610 | |
| 611 | void AArch64TargetInfo::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 612 | void AArch64TargetInfo::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 613 | uint64_t PltEntryAddr) const {} |
Davide Italiano | cde9336 | 2015-09-26 00:32:04 +0000 | [diff] [blame] | 614 | void AArch64TargetInfo::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 615 | uint64_t PltEntryAddr, int32_t Index) const {} |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 616 | bool AArch64TargetInfo::relocNeedsGot(uint32_t Type, |
| 617 | const SymbolBody &S) const { |
| 618 | return false; |
| 619 | } |
| 620 | bool AArch64TargetInfo::relocNeedsPlt(uint32_t Type, |
| 621 | const SymbolBody &S) const { |
| 622 | return false; |
| 623 | } |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 624 | |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 625 | static void updateAArch64Adr(uint8_t *L, uint64_t Imm) { |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 626 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 627 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 628 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 629 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 632 | // Page(Expr) is the page address of the expression Expr, defined |
| 633 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
Davide Italiano | d9b5be4 | 2015-10-02 22:17:09 +0000 | [diff] [blame] | 634 | // supported by the platform has a different value.) |
Davide Italiano | ef4be6b | 2015-10-06 19:01:32 +0000 | [diff] [blame] | 635 | static uint64_t getAArch64Page(uint64_t Expr) { |
Davide Italiano | 318ca22 | 2015-10-02 22:13:51 +0000 | [diff] [blame] | 636 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 639 | void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd, |
| 640 | uint32_t Type, uint64_t P, |
| 641 | uint64_t SA) const { |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 642 | switch (Type) { |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 643 | case R_AARCH64_ABS16: |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 644 | if (!isInt<16>(SA)) |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 645 | error("Relocation R_AARCH64_ABS16 out of range"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 646 | write16le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 647 | break; |
| 648 | case R_AARCH64_ABS32: |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 649 | if (!isInt<32>(SA)) |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 650 | error("Relocation R_AARCH64_ABS32 out of range"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 651 | write32le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 652 | break; |
| 653 | case R_AARCH64_ABS64: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 654 | // No overflow check needed. |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 655 | write64le(Loc, SA); |
Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 656 | break; |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 657 | case R_AARCH64_ADD_ABS_LO12_NC: |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 658 | // No overflow check needed. |
Davide Italiano | a716574 | 2015-10-16 21:06:55 +0000 | [diff] [blame] | 659 | // This relocation stores 12 bits and there's no instruction |
| 660 | // 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] | 661 | // of r_addend bitwise-or'ed Loc. This assumes that the addend |
| 662 | // bits in Loc are zero. |
| 663 | or32le(Loc, (SA & 0xFFF) << 10); |
Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 664 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 665 | case R_AARCH64_ADR_PREL_LO21: { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 666 | uint64_t X = SA - P; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 667 | if (!isInt<21>(X)) |
| 668 | error("Relocation R_AARCH64_ADR_PREL_LO21 out of range"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 669 | updateAArch64Adr(Loc, X & 0x1FFFFF); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 670 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 671 | } |
| 672 | case R_AARCH64_ADR_PREL_PG_HI21: { |
Rafael Espindola | 826941a | 2015-10-15 18:19:39 +0000 | [diff] [blame] | 673 | uint64_t X = getAArch64Page(SA) - getAArch64Page(P); |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 674 | if (!isInt<33>(X)) |
| 675 | error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 676 | updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12] |
Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 677 | break; |
Rui Ueyama | ee8c53b | 2015-10-06 19:57:01 +0000 | [diff] [blame] | 678 | } |
Igor Kudrin | b34115b | 2015-11-13 03:26:59 +0000 | [diff] [blame] | 679 | case R_AARCH64_JUMP26: |
| 680 | case R_AARCH64_CALL26: { |
| 681 | uint64_t X = SA - P; |
| 682 | if (!isInt<28>(X)) { |
| 683 | if (Type == R_AARCH64_JUMP26) |
| 684 | error("Relocation R_AARCH64_JUMP26 out of range"); |
| 685 | error("Relocation R_AARCH64_CALL26 out of range"); |
| 686 | } |
| 687 | or32le(Loc, (X & 0x0FFFFFFC) >> 2); |
| 688 | break; |
| 689 | } |
Davide Italiano | bbcc7f6 | 2015-11-08 04:45:26 +0000 | [diff] [blame] | 690 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| 691 | // No overflow check needed. |
| 692 | or32le(Loc, (SA & 0xFF8) << 7); |
| 693 | break; |
Davide Italiano | 3300b79 | 2015-10-29 19:55:59 +0000 | [diff] [blame] | 694 | case R_AARCH64_PREL16: |
| 695 | if (!isInt<16>(SA)) |
| 696 | error("Relocation R_AARCH64_PREL16 out of range"); |
| 697 | write16le(Loc, SA - P); |
| 698 | break; |
| 699 | case R_AARCH64_PREL32: |
| 700 | if (!isInt<32>(SA)) |
| 701 | error("Relocation R_AARCH64_PREL32 out of range"); |
| 702 | write32le(Loc, SA - P); |
| 703 | break; |
Davide Italiano | b12d668 | 2015-10-28 16:14:18 +0000 | [diff] [blame] | 704 | case R_AARCH64_PREL64: |
| 705 | // No overflow check needed. |
| 706 | write64le(Loc, SA - P); |
| 707 | break; |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 708 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 709 | error("unrecognized reloc " + Twine(Type)); |
Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 712 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 713 | template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { |
Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 714 | PageSize = 65536; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 715 | GotRefReloc = R_MIPS_GOT16; |
| 716 | GotHeaderEntriesNum = 2; |
| 717 | } |
| 718 | |
| 719 | template <class ELFT> |
| 720 | void MipsTargetInfo<ELFT>::writeGotHeaderEntries(uint8_t *Buf) const { |
| 721 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off; |
| 722 | auto *P = reinterpret_cast<Elf_Off *>(Buf); |
| 723 | // Module pointer |
| 724 | P[1] = ELFT::Is64Bits ? 0x8000000000000000 : 0x80000000; |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 727 | template <class ELFT> |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 728 | void MipsTargetInfo<ELFT>::writeGotPltEntry(uint8_t *Buf, uint64_t Plt) const {} |
| 729 | template <class ELFT> |
| 730 | void MipsTargetInfo<ELFT>::writePltZeroEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
| 731 | uint64_t PltEntryAddr) const {} |
| 732 | template <class ELFT> |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 733 | void MipsTargetInfo<ELFT>::writePltEntry(uint8_t *Buf, uint64_t GotEntryAddr, |
George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 734 | uint64_t PltEntryAddr, int32_t Index) const {} |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 735 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 736 | template <class ELFT> |
| 737 | bool MipsTargetInfo<ELFT>::relocNeedsGot(uint32_t Type, |
| 738 | const SymbolBody &S) const { |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 739 | return Type == R_MIPS_GOT16; |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 740 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 741 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 742 | template <class ELFT> |
| 743 | bool MipsTargetInfo<ELFT>::relocNeedsPlt(uint32_t Type, |
| 744 | const SymbolBody &S) const { |
Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 745 | return false; |
| 746 | } |
Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 747 | |
Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 748 | template <class ELFT> |
Rui Ueyama | 96f0e0b | 2015-10-23 02:40:46 +0000 | [diff] [blame] | 749 | void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint8_t *BufEnd, |
| 750 | uint32_t Type, uint64_t P, |
| 751 | uint64_t SA) const { |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 752 | const endianness E = ELFT::TargetEndianness; |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 753 | switch (Type) { |
| 754 | case R_MIPS_32: |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 755 | add32<E>(Loc, SA); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 756 | break; |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 757 | case R_MIPS_GOT16: { |
| 758 | int64_t V = SA - getMipsGpAddr<ELFT>(); |
| 759 | if (!isInt<16>(V)) |
| 760 | error("Relocation R_MIPS_GOT16 out of range"); |
Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 761 | write32<E>(Loc, (read32<E>(Loc) & 0xffff0000) | (V & 0xffff)); |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 762 | break; |
| 763 | } |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 764 | default: |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 765 | error("unrecognized reloc " + Twine(Type)); |
Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 768 | |
| 769 | template <class ELFT> |
| 770 | typename llvm::object::ELFFile<ELFT>::uintX_t getMipsGpAddr() { |
| 771 | const unsigned GPOffset = 0x7ff0; |
| 772 | return Out<ELFT>::Got->getVA() ? (Out<ELFT>::Got->getVA() + GPOffset) : 0; |
| 773 | } |
| 774 | |
| 775 | template uint32_t getMipsGpAddr<ELF32LE>(); |
| 776 | template uint32_t getMipsGpAddr<ELF32BE>(); |
| 777 | template uint64_t getMipsGpAddr<ELF64LE>(); |
| 778 | template uint64_t getMipsGpAddr<ELF64BE>(); |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 779 | } |
| 780 | } |