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