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