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