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