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