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