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