Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 1 | //===- AArch64.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 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 10 | #include "Symbols.h" |
| 11 | #include "SyntheticSections.h" |
| 12 | #include "Target.h" |
| 13 | #include "Thunks.h" |
Bob Haarman | b8a59c8 | 2017-10-25 22:28:38 +0000 | [diff] [blame] | 14 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 15 | #include "llvm/Object/ELF.h" |
| 16 | #include "llvm/Support/Endian.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::support::endian; |
| 20 | using namespace llvm::ELF; |
| 21 | using namespace lld; |
| 22 | using namespace lld::elf; |
| 23 | |
| 24 | // Page(Expr) is the page address of the expression Expr, defined |
| 25 | // as (Expr & ~0xFFF). (This applies even if the machine page size |
| 26 | // supported by the platform has a different value.) |
| 27 | uint64_t elf::getAArch64Page(uint64_t Expr) { |
| 28 | return Expr & ~static_cast<uint64_t>(0xFFF); |
| 29 | } |
| 30 | |
| 31 | namespace { |
| 32 | class AArch64 final : public TargetInfo { |
| 33 | public: |
| 34 | AArch64(); |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 35 | RelExpr getRelExpr(RelType Type, const Symbol &S, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 36 | const uint8_t *Loc) const override; |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 37 | bool isPicRel(RelType Type) const override; |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 38 | void writeGotPlt(uint8_t *Buf, const Symbol &S) const override; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 39 | void writePltHeader(uint8_t *Buf) const override; |
| 40 | void writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr, |
| 41 | int32_t Index, unsigned RelOff) const override; |
Peter Smith | 31dddc9 | 2017-11-29 11:15:12 +0000 | [diff] [blame] | 42 | bool needsThunk(RelExpr Expr, RelType Type, const InputFile *File, |
| 43 | uint64_t BranchAddr, const Symbol &S) const override; |
| 44 | bool inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const override; |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 45 | bool usesOnlyLowPageBits(RelType Type) const override; |
| 46 | void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
| 47 | RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 48 | RelExpr Expr) const override; |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 49 | void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
| 50 | void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
| 51 | void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 52 | }; |
| 53 | } // namespace |
| 54 | |
| 55 | AArch64::AArch64() { |
| 56 | CopyRel = R_AARCH64_COPY; |
| 57 | RelativeRel = R_AARCH64_RELATIVE; |
| 58 | IRelativeRel = R_AARCH64_IRELATIVE; |
| 59 | GotRel = R_AARCH64_GLOB_DAT; |
| 60 | PltRel = R_AARCH64_JUMP_SLOT; |
| 61 | TlsDescRel = R_AARCH64_TLSDESC; |
| 62 | TlsGotRel = R_AARCH64_TLS_TPREL64; |
| 63 | GotEntrySize = 8; |
| 64 | GotPltEntrySize = 8; |
| 65 | PltEntrySize = 16; |
| 66 | PltHeaderSize = 32; |
| 67 | DefaultMaxPageSize = 65536; |
| 68 | |
| 69 | // It doesn't seem to be documented anywhere, but tls on aarch64 uses variant |
| 70 | // 1 of the tls structures and the tcb size is 16. |
| 71 | TcbSize = 16; |
Peter Smith | 31dddc9 | 2017-11-29 11:15:12 +0000 | [diff] [blame] | 72 | NeedsThunks = true; |
| 73 | |
| 74 | // See comment in Arch/ARM.cpp for a more detailed explanation of |
| 75 | // ThunkSectionSpacing. For AArch64 the only branches we are permitted to |
| 76 | // Thunk have a range of +/- 128 MiB |
| 77 | ThunkSectionSpacing = (128 * 1024 * 1024) - 0x30000; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 80 | RelExpr AArch64::getRelExpr(RelType Type, const Symbol &S, |
Rui Ueyama | be85529 | 2017-10-12 03:14:06 +0000 | [diff] [blame] | 81 | const uint8_t *Loc) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 82 | switch (Type) { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 83 | case R_AARCH64_TLSDESC_ADR_PAGE21: |
| 84 | return R_TLSDESC_PAGE; |
| 85 | case R_AARCH64_TLSDESC_LD64_LO12: |
| 86 | case R_AARCH64_TLSDESC_ADD_LO12: |
| 87 | return R_TLSDESC; |
| 88 | case R_AARCH64_TLSDESC_CALL: |
| 89 | return R_TLSDESC_CALL; |
| 90 | case R_AARCH64_TLSLE_ADD_TPREL_HI12: |
| 91 | case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: |
| 92 | return R_TLS; |
| 93 | case R_AARCH64_CALL26: |
| 94 | case R_AARCH64_CONDBR19: |
| 95 | case R_AARCH64_JUMP26: |
| 96 | case R_AARCH64_TSTBR14: |
| 97 | return R_PLT_PC; |
| 98 | case R_AARCH64_PREL16: |
| 99 | case R_AARCH64_PREL32: |
| 100 | case R_AARCH64_PREL64: |
| 101 | case R_AARCH64_ADR_PREL_LO21: |
Davide Italiano | f681a8f | 2017-09-20 23:49:50 +0000 | [diff] [blame] | 102 | case R_AARCH64_LD_PREL_LO19: |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 103 | return R_PC; |
| 104 | case R_AARCH64_ADR_PREL_PG_HI21: |
| 105 | return R_PAGE_PC; |
| 106 | case R_AARCH64_LD64_GOT_LO12_NC: |
| 107 | case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: |
| 108 | return R_GOT; |
| 109 | case R_AARCH64_ADR_GOT_PAGE: |
| 110 | case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: |
| 111 | return R_GOT_PAGE_PC; |
| 112 | case R_AARCH64_NONE: |
| 113 | return R_NONE; |
Rui Ueyama | be85529 | 2017-10-12 03:14:06 +0000 | [diff] [blame] | 114 | default: |
| 115 | return R_ABS; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 119 | RelExpr AArch64::adjustRelaxExpr(RelType Type, const uint8_t *Data, |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 120 | RelExpr Expr) const { |
| 121 | if (Expr == R_RELAX_TLS_GD_TO_IE) { |
| 122 | if (Type == R_AARCH64_TLSDESC_ADR_PAGE21) |
| 123 | return R_RELAX_TLS_GD_TO_IE_PAGE_PC; |
| 124 | return R_RELAX_TLS_GD_TO_IE_ABS; |
| 125 | } |
| 126 | return Expr; |
| 127 | } |
| 128 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 129 | bool AArch64::usesOnlyLowPageBits(RelType Type) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 130 | switch (Type) { |
| 131 | default: |
| 132 | return false; |
| 133 | case R_AARCH64_ADD_ABS_LO12_NC: |
| 134 | case R_AARCH64_LD64_GOT_LO12_NC: |
| 135 | case R_AARCH64_LDST128_ABS_LO12_NC: |
| 136 | case R_AARCH64_LDST16_ABS_LO12_NC: |
| 137 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| 138 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| 139 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| 140 | case R_AARCH64_TLSDESC_ADD_LO12: |
| 141 | case R_AARCH64_TLSDESC_LD64_LO12: |
| 142 | case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 147 | bool AArch64::isPicRel(RelType Type) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 148 | return Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64; |
| 149 | } |
| 150 | |
Rui Ueyama | f52496e | 2017-11-03 21:21:47 +0000 | [diff] [blame] | 151 | void AArch64::writeGotPlt(uint8_t *Buf, const Symbol &) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 152 | write64le(Buf, InX::Plt->getVA()); |
| 153 | } |
| 154 | |
| 155 | void AArch64::writePltHeader(uint8_t *Buf) const { |
| 156 | const uint8_t PltData[] = { |
| 157 | 0xf0, 0x7b, 0xbf, 0xa9, // stp x16, x30, [sp,#-16]! |
| 158 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[2])) |
| 159 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[2]))] |
| 160 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[2])) |
| 161 | 0x20, 0x02, 0x1f, 0xd6, // br x17 |
| 162 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 163 | 0x1f, 0x20, 0x03, 0xd5, // nop |
| 164 | 0x1f, 0x20, 0x03, 0xd5 // nop |
| 165 | }; |
| 166 | memcpy(Buf, PltData, sizeof(PltData)); |
| 167 | |
| 168 | uint64_t Got = InX::GotPlt->getVA(); |
| 169 | uint64_t Plt = InX::Plt->getVA(); |
| 170 | relocateOne(Buf + 4, R_AARCH64_ADR_PREL_PG_HI21, |
| 171 | getAArch64Page(Got + 16) - getAArch64Page(Plt + 4)); |
| 172 | relocateOne(Buf + 8, R_AARCH64_LDST64_ABS_LO12_NC, Got + 16); |
| 173 | relocateOne(Buf + 12, R_AARCH64_ADD_ABS_LO12_NC, Got + 16); |
| 174 | } |
| 175 | |
| 176 | void AArch64::writePlt(uint8_t *Buf, uint64_t GotPltEntryAddr, |
| 177 | uint64_t PltEntryAddr, int32_t Index, |
| 178 | unsigned RelOff) const { |
| 179 | const uint8_t Inst[] = { |
| 180 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Page(&(.plt.got[n])) |
| 181 | 0x11, 0x02, 0x40, 0xf9, // ldr x17, [x16, Offset(&(.plt.got[n]))] |
| 182 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, Offset(&(.plt.got[n])) |
| 183 | 0x20, 0x02, 0x1f, 0xd6 // br x17 |
| 184 | }; |
| 185 | memcpy(Buf, Inst, sizeof(Inst)); |
| 186 | |
| 187 | relocateOne(Buf, R_AARCH64_ADR_PREL_PG_HI21, |
| 188 | getAArch64Page(GotPltEntryAddr) - getAArch64Page(PltEntryAddr)); |
| 189 | relocateOne(Buf + 4, R_AARCH64_LDST64_ABS_LO12_NC, GotPltEntryAddr); |
| 190 | relocateOne(Buf + 8, R_AARCH64_ADD_ABS_LO12_NC, GotPltEntryAddr); |
| 191 | } |
| 192 | |
Peter Smith | 31dddc9 | 2017-11-29 11:15:12 +0000 | [diff] [blame] | 193 | bool AArch64::needsThunk(RelExpr Expr, RelType Type, const InputFile *File, |
| 194 | uint64_t BranchAddr, const Symbol &S) const { |
| 195 | // ELF for the ARM 64-bit architecture, section Call and Jump relocations |
| 196 | // only permits range extension thunks for R_AARCH64_CALL26 and |
| 197 | // R_AARCH64_JUMP26 relocation types. |
| 198 | if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26) |
| 199 | return false; |
| 200 | uint64_t Dst = (Expr == R_PLT_PC) ? S.getPltVA() : S.getVA(); |
| 201 | return !inBranchRange(Type, BranchAddr, Dst); |
| 202 | } |
| 203 | |
| 204 | bool AArch64::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const { |
| 205 | if (Type != R_AARCH64_CALL26 && Type != R_AARCH64_JUMP26) |
| 206 | return true; |
| 207 | // The AArch64 call and unconditional branch instructions have a range of |
| 208 | // +/- 128 MiB. |
| 209 | uint64_t Range = 128 * 1024 * 1024; |
| 210 | if (Dst > Src) { |
| 211 | // Immediate of branch is signed. |
| 212 | Range -= 4; |
| 213 | return Dst - Src <= Range; |
| 214 | } |
| 215 | return Src - Dst <= Range; |
| 216 | } |
| 217 | |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 218 | static void write32AArch64Addr(uint8_t *L, uint64_t Imm) { |
| 219 | uint32_t ImmLo = (Imm & 0x3) << 29; |
| 220 | uint32_t ImmHi = (Imm & 0x1FFFFC) << 3; |
| 221 | uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3); |
| 222 | write32le(L, (read32le(L) & ~Mask) | ImmLo | ImmHi); |
| 223 | } |
| 224 | |
| 225 | // Return the bits [Start, End] from Val shifted Start bits. |
| 226 | // For instance, getBits(0xF0, 4, 8) returns 0xF. |
| 227 | static uint64_t getBits(uint64_t Val, int Start, int End) { |
| 228 | uint64_t Mask = ((uint64_t)1 << (End + 1 - Start)) - 1; |
| 229 | return (Val >> Start) & Mask; |
| 230 | } |
| 231 | |
| 232 | static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); } |
| 233 | |
| 234 | // Update the immediate field in a AARCH64 ldr, str, and add instruction. |
| 235 | static void or32AArch64Imm(uint8_t *L, uint64_t Imm) { |
| 236 | or32le(L, (Imm & 0xFFF) << 10); |
| 237 | } |
| 238 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 239 | void AArch64::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 240 | switch (Type) { |
| 241 | case R_AARCH64_ABS16: |
| 242 | case R_AARCH64_PREL16: |
| 243 | checkIntUInt<16>(Loc, Val, Type); |
| 244 | write16le(Loc, Val); |
| 245 | break; |
| 246 | case R_AARCH64_ABS32: |
| 247 | case R_AARCH64_PREL32: |
| 248 | checkIntUInt<32>(Loc, Val, Type); |
| 249 | write32le(Loc, Val); |
| 250 | break; |
| 251 | case R_AARCH64_ABS64: |
| 252 | case R_AARCH64_GLOB_DAT: |
| 253 | case R_AARCH64_PREL64: |
| 254 | write64le(Loc, Val); |
| 255 | break; |
| 256 | case R_AARCH64_ADD_ABS_LO12_NC: |
| 257 | or32AArch64Imm(Loc, Val); |
| 258 | break; |
| 259 | case R_AARCH64_ADR_GOT_PAGE: |
| 260 | case R_AARCH64_ADR_PREL_PG_HI21: |
| 261 | case R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: |
| 262 | case R_AARCH64_TLSDESC_ADR_PAGE21: |
| 263 | checkInt<33>(Loc, Val, Type); |
| 264 | write32AArch64Addr(Loc, Val >> 12); |
| 265 | break; |
| 266 | case R_AARCH64_ADR_PREL_LO21: |
| 267 | checkInt<21>(Loc, Val, Type); |
| 268 | write32AArch64Addr(Loc, Val); |
| 269 | break; |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 270 | case R_AARCH64_JUMP26: |
Peter Smith | 20489ec | 2017-09-07 16:29:52 +0000 | [diff] [blame] | 271 | // Normally we would just write the bits of the immediate field, however |
| 272 | // when patching instructions for the cpu errata fix -fix-cortex-a53-843419 |
| 273 | // we want to replace a non-branch instruction with a branch immediate |
| 274 | // instruction. By writing all the bits of the instruction including the |
| 275 | // opcode and the immediate (0 001 | 01 imm26) we can do this |
| 276 | // transformation by placing a R_AARCH64_JUMP26 relocation at the offset of |
| 277 | // the instruction we want to patch. |
| 278 | write32le(Loc, 0x14000000); |
| 279 | LLVM_FALLTHROUGH; |
| 280 | case R_AARCH64_CALL26: |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 281 | checkInt<28>(Loc, Val, Type); |
| 282 | or32le(Loc, (Val & 0x0FFFFFFC) >> 2); |
| 283 | break; |
| 284 | case R_AARCH64_CONDBR19: |
Davide Italiano | f681a8f | 2017-09-20 23:49:50 +0000 | [diff] [blame] | 285 | case R_AARCH64_LD_PREL_LO19: |
Davide Italiano | bb0690e | 2017-09-21 00:26:28 +0000 | [diff] [blame] | 286 | checkAlignment<4>(Loc, Val, Type); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 287 | checkInt<21>(Loc, Val, Type); |
| 288 | or32le(Loc, (Val & 0x1FFFFC) << 3); |
| 289 | break; |
| 290 | case R_AARCH64_LD64_GOT_LO12_NC: |
| 291 | case R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC: |
| 292 | case R_AARCH64_TLSDESC_LD64_LO12: |
| 293 | checkAlignment<8>(Loc, Val, Type); |
| 294 | or32le(Loc, (Val & 0xFF8) << 7); |
| 295 | break; |
| 296 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| 297 | or32AArch64Imm(Loc, getBits(Val, 0, 11)); |
| 298 | break; |
| 299 | case R_AARCH64_LDST16_ABS_LO12_NC: |
Peter Smith | 1d5a070 | 2017-09-06 14:23:06 +0000 | [diff] [blame] | 300 | checkAlignment<2>(Loc, Val, Type); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 301 | or32AArch64Imm(Loc, getBits(Val, 1, 11)); |
| 302 | break; |
| 303 | case R_AARCH64_LDST32_ABS_LO12_NC: |
Peter Smith | 1d5a070 | 2017-09-06 14:23:06 +0000 | [diff] [blame] | 304 | checkAlignment<4>(Loc, Val, Type); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 305 | or32AArch64Imm(Loc, getBits(Val, 2, 11)); |
| 306 | break; |
| 307 | case R_AARCH64_LDST64_ABS_LO12_NC: |
Peter Smith | 1d5a070 | 2017-09-06 14:23:06 +0000 | [diff] [blame] | 308 | checkAlignment<8>(Loc, Val, Type); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 309 | or32AArch64Imm(Loc, getBits(Val, 3, 11)); |
| 310 | break; |
| 311 | case R_AARCH64_LDST128_ABS_LO12_NC: |
Peter Smith | 1d5a070 | 2017-09-06 14:23:06 +0000 | [diff] [blame] | 312 | checkAlignment<16>(Loc, Val, Type); |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 313 | or32AArch64Imm(Loc, getBits(Val, 4, 11)); |
| 314 | break; |
| 315 | case R_AARCH64_MOVW_UABS_G0_NC: |
| 316 | or32le(Loc, (Val & 0xFFFF) << 5); |
| 317 | break; |
| 318 | case R_AARCH64_MOVW_UABS_G1_NC: |
| 319 | or32le(Loc, (Val & 0xFFFF0000) >> 11); |
| 320 | break; |
| 321 | case R_AARCH64_MOVW_UABS_G2_NC: |
| 322 | or32le(Loc, (Val & 0xFFFF00000000) >> 27); |
| 323 | break; |
| 324 | case R_AARCH64_MOVW_UABS_G3: |
| 325 | or32le(Loc, (Val & 0xFFFF000000000000) >> 43); |
| 326 | break; |
| 327 | case R_AARCH64_TSTBR14: |
| 328 | checkInt<16>(Loc, Val, Type); |
| 329 | or32le(Loc, (Val & 0xFFFC) << 3); |
| 330 | break; |
| 331 | case R_AARCH64_TLSLE_ADD_TPREL_HI12: |
| 332 | checkInt<24>(Loc, Val, Type); |
| 333 | or32AArch64Imm(Loc, Val >> 12); |
| 334 | break; |
| 335 | case R_AARCH64_TLSLE_ADD_TPREL_LO12_NC: |
| 336 | case R_AARCH64_TLSDESC_ADD_LO12: |
| 337 | or32AArch64Imm(Loc, Val); |
| 338 | break; |
| 339 | default: |
| 340 | error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type)); |
| 341 | } |
| 342 | } |
| 343 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 344 | void AArch64::relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 345 | // TLSDESC Global-Dynamic relocation are in the form: |
| 346 | // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] |
| 347 | // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] |
| 348 | // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] |
| 349 | // .tlsdesccall [R_AARCH64_TLSDESC_CALL] |
| 350 | // blr x1 |
| 351 | // And it can optimized to: |
| 352 | // movz x0, #0x0, lsl #16 |
| 353 | // movk x0, #0x10 |
| 354 | // nop |
| 355 | // nop |
| 356 | checkUInt<32>(Loc, Val, Type); |
| 357 | |
| 358 | switch (Type) { |
| 359 | case R_AARCH64_TLSDESC_ADD_LO12: |
| 360 | case R_AARCH64_TLSDESC_CALL: |
| 361 | write32le(Loc, 0xd503201f); // nop |
| 362 | return; |
| 363 | case R_AARCH64_TLSDESC_ADR_PAGE21: |
| 364 | write32le(Loc, 0xd2a00000 | (((Val >> 16) & 0xffff) << 5)); // movz |
| 365 | return; |
| 366 | case R_AARCH64_TLSDESC_LD64_LO12: |
| 367 | write32le(Loc, 0xf2800000 | ((Val & 0xffff) << 5)); // movk |
| 368 | return; |
| 369 | default: |
| 370 | llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); |
| 371 | } |
| 372 | } |
| 373 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 374 | void AArch64::relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 375 | // TLSDESC Global-Dynamic relocation are in the form: |
| 376 | // adrp x0, :tlsdesc:v [R_AARCH64_TLSDESC_ADR_PAGE21] |
| 377 | // ldr x1, [x0, #:tlsdesc_lo12:v [R_AARCH64_TLSDESC_LD64_LO12] |
| 378 | // add x0, x0, :tlsdesc_los:v [R_AARCH64_TLSDESC_ADD_LO12] |
| 379 | // .tlsdesccall [R_AARCH64_TLSDESC_CALL] |
| 380 | // blr x1 |
| 381 | // And it can optimized to: |
| 382 | // adrp x0, :gottprel:v |
| 383 | // ldr x0, [x0, :gottprel_lo12:v] |
| 384 | // nop |
| 385 | // nop |
| 386 | |
| 387 | switch (Type) { |
| 388 | case R_AARCH64_TLSDESC_ADD_LO12: |
| 389 | case R_AARCH64_TLSDESC_CALL: |
| 390 | write32le(Loc, 0xd503201f); // nop |
| 391 | break; |
| 392 | case R_AARCH64_TLSDESC_ADR_PAGE21: |
| 393 | write32le(Loc, 0x90000000); // adrp |
| 394 | relocateOne(Loc, R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21, Val); |
| 395 | break; |
| 396 | case R_AARCH64_TLSDESC_LD64_LO12: |
| 397 | write32le(Loc, 0xf9400000); // ldr |
| 398 | relocateOne(Loc, R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC, Val); |
| 399 | break; |
| 400 | default: |
| 401 | llvm_unreachable("unsupported relocation for TLS GD to LE relaxation"); |
| 402 | } |
| 403 | } |
| 404 | |
Rui Ueyama | 67533a2 | 2017-10-11 22:49:24 +0000 | [diff] [blame] | 405 | void AArch64::relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const { |
Rui Ueyama | 21c0a9c | 2017-06-16 17:32:43 +0000 | [diff] [blame] | 406 | checkUInt<32>(Loc, Val, Type); |
| 407 | |
| 408 | if (Type == R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21) { |
| 409 | // Generate MOVZ. |
| 410 | uint32_t RegNo = read32le(Loc) & 0x1f; |
| 411 | write32le(Loc, (0xd2a00000 | RegNo) | (((Val >> 16) & 0xffff) << 5)); |
| 412 | return; |
| 413 | } |
| 414 | if (Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC) { |
| 415 | // Generate MOVK. |
| 416 | uint32_t RegNo = read32le(Loc) & 0x1f; |
| 417 | write32le(Loc, (0xf2800000 | RegNo) | ((Val & 0xffff) << 5)); |
| 418 | return; |
| 419 | } |
| 420 | llvm_unreachable("invalid relocation for TLS IE to LE relaxation"); |
| 421 | } |
| 422 | |
Rui Ueyama | e145bc2 | 2017-06-16 20:15:03 +0000 | [diff] [blame] | 423 | TargetInfo *elf::getAArch64TargetInfo() { |
| 424 | static AArch64 Target; |
| 425 | return &Target; |
| 426 | } |