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