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