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