1 //=--- AArch64MCExpr.h - AArch64 specific MC expression classes ---*- C++ -*-=// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file describes AArch64-specific MCExprs, used for modifiers like 10 // ":lo12:" or ":gottprel_g1:". 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCEXPR_H 15 #define LLVM_LIB_TARGET_AARCH64_MCTARGETDESC_AARCH64MCEXPR_H 16 17 #include "Utils/AArch64BaseInfo.h" 18 #include "llvm/MC/MCExpr.h" 19 #include "llvm/Support/Casting.h" 20 #include "llvm/Support/ErrorHandling.h" 21 22 namespace llvm { 23 24 class AArch64MCExpr : public MCTargetExpr { 25 public: 26 enum VariantKind { 27 // clang-format off 28 // Symbol locations specifying (roughly speaking) what calculation should be 29 // performed to construct the final address for the relocated 30 // symbol. E.g. direct, via the GOT, ... 31 VK_ABS = 0x001, 32 VK_SABS = 0x002, 33 VK_PREL = 0x003, 34 VK_GOT = 0x004, 35 VK_DTPREL = 0x005, 36 VK_GOTTPREL = 0x006, 37 VK_TPREL = 0x007, 38 VK_TLSDESC = 0x008, 39 VK_SECREL = 0x009, 40 VK_AUTH = 0x00a, 41 VK_AUTHADDR = 0x00b, 42 VK_GOT_AUTH = 0x00c, 43 VK_TLSDESC_AUTH = 0x00d, 44 VK_SymLocBits = 0x00f, 45 46 // Variants specifying which part of the final address calculation is 47 // used. E.g. the low 12 bits for an ADD/LDR, the middle 16 bits for a 48 // MOVZ/MOVK. 49 VK_PAGE = 0x010, 50 VK_PAGEOFF = 0x020, 51 VK_HI12 = 0x030, 52 VK_G0 = 0x040, 53 VK_G1 = 0x050, 54 VK_G2 = 0x060, 55 VK_G3 = 0x070, 56 VK_LO15 = 0x080, 57 VK_AddressFragBits = 0x0f0, 58 59 // Whether the final relocation is a checked one (where a linker should 60 // perform a range-check on the final address) or not. Note that this field 61 // is unfortunately sometimes omitted from the assembly syntax. E.g. :lo12: 62 // on its own is a non-checked relocation. We side with ELF on being 63 // explicit about this! 64 VK_NC = 0x100, 65 66 // Convenience definitions for referring to specific textual representations 67 // of relocation specifiers. Note that this means the "_NC" is sometimes 68 // omitted in line with assembly syntax here (VK_LO12 rather than VK_LO12_NC 69 // since a user would write ":lo12:"). 70 VK_CALL = VK_ABS, 71 VK_ABS_PAGE = VK_ABS | VK_PAGE, 72 VK_ABS_PAGE_NC = VK_ABS | VK_PAGE | VK_NC, 73 VK_ABS_G3 = VK_ABS | VK_G3, 74 VK_ABS_G2 = VK_ABS | VK_G2, 75 VK_ABS_G2_S = VK_SABS | VK_G2, 76 VK_ABS_G2_NC = VK_ABS | VK_G2 | VK_NC, 77 VK_ABS_G1 = VK_ABS | VK_G1, 78 VK_ABS_G1_S = VK_SABS | VK_G1, 79 VK_ABS_G1_NC = VK_ABS | VK_G1 | VK_NC, 80 VK_ABS_G0 = VK_ABS | VK_G0, 81 VK_ABS_G0_S = VK_SABS | VK_G0, 82 VK_ABS_G0_NC = VK_ABS | VK_G0 | VK_NC, 83 VK_LO12 = VK_ABS | VK_PAGEOFF | VK_NC, 84 VK_PREL_G3 = VK_PREL | VK_G3, 85 VK_PREL_G2 = VK_PREL | VK_G2, 86 VK_PREL_G2_NC = VK_PREL | VK_G2 | VK_NC, 87 VK_PREL_G1 = VK_PREL | VK_G1, 88 VK_PREL_G1_NC = VK_PREL | VK_G1 | VK_NC, 89 VK_PREL_G0 = VK_PREL | VK_G0, 90 VK_PREL_G0_NC = VK_PREL | VK_G0 | VK_NC, 91 VK_GOT_LO12 = VK_GOT | VK_PAGEOFF | VK_NC, 92 VK_GOT_PAGE = VK_GOT | VK_PAGE, 93 VK_GOT_PAGE_LO15 = VK_GOT | VK_LO15 | VK_NC, 94 VK_GOT_AUTH_LO12 = VK_GOT_AUTH | VK_PAGEOFF | VK_NC, 95 VK_GOT_AUTH_PAGE = VK_GOT_AUTH | VK_PAGE, 96 VK_DTPREL_G2 = VK_DTPREL | VK_G2, 97 VK_DTPREL_G1 = VK_DTPREL | VK_G1, 98 VK_DTPREL_G1_NC = VK_DTPREL | VK_G1 | VK_NC, 99 VK_DTPREL_G0 = VK_DTPREL | VK_G0, 100 VK_DTPREL_G0_NC = VK_DTPREL | VK_G0 | VK_NC, 101 VK_DTPREL_HI12 = VK_DTPREL | VK_HI12, 102 VK_DTPREL_LO12 = VK_DTPREL | VK_PAGEOFF, 103 VK_DTPREL_LO12_NC = VK_DTPREL | VK_PAGEOFF | VK_NC, 104 VK_GOTTPREL_PAGE = VK_GOTTPREL | VK_PAGE, 105 VK_GOTTPREL_LO12_NC = VK_GOTTPREL | VK_PAGEOFF | VK_NC, 106 VK_GOTTPREL_G1 = VK_GOTTPREL | VK_G1, 107 VK_GOTTPREL_G0_NC = VK_GOTTPREL | VK_G0 | VK_NC, 108 VK_TPREL_G2 = VK_TPREL | VK_G2, 109 VK_TPREL_G1 = VK_TPREL | VK_G1, 110 VK_TPREL_G1_NC = VK_TPREL | VK_G1 | VK_NC, 111 VK_TPREL_G0 = VK_TPREL | VK_G0, 112 VK_TPREL_G0_NC = VK_TPREL | VK_G0 | VK_NC, 113 VK_TPREL_HI12 = VK_TPREL | VK_HI12, 114 VK_TPREL_LO12 = VK_TPREL | VK_PAGEOFF, 115 VK_TPREL_LO12_NC = VK_TPREL | VK_PAGEOFF | VK_NC, 116 VK_TLSDESC_LO12 = VK_TLSDESC | VK_PAGEOFF, 117 VK_TLSDESC_PAGE = VK_TLSDESC | VK_PAGE, 118 VK_TLSDESC_AUTH_LO12 = VK_TLSDESC_AUTH | VK_PAGEOFF, 119 VK_TLSDESC_AUTH_PAGE = VK_TLSDESC_AUTH | VK_PAGE, 120 VK_SECREL_LO12 = VK_SECREL | VK_PAGEOFF, 121 VK_SECREL_HI12 = VK_SECREL | VK_HI12, 122 123 VK_INVALID = 0xfff 124 // clang-format on 125 }; 126 127 private: 128 const MCExpr *Expr; 129 const VariantKind Kind; 130 131 protected: 132 explicit AArch64MCExpr(const MCExpr *Expr, VariantKind Kind) 133 : Expr(Expr), Kind(Kind) {} 134 135 public: 136 /// @name Construction 137 /// @{ 138 139 static const AArch64MCExpr *create(const MCExpr *Expr, VariantKind Kind, 140 MCContext &Ctx); 141 142 /// @} 143 /// @name Accessors 144 /// @{ 145 146 /// Get the kind of this expression. 147 VariantKind getKind() const { return Kind; } 148 149 /// Get the expression this modifier applies to. 150 const MCExpr *getSubExpr() const { return Expr; } 151 152 /// @} 153 /// @name VariantKind information extractors. 154 /// @{ 155 156 static VariantKind getSymbolLoc(VariantKind Kind) { 157 return static_cast<VariantKind>(Kind & VK_SymLocBits); 158 } 159 160 static VariantKind getAddressFrag(VariantKind Kind) { 161 return static_cast<VariantKind>(Kind & VK_AddressFragBits); 162 } 163 164 static bool isNotChecked(VariantKind Kind) { return Kind & VK_NC; } 165 166 /// @} 167 168 /// Convert the variant kind into an ELF-appropriate modifier 169 /// (e.g. ":got:", ":lo12:"). 170 StringRef getVariantKindName() const; 171 172 void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override; 173 174 void visitUsedExpr(MCStreamer &Streamer) const override; 175 176 MCFragment *findAssociatedFragment() const override; 177 178 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, 179 const MCFixup *Fixup) const override; 180 181 void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override; 182 183 static bool classof(const MCExpr *E) { 184 return E->getKind() == MCExpr::Target; 185 } 186 }; 187 188 class AArch64AuthMCExpr final : public AArch64MCExpr { 189 uint16_t Discriminator; 190 AArch64PACKey::ID Key; 191 192 explicit AArch64AuthMCExpr(const MCExpr *Expr, uint16_t Discriminator, 193 AArch64PACKey::ID Key, bool HasAddressDiversity) 194 : AArch64MCExpr(Expr, HasAddressDiversity ? VK_AUTHADDR : VK_AUTH), 195 Discriminator(Discriminator), Key(Key) {} 196 197 public: 198 static const AArch64AuthMCExpr * 199 create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, 200 bool HasAddressDiversity, MCContext &Ctx); 201 202 AArch64PACKey::ID getKey() const { return Key; } 203 uint16_t getDiscriminator() const { return Discriminator; } 204 bool hasAddressDiversity() const { return getKind() == VK_AUTHADDR; } 205 206 void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override; 207 208 void visitUsedExpr(MCStreamer &Streamer) const override; 209 210 MCFragment *findAssociatedFragment() const override; 211 212 bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, 213 const MCFixup *Fixup) const override; 214 215 static bool classof(const MCExpr *E) { 216 return isa<AArch64MCExpr>(E) && classof(cast<AArch64MCExpr>(E)); 217 } 218 219 static bool classof(const AArch64MCExpr *E) { 220 return E->getKind() == VK_AUTH || E->getKind() == VK_AUTHADDR; 221 } 222 }; 223 } // end namespace llvm 224 225 #endif 226