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