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