| 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 |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 14 | // in this file. |
| Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 15 | // |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 16 | // Some functions defined in this file has "relaxTls" as part of their names. |
| 17 | // They do peephole optimization for TLS variables by rewriting instructions. |
| 18 | // They are not part of the ABI but optional optimization, so you can skip |
| 19 | // them if you are not interested in how TLS variables are optimized. |
| 20 | // See the following paper for the details. |
| 21 | // |
| 22 | // Ulrich Drepper, ELF Handling For Thread-Local Storage |
| 23 | // http://www.akkadia.org/drepper/tls.pdf |
| 24 | // |
| Rui Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 26 | |
| 27 | #include "Target.h" |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 28 | #include "Error.h" |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 29 | #include "InputFiles.h" |
| Rui Ueyama | af21d92 | 2015-10-08 20:06:07 +0000 | [diff] [blame] | 30 | #include "OutputSections.h" |
| Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 31 | #include "Symbols.h" |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 32 | |
| 33 | #include "llvm/ADT/ArrayRef.h" |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 34 | #include "llvm/Object/ELF.h" |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Endian.h" |
| 36 | #include "llvm/Support/ELF.h" |
| 37 | |
| 38 | using namespace llvm; |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 39 | using namespace llvm::object; |
| Rafael Espindola | 0872ea3 | 2015-09-24 14:16:02 +0000 | [diff] [blame] | 40 | using namespace llvm::support::endian; |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 41 | using namespace llvm::ELF; |
| 42 | |
| 43 | namespace lld { |
| Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 44 | namespace elf { |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 45 | |
| Rui Ueyama | c1c282a | 2016-02-11 21:18:01 +0000 | [diff] [blame] | 46 | TargetInfo *Target; |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 47 | |
| Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 48 | 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] | 49 | |
| Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 50 | template <unsigned N> static void checkInt(int64_t V, uint32_t Type) { |
| 51 | if (isInt<N>(V)) |
| 52 | return; |
| 53 | StringRef S = getELFRelocationTypeName(Config->EMachine, Type); |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 54 | error("relocation " + S + " out of range"); |
| Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | template <unsigned N> static void checkUInt(uint64_t V, uint32_t Type) { |
| 58 | if (isUInt<N>(V)) |
| 59 | return; |
| 60 | StringRef S = getELFRelocationTypeName(Config->EMachine, Type); |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 61 | error("relocation " + S + " out of range"); |
| Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| Igor Kudrin | fea8ed5 | 2015-11-26 10:05:24 +0000 | [diff] [blame] | 64 | template <unsigned N> static void checkIntUInt(uint64_t V, uint32_t Type) { |
| 65 | if (isInt<N>(V) || isUInt<N>(V)) |
| 66 | return; |
| 67 | StringRef S = getELFRelocationTypeName(Config->EMachine, Type); |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 68 | error("relocation " + S + " out of range"); |
| Igor Kudrin | fea8ed5 | 2015-11-26 10:05:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 71 | template <unsigned N> static void checkAlignment(uint64_t V, uint32_t Type) { |
| 72 | if ((V & (N - 1)) == 0) |
| 73 | return; |
| 74 | StringRef S = getELFRelocationTypeName(Config->EMachine, Type); |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 75 | error("improper alignment for relocation " + S); |
| Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 78 | namespace { |
| 79 | class X86TargetInfo final : public TargetInfo { |
| 80 | public: |
| 81 | X86TargetInfo(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 82 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 83 | uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 84 | void writeGotPltHeader(uint8_t *Buf) const override; |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 85 | uint32_t getDynRel(uint32_t Type) const override; |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 86 | bool isTlsLocalDynamicRel(uint32_t Type) const override; |
| 87 | bool isTlsGlobalDynamicRel(uint32_t Type) const override; |
| 88 | bool isTlsInitialExecRel(uint32_t Type) const override; |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 89 | void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 90 | void writePltZero(uint8_t *Buf) const override; |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 91 | void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, |
| 92 | int32_t Index, unsigned RelOff) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 93 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 94 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 95 | void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 96 | void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 97 | void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 98 | void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | class X86_64TargetInfo final : public TargetInfo { |
| 102 | public: |
| 103 | X86_64TargetInfo(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 104 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| George Rimar | 8697105 | 2016-03-29 08:35:42 +0000 | [diff] [blame] | 105 | uint32_t getDynRel(uint32_t Type) const override; |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 106 | bool isTlsLocalDynamicRel(uint32_t Type) const override; |
| 107 | bool isTlsGlobalDynamicRel(uint32_t Type) const override; |
| 108 | bool isTlsInitialExecRel(uint32_t Type) const override; |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 109 | void writeGotPltHeader(uint8_t *Buf) const override; |
| 110 | void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 111 | void writePltZero(uint8_t *Buf) const override; |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 112 | void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, |
| 113 | int32_t Index, unsigned RelOff) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 114 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 115 | |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 116 | RelExpr adjustRelaxGotExpr(uint32_t Type, const uint8_t *Data, |
| 117 | RelExpr Expr) const override; |
| George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 118 | void relaxGot(uint8_t *Loc, uint64_t Val) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 119 | void relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 120 | void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 121 | void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 122 | void relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| George Rimar | b720430 | 2016-06-02 09:22:00 +0000 | [diff] [blame] | 123 | |
| 124 | private: |
| 125 | void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op, |
| 126 | uint8_t ModRm) const; |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 129 | class PPCTargetInfo final : public TargetInfo { |
| 130 | public: |
| 131 | PPCTargetInfo(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 132 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 133 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 136 | class PPC64TargetInfo final : public TargetInfo { |
| 137 | public: |
| 138 | PPC64TargetInfo(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 139 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 140 | void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, |
| 141 | int32_t Index, unsigned RelOff) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 142 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 143 | }; |
| 144 | |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 145 | class AArch64TargetInfo final : public TargetInfo { |
| 146 | public: |
| 147 | AArch64TargetInfo(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 148 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 149 | uint32_t getDynRel(uint32_t Type) const override; |
| 150 | bool isTlsGlobalDynamicRel(uint32_t Type) const override; |
| 151 | bool isTlsInitialExecRel(uint32_t Type) const override; |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 152 | void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 153 | void writePltZero(uint8_t *Buf) const override; |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 154 | void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, |
| 155 | int32_t Index, unsigned RelOff) const override; |
| Rafael Espindola | b8ff59a | 2016-04-28 14:34:39 +0000 | [diff] [blame] | 156 | bool usesOnlyLowPageBits(uint32_t Type) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 157 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 158 | void relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 159 | void relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 162 | class AMDGPUTargetInfo final : public TargetInfo { |
| 163 | public: |
| Rui Ueyama | 012eb78 | 2016-01-29 04:05:09 +0000 | [diff] [blame] | 164 | AMDGPUTargetInfo() {} |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 165 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| 166 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 169 | template <class ELFT> class MipsTargetInfo final : public TargetInfo { |
| 170 | public: |
| 171 | MipsTargetInfo(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 172 | RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 173 | uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 174 | uint32_t getDynRel(uint32_t Type) const override; |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 175 | void writeGotPlt(uint8_t *Buf, uint64_t Plt) const override; |
| 176 | void writePltZero(uint8_t *Buf) const override; |
| 177 | void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, |
| 178 | int32_t Index, unsigned RelOff) const override; |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 179 | void writeThunk(uint8_t *Buf, uint64_t S) const override; |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 180 | bool needsThunk(uint32_t Type, const InputFile &File, |
| 181 | const SymbolBody &S) const override; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 182 | void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; |
| Rafael Espindola | b8ff59a | 2016-04-28 14:34:39 +0000 | [diff] [blame] | 183 | bool usesOnlyLowPageBits(uint32_t Type) const override; |
| Rui Ueyama | efc23de | 2015-10-14 21:30:32 +0000 | [diff] [blame] | 184 | }; |
| 185 | } // anonymous namespace |
| 186 | |
| Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 187 | TargetInfo *createTarget() { |
| 188 | switch (Config->EMachine) { |
| 189 | case EM_386: |
| 190 | return new X86TargetInfo(); |
| 191 | case EM_AARCH64: |
| 192 | return new AArch64TargetInfo(); |
| Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 193 | case EM_AMDGPU: |
| 194 | return new AMDGPUTargetInfo(); |
| Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 195 | case EM_MIPS: |
| Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 196 | switch (Config->EKind) { |
| 197 | case ELF32LEKind: |
| 198 | return new MipsTargetInfo<ELF32LE>(); |
| 199 | case ELF32BEKind: |
| 200 | return new MipsTargetInfo<ELF32BE>(); |
| Simon Atanasyan | ae77ab7 | 2016-04-29 10:39:17 +0000 | [diff] [blame] | 201 | case ELF64LEKind: |
| 202 | return new MipsTargetInfo<ELF64LE>(); |
| 203 | case ELF64BEKind: |
| 204 | return new MipsTargetInfo<ELF64BE>(); |
| Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 205 | default: |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 206 | fatal("unsupported MIPS target"); |
| Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 207 | } |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 208 | case EM_PPC: |
| 209 | return new PPCTargetInfo(); |
| Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 210 | case EM_PPC64: |
| 211 | return new PPC64TargetInfo(); |
| 212 | case EM_X86_64: |
| 213 | return new X86_64TargetInfo(); |
| 214 | } |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 215 | fatal("unknown target machine"); |
| Rui Ueyama | 9100439 | 2015-10-13 16:08:15 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 218 | TargetInfo::~TargetInfo() {} |
| 219 | |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 220 | uint64_t TargetInfo::getImplicitAddend(const uint8_t *Buf, |
| 221 | uint32_t Type) const { |
| Rafael Espindola | da99df3 | 2016-03-30 12:40:38 +0000 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| George Rimar | 786e866 | 2016-03-17 05:57:33 +0000 | [diff] [blame] | 225 | uint64_t TargetInfo::getVAStart() const { return Config->Pic ? 0 : VAStart; } |
| Igor Kudrin | f6f4547 | 2015-11-10 08:39:27 +0000 | [diff] [blame] | 226 | |
| Rafael Espindola | b8ff59a | 2016-04-28 14:34:39 +0000 | [diff] [blame] | 227 | bool TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { return false; } |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 228 | |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 229 | bool TargetInfo::needsThunk(uint32_t Type, const InputFile &File, |
| 230 | const SymbolBody &S) const { |
| 231 | return false; |
| 232 | } |
| 233 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 234 | bool TargetInfo::isTlsInitialExecRel(uint32_t Type) const { return false; } |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 235 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 236 | bool TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { return false; } |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 237 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 238 | bool TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const { |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 242 | RelExpr TargetInfo::adjustRelaxGotExpr(uint32_t Type, const uint8_t *Data, |
| 243 | RelExpr Expr) const { |
| 244 | return Expr; |
| George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const { |
| 248 | llvm_unreachable("Should not have claimed to be relaxable"); |
| 249 | } |
| 250 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 251 | void TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, |
| 252 | uint64_t Val) const { |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 253 | llvm_unreachable("Should not have claimed to be relaxable"); |
| 254 | } |
| 255 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 256 | void TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, |
| 257 | uint64_t Val) const { |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 258 | llvm_unreachable("Should not have claimed to be relaxable"); |
| 259 | } |
| 260 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 261 | void TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, |
| 262 | uint64_t Val) const { |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 263 | llvm_unreachable("Should not have claimed to be relaxable"); |
| 264 | } |
| 265 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 266 | void TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, |
| 267 | uint64_t Val) const { |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 268 | llvm_unreachable("Should not have claimed to be relaxable"); |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 269 | } |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 270 | |
| Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 271 | X86TargetInfo::X86TargetInfo() { |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 272 | CopyRel = R_386_COPY; |
| 273 | GotRel = R_386_GLOB_DAT; |
| 274 | PltRel = R_386_JUMP_SLOT; |
| 275 | IRelativeRel = R_386_IRELATIVE; |
| 276 | RelativeRel = R_386_RELATIVE; |
| 277 | TlsGotRel = R_386_TLS_TPOFF; |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 278 | TlsModuleIndexRel = R_386_TLS_DTPMOD32; |
| 279 | TlsOffsetRel = R_386_TLS_DTPOFF32; |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 280 | PltEntrySize = 16; |
| Rui Ueyama | 6251545 | 2016-01-29 03:00:32 +0000 | [diff] [blame] | 281 | PltZeroSize = 16; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 282 | TlsGdToLeSkip = 2; |
| 283 | } |
| 284 | |
| 285 | RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { |
| 286 | switch (Type) { |
| 287 | default: |
| 288 | return R_ABS; |
| Rafael Espindola | df17277 | 2016-04-18 01:29:15 +0000 | [diff] [blame] | 289 | case R_386_TLS_GD: |
| 290 | return R_TLSGD; |
| Rafael Espindola | c4d5697 | 2016-04-18 00:28:57 +0000 | [diff] [blame] | 291 | case R_386_TLS_LDM: |
| 292 | return R_TLSLD; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 293 | case R_386_PLT32: |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 294 | return R_PLT_PC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 295 | case R_386_PC32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 296 | return R_PC; |
| Rafael Espindola | 3f5d634 | 2016-04-18 12:07:13 +0000 | [diff] [blame] | 297 | case R_386_GOTPC: |
| 298 | return R_GOTONLY_PC; |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 299 | case R_386_TLS_IE: |
| 300 | return R_GOT; |
| Rafael Espindola | 3f5d634 | 2016-04-18 12:07:13 +0000 | [diff] [blame] | 301 | case R_386_GOT32: |
| 302 | case R_386_TLS_GOTIE: |
| 303 | return R_GOT_FROM_END; |
| 304 | case R_386_GOTOFF: |
| 305 | return R_GOTREL; |
| 306 | case R_386_TLS_LE: |
| 307 | return R_TLS; |
| 308 | case R_386_TLS_LE_32: |
| 309 | return R_NEG_TLS; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 310 | } |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 313 | void X86TargetInfo::writeGotPltHeader(uint8_t *Buf) const { |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 314 | write32le(Buf, Out<ELF32LE>::Dynamic->getVA()); |
| 315 | } |
| 316 | |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 317 | void X86TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { |
| Rui Ueyama | cf37593 | 2016-01-29 23:58:03 +0000 | [diff] [blame] | 318 | // Entries in .got.plt initially points back to the corresponding |
| 319 | // PLT entries with a fixed offset to skip the first instruction. |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 320 | write32le(Buf, Plt + 6); |
| Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 321 | } |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 322 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 323 | uint32_t X86TargetInfo::getDynRel(uint32_t Type) const { |
| George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 324 | if (Type == R_386_TLS_LE) |
| 325 | return R_386_TLS_TPOFF; |
| 326 | if (Type == R_386_TLS_LE_32) |
| 327 | return R_386_TLS_TPOFF32; |
| 328 | return Type; |
| 329 | } |
| 330 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 331 | bool X86TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const { |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 332 | return Type == R_386_TLS_GD; |
| 333 | } |
| 334 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 335 | bool X86TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 336 | return Type == R_386_TLS_LDO_32 || Type == R_386_TLS_LDM; |
| 337 | } |
| 338 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 339 | bool X86TargetInfo::isTlsInitialExecRel(uint32_t Type) const { |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 340 | return Type == R_386_TLS_IE || Type == R_386_TLS_GOTIE; |
| 341 | } |
| 342 | |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 343 | void X86TargetInfo::writePltZero(uint8_t *Buf) const { |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 344 | // Executable files and shared object files have |
| 345 | // separate procedure linkage tables. |
| George Rimar | 786e866 | 2016-03-17 05:57:33 +0000 | [diff] [blame] | 346 | if (Config->Pic) { |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 347 | const uint8_t V[] = { |
| Rui Ueyama | f53b1b7 | 2016-01-05 16:35:46 +0000 | [diff] [blame] | 348 | 0xff, 0xb3, 0x04, 0x00, 0x00, 0x00, // pushl 4(%ebx) |
| Rui Ueyama | cf37593 | 2016-01-29 23:58:03 +0000 | [diff] [blame] | 349 | 0xff, 0xa3, 0x08, 0x00, 0x00, 0x00, // jmp *8(%ebx) |
| 350 | 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 351 | }; |
| 352 | memcpy(Buf, V, sizeof(V)); |
| 353 | return; |
| 354 | } |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 355 | |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 356 | const uint8_t PltData[] = { |
| 357 | 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushl (GOT+4) |
| Rui Ueyama | cf37593 | 2016-01-29 23:58:03 +0000 | [diff] [blame] | 358 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *(GOT+8) |
| 359 | 0x90, 0x90, 0x90, 0x90 // nop; nop; nop; nop |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 360 | }; |
| 361 | memcpy(Buf, PltData, sizeof(PltData)); |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 362 | uint32_t Got = Out<ELF32LE>::GotPlt->getVA(); |
| Rui Ueyama | cf37593 | 2016-01-29 23:58:03 +0000 | [diff] [blame] | 363 | write32le(Buf + 2, Got + 4); |
| 364 | write32le(Buf + 8, Got + 8); |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 367 | void X86TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, |
| 368 | uint64_t PltEntryAddr, int32_t Index, |
| 369 | unsigned RelOff) const { |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 370 | const uint8_t Inst[] = { |
| 371 | 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, // jmp *foo_in_GOT|*foo@GOT(%ebx) |
| 372 | 0x68, 0x00, 0x00, 0x00, 0x00, // pushl $reloc_offset |
| 373 | 0xe9, 0x00, 0x00, 0x00, 0x00 // jmp .PLT0@PC |
| 374 | }; |
| Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 375 | memcpy(Buf, Inst, sizeof(Inst)); |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 376 | |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 377 | // jmp *foo@GOT(%ebx) or jmp *foo_in_GOT |
| George Rimar | 786e866 | 2016-03-17 05:57:33 +0000 | [diff] [blame] | 378 | Buf[1] = Config->Pic ? 0xa3 : 0x25; |
| Rafael Espindola | e2f4377 | 2016-05-18 20:44:24 +0000 | [diff] [blame] | 379 | uint32_t Got = Out<ELF32LE>::GotPlt->getVA(); |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 380 | write32le(Buf + 2, Config->Shared ? GotEntryAddr - Got : GotEntryAddr); |
| George Rimar | 77b7779 | 2015-11-25 22:15:01 +0000 | [diff] [blame] | 381 | write32le(Buf + 7, RelOff); |
| Rui Ueyama | 6251545 | 2016-01-29 03:00:32 +0000 | [diff] [blame] | 382 | write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16); |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 385 | uint64_t X86TargetInfo::getImplicitAddend(const uint8_t *Buf, |
| 386 | uint32_t Type) const { |
| Rafael Espindola | da99df3 | 2016-03-30 12:40:38 +0000 | [diff] [blame] | 387 | switch (Type) { |
| 388 | default: |
| 389 | return 0; |
| 390 | case R_386_32: |
| 391 | case R_386_GOT32: |
| 392 | case R_386_GOTOFF: |
| 393 | case R_386_GOTPC: |
| 394 | case R_386_PC32: |
| 395 | case R_386_PLT32: |
| 396 | return read32le(Buf); |
| 397 | } |
| 398 | } |
| 399 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 400 | void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, |
| 401 | uint64_t Val) const { |
| Rafael Espindola | 3f5d634 | 2016-04-18 12:07:13 +0000 | [diff] [blame] | 402 | checkInt<32>(Val, Type); |
| 403 | write32le(Loc, Val); |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 406 | void X86TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, |
| 407 | uint64_t Val) const { |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 408 | // Convert |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 409 | // leal x@tlsgd(, %ebx, 1), |
| 410 | // call __tls_get_addr@plt |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 411 | // to |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 412 | // movl %gs:0,%eax |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 413 | // subl $x@ntpoff,%eax |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 414 | const uint8_t Inst[] = { |
| 415 | 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax |
| 416 | 0x81, 0xe8, 0x00, 0x00, 0x00, 0x00 // subl 0(%ebx), %eax |
| 417 | }; |
| 418 | memcpy(Loc - 3, Inst, sizeof(Inst)); |
| Rafael Espindola | ebed1fe | 2016-05-20 21:23:52 +0000 | [diff] [blame] | 419 | relocateOne(Loc + 5, R_386_32, Val); |
| George Rimar | 2558e12 | 2015-12-09 09:55:54 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 422 | void X86TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, |
| 423 | uint64_t Val) const { |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 424 | // Convert |
| 425 | // leal x@tlsgd(, %ebx, 1), |
| 426 | // call __tls_get_addr@plt |
| 427 | // to |
| 428 | // movl %gs:0, %eax |
| 429 | // addl x@gotntpoff(%ebx), %eax |
| George Rimar | 2558e12 | 2015-12-09 09:55:54 +0000 | [diff] [blame] | 430 | const uint8_t Inst[] = { |
| 431 | 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0, %eax |
| 432 | 0x03, 0x83, 0x00, 0x00, 0x00, 0x00 // addl 0(%ebx), %eax |
| 433 | }; |
| 434 | memcpy(Loc - 3, Inst, sizeof(Inst)); |
| Rafael Espindola | 74f3dbe | 2016-05-20 20:09:35 +0000 | [diff] [blame] | 435 | relocateOne(Loc + 5, R_386_32, Val); |
| George Rimar | 2558e12 | 2015-12-09 09:55:54 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 438 | // In some conditions, relocations can be optimized to avoid using GOT. |
| 439 | // This function does that for Initial Exec to Local Exec case. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 440 | void X86TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, |
| 441 | uint64_t Val) const { |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 442 | // Ulrich's document section 6.2 says that @gotntpoff can |
| 443 | // be used with MOVL or ADDL instructions. |
| 444 | // @indntpoff is similar to @gotntpoff, but for use in |
| 445 | // position dependent code. |
| George Rimar | 2558e12 | 2015-12-09 09:55:54 +0000 | [diff] [blame] | 446 | uint8_t *Inst = Loc - 2; |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 447 | uint8_t *Op = Loc - 1; |
| George Rimar | 2558e12 | 2015-12-09 09:55:54 +0000 | [diff] [blame] | 448 | uint8_t Reg = (Loc[-1] >> 3) & 7; |
| 449 | bool IsMov = *Inst == 0x8b; |
| George Rimar | 6f17e09 | 2015-12-17 09:32:21 +0000 | [diff] [blame] | 450 | if (Type == R_386_TLS_IE) { |
| 451 | // For R_386_TLS_IE relocation we perform the next transformations: |
| 452 | // MOVL foo@INDNTPOFF,%EAX is transformed to MOVL $foo,%EAX |
| 453 | // MOVL foo@INDNTPOFF,%REG is transformed to MOVL $foo,%REG |
| 454 | // ADDL foo@INDNTPOFF,%REG is transformed to ADDL $foo,%REG |
| 455 | // First one is special because when EAX is used the sequence is 5 bytes |
| 456 | // long, otherwise it is 6 bytes. |
| 457 | if (*Op == 0xa1) { |
| 458 | *Op = 0xb8; |
| 459 | } else { |
| 460 | *Inst = IsMov ? 0xc7 : 0x81; |
| 461 | *Op = 0xc0 | ((*Op >> 3) & 7); |
| 462 | } |
| 463 | } else { |
| 464 | // R_386_TLS_GOTIE relocation can be optimized to |
| 465 | // R_386_TLS_LE so that it does not use GOT. |
| 466 | // "MOVL foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVL $foo, %REG". |
| 467 | // "ADDL foo@GOTNTPOFF(%RIP), %REG" is transformed to "LEAL foo(%REG), %REG" |
| 468 | // Note: gold converts to ADDL instead of LEAL. |
| 469 | *Inst = IsMov ? 0xc7 : 0x8d; |
| 470 | if (IsMov) |
| 471 | *Op = 0xc0 | ((*Op >> 3) & 7); |
| 472 | else |
| 473 | *Op = 0x80 | Reg | (Reg << 3); |
| 474 | } |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 475 | relocateOne(Loc, R_386_TLS_LE, Val); |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 478 | void X86TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, |
| 479 | uint64_t Val) const { |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 480 | if (Type == R_386_TLS_LDO_32) { |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 481 | relocateOne(Loc, R_386_TLS_LE, Val); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 482 | return; |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 485 | // Convert |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 486 | // leal foo(%reg),%eax |
| 487 | // call ___tls_get_addr |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 488 | // to |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 489 | // movl %gs:0,%eax |
| 490 | // nop |
| 491 | // leal 0(%esi,1),%esi |
| 492 | const uint8_t Inst[] = { |
| 493 | 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, // movl %gs:0,%eax |
| 494 | 0x90, // nop |
| 495 | 0x8d, 0x74, 0x26, 0x00 // leal 0(%esi,1),%esi |
| 496 | }; |
| 497 | memcpy(Loc - 2, Inst, sizeof(Inst)); |
| George Rimar | 2558e12 | 2015-12-09 09:55:54 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 500 | X86_64TargetInfo::X86_64TargetInfo() { |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 501 | CopyRel = R_X86_64_COPY; |
| 502 | GotRel = R_X86_64_GLOB_DAT; |
| 503 | PltRel = R_X86_64_JUMP_SLOT; |
| 504 | RelativeRel = R_X86_64_RELATIVE; |
| 505 | IRelativeRel = R_X86_64_IRELATIVE; |
| 506 | TlsGotRel = R_X86_64_TPOFF64; |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 507 | TlsModuleIndexRel = R_X86_64_DTPMOD64; |
| 508 | TlsOffsetRel = R_X86_64_DTPOFF64; |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 509 | PltEntrySize = 16; |
| Rui Ueyama | 6251545 | 2016-01-29 03:00:32 +0000 | [diff] [blame] | 510 | PltZeroSize = 16; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 511 | TlsGdToLeSkip = 2; |
| 512 | } |
| 513 | |
| 514 | RelExpr X86_64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { |
| 515 | switch (Type) { |
| 516 | default: |
| 517 | return R_ABS; |
| Rafael Espindola | ece62b9 | 2016-04-18 12:44:33 +0000 | [diff] [blame] | 518 | case R_X86_64_TPOFF32: |
| 519 | return R_TLS; |
| Rafael Espindola | c4d5697 | 2016-04-18 00:28:57 +0000 | [diff] [blame] | 520 | case R_X86_64_TLSLD: |
| 521 | return R_TLSLD_PC; |
| Rafael Espindola | df17277 | 2016-04-18 01:29:15 +0000 | [diff] [blame] | 522 | case R_X86_64_TLSGD: |
| 523 | return R_TLSGD_PC; |
| Rafael Espindola | 3151d89 | 2016-04-14 18:39:44 +0000 | [diff] [blame] | 524 | case R_X86_64_SIZE32: |
| 525 | case R_X86_64_SIZE64: |
| 526 | return R_SIZE; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 527 | case R_X86_64_PLT32: |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 528 | return R_PLT_PC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 529 | case R_X86_64_PC32: |
| Rafael Espindola | 926bff8 | 2016-04-25 14:05:44 +0000 | [diff] [blame] | 530 | case R_X86_64_PC64: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 531 | return R_PC; |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 532 | case R_X86_64_GOT32: |
| Rafael Espindola | f4c1cd4 | 2016-04-18 12:58:59 +0000 | [diff] [blame] | 533 | return R_GOT_FROM_END; |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 534 | case R_X86_64_GOTPCREL: |
| Rafael Espindola | dba64b8 | 2016-05-24 11:53:15 +0000 | [diff] [blame] | 535 | case R_X86_64_GOTPCRELX: |
| 536 | case R_X86_64_REX_GOTPCRELX: |
| Rafael Espindola | fe3a2f1 | 2016-05-24 12:12:06 +0000 | [diff] [blame] | 537 | case R_X86_64_GOTTPOFF: |
| 538 | return R_GOT_PC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 539 | } |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 542 | void X86_64TargetInfo::writeGotPltHeader(uint8_t *Buf) const { |
| Rafael Espindola | 4ee6cb3 | 2016-05-09 18:12:15 +0000 | [diff] [blame] | 543 | // The first entry holds the value of _DYNAMIC. It is not clear why that is |
| 544 | // required, but it is documented in the psabi and the glibc dynamic linker |
| Rafael Espindola | e502751 | 2016-05-10 16:23:46 +0000 | [diff] [blame] | 545 | // seems to use it (note that this is relevant for linking ld.so, not any |
| Rafael Espindola | 4ee6cb3 | 2016-05-09 18:12:15 +0000 | [diff] [blame] | 546 | // other program). |
| Igor Kudrin | 351b41d | 2015-11-16 17:44:08 +0000 | [diff] [blame] | 547 | write64le(Buf, Out<ELF64LE>::Dynamic->getVA()); |
| 548 | } |
| 549 | |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 550 | void X86_64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { |
| Rui Ueyama | cf37593 | 2016-01-29 23:58:03 +0000 | [diff] [blame] | 551 | // See comments in X86TargetInfo::writeGotPlt. |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 552 | write32le(Buf, Plt + 6); |
| 553 | } |
| 554 | |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 555 | void X86_64TargetInfo::writePltZero(uint8_t *Buf) const { |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 556 | const uint8_t PltData[] = { |
| 557 | 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, // pushq GOT+8(%rip) |
| 558 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmp *GOT+16(%rip) |
| 559 | 0x0f, 0x1f, 0x40, 0x00 // nopl 0x0(rax) |
| 560 | }; |
| 561 | memcpy(Buf, PltData, sizeof(PltData)); |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 562 | uint64_t Got = Out<ELF64LE>::GotPlt->getVA(); |
| 563 | uint64_t Plt = Out<ELF64LE>::Plt->getVA(); |
| 564 | write32le(Buf + 2, Got - Plt + 2); // GOT+8 |
| 565 | write32le(Buf + 8, Got - Plt + 4); // GOT+16 |
| Rafael Espindola | 7f07442 | 2015-09-22 21:35:51 +0000 | [diff] [blame] | 566 | } |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 567 | |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 568 | void X86_64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, |
| 569 | uint64_t PltEntryAddr, int32_t Index, |
| 570 | unsigned RelOff) const { |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 571 | const uint8_t Inst[] = { |
| 572 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // jmpq *got(%rip) |
| 573 | 0x68, 0x00, 0x00, 0x00, 0x00, // pushq <relocation index> |
| 574 | 0xe9, 0x00, 0x00, 0x00, 0x00 // jmpq plt[0] |
| 575 | }; |
| Rui Ueyama | 1500a90 | 2015-09-29 23:00:47 +0000 | [diff] [blame] | 576 | memcpy(Buf, Inst, sizeof(Inst)); |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 577 | |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 578 | write32le(Buf + 2, GotEntryAddr - PltEntryAddr - 6); |
| 579 | write32le(Buf + 7, Index); |
| Rui Ueyama | 6251545 | 2016-01-29 03:00:32 +0000 | [diff] [blame] | 580 | write32le(Buf + 12, -Index * PltEntrySize - PltZeroSize - 16); |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| George Rimar | 8697105 | 2016-03-29 08:35:42 +0000 | [diff] [blame] | 583 | uint32_t X86_64TargetInfo::getDynRel(uint32_t Type) const { |
| 584 | if (Type == R_X86_64_PC32 || Type == R_X86_64_32) |
| 585 | if (Config->Shared) |
| 586 | error(getELFRelocationTypeName(EM_X86_64, Type) + |
| 587 | " cannot be a dynamic relocation"); |
| 588 | return Type; |
| 589 | } |
| 590 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 591 | bool X86_64TargetInfo::isTlsInitialExecRel(uint32_t Type) const { |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 592 | return Type == R_X86_64_GOTTPOFF; |
| 593 | } |
| 594 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 595 | bool X86_64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const { |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 596 | return Type == R_X86_64_TLSGD; |
| 597 | } |
| 598 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 599 | bool X86_64TargetInfo::isTlsLocalDynamicRel(uint32_t Type) const { |
| Rafael Espindola | 1f04c44 | 2016-03-08 20:24:36 +0000 | [diff] [blame] | 600 | return Type == R_X86_64_DTPOFF32 || Type == R_X86_64_DTPOFF64 || |
| 601 | Type == R_X86_64_TLSLD; |
| George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 604 | void X86_64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, |
| 605 | uint64_t Val) const { |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 606 | // Convert |
| 607 | // .byte 0x66 |
| 608 | // leaq x@tlsgd(%rip), %rdi |
| 609 | // .word 0x6666 |
| 610 | // rex64 |
| 611 | // call __tls_get_addr@plt |
| 612 | // to |
| 613 | // mov %fs:0x0,%rax |
| 614 | // lea x@tpoff,%rax |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 615 | const uint8_t Inst[] = { |
| 616 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax |
| 617 | 0x48, 0x8d, 0x80, 0x00, 0x00, 0x00, 0x00 // lea x@tpoff,%rax |
| 618 | }; |
| 619 | memcpy(Loc - 4, Inst, sizeof(Inst)); |
| Rafael Espindola | 91e9fc0 | 2016-05-20 21:09:59 +0000 | [diff] [blame] | 620 | // The original code used a pc relative relocation and so we have to |
| 621 | // compensate for the -4 in had in the addend. |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 622 | relocateOne(Loc + 8, R_X86_64_TPOFF32, Val + 4); |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 625 | void X86_64TargetInfo::relaxTlsGdToIe(uint8_t *Loc, uint32_t Type, |
| 626 | uint64_t Val) const { |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 627 | // Convert |
| 628 | // .byte 0x66 |
| 629 | // leaq x@tlsgd(%rip), %rdi |
| 630 | // .word 0x6666 |
| 631 | // rex64 |
| 632 | // call __tls_get_addr@plt |
| 633 | // to |
| 634 | // mov %fs:0x0,%rax |
| 635 | // addq x@tpoff,%rax |
| George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 636 | const uint8_t Inst[] = { |
| 637 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00, // mov %fs:0x0,%rax |
| 638 | 0x48, 0x03, 0x05, 0x00, 0x00, 0x00, 0x00 // addq x@tpoff,%rax |
| 639 | }; |
| 640 | memcpy(Loc - 4, Inst, sizeof(Inst)); |
| Rafael Espindola | 91e9fc0 | 2016-05-20 21:09:59 +0000 | [diff] [blame] | 641 | // Both code sequences are PC relatives, but since we are moving the constant |
| 642 | // forward by 8 bytes we have to subtract the value by 8. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 643 | relocateOne(Loc + 8, R_X86_64_PC32, Val - 8); |
| George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 646 | // In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to |
| George Rimar | c55b4e2 | 2015-12-07 16:54:56 +0000 | [diff] [blame] | 647 | // R_X86_64_TPOFF32 so that it does not use GOT. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 648 | void X86_64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, |
| 649 | uint64_t Val) const { |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 650 | // Ulrich's document section 6.5 says that @gottpoff(%rip) must be |
| 651 | // used in MOVQ or ADDQ instructions only. |
| 652 | // "MOVQ foo@GOTTPOFF(%RIP), %REG" is transformed to "MOVQ $foo, %REG". |
| 653 | // "ADDQ foo@GOTTPOFF(%RIP), %REG" is transformed to "LEAQ foo(%REG), %REG" |
| 654 | // (if the register is not RSP/R12) or "ADDQ $foo, %RSP". |
| 655 | // Opcodes info can be found at http://ref.x86asm.net/coder64.html#x48. |
| 656 | uint8_t *Prefix = Loc - 3; |
| 657 | uint8_t *Inst = Loc - 2; |
| 658 | uint8_t *RegSlot = Loc - 1; |
| 659 | uint8_t Reg = Loc[-1] >> 3; |
| 660 | bool IsMov = *Inst == 0x8b; |
| 661 | bool RspAdd = !IsMov && Reg == 4; |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 662 | |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 663 | // r12 and rsp registers requires special handling. |
| 664 | // Problem is that for other registers, for example leaq 0xXXXXXXXX(%r11),%r11 |
| 665 | // result out is 7 bytes: 4d 8d 9b XX XX XX XX, |
| 666 | // but leaq 0xXXXXXXXX(%r12),%r12 is 8 bytes: 4d 8d a4 24 XX XX XX XX. |
| 667 | // The same true for rsp. So we convert to addq for them, saving 1 byte that |
| 668 | // we dont have. |
| 669 | if (RspAdd) |
| 670 | *Inst = 0x81; |
| 671 | else |
| 672 | *Inst = IsMov ? 0xc7 : 0x8d; |
| 673 | if (*Prefix == 0x4c) |
| 674 | *Prefix = (IsMov || RspAdd) ? 0x49 : 0x4d; |
| 675 | *RegSlot = (IsMov || RspAdd) ? (0xc0 | Reg) : (0x80 | Reg | (Reg << 3)); |
| Rafael Espindola | 91e9fc0 | 2016-05-20 21:09:59 +0000 | [diff] [blame] | 676 | // The original code used a pc relative relocation and so we have to |
| 677 | // compensate for the -4 in had in the addend. |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 678 | relocateOne(Loc, R_X86_64_TPOFF32, Val + 4); |
| George Rimar | 77d1cb1 | 2015-11-24 09:00:06 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 681 | void X86_64TargetInfo::relaxTlsLdToLe(uint8_t *Loc, uint32_t Type, |
| 682 | uint64_t Val) const { |
| Rui Ueyama | 55274e3 | 2016-04-23 01:10:15 +0000 | [diff] [blame] | 683 | // Convert |
| 684 | // leaq bar@tlsld(%rip), %rdi |
| 685 | // callq __tls_get_addr@PLT |
| 686 | // leaq bar@dtpoff(%rax), %rcx |
| 687 | // to |
| 688 | // .word 0x6666 |
| 689 | // .byte 0x66 |
| 690 | // mov %fs:0,%rax |
| 691 | // leaq bar@tpoff(%rax), %rcx |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 692 | if (Type == R_X86_64_DTPOFF64) { |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 693 | write64le(Loc, Val); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 694 | return; |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 695 | } |
| 696 | if (Type == R_X86_64_DTPOFF32) { |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 697 | relocateOne(Loc, R_X86_64_TPOFF32, Val); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 698 | return; |
| George Rimar | 25411f25 | 2015-12-04 11:20:13 +0000 | [diff] [blame] | 699 | } |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 700 | |
| 701 | const uint8_t Inst[] = { |
| Rui Ueyama | 02fcf11 | 2016-05-25 04:29:53 +0000 | [diff] [blame] | 702 | 0x66, 0x66, // .word 0x6666 |
| 703 | 0x66, // .byte 0x66 |
| Rafael Espindola | 89cc14f | 2016-03-16 19:03:58 +0000 | [diff] [blame] | 704 | 0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00 // mov %fs:0,%rax |
| 705 | }; |
| 706 | memcpy(Loc - 3, Inst, sizeof(Inst)); |
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 709 | void X86_64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, |
| 710 | uint64_t Val) const { |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 711 | switch (Type) { |
| Rui Ueyama | 3835b49 | 2015-10-23 16:13:27 +0000 | [diff] [blame] | 712 | case R_X86_64_32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 713 | checkUInt<32>(Val, Type); |
| 714 | write32le(Loc, Val); |
| Igor Kudrin | 9b7e7db | 2015-11-26 09:49:44 +0000 | [diff] [blame] | 715 | break; |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 716 | case R_X86_64_32S: |
| Rafael Espindola | ece62b9 | 2016-04-18 12:44:33 +0000 | [diff] [blame] | 717 | case R_X86_64_TPOFF32: |
| Rafael Espindola | f4c1cd4 | 2016-04-18 12:58:59 +0000 | [diff] [blame] | 718 | case R_X86_64_GOT32: |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 719 | case R_X86_64_GOTPCREL: |
| George Rimar | 9f8f4e3 | 2016-03-22 12:15:26 +0000 | [diff] [blame] | 720 | case R_X86_64_GOTPCRELX: |
| 721 | case R_X86_64_REX_GOTPCRELX: |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 722 | case R_X86_64_PC32: |
| Rafael Espindola | 38bd217 | 2016-05-04 15:51:23 +0000 | [diff] [blame] | 723 | case R_X86_64_GOTTPOFF: |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 724 | case R_X86_64_PLT32: |
| 725 | case R_X86_64_TLSGD: |
| 726 | case R_X86_64_TLSLD: |
| Rafael Espindola | 3c20fb4 | 2016-04-18 11:53:42 +0000 | [diff] [blame] | 727 | case R_X86_64_DTPOFF32: |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 728 | case R_X86_64_SIZE32: |
| Rafael Espindola | fb0ceb5 | 2016-05-20 20:02:27 +0000 | [diff] [blame] | 729 | checkInt<32>(Val, Type); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 730 | write32le(Loc, Val); |
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 731 | break; |
| Rui Ueyama | e66f45c | 2016-05-25 04:10:14 +0000 | [diff] [blame] | 732 | case R_X86_64_64: |
| 733 | case R_X86_64_DTPOFF64: |
| 734 | case R_X86_64_SIZE64: |
| 735 | case R_X86_64_PC64: |
| 736 | write64le(Loc, Val); |
| 737 | break; |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 738 | default: |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 739 | fatal("unrecognized reloc " + Twine(Type)); |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 743 | RelExpr X86_64TargetInfo::adjustRelaxGotExpr(uint32_t Type, const uint8_t *Data, |
| 744 | RelExpr RelExpr) const { |
| George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 745 | if (Type != R_X86_64_GOTPCRELX && Type != R_X86_64_REX_GOTPCRELX) |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 746 | return RelExpr; |
| George Rimar | a8f9cf1 | 2016-05-26 13:37:12 +0000 | [diff] [blame] | 747 | const uint8_t Op = Data[-2]; |
| 748 | const uint8_t ModRm = Data[-1]; |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 749 | // FIXME: When PIC is disabled and foo is defined locally in the |
| 750 | // lower 32 bit address space, memory operand in mov can be converted into |
| 751 | // immediate operand. Otherwise, mov must be changed to lea. We support only |
| 752 | // latter relaxation at this moment. |
| George Rimar | 95433df | 2016-05-25 16:51:08 +0000 | [diff] [blame] | 753 | if (Op == 0x8b) |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 754 | return R_RELAX_GOT_PC; |
| George Rimar | 95433df | 2016-05-25 16:51:08 +0000 | [diff] [blame] | 755 | // Relax call and jmp. |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 756 | if (Op == 0xff && (ModRm == 0x15 || ModRm == 0x25)) |
| 757 | return R_RELAX_GOT_PC; |
| 758 | |
| 759 | // Relaxation of test, adc, add, and, cmp, or, sbb, sub, xor. |
| 760 | // If PIC then no relaxation is available. |
| 761 | // We also don't relax test/binop instructions without REX byte, |
| 762 | // they are 32bit operations and not common to have. |
| 763 | assert(Type == R_X86_64_REX_GOTPCRELX); |
| 764 | return Config->Pic ? RelExpr : R_RELAX_GOT_PC_NOPIC; |
| George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| George Rimar | b720430 | 2016-06-02 09:22:00 +0000 | [diff] [blame] | 767 | // A subset of relaxations can only be applied for no-PIC. This method |
| 768 | // handles such relaxations. Instructions encoding information was taken from: |
| 769 | // "Intel 64 and IA-32 Architectures Software Developer's Manual V2" |
| 770 | // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/ |
| 771 | // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf) |
| 772 | void X86_64TargetInfo::relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op, |
| 773 | uint8_t ModRm) const { |
| George Rimar | f10c829 | 2016-06-01 16:45:30 +0000 | [diff] [blame] | 774 | const uint8_t Rex = Loc[-3]; |
| 775 | // Convert "test %reg, foo@GOTPCREL(%rip)" to "test $foo, %reg". |
| 776 | if (Op == 0x85) { |
| 777 | // See "TEST-Logical Compare" (4-428 Vol. 2B), |
| 778 | // TEST r/m64, r64 uses "full" ModR / M byte (no opcode extension). |
| 779 | |
| 780 | // ModR/M byte has form XX YYY ZZZ, where |
| 781 | // YYY is MODRM.reg(register 2), ZZZ is MODRM.rm(register 1). |
| 782 | // XX has different meanings: |
| 783 | // 00: The operand's memory address is in reg1. |
| 784 | // 01: The operand's memory address is reg1 + a byte-sized displacement. |
| 785 | // 10: The operand's memory address is reg1 + a word-sized displacement. |
| 786 | // 11: The operand is reg1 itself. |
| 787 | // If an instruction requires only one operand, the unused reg2 field |
| 788 | // holds extra opcode bits rather than a register code |
| 789 | // 0xC0 == 11 000 000 binary. |
| 790 | // 0x38 == 00 111 000 binary. |
| 791 | // We transfer reg2 to reg1 here as operand. |
| 792 | // See "2.1.3 ModR/M and SIB Bytes" (Vol. 2A 2-3). |
| 793 | *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3; // ModR/M byte. |
| 794 | |
| 795 | // Change opcode from TEST r/m64, r64 to TEST r/m64, imm32 |
| 796 | // See "TEST-Logical Compare" (4-428 Vol. 2B). |
| 797 | *(Loc - 2) = 0xf7; |
| 798 | |
| 799 | // Move R bit to the B bit in REX byte. |
| 800 | // REX byte is encoded as 0100WRXB, where |
| 801 | // 0100 is 4bit fixed pattern. |
| 802 | // REX.W When 1, a 64-bit operand size is used. Otherwise, when 0, the |
| 803 | // default operand size is used (which is 32-bit for most but not all |
| 804 | // instructions). |
| 805 | // REX.R This 1-bit value is an extension to the MODRM.reg field. |
| 806 | // REX.X This 1-bit value is an extension to the SIB.index field. |
| 807 | // REX.B This 1-bit value is an extension to the MODRM.rm field or the |
| 808 | // SIB.base field. |
| 809 | // See "2.2.1.2 More on REX Prefix Fields " (2-8 Vol. 2A). |
| 810 | *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2; |
| 811 | relocateOne(Loc, R_X86_64_PC32, Val); |
| 812 | return; |
| 813 | } |
| 814 | |
| 815 | // If we are here then we need to relax the adc, add, and, cmp, or, sbb, sub |
| 816 | // or xor operations. |
| 817 | |
| 818 | // Convert "binop foo@GOTPCREL(%rip), %reg" to "binop $foo, %reg". |
| 819 | // Logic is close to one for test instruction above, but we also |
| 820 | // write opcode extension here, see below for details. |
| 821 | *(Loc - 1) = 0xc0 | (ModRm & 0x38) >> 3 | (Op & 0x3c); // ModR/M byte. |
| 822 | |
| 823 | // Primary opcode is 0x81, opcode extension is one of: |
| 824 | // 000b = ADD, 001b is OR, 010b is ADC, 011b is SBB, |
| 825 | // 100b is AND, 101b is SUB, 110b is XOR, 111b is CMP. |
| 826 | // This value was wrote to MODRM.reg in a line above. |
| 827 | // See "3.2 INSTRUCTIONS (A-M)" (Vol. 2A 3-15), |
| 828 | // "INSTRUCTION SET REFERENCE, N-Z" (Vol. 2B 4-1) for |
| 829 | // descriptions about each operation. |
| 830 | *(Loc - 2) = 0x81; |
| 831 | *(Loc - 3) = (Rex & ~0x4) | (Rex & 0x4) >> 2; |
| George Rimar | 5c33b91 | 2016-05-25 14:31:37 +0000 | [diff] [blame] | 832 | relocateOne(Loc, R_X86_64_PC32, Val); |
| 833 | } |
| 834 | |
| George Rimar | b720430 | 2016-06-02 09:22:00 +0000 | [diff] [blame] | 835 | void X86_64TargetInfo::relaxGot(uint8_t *Loc, uint64_t Val) const { |
| 836 | const uint8_t Op = Loc[-2]; |
| 837 | const uint8_t ModRm = Loc[-1]; |
| 838 | |
| 839 | // Convert mov foo@GOTPCREL(%rip), %reg to lea foo(%rip), %reg. |
| 840 | if (Op == 0x8b) { |
| 841 | *(Loc - 2) = 0x8d; |
| 842 | relocateOne(Loc, R_X86_64_PC32, Val); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | // Convert call/jmp instructions. |
| 847 | if (Op == 0xff) { |
| 848 | if (ModRm == 0x15) { |
| 849 | // ABI says we can convert call *foo@GOTPCREL(%rip) to nop call foo. |
| 850 | // Instead we convert to addr32 call foo, where addr32 is instruction |
| 851 | // prefix. That makes result expression to be a single instruction. |
| 852 | *(Loc - 2) = 0x67; // addr32 prefix |
| 853 | *(Loc - 1) = 0xe8; // call |
| 854 | } else { |
| 855 | assert(ModRm == 0x25); |
| 856 | // Convert jmp *foo@GOTPCREL(%rip) to jmp foo nop. |
| 857 | // jmp doesn't return, so it is fine to use nop here, it is just a stub. |
| 858 | *(Loc - 2) = 0xe9; // jmp |
| 859 | *(Loc + 3) = 0x90; // nop |
| 860 | Loc -= 1; |
| 861 | Val += 1; |
| 862 | } |
| 863 | relocateOne(Loc, R_X86_64_PC32, Val); |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | assert(!Config->Pic); |
| 868 | // We are relaxing a rip relative to an absolute, so compensate |
| 869 | // for the old -4 addend. |
| 870 | relaxGotNoPic(Loc, Val + 4, Op, ModRm); |
| 871 | } |
| 872 | |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 873 | // Relocation masks following the #lo(value), #hi(value), #ha(value), |
| 874 | // #higher(value), #highera(value), #highest(value), and #highesta(value) |
| 875 | // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi |
| 876 | // document. |
| Rui Ueyama | c44e5a1 | 2015-10-23 16:54:58 +0000 | [diff] [blame] | 877 | static uint16_t applyPPCLo(uint64_t V) { return V; } |
| 878 | static uint16_t applyPPCHi(uint64_t V) { return V >> 16; } |
| 879 | static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; } |
| 880 | static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; } |
| 881 | static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; } |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 882 | static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; } |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 883 | static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; } |
| 884 | |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 885 | PPCTargetInfo::PPCTargetInfo() {} |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 886 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 887 | void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, |
| 888 | uint64_t Val) const { |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 889 | switch (Type) { |
| 890 | case R_PPC_ADDR16_HA: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 891 | write16be(Loc, applyPPCHa(Val)); |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 892 | break; |
| 893 | case R_PPC_ADDR16_LO: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 894 | write16be(Loc, applyPPCLo(Val)); |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 895 | break; |
| 896 | default: |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 897 | fatal("unrecognized reloc " + Twine(Type)); |
| Davide Italiano | 8c344436 | 2016-01-11 19:45:33 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 901 | RelExpr PPCTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { |
| 902 | return R_ABS; |
| 903 | } |
| 904 | |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 905 | PPC64TargetInfo::PPC64TargetInfo() { |
| Rafael Espindola | e4c86d83 | 2016-05-18 21:03:36 +0000 | [diff] [blame] | 906 | PltRel = GotRel = R_PPC64_GLOB_DAT; |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 907 | RelativeRel = R_PPC64_RELATIVE; |
| Hal Finkel | 6c2a3b8 | 2015-10-08 21:51:31 +0000 | [diff] [blame] | 908 | PltEntrySize = 32; |
| Hal Finkel | c848b32 | 2015-10-12 19:34:29 +0000 | [diff] [blame] | 909 | |
| 910 | // We need 64K pages (at least under glibc/Linux, the loader won't |
| 911 | // set different permissions on a finer granularity than that). |
| Hal Finkel | e3c2626 | 2015-10-08 22:23:54 +0000 | [diff] [blame] | 912 | PageSize = 65536; |
| Hal Finkel | 736c741 | 2015-10-15 07:49:07 +0000 | [diff] [blame] | 913 | |
| 914 | // The PPC64 ELF ABI v1 spec, says: |
| 915 | // |
| 916 | // It is normally desirable to put segments with different characteristics |
| 917 | // in separate 256 Mbyte portions of the address space, to give the |
| 918 | // operating system full paging flexibility in the 64-bit address space. |
| 919 | // |
| 920 | // And because the lowest non-zero 256M boundary is 0x10000000, PPC64 linkers |
| 921 | // use 0x10000000 as the starting address. |
| 922 | VAStart = 0x10000000; |
| Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 923 | } |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 924 | |
| Rafael Espindola | 15cec29 | 2016-04-27 12:25:22 +0000 | [diff] [blame] | 925 | static uint64_t PPC64TocOffset = 0x8000; |
| 926 | |
| Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 927 | uint64_t getPPC64TocBase() { |
| Rafael Espindola | 520ed3a | 2016-04-27 12:21:27 +0000 | [diff] [blame] | 928 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The |
| 929 | // TOC starts where the first of these sections starts. We always create a |
| 930 | // .got when we see a relocation that uses it, so for us the start is always |
| 931 | // the .got. |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 932 | uint64_t TocVA = Out<ELF64BE>::Got->getVA(); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 933 | |
| 934 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 935 | // thus permitting a full 64 Kbytes segment. Note that the glibc startup |
| 936 | // code (crt1.o) assumes that you can get from the TOC base to the |
| 937 | // start of the .toc section with only a single (signed) 16-bit relocation. |
| Rafael Espindola | 15cec29 | 2016-04-27 12:25:22 +0000 | [diff] [blame] | 938 | return TocVA + PPC64TocOffset; |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 941 | RelExpr PPC64TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { |
| 942 | switch (Type) { |
| 943 | default: |
| 944 | return R_ABS; |
| Rafael Espindola | 15cec29 | 2016-04-27 12:25:22 +0000 | [diff] [blame] | 945 | case R_PPC64_TOC16: |
| 946 | case R_PPC64_TOC16_DS: |
| 947 | case R_PPC64_TOC16_HA: |
| 948 | case R_PPC64_TOC16_HI: |
| 949 | case R_PPC64_TOC16_LO: |
| 950 | case R_PPC64_TOC16_LO_DS: |
| 951 | return R_GOTREL; |
| Rafael Espindola | 365e5f6 | 2016-04-27 11:54:07 +0000 | [diff] [blame] | 952 | case R_PPC64_TOC: |
| 953 | return R_PPC_TOC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 954 | case R_PPC64_REL24: |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 955 | return R_PPC_PLT_OPD; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 959 | void PPC64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, |
| 960 | uint64_t PltEntryAddr, int32_t Index, |
| 961 | unsigned RelOff) const { |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 962 | uint64_t Off = GotEntryAddr - getPPC64TocBase(); |
| 963 | |
| 964 | // FIXME: What we should do, in theory, is get the offset of the function |
| 965 | // descriptor in the .opd section, and use that as the offset from %r2 (the |
| 966 | // TOC-base pointer). Instead, we have the GOT-entry offset, and that will |
| 967 | // be a pointer to the function descriptor in the .opd section. Using |
| 968 | // this scheme is simpler, but requires an extra indirection per PLT dispatch. |
| 969 | |
| Hal Finkel | fa92f68 | 2015-10-13 21:47:34 +0000 | [diff] [blame] | 970 | write32be(Buf, 0xf8410028); // std %r2, 40(%r1) |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 971 | write32be(Buf + 4, 0x3d620000 | applyPPCHa(Off)); // addis %r11, %r2, X@ha |
| 972 | write32be(Buf + 8, 0xe98b0000 | applyPPCLo(Off)); // ld %r12, X@l(%r11) |
| 973 | write32be(Buf + 12, 0xe96c0000); // ld %r11,0(%r12) |
| 974 | write32be(Buf + 16, 0x7d6903a6); // mtctr %r11 |
| 975 | write32be(Buf + 20, 0xe84c0008); // ld %r2,8(%r12) |
| 976 | write32be(Buf + 24, 0xe96c0010); // ld %r11,16(%r12) |
| 977 | write32be(Buf + 28, 0x4e800420); // bctr |
| 978 | } |
| 979 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 980 | void PPC64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, |
| 981 | uint64_t Val) const { |
| Rafael Espindola | 15cec29 | 2016-04-27 12:25:22 +0000 | [diff] [blame] | 982 | uint64_t TO = PPC64TocOffset; |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 983 | |
| Rafael Espindola | 15cec29 | 2016-04-27 12:25:22 +0000 | [diff] [blame] | 984 | // For a TOC-relative relocation, proceed in terms of the corresponding |
| 985 | // ADDR16 relocation type. |
| Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 986 | switch (Type) { |
| Rafael Espindola | 15cec29 | 2016-04-27 12:25:22 +0000 | [diff] [blame] | 987 | case R_PPC64_TOC16: Type = R_PPC64_ADDR16; Val -= TO; break; |
| 988 | case R_PPC64_TOC16_DS: Type = R_PPC64_ADDR16_DS; Val -= TO; break; |
| 989 | case R_PPC64_TOC16_HA: Type = R_PPC64_ADDR16_HA; Val -= TO; break; |
| 990 | case R_PPC64_TOC16_HI: Type = R_PPC64_ADDR16_HI; Val -= TO; break; |
| 991 | case R_PPC64_TOC16_LO: Type = R_PPC64_ADDR16_LO; Val -= TO; break; |
| 992 | case R_PPC64_TOC16_LO_DS: Type = R_PPC64_ADDR16_LO_DS; Val -= TO; break; |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 993 | default: break; |
| 994 | } |
| 995 | |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 996 | switch (Type) { |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 997 | case R_PPC64_ADDR14: { |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 998 | checkAlignment<4>(Val, Type); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 999 | // Preserve the AA/LK bits in the branch instruction |
| 1000 | uint8_t AALK = Loc[3]; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1001 | write16be(Loc + 2, (AALK & 3) | (Val & 0xfffc)); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1004 | case R_PPC64_ADDR16: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1005 | checkInt<16>(Val, Type); |
| 1006 | write16be(Loc, Val); |
| Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 1007 | break; |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1008 | case R_PPC64_ADDR16_DS: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1009 | checkInt<16>(Val, Type); |
| 1010 | write16be(Loc, (read16be(Loc) & 3) | (Val & ~3)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1011 | break; |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1012 | case R_PPC64_ADDR16_HA: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1013 | write16be(Loc, applyPPCHa(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1014 | break; |
| 1015 | case R_PPC64_ADDR16_HI: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1016 | write16be(Loc, applyPPCHi(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1017 | break; |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1018 | case R_PPC64_ADDR16_HIGHER: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1019 | write16be(Loc, applyPPCHigher(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1020 | break; |
| 1021 | case R_PPC64_ADDR16_HIGHERA: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1022 | write16be(Loc, applyPPCHighera(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1023 | break; |
| 1024 | case R_PPC64_ADDR16_HIGHEST: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1025 | write16be(Loc, applyPPCHighest(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1026 | break; |
| 1027 | case R_PPC64_ADDR16_HIGHESTA: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1028 | write16be(Loc, applyPPCHighesta(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1029 | break; |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1030 | case R_PPC64_ADDR16_LO: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1031 | write16be(Loc, applyPPCLo(Val)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1032 | break; |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1033 | case R_PPC64_ADDR16_LO_DS: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1034 | write16be(Loc, (read16be(Loc) & 3) | (applyPPCLo(Val) & ~3)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | case R_PPC64_ADDR32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1037 | checkInt<32>(Val, Type); |
| 1038 | write32be(Loc, Val); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1039 | break; |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1040 | case R_PPC64_ADDR64: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1041 | write64be(Loc, Val); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1042 | break; |
| 1043 | case R_PPC64_REL16_HA: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1044 | write16be(Loc, applyPPCHa(Val)); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | case R_PPC64_REL16_HI: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1047 | write16be(Loc, applyPPCHi(Val)); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1048 | break; |
| 1049 | case R_PPC64_REL16_LO: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1050 | write16be(Loc, applyPPCLo(Val)); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1051 | break; |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1052 | case R_PPC64_REL24: { |
| 1053 | uint32_t Mask = 0x03FFFFFC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1054 | checkInt<24>(Val, Type); |
| 1055 | write32be(Loc, (read32be(Loc) & ~Mask) | (Val & Mask)); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | } |
| 1058 | case R_PPC64_REL32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1059 | checkInt<32>(Val, Type); |
| 1060 | write32be(Loc, Val); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1061 | break; |
| 1062 | case R_PPC64_REL64: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1063 | write64be(Loc, Val); |
| Hal Finkel | 3c8cc67 | 2015-10-12 20:56:18 +0000 | [diff] [blame] | 1064 | break; |
| Hal Finkel | 6f97c2b | 2015-10-16 21:55:40 +0000 | [diff] [blame] | 1065 | case R_PPC64_TOC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1066 | write64be(Loc, Val); |
| Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | default: |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 1069 | fatal("unrecognized reloc " + Twine(Type)); |
| Rafael Espindola | 3efa4e9 | 2015-09-22 21:12:55 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| Rafael Espindola | 1d6063e | 2015-09-22 21:24:52 +0000 | [diff] [blame] | 1072 | |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1073 | AArch64TargetInfo::AArch64TargetInfo() { |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 1074 | CopyRel = R_AARCH64_COPY; |
| Adhemerval Zanella | 668ad0f | 2016-02-23 16:54:40 +0000 | [diff] [blame] | 1075 | RelativeRel = R_AARCH64_RELATIVE; |
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 1076 | IRelativeRel = R_AARCH64_IRELATIVE; |
| 1077 | GotRel = R_AARCH64_GLOB_DAT; |
| 1078 | PltRel = R_AARCH64_JUMP_SLOT; |
| 1079 | TlsGotRel = R_AARCH64_TLS_TPREL64; |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1080 | PltEntrySize = 16; |
| Rui Ueyama | 6251545 | 2016-01-29 03:00:32 +0000 | [diff] [blame] | 1081 | PltZeroSize = 32; |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1082 | |
| 1083 | // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant |
| 1084 | // 1 of the tls structures and the tcb size is 16. |
| 1085 | TcbSize = 16; |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1086 | } |
| George Rimar | 648a2c3 | 2015-10-20 08:54:27 +0000 | [diff] [blame] | 1087 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1088 | RelExpr AArch64TargetInfo::getRelExpr(uint32_t Type, |
| 1089 | const SymbolBody &S) const { |
| 1090 | switch (Type) { |
| 1091 | default: |
| 1092 | return R_ABS; |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1093 | |
| 1094 | case R_AARCH64_TLSLE_ADD_TPREL_HI12: |
| 1095 | case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: |
| 1096 | return R_TLS; |
| 1097 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1098 | case R_AARCH64_CALL26: |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 1099 | case R_AARCH64_CONDBR19: |
| 1100 | case R_AARCH64_JUMP26: |
| 1101 | case R_AARCH64_TSTBR14: |
| 1102 | return R_PLT_PC; |
| 1103 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1104 | case R_AARCH64_PREL16: |
| 1105 | case R_AARCH64_PREL32: |
| 1106 | case R_AARCH64_PREL64: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1107 | case R_AARCH64_ADR_PREL_LO21: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1108 | return R_PC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1109 | case R_AARCH64_ADR_PREL_PG_HI21: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1110 | return R_PAGE_PC; |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 1111 | case R_AARCH64_LD64_GOT_LO12_NC: |
| 1112 | case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: |
| 1113 | return R_GOT; |
| 1114 | case R_AARCH64_ADR_GOT_PAGE: |
| 1115 | case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: |
| 1116 | return R_GOT_PAGE_PC; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
| Rafael Espindola | b8ff59a | 2016-04-28 14:34:39 +0000 | [diff] [blame] | 1120 | bool AArch64TargetInfo::usesOnlyLowPageBits(uint32_t Type) const { |
| Adhemerval Zanella | 3db3f6d | 2016-03-24 19:12:14 +0000 | [diff] [blame] | 1121 | switch (Type) { |
| 1122 | default: |
| 1123 | return false; |
| Ed Schouten | 39aca42 | 2016-04-06 18:21:07 +0000 | [diff] [blame] | 1124 | case R_AARCH64_ADD_ABS_LO12_NC: |
| Rafael Espindola | 53d0a9f | 2016-06-02 15:24:52 +0000 | [diff] [blame^] | 1125 | case R_AARCH64_LD64_GOT_LO12_NC: |
| 1126 | case R_AARCH64_LDST128_ABS_LO12_NC: |
| Adhemerval Zanella | 3db3f6d | 2016-03-24 19:12:14 +0000 | [diff] [blame] | 1127 | case R_AARCH64_LDST16_ABS_LO12_NC: |
| 1128 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| 1129 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| Rafael Espindola | 53d0a9f | 2016-06-02 15:24:52 +0000 | [diff] [blame^] | 1130 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| Rafael Espindola | de17d28 | 2016-05-04 21:40:07 +0000 | [diff] [blame] | 1131 | case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: |
| Adhemerval Zanella | 3db3f6d | 2016-03-24 19:12:14 +0000 | [diff] [blame] | 1132 | return true; |
| 1133 | } |
| Rafael Espindola | a4e35f7 | 2016-02-24 16:15:13 +0000 | [diff] [blame] | 1134 | } |
| Rafael Espindola | 435c00f | 2016-02-23 20:19:44 +0000 | [diff] [blame] | 1135 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 1136 | bool AArch64TargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const { |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1137 | return Type == R_AARCH64_TLSDESC_ADR_PAGE21 || |
| 1138 | Type == R_AARCH64_TLSDESC_LD64_LO12_NC || |
| 1139 | Type == R_AARCH64_TLSDESC_ADD_LO12_NC || |
| 1140 | Type == R_AARCH64_TLSDESC_CALL; |
| 1141 | } |
| 1142 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 1143 | bool AArch64TargetInfo::isTlsInitialExecRel(uint32_t Type) const { |
| Rafael Espindola | d405f47 | 2016-03-04 21:37:09 +0000 | [diff] [blame] | 1144 | return Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21 || |
| 1145 | Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC; |
| 1146 | } |
| 1147 | |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 1148 | uint32_t AArch64TargetInfo::getDynRel(uint32_t Type) const { |
| Igor Kudrin | cfe47f5 | 2015-12-05 06:20:24 +0000 | [diff] [blame] | 1149 | if (Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64) |
| 1150 | return Type; |
| 1151 | StringRef S = getELFRelocationTypeName(EM_AARCH64, Type); |
| George Rimar | ca1d1fb | 2016-03-15 14:00:22 +0000 | [diff] [blame] | 1152 | error("relocation " + S + " cannot be used when making a shared object; " |
| Igor Kudrin | cfe47f5 | 2015-12-05 06:20:24 +0000 | [diff] [blame] | 1153 | "recompile with -fPIC."); |
| Rui Ueyama | 2192399 | 2016-02-01 23:28:21 +0000 | [diff] [blame] | 1154 | // Keep it going with a dummy value so that we can find more reloc errors. |
| 1155 | return R_AARCH64_ABS32; |
| Igor Kudrin | cfe47f5 | 2015-12-05 06:20:24 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 1158 | void AArch64TargetInfo::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1159 | write64le(Buf, Out<ELF64LE>::Plt->getVA()); |
| 1160 | } |
| 1161 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1162 | static uint64_t getAArch64Page(uint64_t Expr) { |
| 1163 | return Expr & (~static_cast<uint64_t>(0xFFF)); |
| 1164 | } |
| 1165 | |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 1166 | void AArch64TargetInfo::writePltZero(uint8_t *Buf) const { |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1167 | const uint8_t PltData[] = { |
| 1168 | 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! |
| 1169 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) |
| 1170 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] |
| 1171 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) |
| 1172 | 0x20, 0x02, 0x1f, 0xd6, // br x17 |
| 1173 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 1174 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 1175 | 0x1f, 0x20, 0x03, 0xd5 // nop |
| 1176 | }; |
| 1177 | memcpy(Buf, PltData, sizeof(PltData)); |
| 1178 | |
| Rui Ueyama | 900e2d2 | 2016-01-29 03:51:49 +0000 | [diff] [blame] | 1179 | uint64_t Got = Out<ELF64LE>::GotPlt->getVA(); |
| 1180 | uint64_t Plt = Out<ELF64LE>::Plt->getVA(); |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1181 | relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, |
| 1182 | getAArch64Page(Got + 16) - getAArch64Page(Plt + 4)); |
| 1183 | relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16); |
| 1184 | relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16); |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| Rui Ueyama | 9398f86 | 2016-01-29 04:15:02 +0000 | [diff] [blame] | 1187 | void AArch64TargetInfo::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, |
| 1188 | uint64_t PltEntryAddr, int32_t Index, |
| 1189 | unsigned RelOff) const { |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1190 | const uint8_t Inst[] = { |
| 1191 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) |
| 1192 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] |
| 1193 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) |
| 1194 | 0x20, 0x02, 0x1f, 0xd6 // br x17 |
| 1195 | }; |
| 1196 | memcpy(Buf, Inst, sizeof(Inst)); |
| 1197 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1198 | relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21, |
| 1199 | getAArch64Page(GotEntryAddr) - getAArch64Page(PltEntryAddr)); |
| 1200 | relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotEntryAddr); |
| 1201 | relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotEntryAddr); |
| Igor Kudrin | db7de9f | 2015-11-17 18:01:30 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1204 | static void updateAArch64Addr(uint8_t *L, uint64_t Imm) { |
| Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 1205 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 1206 | uint32_t ImmHi = ((Imm & 0x1FFFFC) >> 2) << 5; |
| 1207 | uint64_t Mask = (0x3 << 29) | (0x7FFFF << 5); |
| Rui Ueyama | 87bc41b | 2015-10-06 18:54:43 +0000 | [diff] [blame] | 1208 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
| Davide Italiano | 1f31a2c | 2015-10-02 22:00:42 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1211 | static inline void updateAArch64Add(uint8_t *L, uint64_t Imm) { |
| 1212 | or32le(L, (Imm & 0xFFF) << 10); |
| 1213 | } |
| 1214 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1215 | void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, |
| 1216 | uint64_t Val) const { |
| Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1217 | switch (Type) { |
| Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 1218 | case R_AARCH64_ABS16: |
| Rui Ueyama | e66f45c | 2016-05-25 04:10:14 +0000 | [diff] [blame] | 1219 | case R_AARCH64_PREL16: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1220 | checkIntUInt<16>(Val, Type); |
| 1221 | write16le(Loc, Val); |
| Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 1222 | break; |
| 1223 | case R_AARCH64_ABS32: |
| Rui Ueyama | e66f45c | 2016-05-25 04:10:14 +0000 | [diff] [blame] | 1224 | case R_AARCH64_PREL32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1225 | checkIntUInt<32>(Val, Type); |
| 1226 | write32le(Loc, Val); |
| Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 1227 | break; |
| 1228 | case R_AARCH64_ABS64: |
| Rui Ueyama | e66f45c | 2016-05-25 04:10:14 +0000 | [diff] [blame] | 1229 | case R_AARCH64_PREL64: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1230 | write64le(Loc, Val); |
| Davide Italiano | df88f96 | 2015-10-04 00:59:16 +0000 | [diff] [blame] | 1231 | break; |
| Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 1232 | case R_AARCH64_ADD_ABS_LO12_NC: |
| Davide Italiano | a716574 | 2015-10-16 21:06:55 +0000 | [diff] [blame] | 1233 | // This relocation stores 12 bits and there's no instruction |
| 1234 | // 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] | 1235 | // of r_addend bitwise-or'ed Loc. This assumes that the addend |
| 1236 | // bits in Loc are zero. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1237 | or32le(Loc, (Val & 0xFFF) << 10); |
| Davide Italiano | 0b6974b | 2015-10-03 19:56:07 +0000 | [diff] [blame] | 1238 | break; |
| Rafael Espindola | d79073d | 2016-04-25 12:32:19 +0000 | [diff] [blame] | 1239 | case R_AARCH64_ADR_GOT_PAGE: |
| Rui Ueyama | e66f45c | 2016-05-25 04:10:14 +0000 | [diff] [blame] | 1240 | case R_AARCH64_ADR_PREL_PG_HI21: |
| 1241 | case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: |
| Rafael Espindola | d79073d | 2016-04-25 12:32:19 +0000 | [diff] [blame] | 1242 | checkInt<33>(Val, Type); |
| 1243 | updateAArch64Addr(Loc, (Val >> 12) & 0x1FFFFF); // X[32:12] |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1244 | break; |
| Rafael Espindola | d79073d | 2016-04-25 12:32:19 +0000 | [diff] [blame] | 1245 | case R_AARCH64_ADR_PREL_LO21: |
| 1246 | checkInt<21>(Val, Type); |
| 1247 | updateAArch64Addr(Loc, Val & 0x1FFFFF); |
| Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1248 | break; |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1249 | case R_AARCH64_CALL26: |
| Rafael Espindola | d79073d | 2016-04-25 12:32:19 +0000 | [diff] [blame] | 1250 | case R_AARCH64_JUMP26: |
| 1251 | checkInt<28>(Val, Type); |
| 1252 | or32le(Loc, (Val & 0x0FFFFFFC) >> 2); |
| Igor Kudrin | b34115b | 2015-11-13 03:26:59 +0000 | [diff] [blame] | 1253 | break; |
| Rafael Espindola | d79073d | 2016-04-25 12:32:19 +0000 | [diff] [blame] | 1254 | case R_AARCH64_CONDBR19: |
| 1255 | checkInt<21>(Val, Type); |
| 1256 | or32le(Loc, (Val & 0x1FFFFC) << 3); |
| George Rimar | 4102bfb | 2016-01-11 14:22:00 +0000 | [diff] [blame] | 1257 | break; |
| Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 1258 | case R_AARCH64_LD64_GOT_LO12_NC: |
| George Rimar | 3d737e4 | 2016-01-13 13:04:46 +0000 | [diff] [blame] | 1259 | case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1260 | checkAlignment<8>(Val, Type); |
| 1261 | or32le(Loc, (Val & 0xFF8) << 7); |
| Igor Kudrin | 5d2bffd | 2015-11-24 06:48:31 +0000 | [diff] [blame] | 1262 | break; |
| Davide Italiano | 0d4fbae | 2016-01-14 01:30:21 +0000 | [diff] [blame] | 1263 | case R_AARCH64_LDST128_ABS_LO12_NC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1264 | or32le(Loc, (Val & 0x0FF8) << 6); |
| Davide Italiano | 0d4fbae | 2016-01-14 01:30:21 +0000 | [diff] [blame] | 1265 | break; |
| Davide Italiano | 2dfc5fd | 2016-01-15 01:49:51 +0000 | [diff] [blame] | 1266 | case R_AARCH64_LDST16_ABS_LO12_NC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1267 | or32le(Loc, (Val & 0x0FFC) << 9); |
| Davide Italiano | 2dfc5fd | 2016-01-15 01:49:51 +0000 | [diff] [blame] | 1268 | break; |
| Davide Italiano | dc67f9b | 2015-11-20 21:35:38 +0000 | [diff] [blame] | 1269 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1270 | or32le(Loc, (Val & 0xFFF) << 10); |
| Davide Italiano | dc67f9b | 2015-11-20 21:35:38 +0000 | [diff] [blame] | 1271 | break; |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1272 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1273 | or32le(Loc, (Val & 0xFFC) << 8); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1274 | break; |
| 1275 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1276 | or32le(Loc, (Val & 0xFF8) << 7); |
| Igor Kudrin | b4a0927 | 2015-12-01 08:41:20 +0000 | [diff] [blame] | 1277 | break; |
| Rafael Espindola | d79073d | 2016-04-25 12:32:19 +0000 | [diff] [blame] | 1278 | case R_AARCH64_TSTBR14: |
| 1279 | checkInt<16>(Val, Type); |
| 1280 | or32le(Loc, (Val & 0xFFFC) << 3); |
| George Rimar | 1395dbd | 2016-01-11 14:27:05 +0000 | [diff] [blame] | 1281 | break; |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1282 | case R_AARCH64_TLSLE_ADD_TPREL_HI12: |
| 1283 | checkInt<24>(Val, Type); |
| 1284 | updateAArch64Add(Loc, (Val & 0xFFF000) >> 12); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1285 | break; |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1286 | case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: |
| 1287 | updateAArch64Add(Loc, Val & 0xFFF); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1288 | break; |
| Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1289 | default: |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 1290 | fatal("unrecognized reloc " + Twine(Type)); |
| Davide Italiano | 1d750a6 | 2015-09-27 08:45:38 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1293 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1294 | void AArch64TargetInfo::relaxTlsGdToLe(uint8_t *Loc, uint32_t Type, |
| 1295 | uint64_t Val) const { |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1296 | // TLSDESC Global-Dynamic relocation are in the form: |
| 1297 | // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] |
| 1298 | // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12_NC] |
| 1299 | // add x0, x0, :tlsdesc_los:v [_AARCH64_TLSDESC_ADD_LO12_NC] |
| 1300 | // .tlsdesccall [R_AARCH64_TLSDESC_CALL] |
| 1301 | // And it can optimized to: |
| 1302 | // movz x0, #0x0, lsl #16 |
| 1303 | // movk x0, #0x10 |
| 1304 | // nop |
| 1305 | // nop |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1306 | checkUInt<32>(Val, Type); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1307 | |
| 1308 | uint32_t NewInst; |
| 1309 | switch (Type) { |
| 1310 | case R_AARCH64_TLSDESC_ADD_LO12_NC: |
| 1311 | case R_AARCH64_TLSDESC_CALL: |
| 1312 | // nop |
| 1313 | NewInst = 0xd503201f; |
| 1314 | break; |
| 1315 | case R_AARCH64_TLSDESC_ADR_PAGE21: |
| 1316 | // movz |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1317 | NewInst = 0xd2a00000 | (((Val >> 16) & 0xffff) << 5); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | case R_AARCH64_TLSDESC_LD64_LO12_NC: |
| 1320 | // movk |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1321 | NewInst = 0xf2800000 | ((Val & 0xffff) << 5); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1322 | break; |
| 1323 | default: |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 1324 | llvm_unreachable("unsupported Relocation for TLS GD to LE relax"); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1325 | } |
| 1326 | write32le(Loc, NewInst); |
| 1327 | } |
| 1328 | |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1329 | void AArch64TargetInfo::relaxTlsIeToLe(uint8_t *Loc, uint32_t Type, |
| 1330 | uint64_t Val) const { |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1331 | checkUInt<32>(Val, Type); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1332 | |
| George Rimar | 4d1d16d | 2016-03-06 06:16:05 +0000 | [diff] [blame] | 1333 | uint32_t Inst = read32le(Loc); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1334 | uint32_t NewInst; |
| 1335 | if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { |
| 1336 | // Generate movz. |
| 1337 | unsigned RegNo = (Inst & 0x1f); |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1338 | NewInst = (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1339 | } else if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { |
| 1340 | // Generate movk |
| 1341 | unsigned RegNo = (Inst & 0x1f); |
| Rafael Espindola | 8818ca6 | 2016-05-20 17:41:09 +0000 | [diff] [blame] | 1342 | NewInst = (0xf2800000 | RegNo) | ((Val & 0xffff) << 5); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1343 | } else { |
| George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 1344 | llvm_unreachable("invalid Relocation for TLS IE to LE Relax"); |
| Adhemerval Zanella | 74bcf03 | 2016-02-12 13:43:03 +0000 | [diff] [blame] | 1345 | } |
| 1346 | write32le(Loc, NewInst); |
| 1347 | } |
| 1348 | |
| Rui Ueyama | 1300e6b | 2016-01-07 20:34:16 +0000 | [diff] [blame] | 1349 | // Implementing relocations for AMDGPU is low priority since most |
| 1350 | // programs don't use relocations now. Thus, this function is not |
| 1351 | // actually called (relocateOne is called for each relocation). |
| 1352 | // That's why the AMDGPU port works without implementing this function. |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1353 | void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, |
| 1354 | uint64_t Val) const { |
| 1355 | llvm_unreachable("not implemented"); |
| 1356 | } |
| 1357 | |
| 1358 | RelExpr AMDGPUTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 1359 | llvm_unreachable("not implemented"); |
| Tom Stellard | 80efb16 | 2016-01-07 03:59:08 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| Simon Atanasyan | 9c2d788 | 2015-10-14 14:24:46 +0000 | [diff] [blame] | 1362 | template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() { |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1363 | GotPltHeaderEntriesNum = 2; |
| Simon Atanasyan | eae66c0 | 2016-02-10 10:08:39 +0000 | [diff] [blame] | 1364 | PageSize = 65536; |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1365 | PltEntrySize = 16; |
| 1366 | PltZeroSize = 32; |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 1367 | ThunkSize = 16; |
| Simon Atanasyan | eae66c0 | 2016-02-10 10:08:39 +0000 | [diff] [blame] | 1368 | CopyRel = R_MIPS_COPY; |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1369 | PltRel = R_MIPS_JUMP_SLOT; |
| Simon Atanasyan | da83bbc | 2016-05-04 22:39:40 +0000 | [diff] [blame] | 1370 | if (ELFT::Is64Bits) |
| 1371 | RelativeRel = (R_MIPS_64 << 8) | R_MIPS_REL32; |
| 1372 | else |
| 1373 | RelativeRel = R_MIPS_REL32; |
| Simon Atanasyan | ca558ea | 2016-01-14 21:34:50 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | template <class ELFT> |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1377 | RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type, |
| 1378 | const SymbolBody &S) const { |
| Simon Atanasyan | 8c8a5b5 | 2016-05-08 14:08:40 +0000 | [diff] [blame] | 1379 | if (ELFT::Is64Bits) |
| 1380 | // See comment in the calculateMips64RelChain. |
| 1381 | Type &= 0xff; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1382 | switch (Type) { |
| 1383 | default: |
| 1384 | return R_ABS; |
| Rafael Espindola | ebb04b9 | 2016-05-04 14:44:22 +0000 | [diff] [blame] | 1385 | case R_MIPS_JALR: |
| 1386 | return R_HINT; |
| Rafael Espindola | 1763dc4 | 2016-04-26 22:00:04 +0000 | [diff] [blame] | 1387 | case R_MIPS_GPREL16: |
| 1388 | case R_MIPS_GPREL32: |
| 1389 | return R_GOTREL; |
| Rafael Espindola | b312a74 | 2016-04-21 17:30:24 +0000 | [diff] [blame] | 1390 | case R_MIPS_26: |
| 1391 | return R_PLT; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1392 | case R_MIPS_HI16: |
| Simon Atanasyan | 1ca263c | 2016-04-14 21:10:05 +0000 | [diff] [blame] | 1393 | case R_MIPS_LO16: |
| Simon Atanasyan | be80455 | 2016-05-04 17:47:11 +0000 | [diff] [blame] | 1394 | case R_MIPS_GOT_OFST: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1395 | // MIPS _gp_disp designates offset between start of function and 'gp' |
| 1396 | // pointer into GOT. __gnu_local_gp is equal to the current value of |
| 1397 | // the 'gp'. Therefore any relocations against them do not require |
| 1398 | // dynamic relocation. |
| 1399 | if (&S == ElfSym<ELFT>::MipsGpDisp) |
| 1400 | return R_PC; |
| 1401 | return R_ABS; |
| 1402 | case R_MIPS_PC32: |
| 1403 | case R_MIPS_PC16: |
| 1404 | case R_MIPS_PC19_S2: |
| 1405 | case R_MIPS_PC21_S2: |
| 1406 | case R_MIPS_PC26_S2: |
| 1407 | case R_MIPS_PCHI16: |
| 1408 | case R_MIPS_PCLO16: |
| 1409 | return R_PC; |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 1410 | case R_MIPS_GOT16: |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 1411 | if (S.isLocal()) |
| Simon Atanasyan | 4e3a15c | 2016-05-15 18:13:50 +0000 | [diff] [blame] | 1412 | return R_MIPS_GOT_LOCAL_PAGE; |
| Simon Atanasyan | be80455 | 2016-05-04 17:47:11 +0000 | [diff] [blame] | 1413 | // fallthrough |
| 1414 | case R_MIPS_CALL16: |
| 1415 | case R_MIPS_GOT_DISP: |
| Rafael Espindola | 5628ee7 | 2016-04-15 19:14:18 +0000 | [diff] [blame] | 1416 | if (!S.isPreemptible()) |
| Simon Atanasyan | 4e3a15c | 2016-05-15 18:13:50 +0000 | [diff] [blame] | 1417 | return R_MIPS_GOT_LOCAL; |
| Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 1418 | return R_GOT_OFF; |
| Simon Atanasyan | be80455 | 2016-05-04 17:47:11 +0000 | [diff] [blame] | 1419 | case R_MIPS_GOT_PAGE: |
| Simon Atanasyan | 4e3a15c | 2016-05-15 18:13:50 +0000 | [diff] [blame] | 1420 | return R_MIPS_GOT_LOCAL_PAGE; |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | template <class ELFT> |
| George Rimar | 98b060d | 2016-03-06 06:01:07 +0000 | [diff] [blame] | 1425 | uint32_t MipsTargetInfo<ELFT>::getDynRel(uint32_t Type) const { |
| Simon Atanasyan | ca558ea | 2016-01-14 21:34:50 +0000 | [diff] [blame] | 1426 | if (Type == R_MIPS_32 || Type == R_MIPS_64) |
| Simon Atanasyan | da83bbc | 2016-05-04 22:39:40 +0000 | [diff] [blame] | 1427 | return RelativeRel; |
| Simon Atanasyan | ca558ea | 2016-01-14 21:34:50 +0000 | [diff] [blame] | 1428 | StringRef S = getELFRelocationTypeName(EM_MIPS, Type); |
| George Rimar | ca1d1fb | 2016-03-15 14:00:22 +0000 | [diff] [blame] | 1429 | error("relocation " + S + " cannot be used when making a shared object; " |
| Simon Atanasyan | ca558ea | 2016-01-14 21:34:50 +0000 | [diff] [blame] | 1430 | "recompile with -fPIC."); |
| Rui Ueyama | 2192399 | 2016-02-01 23:28:21 +0000 | [diff] [blame] | 1431 | // Keep it going with a dummy value so that we can find more reloc errors. |
| 1432 | return R_MIPS_32; |
| Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | template <class ELFT> |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1436 | void MipsTargetInfo<ELFT>::writeGotPlt(uint8_t *Buf, uint64_t Plt) const { |
| 1437 | write32<ELFT::TargetEndianness>(Buf, Out<ELFT>::Plt->getVA()); |
| Rafael Espindola | 3ef3a4c | 2015-09-29 23:22:16 +0000 | [diff] [blame] | 1438 | } |
| Simon Atanasyan | 49829a1 | 2015-09-29 05:34:03 +0000 | [diff] [blame] | 1439 | |
| Simon Atanasyan | 3503119 | 2015-12-15 06:06:34 +0000 | [diff] [blame] | 1440 | static uint16_t mipsHigh(uint64_t V) { return (V + 0x8000) >> 16; } |
| Simon Atanasyan | 2cd670d | 2015-12-13 06:49:01 +0000 | [diff] [blame] | 1441 | |
| Simon Atanasyan | e364e2e | 2016-02-04 12:31:39 +0000 | [diff] [blame] | 1442 | template <endianness E, uint8_t BSIZE, uint8_t SHIFT> |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 1443 | static int64_t getPcRelocAddend(const uint8_t *Loc) { |
| Rafael Espindola | 8cc68c3 | 2016-03-30 13:18:08 +0000 | [diff] [blame] | 1444 | uint32_t Instr = read32<E>(Loc); |
| 1445 | uint32_t Mask = 0xffffffff >> (32 - BSIZE); |
| 1446 | return SignExtend64<BSIZE + SHIFT>((Instr & Mask) << SHIFT); |
| 1447 | } |
| 1448 | |
| 1449 | template <endianness E, uint8_t BSIZE, uint8_t SHIFT> |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1450 | static void applyMipsPcReloc(uint8_t *Loc, uint32_t Type, uint64_t V) { |
| Simon Atanasyan | e364e2e | 2016-02-04 12:31:39 +0000 | [diff] [blame] | 1451 | uint32_t Mask = 0xffffffff >> (32 - BSIZE); |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1452 | uint32_t Instr = read32<E>(Loc); |
| Rafael Espindola | 66ea7bb | 2016-03-31 12:09:36 +0000 | [diff] [blame] | 1453 | if (SHIFT > 0) |
| 1454 | checkAlignment<(1 << SHIFT)>(V, Type); |
| Simon Atanasyan | e364e2e | 2016-02-04 12:31:39 +0000 | [diff] [blame] | 1455 | checkInt<BSIZE + SHIFT>(V, Type); |
| 1456 | write32<E>(Loc, (Instr & ~Mask) | ((V >> SHIFT) & Mask)); |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1459 | template <endianness E> |
| Simon Atanasyan | a888e67 | 2016-03-04 10:55:12 +0000 | [diff] [blame] | 1460 | static void writeMipsHi16(uint8_t *Loc, uint64_t V) { |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1461 | uint32_t Instr = read32<E>(Loc); |
| Simon Atanasyan | a888e67 | 2016-03-04 10:55:12 +0000 | [diff] [blame] | 1462 | write32<E>(Loc, (Instr & 0xffff0000) | mipsHigh(V)); |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| Simon Atanasyan | 3b37785 | 2016-03-04 10:55:20 +0000 | [diff] [blame] | 1465 | template <endianness E> |
| Simon Atanasyan | 0dcf541 | 2016-03-04 10:55:24 +0000 | [diff] [blame] | 1466 | static void writeMipsLo16(uint8_t *Loc, uint64_t V) { |
| 1467 | uint32_t Instr = read32<E>(Loc); |
| 1468 | write32<E>(Loc, (Instr & 0xffff0000) | (V & 0xffff)); |
| 1469 | } |
| 1470 | |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 1471 | template <endianness E> static int16_t readSignedLo16(const uint8_t *Loc) { |
| Simon Atanasyan | 4e18a31 | 2016-03-04 10:55:29 +0000 | [diff] [blame] | 1472 | return SignExtend32<16>(read32<E>(Loc) & 0xffff); |
| 1473 | } |
| 1474 | |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1475 | template <class ELFT> |
| 1476 | void MipsTargetInfo<ELFT>::writePltZero(uint8_t *Buf) const { |
| 1477 | const endianness E = ELFT::TargetEndianness; |
| 1478 | write32<E>(Buf, 0x3c1c0000); // lui $28, %hi(&GOTPLT[0]) |
| 1479 | write32<E>(Buf + 4, 0x8f990000); // lw $25, %lo(&GOTPLT[0])($28) |
| 1480 | write32<E>(Buf + 8, 0x279c0000); // addiu $28, $28, %lo(&GOTPLT[0]) |
| 1481 | write32<E>(Buf + 12, 0x031cc023); // subu $24, $24, $28 |
| 1482 | write32<E>(Buf + 16, 0x03e07825); // move $15, $31 |
| 1483 | write32<E>(Buf + 20, 0x0018c082); // srl $24, $24, 2 |
| 1484 | write32<E>(Buf + 24, 0x0320f809); // jalr $25 |
| 1485 | write32<E>(Buf + 28, 0x2718fffe); // subu $24, $24, 2 |
| 1486 | uint64_t Got = Out<ELFT>::GotPlt->getVA(); |
| Simon Atanasyan | a888e67 | 2016-03-04 10:55:12 +0000 | [diff] [blame] | 1487 | writeMipsHi16<E>(Buf, Got); |
| Simon Atanasyan | 0dcf541 | 2016-03-04 10:55:24 +0000 | [diff] [blame] | 1488 | writeMipsLo16<E>(Buf + 4, Got); |
| 1489 | writeMipsLo16<E>(Buf + 8, Got); |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | template <class ELFT> |
| 1493 | void MipsTargetInfo<ELFT>::writePlt(uint8_t *Buf, uint64_t GotEntryAddr, |
| 1494 | uint64_t PltEntryAddr, int32_t Index, |
| 1495 | unsigned RelOff) const { |
| 1496 | const endianness E = ELFT::TargetEndianness; |
| 1497 | write32<E>(Buf, 0x3c0f0000); // lui $15, %hi(.got.plt entry) |
| 1498 | write32<E>(Buf + 4, 0x8df90000); // l[wd] $25, %lo(.got.plt entry)($15) |
| 1499 | write32<E>(Buf + 8, 0x03200008); // jr $25 |
| 1500 | write32<E>(Buf + 12, 0x25f80000); // addiu $24, $15, %lo(.got.plt entry) |
| Simon Atanasyan | a888e67 | 2016-03-04 10:55:12 +0000 | [diff] [blame] | 1501 | writeMipsHi16<E>(Buf, GotEntryAddr); |
| Simon Atanasyan | 0dcf541 | 2016-03-04 10:55:24 +0000 | [diff] [blame] | 1502 | writeMipsLo16<E>(Buf + 4, GotEntryAddr); |
| 1503 | writeMipsLo16<E>(Buf + 12, GotEntryAddr); |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | template <class ELFT> |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 1507 | void MipsTargetInfo<ELFT>::writeThunk(uint8_t *Buf, uint64_t S) const { |
| 1508 | // Write MIPS LA25 thunk code to call PIC function from the non-PIC one. |
| 1509 | // See MipsTargetInfo::writeThunk for details. |
| 1510 | const endianness E = ELFT::TargetEndianness; |
| 1511 | write32<E>(Buf, 0x3c190000); // lui $25, %hi(func) |
| 1512 | write32<E>(Buf + 4, 0x08000000); // j func |
| 1513 | write32<E>(Buf + 8, 0x27390000); // addiu $25, $25, %lo(func) |
| 1514 | write32<E>(Buf + 12, 0x00000000); // nop |
| 1515 | writeMipsHi16<E>(Buf, S); |
| 1516 | write32<E>(Buf + 4, 0x08000000 | (S >> 2)); |
| 1517 | writeMipsLo16<E>(Buf + 8, S); |
| 1518 | } |
| 1519 | |
| 1520 | template <class ELFT> |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 1521 | bool MipsTargetInfo<ELFT>::needsThunk(uint32_t Type, const InputFile &File, |
| 1522 | const SymbolBody &S) const { |
| 1523 | // Any MIPS PIC code function is invoked with its address in register $t9. |
| 1524 | // So if we have a branch instruction from non-PIC code to the PIC one |
| 1525 | // we cannot make the jump directly and need to create a small stubs |
| 1526 | // to save the target function address. |
| 1527 | // See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 1528 | if (Type != R_MIPS_26) |
| 1529 | return false; |
| 1530 | auto *F = dyn_cast<ELFFileBase<ELFT>>(&File); |
| 1531 | if (!F) |
| 1532 | return false; |
| 1533 | // If current file has PIC code, LA25 stub is not required. |
| 1534 | if (F->getObj().getHeader()->e_flags & EF_MIPS_PIC) |
| 1535 | return false; |
| 1536 | auto *D = dyn_cast<DefinedRegular<ELFT>>(&S); |
| 1537 | if (!D || !D->Section) |
| 1538 | return false; |
| 1539 | // LA25 is required if target file has PIC code |
| 1540 | // or target symbol is a PIC symbol. |
| 1541 | return (D->Section->getFile()->getObj().getHeader()->e_flags & EF_MIPS_PIC) || |
| Rui Ueyama | b5792b2 | 2016-04-04 19:09:08 +0000 | [diff] [blame] | 1542 | (D->StOther & STO_MIPS_MIPS16) == STO_MIPS_PIC; |
| Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | template <class ELFT> |
| Rafael Espindola | 666625b | 2016-04-01 14:36:09 +0000 | [diff] [blame] | 1546 | uint64_t MipsTargetInfo<ELFT>::getImplicitAddend(const uint8_t *Buf, |
| Rafael Espindola | 8cc68c3 | 2016-03-30 13:18:08 +0000 | [diff] [blame] | 1547 | uint32_t Type) const { |
| 1548 | const endianness E = ELFT::TargetEndianness; |
| 1549 | switch (Type) { |
| 1550 | default: |
| 1551 | return 0; |
| 1552 | case R_MIPS_32: |
| 1553 | case R_MIPS_GPREL32: |
| 1554 | return read32<E>(Buf); |
| 1555 | case R_MIPS_26: |
| 1556 | // FIXME (simon): If the relocation target symbol is not a PLT entry |
| 1557 | // we should use another expression for calculation: |
| 1558 | // ((A << 2) | (P & 0xf0000000)) >> 2 |
| 1559 | return SignExtend64<28>((read32<E>(Buf) & 0x3ffffff) << 2); |
| 1560 | case R_MIPS_GPREL16: |
| 1561 | case R_MIPS_LO16: |
| 1562 | case R_MIPS_PCLO16: |
| 1563 | case R_MIPS_TLS_DTPREL_HI16: |
| 1564 | case R_MIPS_TLS_DTPREL_LO16: |
| 1565 | case R_MIPS_TLS_TPREL_HI16: |
| 1566 | case R_MIPS_TLS_TPREL_LO16: |
| 1567 | return readSignedLo16<E>(Buf); |
| 1568 | case R_MIPS_PC16: |
| 1569 | return getPcRelocAddend<E, 16, 2>(Buf); |
| 1570 | case R_MIPS_PC19_S2: |
| 1571 | return getPcRelocAddend<E, 19, 2>(Buf); |
| 1572 | case R_MIPS_PC21_S2: |
| 1573 | return getPcRelocAddend<E, 21, 2>(Buf); |
| 1574 | case R_MIPS_PC26_S2: |
| 1575 | return getPcRelocAddend<E, 26, 2>(Buf); |
| 1576 | case R_MIPS_PC32: |
| 1577 | return getPcRelocAddend<E, 32, 0>(Buf); |
| 1578 | } |
| 1579 | } |
| 1580 | |
| Simon Atanasyan | 8c8a5b5 | 2016-05-08 14:08:40 +0000 | [diff] [blame] | 1581 | static std::pair<uint32_t, uint64_t> calculateMips64RelChain(uint32_t Type, |
| 1582 | uint64_t Val) { |
| 1583 | // MIPS N64 ABI packs multiple relocations into the single relocation |
| 1584 | // record. In general, all up to three relocations can have arbitrary |
| 1585 | // types. In fact, Clang and GCC uses only a few combinations. For now, |
| 1586 | // we support two of them. That is allow to pass at least all LLVM |
| 1587 | // test suite cases. |
| 1588 | // <any relocation> / R_MIPS_SUB / R_MIPS_HI16 | R_MIPS_LO16 |
| 1589 | // <any relocation> / R_MIPS_64 / R_MIPS_NONE |
| 1590 | // The first relocation is a 'real' relocation which is calculated |
| 1591 | // using the corresponding symbol's value. The second and the third |
| 1592 | // relocations used to modify result of the first one: extend it to |
| 1593 | // 64-bit, extract high or low part etc. For details, see part 2.9 Relocation |
| 1594 | // at the https://dmz-portal.mips.com/mw/images/8/82/007-4658-001.pdf |
| 1595 | uint32_t Type2 = (Type >> 8) & 0xff; |
| 1596 | uint32_t Type3 = (Type >> 16) & 0xff; |
| 1597 | if (Type2 == R_MIPS_NONE && Type3 == R_MIPS_NONE) |
| 1598 | return std::make_pair(Type, Val); |
| 1599 | if (Type2 == R_MIPS_64 && Type3 == R_MIPS_NONE) |
| 1600 | return std::make_pair(Type2, Val); |
| 1601 | if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16)) |
| 1602 | return std::make_pair(Type3, -Val); |
| 1603 | error("unsupported relocations combination " + Twine(Type)); |
| 1604 | return std::make_pair(Type & 0xff, Val); |
| 1605 | } |
| 1606 | |
| Rafael Espindola | 8cc68c3 | 2016-03-30 13:18:08 +0000 | [diff] [blame] | 1607 | template <class ELFT> |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1608 | void MipsTargetInfo<ELFT>::relocateOne(uint8_t *Loc, uint32_t Type, |
| 1609 | uint64_t Val) const { |
| Rafael Espindola | e7e57b2 | 2015-11-09 21:43:00 +0000 | [diff] [blame] | 1610 | const endianness E = ELFT::TargetEndianness; |
| Simon Atanasyan | a8e9cb3 | 2016-03-17 12:36:08 +0000 | [diff] [blame] | 1611 | // Thread pointer and DRP offsets from the start of TLS data area. |
| 1612 | // https://www.linux-mips.org/wiki/NPTL |
| Simon Atanasyan | 8c8a5b5 | 2016-05-08 14:08:40 +0000 | [diff] [blame] | 1613 | if (Type == R_MIPS_TLS_DTPREL_HI16 || Type == R_MIPS_TLS_DTPREL_LO16) |
| 1614 | Val -= 0x8000; |
| 1615 | else if (Type == R_MIPS_TLS_TPREL_HI16 || Type == R_MIPS_TLS_TPREL_LO16) |
| 1616 | Val -= 0x7000; |
| 1617 | if (ELFT::Is64Bits) |
| 1618 | std::tie(Type, Val) = calculateMips64RelChain(Type, Val); |
| Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1619 | switch (Type) { |
| 1620 | case R_MIPS_32: |
| Simon Atanasyan | 9ac8198 | 2016-05-06 15:02:50 +0000 | [diff] [blame] | 1621 | case R_MIPS_GPREL32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1622 | write32<E>(Loc, Val); |
| Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1623 | break; |
| Simon Atanasyan | da83bbc | 2016-05-04 22:39:40 +0000 | [diff] [blame] | 1624 | case R_MIPS_64: |
| 1625 | write64<E>(Loc, Val); |
| 1626 | break; |
| Simon Atanasyan | 10296c2 | 2016-05-07 07:36:47 +0000 | [diff] [blame] | 1627 | case R_MIPS_26: |
| 1628 | write32<E>(Loc, (read32<E>(Loc) & ~0x3ffffff) | (Val >> 2)); |
| Simon Atanasyan | 2287dc3 | 2016-02-10 19:57:19 +0000 | [diff] [blame] | 1629 | break; |
| Simon Atanasyan | be80455 | 2016-05-04 17:47:11 +0000 | [diff] [blame] | 1630 | case R_MIPS_GOT_DISP: |
| 1631 | case R_MIPS_GOT_PAGE: |
| Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 1632 | case R_MIPS_GOT16: |
| Simon Atanasyan | 9ac8198 | 2016-05-06 15:02:50 +0000 | [diff] [blame] | 1633 | case R_MIPS_GPREL16: |
| Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 1634 | checkInt<16>(Val, Type); |
| 1635 | // fallthrough |
| Rui Ueyama | 7ee3cf7 | 2015-12-03 20:59:51 +0000 | [diff] [blame] | 1636 | case R_MIPS_CALL16: |
| Simon Atanasyan | be80455 | 2016-05-04 17:47:11 +0000 | [diff] [blame] | 1637 | case R_MIPS_GOT_OFST: |
| Simon Atanasyan | 5b9ac41 | 2016-05-06 15:02:54 +0000 | [diff] [blame] | 1638 | case R_MIPS_LO16: |
| 1639 | case R_MIPS_PCLO16: |
| Simon Atanasyan | 8c8a5b5 | 2016-05-08 14:08:40 +0000 | [diff] [blame] | 1640 | case R_MIPS_TLS_DTPREL_LO16: |
| 1641 | case R_MIPS_TLS_TPREL_LO16: |
| Rafael Espindola | 58cd5db | 2016-04-19 22:46:03 +0000 | [diff] [blame] | 1642 | writeMipsLo16<E>(Loc, Val); |
| Rui Ueyama | 7ee3cf7 | 2015-12-03 20:59:51 +0000 | [diff] [blame] | 1643 | break; |
| Simon Atanasyan | 3b37785 | 2016-03-04 10:55:20 +0000 | [diff] [blame] | 1644 | case R_MIPS_HI16: |
| Simon Atanasyan | 5b9ac41 | 2016-05-06 15:02:54 +0000 | [diff] [blame] | 1645 | case R_MIPS_PCHI16: |
| Simon Atanasyan | 8c8a5b5 | 2016-05-08 14:08:40 +0000 | [diff] [blame] | 1646 | case R_MIPS_TLS_DTPREL_HI16: |
| 1647 | case R_MIPS_TLS_TPREL_HI16: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1648 | writeMipsHi16<E>(Loc, Val); |
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 1649 | break; |
| Simon Atanasyan | e436185 | 2015-12-13 06:49:14 +0000 | [diff] [blame] | 1650 | case R_MIPS_JALR: |
| 1651 | // Ignore this optimization relocation for now |
| 1652 | break; |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1653 | case R_MIPS_PC16: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1654 | applyMipsPcReloc<E, 16, 2>(Loc, Type, Val); |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1655 | break; |
| 1656 | case R_MIPS_PC19_S2: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1657 | applyMipsPcReloc<E, 19, 2>(Loc, Type, Val); |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1658 | break; |
| 1659 | case R_MIPS_PC21_S2: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1660 | applyMipsPcReloc<E, 21, 2>(Loc, Type, Val); |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1661 | break; |
| 1662 | case R_MIPS_PC26_S2: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1663 | applyMipsPcReloc<E, 26, 2>(Loc, Type, Val); |
| Simon Atanasyan | e364e2e | 2016-02-04 12:31:39 +0000 | [diff] [blame] | 1664 | break; |
| 1665 | case R_MIPS_PC32: |
| Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 1666 | applyMipsPcReloc<E, 32, 0>(Loc, Type, Val); |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1667 | break; |
| Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1668 | default: |
| George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 1669 | fatal("unrecognized reloc " + Twine(Type)); |
| Simon Atanasyan | 3b732ac | 2015-10-12 15:10:02 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } |
| Igor Kudrin | 15cd9ff | 2015-11-06 07:43:03 +0000 | [diff] [blame] | 1672 | |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1673 | template <class ELFT> |
| Rafael Espindola | b8ff59a | 2016-04-28 14:34:39 +0000 | [diff] [blame] | 1674 | bool MipsTargetInfo<ELFT>::usesOnlyLowPageBits(uint32_t Type) const { |
| Simon Atanasyan | be80455 | 2016-05-04 17:47:11 +0000 | [diff] [blame] | 1675 | return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST; |
| Simon Atanasyan | 0fc0acf | 2015-12-21 17:36:40 +0000 | [diff] [blame] | 1676 | } |
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 1677 | } |
| 1678 | } |