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