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