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