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